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

Exception shouldn't happen when pasting an entry with a publication date-range of form yyyy-yyyy #8247

Merged
merged 17 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,5 @@ lib/ojdbc.jar

# do not ignore JabRef icons (they are ignored by the macos setting above)
!src/main/java/org/jabref/gui/icon

JabRef_Main.xml
2 changes: 1 addition & 1 deletion .idea/runConfigurations/JabRef_Main.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- We fixed an issue where an exception occurred when pasting an entry with a publication date-range of the form 1910/1917 [#7864](https://github.com/JabRef/jabref/issues/7864)
calixtus marked this conversation as resolved.
Show resolved Hide resolved
- We fixed an issue where an exception occured when a preview style was edited and afterwards another preview style selected. [#8280](https://github.com/JabRef/jabref/issues/8280)
- We fixed an issue where the actions to move a file to a directory were incorrectly disabled. [#7908](https://github.com/JabRef/jabref/issues/7908)
- We fixed an issue where an exception occurred when a linked online file was edited in the entry editor [#8008](https://github.com/JabRef/jabref/issues/8008)
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/org/jabref/model/entry/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Date {
}

private final TemporalAccessor date;
private final TemporalAccessor endDate;

public Date(int year, int month, int dayOfMonth) {
this(LocalDate.of(year, month, dayOfMonth));
Expand All @@ -57,15 +58,48 @@ public Date(int year) {

public Date(TemporalAccessor date) {
this.date = date;
endDate = null;
}

/**
* CS427 Issue link: https://github.com/JabRef/jabref/issues/7864
calixtus marked this conversation as resolved.
Show resolved Hide resolved
* Creates a Date from date and endDate.
*
* @param date the start date
* @param endDate the start date
*/
public Date(TemporalAccessor date, TemporalAccessor endDate) {
this.date = date;
this.endDate = endDate;
}

/**
* Creates a Date from date and endDate.
* CS427 Issue link: https://github.com/JabRef/jabref/issues/7864
*
* @param dateString the string to extract the date information
* @throws DateTimeParseException if dataString is mal-formatted
*/
public static Optional<Date> parse(String dateString) {
Objects.requireNonNull(dateString);

if (dateString.isEmpty()) {
return Optional.empty();
}

// if dateString has format of uuuu/uuuu, treat as date range
if (dateString.matches("[0-9]{4}/[0-9]{4}")) {
try {
String[] strDates = dateString.split("/");
TemporalAccessor parsedDate = SIMPLE_DATE_FORMATS.parse(strDates[0]);
TemporalAccessor parsedEndDate = SIMPLE_DATE_FORMATS.parse(strDates[1]);
return Optional.of(new Date(parsedDate, parsedEndDate));
} catch (DateTimeParseException ignored) {
return Optional.empty();
}

}

try {
TemporalAccessor parsedDate = SIMPLE_DATE_FORMATS.parse(dateString);
return Optional.of(new Date(parsedDate));
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/jabref/model/entry/DateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

class DateTest {

@Test
void parseCorrectlyYearRangeDate() throws Exception {
Date expectedDataRange = new Date(Year.of(2014), Year.of(2017));
assertEquals(Optional.of(expectedDataRange), Date.parse("2014/2017"));
}

@Test
void parseCorrectlyDayMonthYearDate() throws Exception {
Date expected = new Date(LocalDate.of(2014, 6, 19));
Expand Down