]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
HM-5: Added ambigious input tests, code clean-up
authorcesarvh <cesarv.h@berkeley.edu>
Fri, 9 Nov 2018 00:04:19 +0000 (16:04 -0800)
committercesarvh <cesarv.h@berkeley.edu>
Tue, 19 Mar 2019 19:40:44 +0000 (12:40 -0700)
services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4
services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/DateUtils.java
services/structureddate/structureddate/src/test/java/org/collectionspace/services/structureddate/StructuredDateEvaluatorTest.java
services/structureddate/structureddate/src/test/resources/test-dates.yaml

index 4d181dc07d7186cdaedffb4b57928a9c9c53c88c..5bd8e4f57fa6814d8d759d840c1ba73bf7dada95 100644 (file)
@@ -170,5 +170,5 @@ SLASH:          '/' ;
 DOT:            '.' ;
 QUESTION:       '?' ;
 OTHER:          . ;
-UNKNOWN:               'unknown' | 'UNKNOWN' ;
+UNKNOWN:        'unknown' ;
 STRING:         [a-z]+ ;
index cddb0e81d929a7559ac319b2bddc84938775b43e..67c1abc0d9ea045b2f1c3ae3ccaba60645359a61 100644 (file)
@@ -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) {
index 3d487babb323d1f0040baa735ea78b8db552fdc8..039252b8be19f29eabf30f984f6b72ca01f06cd0 100644 (file)
@@ -54,16 +54,18 @@ public class StructuredDateEvaluatorTest {
        private StructuredDateInternal createStructuredDateFromYamlSpec(String displayDate, Map<String, Object> 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);
+                               }
                        }
                }
 
index 08939797b0e770c7824c5bffcb5e0d19d535fa81..2b7e1925f1b3cdc584a6822ef32da822e4248173 100644 (file)
   "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
 # -------------------------------------------------------------------------------------------------------
   '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