Skip to content

Commit

Permalink
adding more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lfoppiano committed Jun 2, 2021
1 parent 07ddfd3 commit ef92564
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 2 deletions.
Expand Up @@ -328,7 +328,7 @@ public Date normalize(Date date) {
*
* @return the date where invalid information are removed or reverted
*/
private static Date postValidate(Date originalDate) {
public static Date postValidate(Date originalDate) {
Date validatedDate = new Date();

if (originalDate.getDay() > -1) {
Expand Down
152 changes: 151 additions & 1 deletion grobid-core/src/test/java/org/grobid/core/engines/DateParserTest.java
Expand Up @@ -22,7 +22,7 @@ public void setUp() throws Exception {
public void testPostValidation_invalidYear_onlyString_shouldSetNull() {
Date inputDate = new Date();
inputDate.setYearString("10360 10370");
Date outputDate = target.normalize(inputDate);
Date outputDate = DateParser.postValidate(inputDate);

assertThat(outputDate.getYearString(), is(nullValue()));
}
Expand All @@ -31,8 +31,158 @@ public void testPostValidation_invalidYear_onlyString_shouldSetNull() {
public void testPostValidation_invalidYear2_onlyString_shouldSetNull() {
Date inputDate = new Date();
inputDate.setYearString("10360 10370 10380 10390 10400");
Date outputDate = DateParser.postValidate(inputDate);

assertThat(outputDate.getYearString(), is(nullValue()));
}

@Test
public void testPostValidation_invalidYear_bothStringAndInt_shouldSetNull() {
Date inputDate = new Date();
inputDate.setYear(1036010370);
inputDate.setYearString("10360 10370");
Date outputDate = DateParser.postValidate(inputDate);

assertThat(outputDate.getYearString(), is(nullValue()));
assertThat(outputDate.getYear(), is(-1));
}

@Test
public void testPostValidation_invalidMonth_bothStringAndInt_shouldSetNull() {
Date inputDate = new Date();
inputDate.setMonth(1234);
inputDate.setMonthString("1234");
Date outputDate = DateParser.postValidate(inputDate);

assertThat(outputDate.getMonthString(), is(nullValue()));
assertThat(outputDate.getMonth(), is(-1));
}

@Test
public void testPostValidation_invalidDay_bothStringAndInt_shouldSetNull() {
Date inputDate = new Date();
inputDate.setDay(12345);
inputDate.setDayString("12345");
Date outputDate = DateParser.postValidate(inputDate);

assertThat(outputDate.getDayString(), is(nullValue()));
assertThat(outputDate.getDay(), is(-1));
}

@Test
public void testNormalize_yearContainsWholeDate_shouldReconstructCorrectly() {
Date inputDate = new Date();
inputDate.setYear(20021212);
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getMonth(), is(12));
assertThat(outputDate.getDay(), is(12));
assertThat(outputDate.getYear(), is(2002));
}

@Test
public void testNormalize_dayContainsWholeDate_shouldReturnEmptyDate() {
Date inputDate = new Date();
inputDate.setDay(20021212);
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getMonth(), is(-1));
assertThat(outputDate.getDay(), is(-1));
assertThat(outputDate.getYear(), is(-1));
}

@Test
public void testNormalize_monthContainsWholeDate_shouldReturnEmptyDate() {
Date inputDate = new Date();
inputDate.setMonth(20021212);
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getMonth(), is(-1));
assertThat(outputDate.getDay(), is(-1));
assertThat(outputDate.getYear(), is(-1));
}

@Test
public void testNormalize_yearOnly_validValue_shouldParseYearCorrectly() {
Date inputDate = new Date();
inputDate.setYearString("2002");
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getMonth(), is(-1));
assertThat(outputDate.getDay(), is(-1));
assertThat(outputDate.getYear(), is(2002));
assertThat(outputDate.getYearString(), is("2002"));
}

@Test
public void testNormalize_wholeDate_invalidYearValue_shouldRemoveValue() {
Date inputDate = new Date();
inputDate.setDayString("12");
inputDate.setMonthString("12");
inputDate.setYearString("2222222012");
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getDay(), is(12));
assertThat(outputDate.getDayString(), is("12"));
assertThat(outputDate.getMonth(), is(12));
assertThat(outputDate.getMonthString(), is("12"));
assertThat(outputDate.getYear(), is(-1));
assertThat(outputDate.getYearString(), is(nullValue()));
}

@Test
public void testNormalize_monthOnly_validValue_shouldParseMonthCorrectly() {
Date inputDate = new Date();
inputDate.setMonthString("12");
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getMonth(), is(12));
assertThat(outputDate.getDay(), is(-1));
assertThat(outputDate.getYear(), is(-1));
assertThat(outputDate.getMonthString(), is("12"));
}

@Test
public void testNormalize_wholeDate_invalidMonthValue_shouldRemoveValue() {
Date inputDate = new Date();
inputDate.setDayString("12");
inputDate.setMonthString("1222222222");
inputDate.setYearString("2012");
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getMonth(), is(-1));
assertThat(outputDate.getMonthString(), is(nullValue()));
assertThat(outputDate.getDay(), is(12));
assertThat(outputDate.getDayString(), is("12"));
assertThat(outputDate.getYear(), is(2012));
assertThat(outputDate.getYearString(), is("2012"));
}

@Test
public void testNormalize_dayOnly_validValue_shouldParseDayCorrectly() {
Date inputDate = new Date();
inputDate.setDayString("12");
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getMonth(), is(-1));
assertThat(outputDate.getDay(), is(12));
assertThat(outputDate.getYear(), is(-1));
assertThat(outputDate.getDayString(), is("12"));
}

@Test
public void testNormalize_wholeDate_invalidDayValue_shouldRemoveValue() {
Date inputDate = new Date();
inputDate.setDayString("1221");
inputDate.setMonthString("12");
inputDate.setYearString("2012");
Date outputDate = target.normalize(inputDate);

assertThat(outputDate.getDay(), is(-1));
assertThat(outputDate.getDayString(), is(nullValue()));
assertThat(outputDate.getMonth(), is(12));
assertThat(outputDate.getMonthString(), is("12"));
assertThat(outputDate.getYear(), is(2012));
assertThat(outputDate.getYearString(), is("2012"));
}
}

0 comments on commit ef92564

Please sign in to comment.