]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1469d9124634b75d81f62caa6f36f199c235d978
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.structureddate;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.commons.beanutils.PropertyUtils;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.testng.Assert;
14 import org.testng.annotations.BeforeClass;
15 import org.testng.annotations.Test;
16 import org.yaml.snakeyaml.Yaml;
17
18 public class StructuredDateEvaluatorTest {
19         public static final String TEST_CASE_FILE = "/test-dates.yaml";
20         public static final List<String> YAML_DATE_SPEC = Arrays.asList("year", "month", "day", "era", "certainty", "qualifierType", "qualifierValue", "qualifierUnit");
21
22         final Logger logger = LoggerFactory.getLogger(StructuredDateEvaluatorTest.class);
23
24         @BeforeClass
25         public void setUp() {
26
27         };
28
29         @Test
30         public void test() {
31                 Yaml yaml = new Yaml();
32                 Map<String, Object> testCases = (Map<String, Object>) yaml.load(getClass().getResourceAsStream(TEST_CASE_FILE));
33
34                 for (String displayDate : testCases.keySet()) {
35                         logger.debug("Testing input: " + displayDate);
36
37                         Map<String, Object> expectedStructuredDateFields = (Map<String, Object>) testCases.get(displayDate);
38
39                         StructuredDateInternal expectedStructuredDate = createStructuredDateFromYamlSpec(displayDate, expectedStructuredDateFields);
40                         StructuredDateInternal actualStructuredDate = null;
41
42                         try {
43                                 actualStructuredDate = StructuredDateInternal.parse(displayDate);
44                         }
45                         catch(StructuredDateFormatException e) {
46                                 logger.debug(e.getMessage());
47                         }
48
49                         Assert.assertEquals(actualStructuredDate, expectedStructuredDate);
50                 }
51         }
52
53         private StructuredDateInternal createStructuredDateFromYamlSpec(String displayDate, Map<String, Object> structuredDateFields) {
54                 StructuredDateInternal structuredDate = null;
55
56                 if (structuredDateFields != null) {
57                         if (structuredDateFields.containsKey("latestDate")) {
58                                 Object latestDate = structuredDateFields.get("latestDate");
59
60                                 if (latestDate instanceof String && latestDate.equals("current date")) {
61                                         Date currentDate = DateUtils.getCurrentDate();
62                                         ArrayList latestDateItems = new ArrayList<>();
63
64                                         latestDateItems.add(currentDate.getYear());
65                                         latestDateItems.add(currentDate.getMonth());
66                                         latestDateItems.add(currentDate.getDay());
67                                         latestDateItems.add(currentDate.getEra().toDisplayString());
68
69                                         structuredDateFields.put("latestDate", latestDateItems);
70                                 }
71                         }
72
73                         if (!structuredDateFields.containsKey("displayDate")) {
74                                 structuredDateFields.put("displayDate", displayDate);
75                         }
76
77                         if (!structuredDateFields.containsKey("scalarValuesComputed")) {
78                                 structuredDateFields.put("scalarValuesComputed", true);
79                         }
80
81                         structuredDate = new StructuredDateInternal();
82
83                         for (String propertyName : structuredDateFields.keySet()) {
84                                 Object value = structuredDateFields.get(propertyName);
85
86                                 try {
87                                         Class propertyType = PropertyUtils.getPropertyType(structuredDate, propertyName);
88
89                                         if (propertyType.equals(Date.class)) {
90                                                 value = createDateFromYamlSpec((List<Object>) value);
91                                         }
92
93                                         PropertyUtils.setProperty(structuredDate, propertyName, value);
94                                 }
95                                 catch(NoSuchMethodException e) {
96                                         logger.warn(propertyName + " is not a property");
97                                 }
98                                 catch(InvocationTargetException e) {
99                                         logger.error(propertyName + " accessor threw an exception");
100                                 }
101                                 catch(IllegalAccessException e) {
102                                         logger.error("could not access property " + propertyName);
103                                 }
104                         }
105                 }
106
107                 return structuredDate;
108         }
109
110         private Date createDateFromYamlSpec(List<Object> dateFields) {
111                 Date date = new Date();
112                 Iterator<Object> fieldIterator = dateFields.iterator();
113
114                 for (String propertyName : YAML_DATE_SPEC) {
115                         Object value = fieldIterator.hasNext() ? fieldIterator.next() : null;
116
117                         try {
118                                 Class propertyType = PropertyUtils.getPropertyType(date, propertyName);
119
120                                 if (value != null && Enum.class.isAssignableFrom(propertyType)) {
121                                         value = Enum.valueOf(propertyType, (String) value);
122                                 }
123
124                                 PropertyUtils.setProperty(date, propertyName, value);
125                         }
126                         catch(NoSuchMethodException e) {
127                                 logger.warn(propertyName + " is not a property");
128                         }
129                         catch(InvocationTargetException e) {
130                                 logger.error(propertyName + " accessor threw an exception");
131                         }
132                         catch(IllegalAccessException e) {
133                                 logger.error("could not access property " + propertyName);
134                         }
135                 }
136
137                 return date;
138         }
139 }