-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add date checker * Fix changelog * Add for more date fields * Add more tests for parsing dates * Update CHANGELOG.md
- Loading branch information
1 parent
5e629b2
commit 787902c
Showing
6 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.jabref.logic.integrity; | ||
|
||
import java.util.Optional; | ||
|
||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.entry.Date; | ||
import org.jabref.model.strings.StringUtil; | ||
|
||
public class DateChecker implements ValueChecker { | ||
|
||
@Override | ||
public Optional<String> checkValue(String value) { | ||
if (StringUtil.isBlank(value)) { | ||
return Optional.empty(); | ||
} | ||
|
||
Optional<Date> parsedDate = Date.parse(value); | ||
if (!parsedDate.isPresent()) { | ||
return Optional.of(Localization.lang("incorrect format")); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/java/org/jabref/logic/integrity/DateCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.jabref.logic.integrity; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class DateCheckerTest { | ||
|
||
private DateChecker checker; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
checker = new DateChecker(); | ||
} | ||
|
||
@Test | ||
void complainsAboutInvalidIsoLikeDate() { | ||
assertEquals(Optional.of("incorrect format"), checker.checkValue("2018-04-21TZ")); | ||
} | ||
|
||
@Test | ||
void acceptsValidIsoDate() { | ||
assertEquals(Optional.empty(), checker.checkValue("2018-04-21")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,43 @@ | ||
package org.jabref.model.entry; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Year; | ||
import java.time.YearMonth; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class DateTest { | ||
class DateTest { | ||
|
||
@Test | ||
public void parseCorrectlyDayMonthYearDate() throws Exception { | ||
void parseCorrectlyDayMonthYearDate() throws Exception { | ||
Date expected = new Date(LocalDate.of(2014, 6, 19)); | ||
assertEquals(Optional.of(expected), Date.parse("19-06-2014")); | ||
} | ||
|
||
public void parseDateNull() { | ||
assertThrows(NullPointerException.class, () -> assertEquals(Optional.empty(), Date.parse(null))); | ||
@Test | ||
void parseCorrectlyMonthYearDate() throws Exception { | ||
Date expected = new Date(YearMonth.of(2014, 6)); | ||
assertEquals(Optional.of(expected), Date.parse("06-2014")); | ||
} | ||
|
||
@Test | ||
void parseCorrectlyYearMonthDate() throws Exception { | ||
Date expected = new Date(YearMonth.of(2014, 6)); | ||
assertEquals(Optional.of(expected), Date.parse("2014-06")); | ||
} | ||
|
||
@Test | ||
void parseCorrectlyYearDate() throws Exception { | ||
Date expected = new Date(Year.of(2014)); | ||
assertEquals(Optional.of(expected), Date.parse("2014")); | ||
} | ||
|
||
@Test | ||
void parseDateNull() { | ||
assertThrows(NullPointerException.class, () -> Date.parse(null)); | ||
} | ||
} |