vendredi 21 août 2015

How to reflection test entity annotations using JUnit

How can I test the integrity of annotations in Entity generated from Tables in a DB. What I am trying to do is to test if the annotations are there and to check if they are filled out properly such as using Object data types instead of variables Eg.(Integers instead of int). check if Calendar type is used instead of Date type.

Currently I can create these tables and manually assign annotations such as:

 @Entity
   @Id, 
   @GeneratedValue(), 
   @Column(name="columnName"), 
   @Size(min = 1, max =20, message = "{ClassName.variableName.messageType"}),
   @NotNull(message = "{ClassName.variableName.messageType"}),
   @Temporal(TemporalType.DATE)...    

The part where I am trying to solve is that i need to check the integrity of thesees annotations, such as if they are filled out properly or even there. the code that i have to be tested is:

    @Log
    @Entity
    @Table(name = "TNOTE")
    @NamedQueries({
    @NamedQuery(name = "Note.findAll", query = "SELECT n FROM Note n"),
    @NamedQuery(name = "Note.findAssociated", query = "SELECT n FROM Note n  WHERE n.associatedRecordType = :type AND n.associatedRecordId = :id AND n.noteTypeId=:noteType ORDER BY n.lastModDate DESC, n.lastModTime DESC") })
    public class Note extends BaseEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "TrentKeyGen")
@Column(name = "NOTE_ID")
private String id;

@Column(name = "ORG_PARTY_ID")
private String organisationId;

@Column(name = "ENTITY_NM")
@NotNull(message = "{Notes.associatedRecordType.isNull}")
@Size(min = 1, max = 20, message = "{Notes.associatedRecordType.Invalid}")
private String associatedRecordType;

@Column(name = "KEY_ITEM")
@NotNull(message = "{Notes.associatedRecordId.isNull}")
@Size(min = 1, max = 255, message = "{Notes.associatedRecordId.Invalid}")
private String associatedRecordId;

@Column(name = "NOTE_TYPE_ID")
@NotNull(message = "{Notes.noteTypeId.isNull}")
@Size(min = 1, max = 10, message = "{Notes.noteTypeId.Invalid}")
private String noteTypeId;

@Temporal(TemporalType.DATE)
@Column(name = "NOTE_D")
private Calendar startDate;

@Temporal(TemporalType.DATE)
@Column(name = "NOTE_ED")
private Calendar endDate;

@Column(name = "IS_PUBLIC")
@Convert(converter = BooleanToStringConverter.class)
private Boolean isPublic;

@Column(name = "PRIVATE_USER_NM")
@Size(min = 0, max = 20, message = "{Notes.notePrivateUserName.Invalid}")
private String privateUserName;

@Column(name = "NOTE_CREATOR")
@Size(min = 0, max = 20, message = "{Notes.noteCreator.Invalid}")
private String creator;

@Column(name = "NOTE_CAPTION")
@Size(min = 0, max = 255, message = "{Notes.noteCaption.Invalid}")
private String caption;

@Column(name = "NOTE")
@Lob
private String note;

@Override
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_mod_d")
public Date getLastModDate() {
    return super.getLastModDate();
}

@Override
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_mod_t")
public Date getLastModTime() {
    return super.getLastModTime();
}





Aucun commentaire:

Enregistrer un commentaire