1 package org.collectionspace.services.structureddate;
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Iterator;
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;
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");
22 final Logger logger = LoggerFactory.getLogger(StructuredDateEvaluatorTest.class);
31 Yaml yaml = new Yaml();
32 Map<String, Object> testCases = (Map<String, Object>) yaml.load(getClass().getResourceAsStream(TEST_CASE_FILE));
34 for (String displayDate : testCases.keySet()) {
35 logger.debug("Testing input: " + displayDate);
37 Map<String, Object> expectedStructuredDateFields = (Map<String, Object>) testCases.get(displayDate);
39 StructuredDateInternal expectedStructuredDate = createStructuredDateFromYamlSpec(displayDate, expectedStructuredDateFields);
40 StructuredDateInternal actualStructuredDate = null;
43 actualStructuredDate = StructuredDateInternal.parse(displayDate);
45 catch(StructuredDateFormatException e) {
46 logger.debug(e.getMessage());
49 Assert.assertEquals(actualStructuredDate, expectedStructuredDate);
53 private StructuredDateInternal createStructuredDateFromYamlSpec(String displayDate, Map<String, Object> structuredDateFields) {
54 StructuredDateInternal structuredDate = null;
56 if (structuredDateFields != null) {
57 if (structuredDateFields.containsKey("latestDate")) {
58 Object latestDate = structuredDateFields.get("latestDate");
60 if (latestDate instanceof String && latestDate.equals("current date")) {
61 Date currentDate = DateUtils.getCurrentDate();
62 ArrayList latestDateItems = new ArrayList<>();
64 latestDateItems.add(currentDate.getYear());
65 latestDateItems.add(currentDate.getMonth());
66 latestDateItems.add(currentDate.getDay());
67 latestDateItems.add(currentDate.getEra().toDisplayString());
69 structuredDateFields.put("latestDate", latestDateItems);
73 if (!structuredDateFields.containsKey("displayDate")) {
74 structuredDateFields.put("displayDate", displayDate);
77 if (!structuredDateFields.containsKey("scalarValuesComputed")) {
78 structuredDateFields.put("scalarValuesComputed", true);
81 structuredDate = new StructuredDateInternal();
83 for (String propertyName : structuredDateFields.keySet()) {
84 Object value = structuredDateFields.get(propertyName);
87 Class propertyType = PropertyUtils.getPropertyType(structuredDate, propertyName);
89 if (propertyType.equals(Date.class)) {
90 value = createDateFromYamlSpec((List<Object>) value);
93 PropertyUtils.setProperty(structuredDate, propertyName, value);
95 catch(NoSuchMethodException e) {
96 logger.warn(propertyName + " is not a property");
98 catch(InvocationTargetException e) {
99 logger.error(propertyName + " accessor threw an exception");
101 catch(IllegalAccessException e) {
102 logger.error("could not access property " + propertyName);
107 return structuredDate;
110 private Date createDateFromYamlSpec(List<Object> dateFields) {
111 Date date = new Date();
112 Iterator<Object> fieldIterator = dateFields.iterator();
114 for (String propertyName : YAML_DATE_SPEC) {
115 Object value = fieldIterator.hasNext() ? fieldIterator.next() : null;
118 Class propertyType = PropertyUtils.getPropertyType(date, propertyName);
120 if (value != null && Enum.class.isAssignableFrom(propertyType)) {
121 value = Enum.valueOf(propertyType, (String) value);
124 PropertyUtils.setProperty(date, propertyName, value);
126 catch(NoSuchMethodException e) {
127 logger.warn(propertyName + " is not a property");
129 catch(InvocationTargetException e) {
130 logger.error(propertyName + " accessor threw an exception");
132 catch(IllegalAccessException e) {
133 logger.error("could not access property " + propertyName);