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