1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.strategy.parser;
|
11
|
|
12
|
import static org.junit.Assert.assertFalse;
|
13
|
import static org.junit.Assert.assertNotNull;
|
14
|
import static org.junit.Assert.assertNull;
|
15
|
import static org.junit.Assert.assertTrue;
|
16
|
import org.junit.Assert;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.joda.time.DateTimeFieldType;
|
20
|
import org.joda.time.Partial;
|
21
|
import org.junit.After;
|
22
|
import org.junit.AfterClass;
|
23
|
import org.junit.Before;
|
24
|
import org.junit.BeforeClass;
|
25
|
import org.junit.Test;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
28
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
29
|
|
30
|
/**
|
31
|
* @author a.mueller
|
32
|
*
|
33
|
*/
|
34
|
public class TimePeriodParserTest {
|
35
|
private static final Logger logger = Logger.getLogger(TimePeriodParserTest.class);
|
36
|
|
37
|
private TimePeriod onlyStartYear;
|
38
|
private TimePeriod onlyEndYear;
|
39
|
private TimePeriod startAndEndYear;
|
40
|
private TimePeriod noStartAndEndYear;
|
41
|
|
42
|
|
43
|
/**
|
44
|
* @throws java.lang.Exception
|
45
|
*/
|
46
|
@BeforeClass
|
47
|
public static void setUpBeforeClass() throws Exception {
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* @throws java.lang.Exception
|
52
|
*/
|
53
|
@AfterClass
|
54
|
public static void tearDownAfterClass() throws Exception {
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* @throws java.lang.Exception
|
59
|
*/
|
60
|
@Before
|
61
|
public void setUp() throws Exception {
|
62
|
onlyStartYear = TimePeriod.NewInstance(1922);
|
63
|
onlyEndYear = TimePeriod.NewInstance(null, 1857);;
|
64
|
startAndEndYear = TimePeriod.NewInstance(1931, 1957);
|
65
|
Integer start = null;
|
66
|
Integer end = null;
|
67
|
noStartAndEndYear = TimePeriod.NewInstance(start, end);;
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* @throws java.lang.Exception
|
72
|
*/
|
73
|
@After
|
74
|
public void tearDown() throws Exception {
|
75
|
}
|
76
|
|
77
|
|
78
|
//************************ TESTS ******************************************
|
79
|
|
80
|
|
81
|
@Test
|
82
|
public void testParseSingleDateString() {
|
83
|
String strDate = "1756";
|
84
|
Partial date = TimePeriodParser.parseSingleDate(strDate);
|
85
|
assertNotNull(date);
|
86
|
Assert.assertEquals(Integer.parseInt(strDate), date.get(DateTimeFieldType.year()));
|
87
|
try {
|
88
|
date.get(DateTimeFieldType.monthOfYear());
|
89
|
assertFalse(true); //should not be reached
|
90
|
} catch (Exception e) {
|
91
|
assertTrue(e instanceof IllegalArgumentException);
|
92
|
}
|
93
|
try {
|
94
|
date.get(DateTimeFieldType.dayOfMonth());
|
95
|
assertFalse(true); //should not be reached
|
96
|
} catch (Exception e) {
|
97
|
assertTrue(e instanceof IllegalArgumentException);
|
98
|
}
|
99
|
//to be continued
|
100
|
}
|
101
|
|
102
|
|
103
|
/**
|
104
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#parseString(java.lang.String)}.
|
105
|
*/
|
106
|
@Test
|
107
|
public void testParseStringString() {
|
108
|
String strTimePeriod = "1756";
|
109
|
TimePeriod tp1 = TimePeriodParser.parseString(strTimePeriod);
|
110
|
assertNotNull(tp1);
|
111
|
Assert.assertEquals(strTimePeriod, tp1.getYear());
|
112
|
Assert.assertEquals(strTimePeriod, String.valueOf(tp1.getStartYear()));
|
113
|
assertNull(tp1.getEnd());
|
114
|
assertNull(tp1.getStartMonth());
|
115
|
strTimePeriod = "1756-88";
|
116
|
tp1 = TimePeriodParser.parseString(strTimePeriod);
|
117
|
assertNotNull(tp1);
|
118
|
Assert.assertEquals("1756-1788", tp1.getYear());
|
119
|
Assert.assertEquals("1756", String.valueOf(tp1.getStartYear()));
|
120
|
Assert.assertEquals("1788", String.valueOf(tp1.getEndYear()));
|
121
|
assertNull(tp1.getEndMonth());
|
122
|
assertNull(tp1.getStartMonth());
|
123
|
//unparsable
|
124
|
String strUnparsablePeriod = "wef 1809-78";
|
125
|
TimePeriod tpUnparsable = TimePeriodParser.parseString(strUnparsablePeriod);
|
126
|
assertNotNull(tpUnparsable);
|
127
|
Assert.assertEquals(strUnparsablePeriod, tpUnparsable.getFreeText());
|
128
|
|
129
|
//"1806"[1807]
|
130
|
String strCorrectedPeriod = "\"1806\"[1807]";
|
131
|
TimePeriod tpcorrected = TimePeriodParser.parseString(strCorrectedPeriod);
|
132
|
assertNotNull(tpcorrected);
|
133
|
Assert.assertEquals(strCorrectedPeriod, tpcorrected.getFreeText());
|
134
|
Assert.assertEquals("1807", tpcorrected.getYear());
|
135
|
|
136
|
|
137
|
//fl. 1806
|
138
|
String strFlPeriod = "fl. 1806?";
|
139
|
TimePeriod tpFl = TimePeriodParser.parseString(strFlPeriod);
|
140
|
assertNotNull(tpFl);
|
141
|
Assert.assertEquals(strFlPeriod, tpFl.getFreeText());
|
142
|
Assert.assertEquals("1806", tpFl.getYear());
|
143
|
|
144
|
String strCPeriod = "c. 1806-1810";
|
145
|
TimePeriod tpC = TimePeriodParser.parseString(strCPeriod);
|
146
|
assertNotNull(tpC);
|
147
|
Assert.assertEquals(strCPeriod, tpC.getFreeText());
|
148
|
Assert.assertEquals(Integer.valueOf(1806), tpC.getStartYear());
|
149
|
Assert.assertEquals(Integer.valueOf(1810), tpC.getEndYear());
|
150
|
Assert.assertEquals("1806-1810", tpC.getYear());
|
151
|
|
152
|
//1.1.2011
|
153
|
String strDotDate = "1.2.2011";
|
154
|
TimePeriod tp = TimePeriodParser.parseString(strDotDate);
|
155
|
assertNotNull(tp);
|
156
|
Assert.assertEquals(strDotDate, tp.toString());
|
157
|
Assert.assertEquals("2011", tp.getYear());
|
158
|
Assert.assertEquals(Integer.valueOf(2), tp.getStartMonth());
|
159
|
Assert.assertEquals(Integer.valueOf(1), tp.getStartDay());
|
160
|
|
161
|
strDotDate = "31.03.2012";
|
162
|
tp = TimePeriodParser.parseString(strDotDate);
|
163
|
assertNotNull(tp);
|
164
|
Assert.assertEquals("31.3.2012", tp.toString());
|
165
|
Assert.assertEquals("2012", tp.getYear());
|
166
|
Assert.assertEquals(Integer.valueOf(3), tp.getStartMonth());
|
167
|
Assert.assertEquals(Integer.valueOf(31), tp.getStartDay());
|
168
|
|
169
|
strDotDate = "00.04.2013";
|
170
|
tp = TimePeriodParser.parseString(strDotDate);
|
171
|
assertNotNull(tp);
|
172
|
Assert.assertEquals("4.2013", tp.toString());
|
173
|
Assert.assertEquals("2013", tp.getYear());
|
174
|
Assert.assertEquals(Integer.valueOf(4), tp.getStartMonth());
|
175
|
Assert.assertEquals(null, tp.getStartDay());
|
176
|
|
177
|
strDotDate = "13.00.2014";
|
178
|
tp = TimePeriodParser.parseString(strDotDate);
|
179
|
assertNotNull(tp);
|
180
|
Assert.assertEquals("13.xx.2014", tp.toString());
|
181
|
Assert.assertEquals("2014", tp.getYear());
|
182
|
Assert.assertEquals(null, tp.getStartMonth());
|
183
|
Assert.assertEquals(Integer.valueOf(13), tp.getStartDay());
|
184
|
|
185
|
strDotDate = "31.12.2015 - 02.01.2016";
|
186
|
tp = TimePeriodParser.parseString(strDotDate);
|
187
|
assertNotNull(tp);
|
188
|
Assert.assertEquals("31.12.2015-2.1.2016", tp.toString());
|
189
|
Assert.assertEquals("2015-2016", tp.getYear());
|
190
|
Assert.assertEquals(Integer.valueOf(2015), tp.getStartYear());
|
191
|
Assert.assertEquals(Integer.valueOf(12), tp.getStartMonth());
|
192
|
Assert.assertEquals(Integer.valueOf(31), tp.getStartDay());
|
193
|
Assert.assertEquals(Integer.valueOf(2016), tp.getEndYear());
|
194
|
Assert.assertEquals(Integer.valueOf(1), tp.getEndMonth());
|
195
|
Assert.assertEquals(Integer.valueOf(2), tp.getEndDay());
|
196
|
}
|
197
|
|
198
|
|
199
|
}
|