Skip to content

Commit

Permalink
Add date checker (#4007)
Browse files Browse the repository at this point in the history
* Add date checker

* Fix changelog

* Add for more date fields

* Add more tests for parsing dates

* Update CHANGELOG.md
  • Loading branch information
tobiasdiez authored May 26, 2018
1 parent 5e629b2 commit 787902c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
## [Unreleased]

### Changed
- We added a validity check for dates in the `date` and `urldate` fields.
- We added a text file export for 'Find Unlinked Files'. [#3341](https://github.com/JabRef/jabref/issues/3341)
- We added a fetcher based on RFC-IDs. [#3971](https://github.com/JabRef/jabref/issues/3971)
- We changed the implementation of the `[shorttitle]` key pattern. It now removes small words like `a`, `an`, `on`, `the` etc. Refer to the help page for a complete overview. [Feature request in the forum](http://discourse.jabref.org/t/jabref-differences-in-shorttitle-between-versions-3-8-1-and-4-not-discounting-the-a-an-of-in-titles/1147)
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/jabref/logic/integrity/DateChecker.java
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();
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/logic/integrity/FieldCheckers.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ private static Multimap<String, ValueChecker> getAllMap(BibDatabaseContext datab
fieldCheckers.put(FieldName.YEAR, new YearChecker());
fieldCheckers.put(FieldName.KEY, new ValidBibtexKeyChecker(enforceLegalKey));

if (databaseContext.isBiblatexMode()) {
fieldCheckers.put(FieldName.DATE, new DateChecker());
fieldCheckers.put(FieldName.URLDATE, new DateChecker());
fieldCheckers.put(FieldName.EVENTDATE, new DateChecker());
fieldCheckers.put(FieldName.ORIGDATE, new DateChecker());
}

return fieldCheckers;
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/jabref/model/entry/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ public Date(TemporalAccessor date) {
*/
public static Optional<Date> parse(String dateString) {
Objects.requireNonNull(dateString);
List<String> formatStrings = Arrays.asList("uuuu-M-d", "uuuu-M", "d-M-uuuu", "M/uu", "M/uuuu", "MMMM d, uuuu",
List<String> formatStrings = Arrays.asList(
"uuuu-M-d",
"uuuu-M",
"d-M-uuuu",
"M-uuuu",
"M/uu",
"M/uuuu",
"MMMM d, uuuu",
"MMMM, uuuu",
"d.M.uuuu", "uuuu.M.d", "uuuu");
"d.M.uuuu",
"uuuu.M.d", "uuuu");

for (String formatString : formatStrings) {
try {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/jabref/logic/integrity/DateCheckerTest.java
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"));
}
}
29 changes: 25 additions & 4 deletions src/test/java/org/jabref/model/entry/DateTest.java
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));
}
}

0 comments on commit 787902c

Please sign in to comment.