Hey Community,
dann mache ich mal wieder einen Thread auf, mir wurde es ja gestattet für ‚Wehwehchen‘
Vielleich wäre auch mal ein Thema gut „Fragen die keinen eigenen Thread verdient haben“
Also wie aus meinem anderen Thread bekannt habe ich eine Report-Klasse.
Jetzt wollte ich für diesen im Nachhinein (weil ich noch nicht so weit vorausplanen kann) einen JUnit Test schreiben
UND JavaDoc Kommentare setzen.
Da ich beides in Projekten bisher vernachlässigt habe, wollte ich mal, dass evtl. jemand drüberschauen kann, meine Fragen also:
Was ist überflüssig?
Was kann ich besser machen?
Wo bin ich nicht genau genug?
Was ist fehlerhaft?
Wie geht es besser?
Report
[spoiler]```
/**
-
The report class represents a complete report which was created by a user.
-
The report class is linked with a set of Keywords through the
-
{@link de.binarylogic.xyz.model.ReportKeywordEntry} class.
-
@author BinaryLogic
-
@version 1.0 :: 2014-12-15
*/
public class Report implements Serializable {public final static String ID_FIELD_NAME = „report_id“;
public final static String USER_ID_FIELD_NAME = „user_id“;
private static final long serialVersionUID = 3329580979489979731L;@DatabaseField(generatedId = true, columnName = ID_FIELD_NAME)
private int id;@DatabaseField(dataType = DataType.STRING, unique = true, canBeNull = false)
private String title;@DatabaseField(dataType = DataType.STRING, canBeNull = true)
private String content;@DatabaseField(dataType = DataType.STRING, canBeNull = true)
private String note;/**
- The user is the owner of the report. He creates and stores the report with the UI.
*/
@DatabaseField(canBeNull = false, foreign = true, foreignColumnName = USER_ID_FIELD_NAME)
private User user = null;
/** Default constructor which should only be used by (and is necessary for) ORMLight **/
public Report() { }/**
- Default constructor which should be used by other classes.
- @param user The owner of the report.
- @param title The title e.g. a short description of the content.
-
It is limited to 50 characters.
- @param content The content of the report.
- @param note Additional notes related to the content.
-
@throws IllegalArgumentException
*/
public Report(User user, String title, String content, String note) throws IllegalArgumentException {
setUser(user);
setTitle(title);
setContent(content);
setNote(note);
}
/**
- Sets the title of the report. It should descripes the content or
- be an unique keyword or word group. It’s limited to 50 characters,
- all above are cutt off by this method.
- If the title is null an {@link java.lang.IllegalArgumentException}
- will be thrown.
- @param title
-
@throws IllegalArgumentException
*/
public void setTitle(String title) throws IllegalArgumentException {
if(title == null) {
throw new IllegalArgumentException(„Report title can’t be null.“);
}
else if(title.length() > 50) {
this.title = title.substring(0, 49);
}
else this.title = title;
}
/**
- Returns the title of the report as {@link java.lang.String}
-
@return title
*/
public String getTitle() {
return title;
}
/**
- Sets the user which is the owner of the report. It can’t
- be null.
- @param user
-
@throws IllegalArgumentException
*/
public void setUser(User user) throws IllegalArgumentException {
if(user == null) {
throw new IllegalArgumentException(„User (and owner of the report) can’t be null.“);
}
else this.user = user;
}
/**
- Returns the user as {@link de.binarylogic.xyz.model.User} object.
-
@return {@link de.binarylogic.xyz.model.User}
*/
public User getUser() {
return user;
}
/**
- Sets the content of the report. If it’s null an
- empty {@link java.lang.String} will be generated.
-
@param content
*/
public void setContent(String content) {
if(content == null) {
this.content = new String();
}
else this.content = content;
}
/**
- Return the content of the report as a {@link java.lang.String}
-
@return
*/
public String getContent() {
return content;
}
/**
- Sets the note of the report. The note should descripes the
- content with additional information.
-
@param note
*/
public void setNote(String note) {
if(note == null) {
this.note = „“;
}
else this.note = note;
}
/**
- Returns the note as a {@link java.lang.String}
-
@return
*/
public String getNote() {
return note;
}
/**
- Returns the title, the user and the content in
- a human readable form.
-
@return
*/
public String toString() {
return String.format(
„Title: %s
User: %s
Content:
%s“,
title,
user.getUsername(),
getContent()
);
}
/**
- Returns the id. The ID will be generated through
- the ORMLight framework.
-
@return
*/
public int getId() {
return id;
}
/**
- Checks if all conditions are complied to store
- this Report and link it with a Keyword through the
- {@link de.binarylogic.xyz.model.ReportKeywordEntry} class.
-
@return
*/
public boolean isValid() {
if(user != null && title != null && !title.isEmpty()
&& content != null && note != null) {
return true;
}
return false;
}
}```[/spoiler]
- The user is the owner of the report. He creates and stores the report with the UI.
ReportTest
[spoiler]```
/**
-
A class testing the functionality of
-
the Report class in {@link de.binarylogic.xyz.model.Report.class}
-
@author BinaryLogic
-
@version 1.0 - 2014-12-17
*/
public class ReportTest {private Report report;
@Before
public void initialize() {
report = new Report();
}/**
- The tested constructor is for ORMLight only and it
- should never used otherwise. So this is the only
- way the title, the note, and the content can
- be null.
*/
@Test
public void constructReport() {
report = new Report();
assertNull(report.getTitle());
assertNull(report.getNote());
assertNull(report.getContent());
assertNull(report.getUser());
}
@Test(expected = IllegalArgumentException.class)
public void constructParameterizedReportWithNullUser() throws IllegalArgumentException {
report = new Report(null, „Title“, „Content“, „Note“);
}@Test(expected = IllegalArgumentException.class)
public void constructParameterizedReportWithNullTitle() throws IllegalArgumentException {
User mockedUser = mock(User.class);
report = new Report(mockedUser, null, „Content“, „Note“);
}@Test
public void constructParameterizedReportWithNullContent() {
User mockedUser = mock(User.class);
report = new Report(mockedUser, „Title“, null, „Note“);
assertNotNull(report.getContent());
}@Test
public void constructParameterizedReportWithNullNote() {
User mockedUser = mock(User.class);
report = new Report(mockedUser, „Title“, „Content“, null);
assertNotNull(report.getNote());
}@Test
public void constructParameterizedReport() {
User mockedUser = mock(User.class);
when(mockedUser.getUsername()).thenReturn(„User“);
report = new Report(mockedUser, „Title“, „Content“, „Note“);
assertEquals(„User“, report.getUser().getUsername());
assertEquals(„Title“, report.getTitle());
assertEquals(„Content“, report.getContent());
assertEquals(„Note“, report.getNote());
}@Test(expected = IllegalArgumentException.class)
public void setNullTitle() throws IllegalArgumentException {
report.setTitle(null);
}/**
- This method test for a too long title. This should not be able
- in normal work - because the UI limits the title, so that the
- user can’t type more as 50 characters. But for security the
- title will be cutted off if anyone should pass a string with
- more as 50 characters.
-
@throws IllegalArgumentException
*/
@Test
public void setTooLongTitle() throws IllegalArgumentException {
String random = UUID.randomUUID().toString().concat(UUID.randomUUID().toString());
report.setTitle(random);
assertEquals(random.substring(0, 49), report.getTitle());
}
@Test
public void setNormalTitle() throws IllegalArgumentException {
String random = UUID.randomUUID().toString().substring(0, 30);
report.setTitle(random);
assertEquals(random, report.getTitle());
}@Test(expected = IllegalArgumentException.class)
public void setNullUser() throws IllegalArgumentException {
report.setUser(null);
}@Test
public void setUser() {
String result = „User #1“;
User mockedUser = mock(User.class);
when(mockedUser.getUsername()).thenReturn(result);
report.setUser(mockedUser);
assertEquals(result, report.getUser().getUsername());
}@Test
public void setNullContent() {
report.setContent(null);
assertNotNull(report.getContent());
}@Test
public void setContent() {
String random = UUID.randomUUID().toString();
report.setContent(random);
assertEquals(random, report.getContent());
}@Test
public void setNote() {
String random = UUID.randomUUID().toString();
report.setNote(random);
assertEquals(random, report.getNote());
}@Test
public void setNullNote() {
report.setNote(null);
assertNotNull(report.getNote());
}@Test
public void testToString() {
User mockedUser = mock(User.class);
when(mockedUser.getUsername()).thenReturn(„user“);
report.setUser(mockedUser);
report.setTitle(„This title“);
report.setContent(„Important content“);
String result = „Title: This title
User: user
Content:
Important content“;
assertEquals(result, report.toString());
}@Test
public void getId() throws NoSuchFieldException, IllegalAccessException {
Field field = Report.class.getDeclaredField(„id“);
field.setAccessible(true);
field.set(report, 5);
assertEquals(5, report.getId());
}@Test
public void isValid() {
User mockedUser = mock(User.class);
report = new Report(mockedUser, „Title“, „Content“, „Note“);
assertTrue(report.isValid());
}@Test
public void isNotValid() throws NoSuchFieldException, IllegalAccessException {
User mockedUser = mock(User.class);
Field userField = Report.class.getDeclaredField(„user“);
Field titleField = Report.class.getDeclaredField(„title“);
Field contentField = Report.class.getDeclaredField(„content“);
Field noteField = Report.class.getDeclaredField(„note“);userField.setAccessible(true); titleField.setAccessible(true); contentField.setAccessible(true); noteField.setAccessible(true); // create valid user report = new Report(mockedUser, "Title", "Content", "Note"); userField.set(report, null); assertFalse(report.isValid()); userField.set(report, mockedUser); titleField.set(report, null); assertFalse(report.isValid()); titleField.set(report, ""); assertFalse(report.isValid()); titleField.set(report, "Title"); contentField.set(report, null); assertFalse(report.isValid()); contentField.set(report, "Content"); noteField.set(report, null); assertFalse(report.isValid());
}
}
Mir ist bewusst, dass mein Englisch an einigen Stellen noch Macken hat. Ich hoffe es ist trotzdem verständlich.
Ich bin leider noch nicht ganz fertig mit Fragen:
Ist denn die `isNotValid()` soweit i.O. optisch ist sie ja ziemlich 'BLERGH!' - oder lieber aufspalten, oder ganz anders?
Ich habe versucht die setMethoden zu umgehen um in einem Getter o.ä. Test nicht eine Methode zu verwenden, die ich an anderer Stelle teste,
wie ist das so? Soll ich das mit Gettern auch so machen oder ganz anders?
Gibt es ein Framework, welches mich die JavaDoc-Kommentare irgendwie auslägern lässt in eine Config-File? Der ganze Code wirkt dadurch doch irgendwie unübersichtlich wie ich finde.
Ich hoffe der Präfix ist i.O. so und die Kategoriewahl auch, ansonsten wäre es nett, wenn das geändert werden könnte.
Danke schonmal für eure Antworten
BL