From b00c1a75c0c6efe0adb0db95addc6186e8bc596b Mon Sep 17 00:00:00 2001 From: cesarvh Date: Thu, 8 Nov 2018 16:04:19 -0800 Subject: [PATCH] HM-5: Added ambigious input tests, code clean-up --- .../structureddate/antlr/StructuredDate.g4 | 2 +- .../services/structureddate/DateUtils.java | 19 ++++++++++++++---- .../StructuredDateEvaluatorTest.java | 20 ++++++++++--------- .../src/test/resources/test-dates.yaml | 18 +++++++++++++---- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4 b/services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4 index 4d181dc07..5bd8e4f57 100644 --- a/services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4 +++ b/services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4 @@ -170,5 +170,5 @@ SLASH: '/' ; DOT: '.' ; QUESTION: '?' ; OTHER: . ; -UNKNOWN: 'unknown' | 'UNKNOWN' ; +UNKNOWN: 'unknown' ; STRING: [a-z]+ ; diff --git a/services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/DateUtils.java b/services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/DateUtils.java index cddb0e81d..67c1abc0d 100644 --- a/services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/DateUtils.java +++ b/services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/DateUtils.java @@ -1108,16 +1108,26 @@ public class DateUtils { /** * Calculates the latest date that may be considered to be "after" - * a given date range. We define "after" as the current date. + * a given date range. * * @param startDate The first date in the range * @param endDate The last date in the range * @return The latest date "after" the range */ public static Date getLatestAfterDate(Date startDate, Date endDate) { - // TODO - return getCurrentDate(); + Date currentDate = getCurrentDate(); + if (endDate == null) { + return currentDate; + } + MutableDateTime currentDateTime = convertToDateTime(currentDate); + MutableDateTime endDateTime = convertToDateTime(endDate); + + int comparisonResult = currentDateTime.compareTo(endDateTime); + if (comparisonResult == 1 || comparisonResult == 0) { + return currentDate; + } + return null; } public static Date getCurrentDate() { @@ -1125,7 +1135,8 @@ public class DateUtils { Integer year = (Integer) localDate.getYear(); Integer month = (Integer) localDate.getMonthOfYear(); Integer dayOfMonth = (Integer) localDate.getDayOfMonth(); - return new Date(year, month, dayOfMonth, Date.DEFAULT_ERA); + Era era = (localDate.getEra() == DateTimeConstants.BC) ? Era.BCE : Era.CE; + return new Date(year, month, dayOfMonth, era); } public static int getYearsBetween(Date startDate, Date endDate) { diff --git a/services/structureddate/structureddate/src/test/java/org/collectionspace/services/structureddate/StructuredDateEvaluatorTest.java b/services/structureddate/structureddate/src/test/java/org/collectionspace/services/structureddate/StructuredDateEvaluatorTest.java index 3d487babb..039252b8b 100644 --- a/services/structureddate/structureddate/src/test/java/org/collectionspace/services/structureddate/StructuredDateEvaluatorTest.java +++ b/services/structureddate/structureddate/src/test/java/org/collectionspace/services/structureddate/StructuredDateEvaluatorTest.java @@ -54,16 +54,18 @@ public class StructuredDateEvaluatorTest { private StructuredDateInternal createStructuredDateFromYamlSpec(String displayDate, Map structuredDateFields) { StructuredDateInternal structuredDate = null; - // Can and should we calculate today's date for the "AFTER" dates? if (structuredDateFields != null && structuredDateFields.containsKey("latestDate")) { - if (structuredDateFields.get("latestDate").toString().equals("current date")) { - ArrayList items = new ArrayList<>(); - Date currentDate = DateUtils.getCurrentDate(); - items.add(currentDate.getYear()); - items.add(currentDate.getMonth()); - items.add(currentDate.getDay()); - items.add(Date.DEFAULT_ERA.toString().toUpperCase()); - structuredDateFields.put("latestDate", items); + Object latestDate = structuredDateFields.get("latestDate"); + if (latestDate instanceof String) { + if (latestDate.equals("current date")) { + ArrayList items = new ArrayList<>(); + Date currentDate = DateUtils.getCurrentDate(); + items.add(currentDate.getYear()); + items.add(currentDate.getMonth()); + items.add(currentDate.getDay()); + items.add(currentDate.getEra() == Era.BCE ? "BCE" : "CE"); + structuredDateFields.put("latestDate", items); + } } } diff --git a/services/structureddate/structureddate/src/test/resources/test-dates.yaml b/services/structureddate/structureddate/src/test/resources/test-dates.yaml index 08939797b..2b7e1925f 100644 --- a/services/structureddate/structureddate/src/test/resources/test-dates.yaml +++ b/services/structureddate/structureddate/src/test/resources/test-dates.yaml @@ -1186,6 +1186,17 @@ "13 april 15": # oneDisplayDate - ambiguous day and year, intepreted as year month day earliestSingleDate: [13, 4, 15, CE] + "04/5-6/2018": # Month/Day - Day/Year date + earliestSingleDate: [2018, 4, 5, CE] + latestDate: [2018, 4, 6, CE] + + "04/03-07/09": # Ambigious NumDayInMonthRange - should be interpreted as Month/Day - Day/Year date + earliestSingleDate: [9, 4, 3, CE] + latestDate: [9, 4, 7, CE] + + '04/1996-07/09': # Semi-ambigious NumDayInMonthRange - should be interpreted as Month/Year - Month/Year date + earliestSingleDate: [1996, 4, 1, CE] + latestDate: [9, 7, 31, CE] # ------------------------------------------------------------------------------------------------------- # Invalid dates # ------------------------------------------------------------------------------------------------------- @@ -1229,9 +1240,8 @@ '3/4?/2005': # question mark null - '100 June 2010': + '100 June 2010': # invalid day null - '13 June 0': - null - \ No newline at end of file + '13 June 0': # year 0 + null \ No newline at end of file -- 2.47.3