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