]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3d487babb323d1f0040baa735ea78b8db552fdc8
[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                 // Can and should we calculate today's date for the "AFTER" dates?
58                 if (structuredDateFields != null && structuredDateFields.containsKey("latestDate")) {
59                         if (structuredDateFields.get("latestDate").toString().equals("current date")) {
60                                 ArrayList items = new ArrayList<>();
61                                 Date currentDate = DateUtils.getCurrentDate();
62                                 items.add(currentDate.getYear());
63                                 items.add(currentDate.getMonth());
64                                 items.add(currentDate.getDay());
65                                 items.add(Date.DEFAULT_ERA.toString().toUpperCase());
66                                 structuredDateFields.put("latestDate", items);
67                         }
68                 }
69
70                 if (structuredDateFields != null) {
71                         structuredDate = new StructuredDateInternal();
72
73                         for (String propertyName : structuredDateFields.keySet()) {
74                                 Object value = structuredDateFields.get(propertyName);
75
76                                 try {
77                                         Class propertyType = PropertyUtils.getPropertyType(structuredDate, propertyName);
78
79                                         if (propertyType.equals(Date.class)) {
80                                                 value = createDateFromYamlSpec((List<Object>) value);
81                                         }
82
83                                         PropertyUtils.setProperty(structuredDate, propertyName, value);
84                                 }
85                                 catch(NoSuchMethodException e) {
86                                         logger.warn(propertyName + " is not a property");
87                                 }
88                                 catch(InvocationTargetException e) {
89                                         logger.error(propertyName + " accessor threw an exception");
90                                 }
91                                 catch(IllegalAccessException e) {
92                                         logger.error("could not access property " + propertyName);
93                                 }
94                         }
95                         
96                         if (structuredDate.getDisplayDate() == null) {
97                                 structuredDate.setDisplayDate(displayDate);
98                         }
99                 }
100
101                 return structuredDate;
102         }
103
104         private Date createDateFromYamlSpec(List<Object> dateFields) {
105                 Date date = new Date();
106                 Iterator<Object> fieldIterator = dateFields.iterator();
107
108                 for (String propertyName : YAML_DATE_SPEC) {
109                         Object value = fieldIterator.hasNext() ? fieldIterator.next() : null;
110
111                         try {
112                                 Class propertyType = PropertyUtils.getPropertyType(date, propertyName);
113
114                                 if (value != null && Enum.class.isAssignableFrom(propertyType)) {
115                                         value = Enum.valueOf(propertyType, (String) value);
116                                 }
117
118                                 PropertyUtils.setProperty(date, propertyName, value);
119                         }
120                         catch(NoSuchMethodException e) {
121                                 logger.warn(propertyName + " is not a property");
122                         }
123                         catch(InvocationTargetException e) {
124                                 logger.error(propertyName + " accessor threw an exception");
125                         }
126                         catch(IllegalAccessException e) {
127                                 logger.error("could not access property " + propertyName);
128                         }               
129                 }
130
131                 return date;
132         }
133 }