1 package org.collectionspace.services.structureddate;
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.Arrays;
5 import java.util.Iterator;
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;
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");
21 final Logger logger = LoggerFactory.getLogger(StructuredDateEvaluatorTest.class);
30 Yaml yaml = new Yaml();
31 Map<String, Object> testCases = (Map<String, Object>) yaml.load(getClass().getResourceAsStream(TEST_CASE_FILE));
33 for (String displayDate : testCases.keySet()) {
34 logger.debug("Testing input: " + displayDate);
36 Map<String, Object> expectedStructuredDateFields = (Map<String, Object>) testCases.get(displayDate);
38 StructuredDate expectedStructuredDate = createStructuredDateFromYamlSpec(displayDate, expectedStructuredDateFields);
39 StructuredDate actualStructuredDate = null;
42 actualStructuredDate = StructuredDate.parse(displayDate);
44 catch(StructuredDateFormatException e) {
45 logger.debug(e.getMessage());
48 Assert.assertEquals(actualStructuredDate, expectedStructuredDate);
52 private StructuredDate createStructuredDateFromYamlSpec(String displayDate, Map<String, Object> structuredDateFields) {
53 StructuredDate structuredDate = null;
55 if (structuredDateFields != null) {
56 structuredDate = new StructuredDate();
58 for (String propertyName : structuredDateFields.keySet()) {
59 Object value = structuredDateFields.get(propertyName);
62 Class propertyType = PropertyUtils.getPropertyType(structuredDate, propertyName);
64 if (propertyType.equals(Date.class)) {
65 value = createDateFromYamlSpec((List<Object>) value);
68 PropertyUtils.setProperty(structuredDate, propertyName, value);
70 catch(NoSuchMethodException e) {
71 logger.warn(propertyName + " is not a property");
73 catch(InvocationTargetException e) {
74 logger.error(propertyName + " accessor threw an exception");
76 catch(IllegalAccessException e) {
77 logger.error("could not access property " + propertyName);
81 if (structuredDate.getDisplayDate() == null) {
82 structuredDate.setDisplayDate(displayDate);
86 return structuredDate;
89 private Date createDateFromYamlSpec(List<Object> dateFields) {
90 Date date = new Date();
91 Iterator<Object> fieldIterator = dateFields.iterator();
93 for (String propertyName : YAML_DATE_SPEC) {
94 Object value = fieldIterator.hasNext() ? fieldIterator.next() : null;
97 Class propertyType = PropertyUtils.getPropertyType(date, propertyName);
99 if (value != null && Enum.class.isAssignableFrom(propertyType)) {
100 value = Enum.valueOf(propertyType, (String) value);
103 PropertyUtils.setProperty(date, propertyName, value);
105 catch(NoSuchMethodException e) {
106 logger.warn(propertyName + " is not a property");
108 catch(InvocationTargetException e) {
109 logger.error(propertyName + " accessor threw an exception");
111 catch(IllegalAccessException e) {
112 logger.error("could not access property " + propertyName);