1
|
/**
|
2
|
* Copyright (C) 2018 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
|
package eu.etaxonomy.cdm.model.common;
|
10
|
|
11
|
import static org.junit.Assert.assertEquals;
|
12
|
import static org.junit.Assert.assertFalse;
|
13
|
import static org.junit.Assert.assertNull;
|
14
|
import static org.junit.Assert.assertTrue;
|
15
|
|
16
|
import java.util.Calendar;
|
17
|
|
18
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
19
|
import org.joda.time.DateTimeFieldType;
|
20
|
import org.joda.time.MutableDateTime;
|
21
|
import org.joda.time.Partial;
|
22
|
import org.joda.time.ReadableInstant;
|
23
|
import org.junit.Assert;
|
24
|
import org.junit.Before;
|
25
|
import org.junit.Test;
|
26
|
|
27
|
/**
|
28
|
* @author a.mueller
|
29
|
* @since 29.04.2020
|
30
|
*/
|
31
|
public class ExtendedTimePeriodTest {
|
32
|
|
33
|
@SuppressWarnings("unused")
|
34
|
private static final Logger logger = LogManager.getLogger(ExtendedTimePeriodTest.class);
|
35
|
|
36
|
private ExtendedTimePeriod onlyStartYear;
|
37
|
private ExtendedTimePeriod onlyEndYear;
|
38
|
private ExtendedTimePeriod startAndEndYear;
|
39
|
private ExtendedTimePeriod noStartAndEndYear;
|
40
|
private static final Integer year = 1982;
|
41
|
private static final Integer month = 1;
|
42
|
private static final Integer day = 5;
|
43
|
|
44
|
private static final String SEP = TimePeriod.SEP;
|
45
|
|
46
|
@Before
|
47
|
public void setUp() throws Exception {
|
48
|
onlyStartYear = ExtendedTimePeriod.NewExtendedYearInstance(1922);
|
49
|
onlyEndYear = ExtendedTimePeriod.NewExtendedYearInstance(null, 1857);
|
50
|
startAndEndYear = ExtendedTimePeriod.NewExtendedYearInstance(1931, 1957);
|
51
|
Integer start = null;
|
52
|
Integer end = null;
|
53
|
noStartAndEndYear = ExtendedTimePeriod.NewExtendedYearInstance(start, end);
|
54
|
}
|
55
|
|
56
|
//************************ TESTS ******************************************
|
57
|
|
58
|
@Test
|
59
|
public void testNewExtendedInstance() {
|
60
|
ExtendedTimePeriod tp = ExtendedTimePeriod.NewExtendedInstance();
|
61
|
Assert.assertNotNull(tp);
|
62
|
Assert.assertTrue("VerbatimTimeperiod should be empty",tp.isEmpty());
|
63
|
}
|
64
|
|
65
|
@Test
|
66
|
public void testNewExtendedInstancePartial() {
|
67
|
ExtendedTimePeriod tp = ExtendedTimePeriod.NewExtendedInstance(new Partial().with(DateTimeFieldType.dayOfWeek(), 5));
|
68
|
Assert.assertNotNull(tp);
|
69
|
Assert.assertFalse("VerbatimTimeperiod should not be empty",tp.isEmpty());
|
70
|
}
|
71
|
|
72
|
@Test
|
73
|
public void testNewExtendedInstancePartialPartial() {
|
74
|
ExtendedTimePeriod tp = ExtendedTimePeriod.NewExtendedInstance(new Partial().with(DateTimeFieldType.dayOfMonth(),day));
|
75
|
Assert.assertNotNull(tp);
|
76
|
Assert.assertFalse("VerbatimTimeperiod should not be empty",tp.isEmpty());
|
77
|
Assert.assertEquals("VerbatimTimeperiod's should not be empty", day, tp.getStartDay());
|
78
|
}
|
79
|
|
80
|
@Test
|
81
|
public void testNewExtendedInstanceInteger() {
|
82
|
onlyStartYear = ExtendedTimePeriod.NewExtendedYearInstance(1922);
|
83
|
assertEquals(Integer.valueOf(1922), onlyStartYear.getStartYear());
|
84
|
assertNull(onlyStartYear.getEndYear());
|
85
|
assertEquals("1922", onlyStartYear.getYear());
|
86
|
}
|
87
|
|
88
|
@Test
|
89
|
public void testNewExtendedInstanceIntegerInteger() {
|
90
|
startAndEndYear = ExtendedTimePeriod.NewExtendedYearInstance(1931, 1957);
|
91
|
assertEquals(Integer.valueOf(1957), startAndEndYear.getEndYear());
|
92
|
assertEquals(Integer.valueOf(1931), startAndEndYear.getStartYear());
|
93
|
assertEquals("1931"+SEP+"1957", startAndEndYear.getYear());
|
94
|
}
|
95
|
|
96
|
@Test
|
97
|
public void testNewExtendedInstanceCalendar() {
|
98
|
Calendar cal = Calendar.getInstance();
|
99
|
cal.set(1982, 1, day);
|
100
|
ExtendedTimePeriod tp = ExtendedTimePeriod.NewExtendedInstance(cal);
|
101
|
Assert.assertNotNull(tp);
|
102
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
103
|
Assert.assertEquals("Timeperiod's should not be empty", day, tp.getStartDay());
|
104
|
}
|
105
|
|
106
|
@Test
|
107
|
public void testNewExtendedInstanceCalendarCalendar() {
|
108
|
Calendar cal = Calendar.getInstance();
|
109
|
cal.set(year, month, day);
|
110
|
Calendar cal2 = Calendar.getInstance();
|
111
|
Integer day2 = 20;
|
112
|
Integer month2 = 7;
|
113
|
Integer year2 = 1985;
|
114
|
cal2.set(year2, month2, day2);
|
115
|
Calendar cal3 = Calendar.getInstance();
|
116
|
Integer day3 = 12;
|
117
|
Integer month3 = 3;
|
118
|
Integer year3 = 1980;
|
119
|
cal3.set(year3, month3, day3);
|
120
|
Calendar cal4 = Calendar.getInstance();
|
121
|
Integer day4 = 2;
|
122
|
Integer month4 = 1;
|
123
|
Integer year4 = 1986;
|
124
|
cal4.set(year4, month4, day4);
|
125
|
ExtendedTimePeriod tp = ExtendedTimePeriod.NewExtendedInstance(cal, cal2, cal3, cal4);
|
126
|
Assert.assertNotNull(tp);
|
127
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
128
|
Assert.assertEquals("Timeperiod's start year shold be equal to year", year, tp.getStartYear());
|
129
|
//Calendar starts counting months with 0
|
130
|
Assert.assertEquals("Timeperiod's start month should be equal to month+1 ", (Integer)(month +1), tp.getStartMonth());
|
131
|
Assert.assertEquals("Timeperiod's start day should be equal to day", day, tp.getStartDay());
|
132
|
|
133
|
Assert.assertEquals("Timeperiod's end year should be equal to year2", year2, tp.getEndYear());
|
134
|
//Calendar starts counting months with 0
|
135
|
Assert.assertEquals("Timeperiod's end month should be equal with month2(+1)", (Integer)(month2+1), tp.getEndMonth());
|
136
|
Assert.assertEquals("Timeperiod's end day should be equal with day2", day2, tp.getEndDay());
|
137
|
|
138
|
Assert.assertEquals("Timeperiod's extreme start year shold be equal to year3", year3, tp.getExtremeStartYear());
|
139
|
//Calendar starts counting months with 0
|
140
|
Assert.assertEquals("Timeperiod's extreme start month should be equal to month3+1 ", (Integer)(month3 +1), tp.getExtremeStartMonth());
|
141
|
Assert.assertEquals("Timeperiod's extreme start day should be equal to day3", day3, tp.getExtremeStartDay());
|
142
|
|
143
|
Assert.assertEquals("Timeperiod's extreme end year should be equal to year4", year4, tp.getExtremeEndYear());
|
144
|
//Calendar starts counting months with 0
|
145
|
Assert.assertEquals("Timeperiod's extreme end month should be equal with month4(+1)", (Integer)(month4+1), tp.getExtremeEndMonth());
|
146
|
Assert.assertEquals("Timeperiod's extreme end day should be equal with day4", day4, tp.getExtremeEndDay());
|
147
|
}
|
148
|
|
149
|
@Test
|
150
|
public void testNewExtendedInstanceReadableInstant() {
|
151
|
ReadableInstant readInst = new MutableDateTime();
|
152
|
ExtendedTimePeriod tp = ExtendedTimePeriod.NewExtendedInstance(readInst);
|
153
|
Assert.assertNotNull(tp);
|
154
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
155
|
Assert.assertEquals("Timeperiod's should not be empty", (Integer)readInst.get(DateTimeFieldType.dayOfMonth()), tp.getStartDay());
|
156
|
}
|
157
|
|
158
|
@Test
|
159
|
public void testNewExtendedInstanceReadableInstantReadableInstant() {
|
160
|
ReadableInstant readInst = new MutableDateTime();
|
161
|
ReadableInstant readInst2 = new MutableDateTime();
|
162
|
((MutableDateTime)readInst).addDays(5);
|
163
|
ReadableInstant readInst3 = new MutableDateTime();
|
164
|
ReadableInstant readInst4 = new MutableDateTime();
|
165
|
ExtendedTimePeriod tp = ExtendedTimePeriod.NewExtendedInstance(readInst, readInst2, readInst3, readInst4);
|
166
|
|
167
|
Assert.assertNotNull(tp);
|
168
|
Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
|
169
|
Assert.assertEquals("Timeperiod's day should be equal to readable instant day", (Integer)readInst.get(DateTimeFieldType.dayOfMonth()), tp.getStartDay());
|
170
|
Assert.assertEquals("Timeperiod's day should be equal to readable instant day", (Integer)readInst2.get(DateTimeFieldType.dayOfMonth()), tp.getEndDay());
|
171
|
}
|
172
|
|
173
|
@Test
|
174
|
public void testCalendarToPartial() {
|
175
|
Calendar cal = Calendar.getInstance();
|
176
|
cal.set(year, month, day);
|
177
|
Partial part = TimePeriod.calendarToPartial(cal);
|
178
|
Assert.assertEquals("Partial's day should not be equal to calednars day", day, (Integer)part.get(DateTimeFieldType.dayOfMonth()));
|
179
|
}
|
180
|
|
181
|
@Test
|
182
|
public void testReadableInstantToPartial() {
|
183
|
ReadableInstant readInst = new MutableDateTime();
|
184
|
Partial part = TimePeriod.readableInstantToPartial(readInst);
|
185
|
part.get(DateTimeFieldType.dayOfMonth());
|
186
|
Assert.assertEquals("Partial's day should not be equal to calednars day", (Integer)part.get(DateTimeFieldType.dayOfMonth()), (Integer)part.get(DateTimeFieldType.dayOfMonth()));
|
187
|
}
|
188
|
|
189
|
@Test
|
190
|
public void testTimePeriod() {
|
191
|
TimePeriod tp = new TimePeriod();
|
192
|
Assert.assertNotNull("Time period must be created",tp);
|
193
|
}
|
194
|
|
195
|
@Test
|
196
|
public void testSetStart(){
|
197
|
Partial startDate = new Partial().with(DateTimeFieldType.year(), 2010)
|
198
|
.with(DateTimeFieldType.monthOfYear(), 12)
|
199
|
.with(DateTimeFieldType.dayOfMonth(), 16);
|
200
|
Partial newStartDate = new Partial().with(DateTimeFieldType.year(), 1984)
|
201
|
.with(DateTimeFieldType.monthOfYear(), 12)
|
202
|
.with(DateTimeFieldType.dayOfMonth(), 14);
|
203
|
|
204
|
ExtendedTimePeriod tp = ExtendedTimePeriod.NewExtendedInstance(startDate);
|
205
|
String startString = tp.toString();
|
206
|
assertNull("Freetext should be not set", tp.getFreeText());
|
207
|
tp.setStart(newStartDate);
|
208
|
String changedString = tp.toString();
|
209
|
Assert.assertTrue("Setting the partial should change the string representation of the TimePeriod", !startString.equals(changedString));
|
210
|
|
211
|
// //
|
212
|
// tp = TimePeriodParser.parseStringVerbatim("1752");
|
213
|
// assertNull("Freetext should be not set", tp.getFreeText());
|
214
|
// startString = tp.toString();
|
215
|
// tp.setStart(newStartDate);
|
216
|
// changedString = tp.toString();
|
217
|
// Assert.assertTrue("Setting a partial for a parsed time period should change the string representation of the TimePeriod ", !startString.equals(changedString));
|
218
|
//
|
219
|
// //
|
220
|
// tp = TimePeriodParser.parseStringVerbatim("any strange date");
|
221
|
// assertNotNull("Freetext should be set", tp.getFreeText());
|
222
|
// startString = tp.toString();
|
223
|
// tp.setStart(newStartDate);
|
224
|
// changedString = tp.toString();
|
225
|
// Assert.assertEquals("Setting a partial for a time period having the freetext set should not change the string representation of the TimePeriod ", startString, changedString);
|
226
|
//
|
227
|
|
228
|
//
|
229
|
// tp = TimePeriodParser.parseString("15.12.1730"); //TODO currently not parsed
|
230
|
//
|
231
|
// startString = tp.toString();
|
232
|
// tp.setStart(newStartDate);
|
233
|
// changedString = tp.toString();
|
234
|
//
|
235
|
// Assert.assertTrue("Setting a partial for a parsed time period should change the string representation of the TimePeriod ", !startString.equals(changedString));
|
236
|
}
|
237
|
|
238
|
@Test
|
239
|
public void testIsPeriod() {
|
240
|
assertTrue(startAndEndYear.isPeriod());
|
241
|
assertFalse(onlyStartYear.isPeriod());
|
242
|
assertFalse(onlyEndYear.isPeriod());
|
243
|
assertFalse(noStartAndEndYear.isPeriod());
|
244
|
onlyStartYear.setEndDay(14);
|
245
|
assertFalse(onlyStartYear.isPeriod());
|
246
|
onlyStartYear.setEndYear(1988);
|
247
|
assertTrue(onlyStartYear.isPeriod()); //may be discussed
|
248
|
}
|
249
|
|
250
|
@Test
|
251
|
public void testGetStart() {
|
252
|
TimePeriod tp = new TimePeriod();
|
253
|
Partial start = new Partial(DateTimeFieldType.year(), 1999);
|
254
|
tp.setStart(start);
|
255
|
Assert.assertEquals("Start year should be 1999", Integer.valueOf(1999), tp.getStartYear());
|
256
|
Assert.assertEquals("Start should be 'start'", start, tp.getStart());
|
257
|
}
|
258
|
|
259
|
@Test
|
260
|
public void testGetEnd() {
|
261
|
TimePeriod tp = new TimePeriod();
|
262
|
Partial end = new Partial(DateTimeFieldType.year(), 1999);
|
263
|
tp.setEnd(end);
|
264
|
Assert.assertEquals("End year should be 1999", Integer.valueOf(1999), tp.getEndYear());
|
265
|
Assert.assertEquals("End should be 'end'", end, tp.getEnd());
|
266
|
}
|
267
|
|
268
|
@Test
|
269
|
public void testGetYear() {
|
270
|
TimePeriod tp = new TimePeriod();
|
271
|
tp.setStartYear(1999);
|
272
|
Assert.assertEquals("Year should be 1999", "1999", tp.getYear());
|
273
|
tp.setEndYear(2002);
|
274
|
Assert.assertEquals("Year should be 1999"+SEP+"2002", "1999"+SEP+"2002", tp.getYear());
|
275
|
}
|
276
|
|
277
|
@Test
|
278
|
public void testClone() {
|
279
|
Integer startYear = 1788;
|
280
|
Integer startMonth = 6;
|
281
|
Integer startDay = 25;
|
282
|
Integer endDay = 21;
|
283
|
Integer endMonth = 12;
|
284
|
Integer endYear = 1799;
|
285
|
Integer startYear2 = 1787;
|
286
|
Integer startMonth2 = 5;
|
287
|
Integer startDay2 = 24;
|
288
|
Integer endDay2 = 20;
|
289
|
Integer endMonth2 = 11;
|
290
|
Integer endYear2 = 1798;
|
291
|
String freeText = "A free period";
|
292
|
ExtendedTimePeriod tp1 = ExtendedTimePeriod.NewExtendedYearInstance(startYear,endYear,startYear2,endYear2);
|
293
|
tp1.setStartDay(startDay);
|
294
|
tp1.setStartMonth(startMonth);
|
295
|
tp1.setEndDay(endDay);
|
296
|
tp1.setEndMonth(endMonth);
|
297
|
tp1.setExtremeStartDay(startDay2);
|
298
|
tp1.setExtremeStartMonth(startMonth2);
|
299
|
tp1.setExtremeEndDay(endDay2);
|
300
|
tp1.setExtremeEndMonth(endMonth2);
|
301
|
tp1.setFreeText(freeText);
|
302
|
TimePeriod tpClone = tp1.clone();
|
303
|
Assert.assertEquals("Start year must be 1788.", startYear, tpClone.getStartYear());
|
304
|
Assert.assertEquals("Start month must be 6.", startMonth, tpClone.getStartMonth());
|
305
|
Assert.assertEquals("Start day must be 25.", startDay, tpClone.getStartDay());
|
306
|
Assert.assertEquals("End year must be 1799.", endYear, tpClone.getEndYear());
|
307
|
Assert.assertEquals("End month must be 12.", endMonth, tpClone.getEndMonth());
|
308
|
Assert.assertEquals("End day must be 21.", endDay, tpClone.getEndDay());
|
309
|
|
310
|
ExtendedTimePeriod tpCloneExtended = (ExtendedTimePeriod)tpClone;
|
311
|
Assert.assertEquals("Extreme start year must be 1787.", startYear2, tpCloneExtended.getExtremeStartYear());
|
312
|
Assert.assertEquals("Extreme start month must be 5.", startMonth2, tpCloneExtended.getExtremeStartMonth());
|
313
|
Assert.assertEquals("Extreme start day must be 24.", startDay2, tpCloneExtended.getExtremeStartDay());
|
314
|
Assert.assertEquals("Extreme end year must be 1798.", endYear2, tpCloneExtended.getExtremeEndYear());
|
315
|
Assert.assertEquals("Extreme end month must be 11.", endMonth2, tpCloneExtended.getExtremeEndMonth());
|
316
|
Assert.assertEquals("Extreme end day must be 20.", endDay2, tpCloneExtended.getExtremeEndDay());
|
317
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
318
|
}
|
319
|
|
320
|
@Test
|
321
|
public void testEquals() {
|
322
|
Integer startYear = 1788;
|
323
|
Integer startMonth = 6;
|
324
|
Integer endDay = 21;
|
325
|
Integer endYear = 1799;
|
326
|
String freeText = "A free period";
|
327
|
|
328
|
ExtendedTimePeriod tp1 = ExtendedTimePeriod.NewExtendedYearInstance(startYear);
|
329
|
ExtendedTimePeriod tpClone = tp1.clone();
|
330
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
331
|
|
332
|
tp1.setStartMonth(startMonth);
|
333
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
334
|
tpClone = tp1.clone();
|
335
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
336
|
|
337
|
|
338
|
tp1.setEndYear(endYear);
|
339
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
340
|
tpClone = tp1.clone();
|
341
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
342
|
|
343
|
tp1.setEndDay(endDay);
|
344
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
345
|
tpClone = tp1.clone();
|
346
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
347
|
|
348
|
tp1.setFreeText(freeText);
|
349
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
350
|
tpClone = tp1.clone();
|
351
|
Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
|
352
|
|
353
|
tp1 = ExtendedTimePeriod.NewExtendedInstance();
|
354
|
Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
|
355
|
ExtendedTimePeriod tp2 = ExtendedTimePeriod.NewExtendedInstance();
|
356
|
Assert.assertEquals("Empty time periods must be equal", tp1, tp2);
|
357
|
|
358
|
tp1.setFreeText(freeText);
|
359
|
Assert.assertFalse("Tp2 must not be equal to originial", tp1.equals(tp2));
|
360
|
tp2.setFreeText("jldskjlfi");
|
361
|
Assert.assertFalse("Tp2 must not be equal to originial", tp1.equals(tpClone));
|
362
|
tp2.setFreeText(freeText);
|
363
|
Assert.assertEquals("Tp2 must be equal", tp1, tp2);
|
364
|
}
|
365
|
|
366
|
}
|