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.model.common;
|
11
|
|
12
|
import static org.junit.Assert.assertEquals;
|
13
|
import static org.junit.Assert.assertFalse;
|
14
|
import static org.junit.Assert.assertNotNull;
|
15
|
import static org.junit.Assert.assertNull;
|
16
|
import static org.junit.Assert.assertTrue;
|
17
|
|
18
|
import java.time.Instant;
|
19
|
import java.time.LocalDate;
|
20
|
import java.time.LocalDateTime;
|
21
|
import java.time.Year;
|
22
|
import java.time.ZoneId;
|
23
|
import java.time.temporal.ChronoField;
|
24
|
import java.time.temporal.ChronoUnit;
|
25
|
import java.time.temporal.Temporal;
|
26
|
import java.util.Calendar;
|
27
|
|
28
|
import org.apache.log4j.Logger;
|
29
|
import org.junit.After;
|
30
|
import org.junit.Assert;
|
31
|
import org.junit.Before;
|
32
|
import org.junit.Ignore;
|
33
|
import org.junit.Test;
|
34
|
|
35
|
import eu.etaxonomy.cdm.strategy.cache.common.TimePeriodPartialFormatter;
|
36
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
37
|
|
38
|
/**
|
39
|
* @author a.mueller
|
40
|
*
|
41
|
*/
|
42
|
public class TimePeriodTest {
|
43
|
@SuppressWarnings("unused")
|
44
|
private static final Logger logger = Logger.getLogger(TimePeriodTest.class);
|
45
|
|
46
|
private TimePeriod onlyStartYear;
|
47
|
private TimePeriod onlyEndYear;
|
48
|
private TimePeriod startAndEndYear;
|
49
|
private TimePeriod noStartAndEndYear;
|
50
|
private static final Integer year = 1982;
|
51
|
private static final Integer month = 1;
|
52
|
private static final Integer day = 5;
|
53
|
|
54
|
|
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 method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance()}.
|
82
|
*/
|
83
|
@Test
|
84
|
public void testNewInstance() {
|
85
|
TimePeriod tp = TimePeriod.NewInstance();
|
86
|
Assert.assertNotNull(tp);
|
87
|
Assert.assertTrue("Timeperiod should be empty",tp.isEmpty());
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance(org.joda.time.Partial)}.
|
92
|
*/
|
93
|
@Test
|
94
|
public void testNewInstancePartial() {
|
95
|
TimePeriod tp = TimePeriod.NewInstance(Year.of(1987));
|
96
|
Assert.assertNotNull(tp);
|
97
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance(org.joda.time.Partial, org.joda.time.Partial)}.
|
102
|
* TODO: this was a test for a date with only a day available, this is not as easy as with joda time but maybe we find a better solution
|
103
|
*/
|
104
|
@Test
|
105
|
public void testNewInstancePartialPartial() {
|
106
|
TimePeriod tp = TimePeriod.NewInstance(Year.of(1982));
|
107
|
Assert.assertNotNull(tp);
|
108
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
109
|
Assert.assertEquals("Timeperiod's should not be empty", year, tp.getStartYear());
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance(java.lang.Integer)}.
|
114
|
*/
|
115
|
@Test
|
116
|
public void testNewInstanceInteger() {
|
117
|
onlyStartYear = TimePeriod.NewInstance(1922);
|
118
|
assertEquals(Integer.valueOf(1922), onlyStartYear.getStartYear());
|
119
|
assertNull(onlyStartYear.getEndYear());
|
120
|
assertEquals("1922", onlyStartYear.getYear());
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance(java.lang.Integer, java.lang.Integer)}.
|
125
|
*/
|
126
|
@Test
|
127
|
public void testNewInstanceIntegerInteger() {
|
128
|
startAndEndYear = TimePeriod.NewInstance(1931, 1957);
|
129
|
assertEquals(Integer.valueOf(1957), startAndEndYear.getEndYear());
|
130
|
assertEquals(Integer.valueOf(1931), startAndEndYear.getStartYear());
|
131
|
assertEquals("1931-1957", startAndEndYear.getYear());
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance(java.util.Calendar)}.
|
136
|
*/
|
137
|
@Test
|
138
|
public void testNewInstanceCalendar() {
|
139
|
Calendar cal = Calendar.getInstance();
|
140
|
cal.set(1982, 1, day);
|
141
|
TimePeriod tp = TimePeriod.NewInstance(cal);
|
142
|
Assert.assertNotNull(tp);
|
143
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
144
|
Assert.assertEquals("Timeperiod's should not be empty", day, tp.getStartDay());
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance(java.util.Calendar, java.util.Calendar)}.
|
149
|
*/
|
150
|
@Test
|
151
|
public void testNewInstanceCalendarCalendar() {
|
152
|
Calendar cal = Calendar.getInstance();
|
153
|
cal.set(year, month, day);
|
154
|
Calendar cal2 = Calendar.getInstance();
|
155
|
Integer day2 = 20;
|
156
|
Integer month2 = 7;
|
157
|
Integer year2 = 1985;
|
158
|
cal2.set(year2, month2, day2);
|
159
|
TimePeriod tp = TimePeriod.NewInstance(cal, cal2);
|
160
|
Assert.assertNotNull(tp);
|
161
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
162
|
Assert.assertEquals("Timeperiod's start should not be equal with cal1", year, tp.getStartYear());
|
163
|
//Calendar starts counting months with 0
|
164
|
Assert.assertEquals("Timeperiod's start should not be equal with cal1 ", (Integer)(month +1), tp.getStartMonth());
|
165
|
Assert.assertEquals("Timeperiod's start should not be equal with cal1", day, tp.getStartDay());
|
166
|
|
167
|
Assert.assertEquals("Timeperiod's start should not be equal with cal2", year2, tp.getEndYear());
|
168
|
//Calendar starts counting months with 0
|
169
|
Assert.assertEquals("Timeperiod's start should not be equal with cal2", (Integer)(month2+1), tp.getEndMonth());
|
170
|
Assert.assertEquals("Timeperiod's end should not be equal with cal2", day2, tp.getEndDay());
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance(org.joda.time.ReadableInstant)}.
|
175
|
*/
|
176
|
@Test
|
177
|
public void testNewInstanceReadableInstant() {
|
178
|
Instant readInst = Instant.now();
|
179
|
LocalDateTime ld = LocalDateTime.ofInstant(readInst,ZoneId.systemDefault());
|
180
|
TimePeriod tp = TimePeriod.NewInstance(readInst);
|
181
|
Assert.assertNotNull(tp);
|
182
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
183
|
Assert.assertEquals("Timeperiod's should not be empty", (Integer)ld.get(ChronoField.DAY_OF_MONTH), tp.getStartDay());
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#NewInstance(org.joda.time.ReadableInstant, org.joda.time.ReadableInstant)}.
|
188
|
*/
|
189
|
@Test
|
190
|
public void testNewInstanceReadableInstantReadableInstant() {
|
191
|
Instant readInst = Instant.now();
|
192
|
Instant readInst2 = Instant.now();
|
193
|
readInst2 = readInst.plus(5, ChronoUnit.DAYS);
|
194
|
LocalDateTime ld = LocalDateTime.ofInstant(readInst,ZoneId.systemDefault());
|
195
|
LocalDateTime ld2 = LocalDateTime.ofInstant(readInst2,ZoneId.systemDefault());
|
196
|
TimePeriod tp = TimePeriod.NewInstance(readInst, readInst2);
|
197
|
|
198
|
Assert.assertNotNull(tp);
|
199
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
200
|
Assert.assertEquals("Timeperiod's day should not be equal to readable instant", (Integer)ld.get(ChronoField.DAY_OF_MONTH), tp.getStartDay());
|
201
|
Assert.assertEquals("Timeperiod's day should not be equal to readable instant", (Integer)ld2.get(ChronoField.DAY_OF_MONTH), tp.getEndDay());
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#calendarToPartial(java.util.Calendar)}.
|
206
|
*/
|
207
|
@Test
|
208
|
public void testCalendarToPartial() {
|
209
|
Calendar cal = Calendar.getInstance();
|
210
|
cal.set(year, month, day);
|
211
|
LocalDate part = TimePeriod.calendarToPartial(cal);
|
212
|
Assert.assertEquals("Partial's day should not be equal to calednars day", day, (Integer)part.get(ChronoField.DAY_OF_MONTH));
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#readableInstantToPartial(org.joda.time.ReadableInstant)}.
|
217
|
*/
|
218
|
@Test
|
219
|
public void testReadableInstantToPartial() {
|
220
|
Instant readInst = Instant.now();
|
221
|
|
222
|
LocalDate part = TimePeriod.readableInstantToPartial(readInst);
|
223
|
part.get(ChronoField.DAY_OF_MONTH);
|
224
|
Assert.assertEquals("Partial's day should not be equal to calednars day", (Integer)part.get(ChronoField.DAY_OF_MONTH), (Integer)part.get(ChronoField.DAY_OF_MONTH));
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#TimePeriod()}.
|
229
|
*/
|
230
|
@Test
|
231
|
public void testTimePeriod() {
|
232
|
TimePeriod tp = new TimePeriod();
|
233
|
Assert.assertNotNull("Time period must be created",tp);
|
234
|
}
|
235
|
|
236
|
// @Ignore
|
237
|
@Test
|
238
|
public void testSetStart(){
|
239
|
LocalDate startDate = LocalDate.of(2010,12,16);
|
240
|
|
241
|
LocalDate newStartDate = LocalDate.of(1984, 12, 14);
|
242
|
|
243
|
TimePeriod tp = TimePeriod.NewInstance(startDate);
|
244
|
String startString = tp.toString();
|
245
|
assertNull("Freetext should be not set", tp.getFreeText());
|
246
|
tp.setStart(newStartDate);
|
247
|
String changedString = tp.toString();
|
248
|
Assert.assertTrue("Setting the partial should change the string representation of the TimePeriod", !startString.equals(changedString));
|
249
|
|
250
|
//
|
251
|
tp = TimePeriodParser.parseString("1752");
|
252
|
assertNull("Freetext should be not set", tp.getFreeText());
|
253
|
startString = tp.toString();
|
254
|
tp.setStart(newStartDate);
|
255
|
changedString = tp.toString();
|
256
|
Assert.assertTrue("Setting a partial for a parsed time period should change the string representation of the TimePeriod ", !startString.equals(changedString));
|
257
|
|
258
|
//
|
259
|
tp = TimePeriodParser.parseString("any strange date");
|
260
|
assertNotNull("Freetext should be set", tp.getFreeText());
|
261
|
startString = tp.toString();
|
262
|
tp.setStart(newStartDate);
|
263
|
changedString = tp.toString();
|
264
|
Assert.assertEquals("Setting a partial for a time period having the freetext set should not change the string representation of the TimePeriod ", startString, changedString);
|
265
|
|
266
|
|
267
|
//
|
268
|
// tp = TimePeriodParser.parseString("15.12.1730"); //TODO currently not parsed
|
269
|
//
|
270
|
// startString = tp.toString();
|
271
|
// tp.setStart(newStartDate);
|
272
|
// changedString = tp.toString();
|
273
|
//
|
274
|
// Assert.assertTrue("Setting a partial for a parsed time period should change the string representation of the TimePeriod ", !startString.equals(changedString));
|
275
|
}
|
276
|
|
277
|
/**
|
278
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#isPeriod()}.
|
279
|
*/
|
280
|
@Test
|
281
|
public void testIsPeriod() {
|
282
|
assertTrue(startAndEndYear.isPeriod());
|
283
|
assertFalse(onlyStartYear.isPeriod());
|
284
|
assertFalse(onlyEndYear.isPeriod());
|
285
|
assertFalse(noStartAndEndYear.isPeriod());
|
286
|
// this is not allowed anymore
|
287
|
// onlyStartYear.setEndDay(14);
|
288
|
// assertFalse(onlyStartYear.isPeriod());
|
289
|
onlyStartYear.setEndYear(1988);
|
290
|
assertTrue(onlyStartYear.isPeriod()); //may be discussed
|
291
|
}
|
292
|
|
293
|
/**
|
294
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getStart()}.
|
295
|
*/
|
296
|
@Test
|
297
|
public void testGetStart() {
|
298
|
TimePeriod tp = new TimePeriod();
|
299
|
Temporal start = Year.of(1999);
|
300
|
tp.setStart(start);
|
301
|
Assert.assertEquals("Start year should be 1999", Integer.valueOf(1999), tp.getStartYear());
|
302
|
Assert.assertEquals("Start should be 'start'", start, tp.getStart());
|
303
|
}
|
304
|
|
305
|
|
306
|
/**
|
307
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getEnd()}.
|
308
|
*/
|
309
|
@Test
|
310
|
public void testGetEnd() {
|
311
|
TimePeriod tp = new TimePeriod();
|
312
|
Temporal end = Year.of(1999);
|
313
|
tp.setEnd(end);
|
314
|
Assert.assertEquals("End year should be 1999", Integer.valueOf(1999), tp.getEndYear());
|
315
|
Assert.assertEquals("End should be 'end'", end, tp.getEnd());
|
316
|
}
|
317
|
|
318
|
/**
|
319
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getYear()}.
|
320
|
*/
|
321
|
@Test
|
322
|
public void testGetYear() {
|
323
|
TimePeriod tp = new TimePeriod();
|
324
|
tp.setStartYear(1999);
|
325
|
Assert.assertEquals("Year should be 1999", "1999", tp.getYear());
|
326
|
tp.setEndYear(2002);
|
327
|
Assert.assertEquals("Year should be 1999-2002", "1999-2002", tp.getYear());
|
328
|
}
|
329
|
|
330
|
|
331
|
/**
|
332
|
* TODO should be partly moved to a test class for {@link TimePeriodPartialFormatter}
|
333
|
*/
|
334
|
@Test
|
335
|
@Ignore
|
336
|
public void testToStringTimePeriod() {
|
337
|
TimePeriod tp1 = TimePeriod.NewInstance(1788,1799);
|
338
|
assertNotNull(tp1);
|
339
|
// TODO: fix the possibility to have missing month
|
340
|
// Assert.assertEquals("1788-1799", tp1.toString());
|
341
|
// tp1.setStartDay(3);
|
342
|
|
343
|
Assert.assertEquals("3.xx.1788-1799", tp1.toString());
|
344
|
tp1.setEndMonth(11);
|
345
|
Assert.assertEquals("3.xx.1788-11.1799", tp1.toString());
|
346
|
}
|
347
|
|
348
|
|
349
|
/**
|
350
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#clone()}.
|
351
|
*/
|
352
|
@Test
|
353
|
public void testClone() {
|
354
|
Integer startYear = 1788;
|
355
|
Integer startMonth = 6;
|
356
|
Integer startDay = 25;
|
357
|
Integer endDay = 21;
|
358
|
Integer endMonth = 12;
|
359
|
Integer endYear = 1799;
|
360
|
String freeText = "A free period";
|
361
|
TimePeriod tp1 = TimePeriod.NewInstance(startYear,endYear);
|
362
|
tp1.setStartDay(startDay);
|
363
|
tp1.setStartMonth(startMonth);
|
364
|
tp1.setEndDay(endDay);
|
365
|
tp1.setEndMonth(endMonth);
|
366
|
tp1.setFreeText(freeText);
|
367
|
TimePeriod tpClone = (TimePeriod)tp1.clone();
|
368
|
Assert.assertEquals("Start year must be 1788.", startYear, tpClone.getStartYear());
|
369
|
Assert.assertEquals("Start month must be 6.", startMonth, tpClone.getStartMonth());
|
370
|
Assert.assertEquals("Start day must be 25.", startDay, tpClone.getStartDay());
|
371
|
Assert.assertEquals("End year must be 1799.", endYear, tpClone.getEndYear());
|
372
|
Assert.assertEquals("End month must be 12.", endMonth, tpClone.getEndMonth());
|
373
|
Assert.assertEquals("End day must be 21.", endDay, tpClone.getEndDay());
|
374
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
375
|
}
|
376
|
|
377
|
/**
|
378
|
* Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#clone()}.
|
379
|
*/
|
380
|
@Test
|
381
|
public void testEquals() {
|
382
|
Integer startYear = 1788;
|
383
|
Integer startMonth = 6;
|
384
|
Integer endDay = 21;
|
385
|
Integer endYear = 1799;
|
386
|
String freeText = "A free period";
|
387
|
|
388
|
TimePeriod tp1 = TimePeriod.NewInstance(startYear);
|
389
|
TimePeriod tpClone = (TimePeriod)tp1.clone();
|
390
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
391
|
|
392
|
tp1.setStartMonth(startMonth);
|
393
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
394
|
tpClone = (TimePeriod)tp1.clone();
|
395
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
396
|
|
397
|
|
398
|
tp1.setEndYear(endYear);
|
399
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
400
|
tpClone = (TimePeriod)tp1.clone();
|
401
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
402
|
|
403
|
tp1.setEndDay(endDay);
|
404
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
405
|
tpClone = (TimePeriod)tp1.clone();
|
406
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
407
|
|
408
|
tp1.setFreeText(freeText);
|
409
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
410
|
tpClone = (TimePeriod)tp1.clone();
|
411
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
412
|
|
413
|
tp1 = TimePeriod.NewInstance();
|
414
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
415
|
TimePeriod tp2 = TimePeriod.NewInstance();
|
416
|
Assert.assertEquals("Empty time periods must be equal", tp1, tp2);
|
417
|
|
418
|
tp1.setFreeText(freeText);
|
419
|
Assert.assertFalse("Tp2 must not be equal to originial", tp1.equals(tp2));
|
420
|
tp2.setFreeText("jldskjlfi");
|
421
|
Assert.assertFalse("Tp2 must not be equal to originial", tp1.equals(tpClone));
|
422
|
tp2.setFreeText(freeText);
|
423
|
Assert.assertEquals("Tp2 must be equal", tp1, tp2);
|
424
|
}
|
425
|
|
426
|
|
427
|
|
428
|
}
|