]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3f1befd7b152a7373653f4285605b3c707b1eb29
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.structureddate;
2
3 import java.lang.reflect.Array;
4 import java.lang.reflect.InvocationTargetException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Stack;
11
12 import org.apache.commons.beanutils.PropertyUtils;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.testng.Assert;
16 import org.testng.annotations.BeforeClass;
17 import org.testng.annotations.Test;
18 import org.yaml.snakeyaml.Yaml;
19
20 public class StructuredDateEvaluatorTest {
21         public static final String TEST_CASE_FILE = "/test-dates.yaml";
22         public static final List<String> YAML_DATE_SPEC = Arrays.asList("year", "month", "day", "era", "certainty", "qualifierType", "qualifierValue", "qualifierUnit");
23
24         final Logger logger = LoggerFactory.getLogger(StructuredDateEvaluatorTest.class);
25
26         @BeforeClass
27         public void setUp() {
28
29         };
30
31         @Test
32         public void test() {
33                 Yaml yaml = new Yaml();
34                 Map<String, Object> testCases = (Map<String, Object>) yaml.load(getClass().getResourceAsStream(TEST_CASE_FILE));
35
36                 for (String displayDate : testCases.keySet()) {
37                         logger.debug("Testing input: " + displayDate);
38
39                         Map<String, Object> expectedStructuredDateFields = (Map<String, Object>) testCases.get(displayDate);
40
41                         StructuredDateInternal expectedStructuredDate = createStructuredDateFromYamlSpec(displayDate, expectedStructuredDateFields);
42                         StructuredDateInternal actualStructuredDate = null;
43                         
44                         try {
45                                 actualStructuredDate = StructuredDateInternal.parse(displayDate);
46                         }
47                         catch(StructuredDateFormatException e) {
48                                 logger.debug(e.getMessage());
49                         }
50
51                         Assert.assertEquals(actualStructuredDate, expectedStructuredDate);
52                 }
53         }
54
55         private StructuredDateInternal createStructuredDateFromYamlSpec(String displayDate, Map<String, Object> structuredDateFields) {
56                 StructuredDateInternal structuredDate = null;
57
58                 if (structuredDateFields != null && structuredDateFields.containsKey("latestDate")) {
59                         Object latestDate = structuredDateFields.get("latestDate");
60                         if (latestDate instanceof String) {
61                                 Date currentDate = DateUtils.getCurrentDate();
62                                 ArrayList latestDateItems = new ArrayList<>();
63                                 if (latestDate.equals("current date")) {
64                                         latestDateItems.add(currentDate.getYear());
65                                         latestDateItems.add(currentDate.getMonth());
66                                         latestDateItems.add(currentDate.getDay());
67                                         latestDateItems.add(currentDate.getEra() == Era.BCE ? "BCE" : "CE");
68                                         structuredDateFields.put("latestDate", latestDateItems);
69                                 }
70                                 if (latestDate.equals("uncalibrated latest date")) {
71                                         Stack<ArrayList> results = calculateUncalibratedDate(displayDate, currentDate.getYear());
72                                         structuredDateFields.put("latestDate", results.pop());
73                                         structuredDateFields.put("earliestSingleDate", results.pop());
74                                 }
75                         }
76                 }
77
78                 if (structuredDateFields != null) {
79                         structuredDate = new StructuredDateInternal();
80
81                         for (String propertyName : structuredDateFields.keySet()) {
82                                 Object value = structuredDateFields.get(propertyName);
83
84                                 try {
85                                         Class propertyType = PropertyUtils.getPropertyType(structuredDate, propertyName);
86
87                                         if (propertyType.equals(Date.class)) {
88                                                 value = createDateFromYamlSpec((List<Object>) value);
89                                         }
90
91                                         PropertyUtils.setProperty(structuredDate, propertyName, value);
92                                 }
93                                 catch(NoSuchMethodException e) {
94                                         logger.warn(propertyName + " is not a property");
95                                 }
96                                 catch(InvocationTargetException e) {
97                                         logger.error(propertyName + " accessor threw an exception");
98                                 }
99                                 catch(IllegalAccessException e) {
100                                         logger.error("could not access property " + propertyName);
101                                 }
102                         }
103                         
104                         if (structuredDate.getDisplayDate() == null) {
105                                 structuredDate.setDisplayDate(displayDate);
106                         }
107                 }
108
109                 return structuredDate;
110         }
111
112
113         /** 
114          * Calculates the uncalibrated date, since the yalm expected dates need to be dynamic
115          * as they will change from year to year. 
116          * @param displayDate The current test's display date
117          * @param currentYear The current year
118          * 
119          * @return a stack consisting of two ArrayLists, each containing the expected dates
120         */
121         public Stack<ArrayList> calculateUncalibratedDate(String displayDate, Integer currentYear) {
122                 Stack<ArrayList> stack = new Stack<ArrayList>();
123                 ArrayList latestDate = new ArrayList<>();
124                 ArrayList earliestDate = new ArrayList<>();
125
126
127                 String reg = "±|\\+/-";
128                 String[] splitDateTokens = displayDate.split(reg);
129                 String[] tokensPartTwo = splitDateTokens[1].split(" ");
130
131                 Integer mainYear = Integer.parseInt(splitDateTokens[0].replaceAll("\\s|,", ""));
132                 Integer offset;
133
134                 try {
135                         offset = Integer.parseInt(tokensPartTwo[0]);
136                 } catch (Exception e) {
137                         offset = Integer.parseInt(tokensPartTwo[1].replaceAll("\\s|,", ""));
138                 }
139
140                 Integer earliestYear = currentYear - (mainYear + offset);
141                 Integer latestYear   = currentYear - (mainYear - offset);
142                 
143                 String earliestEra = earliestYear < 0 ? "BCE" : "CE";
144                 String latestEra = latestYear < 0 ? "BCE" : "CE";
145                 
146                 earliestYear = Math.abs(earliestYear);
147                 latestYear = Math.abs(latestYear);
148
149                 latestDate.add(latestYear);
150                 latestDate.add(12);
151                 latestDate.add(DateUtils.getDaysInMonth(12, latestYear, null));
152                 latestDate.add(latestEra);
153
154                 earliestDate.add(earliestYear);
155                 earliestDate.add(1);
156                 earliestDate.add(1);
157                 earliestDate.add(earliestEra);
158
159                 stack.push(earliestDate);
160                 stack.push(latestDate);
161
162                 return stack;
163         }
164
165         private Date createDateFromYamlSpec(List<Object> dateFields) {
166                 Date date = new Date();
167                 Iterator<Object> fieldIterator = dateFields.iterator();
168
169                 for (String propertyName : YAML_DATE_SPEC) {
170                         Object value = fieldIterator.hasNext() ? fieldIterator.next() : null;
171
172                         try {
173                                 Class propertyType = PropertyUtils.getPropertyType(date, propertyName);
174
175                                 if (value != null && Enum.class.isAssignableFrom(propertyType)) {
176                                         value = Enum.valueOf(propertyType, (String) value);
177                                 }
178
179                                 PropertyUtils.setProperty(date, propertyName, value);
180                         }
181                         catch(NoSuchMethodException e) {
182                                 logger.warn(propertyName + " is not a property");
183                         }
184                         catch(InvocationTargetException e) {
185                                 logger.error(propertyName + " accessor threw an exception");
186                         }
187                         catch(IllegalAccessException e) {
188                                 logger.error("could not access property " + propertyName);
189                         }               
190                 }
191
192                 return date;
193         }
194 }