Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date checker #4007

Merged
merged 7 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}