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
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.joda.time.DateTimeFieldType;
|
19
|
import org.joda.time.Partial;
|
20
|
import org.junit.After;
|
21
|
import org.junit.AfterClass;
|
22
|
import org.junit.Assert;
|
23
|
import org.junit.Before;
|
24
|
import org.junit.BeforeClass;
|
25
|
import org.junit.Test;
|
26
|
|
27
|
import eu.etaxonomy.cdm.common.UTF8;
|
28
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
29
|
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
*
|
34
|
*/
|
35
|
public class TimePeriodParserTest {
|
36
|
@SuppressWarnings("unused")
|
37
|
private static final Logger logger = Logger.getLogger(TimePeriodParserTest.class);
|
38
|
|
39
|
// private TimePeriod onlyStartYear;
|
40
|
// private TimePeriod onlyEndYear;
|
41
|
// private TimePeriod startAndEndYear;
|
42
|
// private TimePeriod noStartAndEndYear;
|
43
|
|
44
|
|
45
|
/**
|
46
|
* @throws java.lang.Exception
|
47
|
*/
|
48
|
@BeforeClass
|
49
|
public static void setUpBeforeClass() throws Exception {
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* @throws java.lang.Exception
|
54
|
*/
|
55
|
@AfterClass
|
56
|
public static void tearDownAfterClass() throws Exception {
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* @throws java.lang.Exception
|
61
|
*/
|
62
|
@Before
|
63
|
public void setUp() throws Exception {
|
64
|
// onlyStartYear = TimePeriod.NewInstance(1922);
|
65
|
// onlyEndYear = TimePeriod.NewInstance(null, 1857);
|
66
|
// startAndEndYear = TimePeriod.NewInstance(1931, 1957);
|
67
|
// Integer start = null;
|
68
|
// Integer end = null;
|
69
|
// noStartAndEndYear = TimePeriod.NewInstance(start, end);
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* @throws java.lang.Exception
|
74
|
*/
|
75
|
@After
|
76
|
public void tearDown() throws Exception {
|
77
|
}
|
78
|
|
79
|
|
80
|
//************************ TESTS ******************************************
|
81
|
|
82
|
|
83
|
@Test
|
84
|
public void testParseSingleDateString() {
|
85
|
String strDate = "1756";
|
86
|
Partial date = TimePeriodParser.parseSingleDate(strDate);
|
87
|
assertNotNull(date);
|
88
|
Assert.assertEquals(Integer.parseInt(strDate), date.get(DateTimeFieldType.year()));
|
89
|
try {
|
90
|
date.get(DateTimeFieldType.monthOfYear());
|
91
|
assertFalse(true); //should not be reached
|
92
|
} catch (Exception e) {
|
93
|
assertTrue(e instanceof IllegalArgumentException);
|
94
|
}
|
95
|
try {
|
96
|
date.get(DateTimeFieldType.dayOfMonth());
|
97
|
assertFalse(true); //should not be reached
|
98
|
} catch (Exception e) {
|
99
|
assertTrue(e instanceof IllegalArgumentException);
|
100
|
}
|
101
|
//to be continued
|
102
|
}
|
103
|
|
104
|
|
105
|
/**
|
106
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#parseString(java.lang.String)}.
|
107
|
*/
|
108
|
@Test
|
109
|
public void testParseStringString() {
|
110
|
String strTimePeriod = "1756";
|
111
|
TimePeriod tp1 = TimePeriodParser.parseString(strTimePeriod);
|
112
|
assertNotNull(tp1);
|
113
|
Assert.assertEquals(strTimePeriod, tp1.getYear());
|
114
|
Assert.assertEquals(strTimePeriod, String.valueOf(tp1.getStartYear()));
|
115
|
assertNull(tp1.getEnd());
|
116
|
assertNull(tp1.getStartMonth());
|
117
|
strTimePeriod = "1756-88";
|
118
|
tp1 = TimePeriodParser.parseString(strTimePeriod);
|
119
|
assertNotNull(tp1);
|
120
|
Assert.assertEquals("1756-1788", tp1.getYear());
|
121
|
Assert.assertEquals("1756", String.valueOf(tp1.getStartYear()));
|
122
|
Assert.assertEquals("1788", String.valueOf(tp1.getEndYear()));
|
123
|
assertNull(tp1.getEndMonth());
|
124
|
assertNull(tp1.getStartMonth());
|
125
|
//unparsable
|
126
|
String strUnparsablePeriod = "wef 1809-78";
|
127
|
TimePeriod tpUnparsable = TimePeriodParser.parseString(strUnparsablePeriod);
|
128
|
assertNotNull(tpUnparsable);
|
129
|
Assert.assertEquals(strUnparsablePeriod, tpUnparsable.getFreeText());
|
130
|
|
131
|
//"1806"[1807]
|
132
|
String strCorrectedPeriod = "\"1806\"[1807]";
|
133
|
TimePeriod tpcorrected = TimePeriodParser.parseString(strCorrectedPeriod);
|
134
|
assertNotNull(tpcorrected);
|
135
|
Assert.assertEquals(strCorrectedPeriod, tpcorrected.getFreeText());
|
136
|
Assert.assertEquals("1807", tpcorrected.getYear());
|
137
|
|
138
|
//„1806‟[1807]
|
139
|
String strCorrectedEnPeriod = UTF8.QUOT_DBL_LOW9 + "1806"+UTF8.QUOT_DBL_HIGH_REV9+"[1807]";
|
140
|
TimePeriod tpcorrectedEn = TimePeriodParser.parseString(strCorrectedEnPeriod);
|
141
|
assertNotNull(tpcorrectedEn);
|
142
|
Assert.assertEquals(strCorrectedEnPeriod, tpcorrectedEn.getFreeText());
|
143
|
Assert.assertEquals("1807", tpcorrectedEn.getYear());
|
144
|
|
145
|
|
146
|
//fl. 1806
|
147
|
String strFlPeriod = "fl. 1806?";
|
148
|
TimePeriod tpFl = TimePeriodParser.parseString(strFlPeriod);
|
149
|
assertNotNull(tpFl);
|
150
|
Assert.assertEquals(strFlPeriod, tpFl.getFreeText());
|
151
|
Assert.assertEquals("1806", tpFl.getYear());
|
152
|
|
153
|
String strCPeriod = "c. 1806-1810";
|
154
|
TimePeriod tpC = TimePeriodParser.parseString(strCPeriod);
|
155
|
assertNotNull(tpC);
|
156
|
Assert.assertEquals(strCPeriod, tpC.getFreeText());
|
157
|
Assert.assertEquals(Integer.valueOf(1806), tpC.getStartYear());
|
158
|
Assert.assertEquals(Integer.valueOf(1810), tpC.getEndYear());
|
159
|
Assert.assertEquals("1806-1810", tpC.getYear());
|
160
|
|
161
|
//1.1.2011
|
162
|
String strDotDate = "1.2.2011";
|
163
|
TimePeriod tp = TimePeriodParser.parseString(strDotDate);
|
164
|
assertNotNull(tp);
|
165
|
Assert.assertEquals(strDotDate, tp.toString());
|
166
|
Assert.assertEquals("2011", tp.getYear());
|
167
|
Assert.assertEquals(Integer.valueOf(2), tp.getStartMonth());
|
168
|
Assert.assertEquals(Integer.valueOf(1), tp.getStartDay());
|
169
|
|
170
|
strDotDate = "31.03.2012";
|
171
|
tp = TimePeriodParser.parseString(strDotDate);
|
172
|
assertNotNull(tp);
|
173
|
Assert.assertEquals("31.3.2012", tp.toString());
|
174
|
Assert.assertEquals("2012", tp.getYear());
|
175
|
Assert.assertEquals(Integer.valueOf(3), tp.getStartMonth());
|
176
|
Assert.assertEquals(Integer.valueOf(31), tp.getStartDay());
|
177
|
|
178
|
strDotDate = "00.04.2013";
|
179
|
tp = TimePeriodParser.parseString(strDotDate);
|
180
|
assertNotNull(tp);
|
181
|
Assert.assertEquals("4.2013", tp.toString());
|
182
|
Assert.assertEquals("2013", tp.getYear());
|
183
|
Assert.assertEquals(Integer.valueOf(4), tp.getStartMonth());
|
184
|
Assert.assertEquals(null, tp.getStartDay());
|
185
|
|
186
|
strDotDate = "13.00.2014";
|
187
|
tp = TimePeriodParser.parseString(strDotDate);
|
188
|
assertNotNull(tp);
|
189
|
Assert.assertEquals("13.xx.2014", tp.toString());
|
190
|
Assert.assertEquals("2014", tp.getYear());
|
191
|
Assert.assertEquals(null, tp.getStartMonth());
|
192
|
Assert.assertEquals(Integer.valueOf(13), tp.getStartDay());
|
193
|
|
194
|
strDotDate = "31.12.2015 - 02.01.2016";
|
195
|
tp = TimePeriodParser.parseString(strDotDate);
|
196
|
assertNotNull(tp);
|
197
|
Assert.assertEquals("31.12.2015-2.1.2016", tp.toString());
|
198
|
Assert.assertEquals("2015-2016", tp.getYear());
|
199
|
Assert.assertEquals(Integer.valueOf(2015), tp.getStartYear());
|
200
|
Assert.assertEquals(Integer.valueOf(12), tp.getStartMonth());
|
201
|
Assert.assertEquals(Integer.valueOf(31), tp.getStartDay());
|
202
|
Assert.assertEquals(Integer.valueOf(2016), tp.getEndYear());
|
203
|
Assert.assertEquals(Integer.valueOf(1), tp.getEndMonth());
|
204
|
Assert.assertEquals(Integer.valueOf(2), tp.getEndDay());
|
205
|
|
206
|
String strSlashDate = "31/12/2015 - 2/1/2016";
|
207
|
tp = TimePeriodParser.parseString(strSlashDate);
|
208
|
assertNotNull(tp);
|
209
|
Assert.assertEquals("31.12.2015-2.1.2016", tp.toString());
|
210
|
Assert.assertEquals("2015-2016", tp.getYear());
|
211
|
Assert.assertEquals(Integer.valueOf(2015), tp.getStartYear());
|
212
|
Assert.assertEquals(Integer.valueOf(12), tp.getStartMonth());
|
213
|
Assert.assertEquals(Integer.valueOf(31), tp.getStartDay());
|
214
|
Assert.assertEquals(Integer.valueOf(2016), tp.getEndYear());
|
215
|
Assert.assertEquals(Integer.valueOf(1), tp.getEndMonth());
|
216
|
Assert.assertEquals(Integer.valueOf(2), tp.getEndDay());
|
217
|
|
218
|
strSlashDate = "1/12/2015 - 2/1/2016";
|
219
|
tp = TimePeriodParser.parseString(strSlashDate);
|
220
|
assertNotNull(tp);
|
221
|
Assert.assertEquals("1.12.2015-2.1.2016", tp.toString());
|
222
|
Assert.assertEquals("2015-2016", tp.getYear());
|
223
|
Assert.assertEquals(Integer.valueOf(2015), tp.getStartYear());
|
224
|
Assert.assertEquals(Integer.valueOf(12), tp.getStartMonth());
|
225
|
Assert.assertEquals(Integer.valueOf(1), tp.getStartDay());
|
226
|
Assert.assertEquals(Integer.valueOf(2016), tp.getEndYear());
|
227
|
Assert.assertEquals(Integer.valueOf(1), tp.getEndMonth());
|
228
|
Assert.assertEquals(Integer.valueOf(2), tp.getEndDay());
|
229
|
}
|
230
|
|
231
|
@Test
|
232
|
public void testParseDateWithMonths() {
|
233
|
String strDate = "24 Aug. 1957";
|
234
|
TimePeriod tp = TimePeriodParser.parseString(strDate);
|
235
|
assertNotNull(tp);
|
236
|
Assert.assertEquals("24.8.1957", tp.toString());
|
237
|
Assert.assertEquals("1957", tp.getYear());
|
238
|
Assert.assertEquals(Integer.valueOf(1957), tp.getStartYear());
|
239
|
Assert.assertEquals(Integer.valueOf(8), tp.getStartMonth());
|
240
|
Assert.assertEquals(Integer.valueOf(24), tp.getStartDay());
|
241
|
|
242
|
String strSingleDay = "8 March 1957";
|
243
|
tp = TimePeriodParser.parseString(strSingleDay);
|
244
|
assertNotNull(tp);
|
245
|
Assert.assertEquals("8.3.1957", tp.toString());
|
246
|
Assert.assertEquals("1957", tp.getYear());
|
247
|
Assert.assertEquals(Integer.valueOf(1957), tp.getStartYear());
|
248
|
Assert.assertEquals(Integer.valueOf(3), tp.getStartMonth());
|
249
|
Assert.assertEquals(Integer.valueOf(8), tp.getStartDay());
|
250
|
|
251
|
String strNoSpace = "26.Apr.2003";
|
252
|
tp = TimePeriodParser.parseString(strNoSpace);
|
253
|
assertNotNull(tp);
|
254
|
Assert.assertEquals("26.4.2003", tp.toString());
|
255
|
Assert.assertEquals("2003", tp.getYear());
|
256
|
Assert.assertEquals(Integer.valueOf(2003), tp.getStartYear());
|
257
|
Assert.assertEquals(Integer.valueOf(4), tp.getStartMonth());
|
258
|
Assert.assertEquals(Integer.valueOf(26), tp.getStartDay());
|
259
|
|
260
|
String strMissingDay = "Feb. 1894";
|
261
|
tp = TimePeriodParser.parseString(strMissingDay);
|
262
|
assertNotNull(tp);
|
263
|
Assert.assertEquals("2.1894", tp.toString());
|
264
|
Assert.assertEquals("1894", tp.getYear());
|
265
|
Assert.assertEquals(Integer.valueOf(1894), tp.getStartYear());
|
266
|
Assert.assertEquals(Integer.valueOf(2), tp.getStartMonth());
|
267
|
Assert.assertEquals(null, tp.getStartDay());
|
268
|
|
269
|
}
|
270
|
|
271
|
|
272
|
@Test
|
273
|
public void testParseVerbatim() {
|
274
|
String strDate = "1957 [\"1958\"]";
|
275
|
VerbatimTimePeriod tp = TimePeriodParser.parseStringVerbatim(strDate);
|
276
|
assertNotNull(tp);
|
277
|
Assert.assertEquals(strDate, tp.toString());
|
278
|
Assert.assertEquals("1957", tp.getYear());
|
279
|
Assert.assertEquals(Integer.valueOf(1957), tp.getStartYear());
|
280
|
// Assert.assertEquals(Integer.valueOf(8), tp.getStartMonth());
|
281
|
// Assert.assertEquals(Integer.valueOf(24), tp.getStartDay());
|
282
|
Assert.assertEquals("1958", tp.getVerbatimDate());
|
283
|
|
284
|
}
|
285
|
|
286
|
|
287
|
|
288
|
}
|