Project

General

Profile

Download (17 KB) Statistics
| Branch: | Tag: | Revision:
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.assertNotNull;
14
import static org.junit.Assert.assertNull;
15
import static org.junit.Assert.assertTrue;
16

    
17
import java.util.Calendar;
18

    
19
import org.apache.log4j.Logger;
20
import org.joda.time.DateTimeFieldType;
21
import org.joda.time.MutableDateTime;
22
import org.joda.time.Partial;
23
import org.joda.time.ReadableInstant;
24
import org.junit.Assert;
25
import org.junit.Before;
26
import org.junit.Test;
27

    
28
import eu.etaxonomy.cdm.strategy.cache.common.TimePeriodPartialFormatter;
29
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
30

    
31
/**
32
 * @author a.mueller
33
 * @since 08.05.2018
34
 */
35
public class VerbatimTimePeriodTest {
36

    
37
    @SuppressWarnings("unused")
38
    private static final Logger logger = Logger.getLogger(VerbatimTimePeriodTest.class);
39

    
40
    private VerbatimTimePeriod onlyStartYear;
41
    private VerbatimTimePeriod onlyEndYear;
42
    private VerbatimTimePeriod startAndEndYear;
43
    private VerbatimTimePeriod noStartAndEndYear;
44
    private static final Integer year = 1982;
45
    private static final Integer month = 1;
46
    private static final Integer day = 5;
47

    
48
    /**
49
     * @throws java.lang.Exception
50
     */
51
    @Before
52
    public void setUp() throws Exception {
53
        onlyStartYear = VerbatimTimePeriod.NewVerbatimInstance(1922);
54
        onlyEndYear = VerbatimTimePeriod.NewVerbatimInstance(null, 1857);;
55
        startAndEndYear = VerbatimTimePeriod.NewVerbatimInstance(1931, 1957);
56
        Integer start = null;
57
        Integer end = null;
58
        noStartAndEndYear = VerbatimTimePeriod.NewVerbatimInstance(start, end);
59
    }
60

    
61
  //************************ TESTS ******************************************
62

    
63
    /**
64
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance()}.
65
     */
66
    @Test
67
    public void testNewVerbatimInstance() {
68
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance();
69
        Assert.assertNotNull(tp);
70
        Assert.assertTrue("VerbatimTimeperiod should be empty",tp.isEmpty());
71
    }
72

    
73
    /**
74
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(org.joda.time.Partial)}.
75
     */
76
    @Test
77
    public void testNewVerbatimInstancePartial() {
78
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(new Partial().with(DateTimeFieldType.dayOfWeek(), 5));
79
        Assert.assertNotNull(tp);
80
        Assert.assertFalse("VerbatimTimeperiod should not be empty",tp.isEmpty());
81
    }
82

    
83
    /**
84
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(org.joda.time.Partial, org.joda.time.Partial)}.
85
     */
86
    @Test
87
    public void testNewVerbatimInstancePartialPartial() {
88
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(new Partial().with(DateTimeFieldType.dayOfMonth(),day));
89
        Assert.assertNotNull(tp);
90
        Assert.assertFalse("VerbatimTimeperiod should not be empty",tp.isEmpty());
91
        Assert.assertEquals("VerbatimTimeperiod's should not be empty", day, tp.getStartDay());
92
    }
93

    
94
    /**
95
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(java.lang.Integer)}.
96
     */
97
    @Test
98
    public void testNewVerbatimInstanceInteger() {
99
        onlyStartYear = VerbatimTimePeriod.NewVerbatimInstance(1922);
100
        assertEquals(Integer.valueOf(1922), onlyStartYear.getStartYear());
101
        assertNull(onlyStartYear.getEndYear());
102
        assertEquals("1922", onlyStartYear.getYear());
103
    }
104

    
105
    /**
106
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(java.lang.Integer, java.lang.Integer)}.
107
     */
108
    @Test
109
    public void testNewVerbatimInstanceIntegerInteger() {
110
        startAndEndYear = VerbatimTimePeriod.NewVerbatimInstance(1931, 1957);
111
        assertEquals(Integer.valueOf(1957), startAndEndYear.getEndYear());
112
        assertEquals(Integer.valueOf(1931), startAndEndYear.getStartYear());
113
        assertEquals("1931-1957", startAndEndYear.getYear());
114
    }
115

    
116
    /**
117
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(java.util.Calendar)}.
118
     */
119
    @Test
120
    public void testNewVerbatimInstanceCalendar() {
121
        Calendar cal = Calendar.getInstance();
122
        cal.set(1982, 1, day);
123
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(cal);
124
        Assert.assertNotNull(tp);
125
        Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
126
        Assert.assertEquals("Timeperiod's should not be empty", day, tp.getStartDay());
127
    }
128

    
129
    /**
130
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(java.util.Calendar, java.util.Calendar)}.
131
     */
132
    @Test
133
    public void testNewVerbatimInstanceCalendarCalendar() {
134
        Calendar cal = Calendar.getInstance();
135
        cal.set(year, month, day);
136
        Calendar cal2 = Calendar.getInstance();
137
        Integer day2 = 20;
138
        Integer month2 = 7;
139
        Integer year2 = 1985;
140
        cal2.set(year2, month2, day2);
141
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(cal, cal2);
142
        Assert.assertNotNull(tp);
143
        Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
144
        Assert.assertEquals("Timeperiod's start should not be equal with cal1", year, tp.getStartYear());
145
        //Calendar starts counting months with 0
146
        Assert.assertEquals("Timeperiod's start should not be equal with cal1 ", (Integer)(month +1), tp.getStartMonth());
147
        Assert.assertEquals("Timeperiod's start should not be equal with cal1", day, tp.getStartDay());
148

    
149
        Assert.assertEquals("Timeperiod's start should not be equal with cal2", year2, tp.getEndYear());
150
        //Calendar starts counting months with 0
151
        Assert.assertEquals("Timeperiod's start should not be equal with cal2", (Integer)(month2+1), tp.getEndMonth());
152
        Assert.assertEquals("Timeperiod's end should not be equal with cal2", day2, tp.getEndDay());
153
    }
154

    
155
    /**
156
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(org.joda.time.ReadableInstant)}.
157
     */
158
    @Test
159
    public void testNewVerbatimInstanceReadableInstant() {
160
        ReadableInstant readInst = new MutableDateTime();
161
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(readInst);
162
        Assert.assertNotNull(tp);
163
        Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
164
        Assert.assertEquals("Timeperiod's should not be empty", (Integer)readInst.get(DateTimeFieldType.dayOfMonth()), tp.getStartDay());
165
    }
166

    
167
    /**
168
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(org.joda.time.ReadableInstant, org.joda.time.ReadableInstant)}.
169
     */
170
    @Test
171
    public void testNewVerbatimInstanceReadableInstantReadableInstant() {
172
        ReadableInstant readInst = new MutableDateTime();
173
        ReadableInstant readInst2 = new MutableDateTime();
174
        ((MutableDateTime)readInst).addDays(5);
175
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(readInst, readInst2);
176

    
177
        Assert.assertNotNull(tp);
178
        Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
179
        Assert.assertEquals("Timeperiod's day should not be equal to readable instant", (Integer)readInst.get(DateTimeFieldType.dayOfMonth()), tp.getStartDay());
180
        Assert.assertEquals("Timeperiod's day should not be equal to readable instant", (Integer)readInst2.get(DateTimeFieldType.dayOfMonth()), tp.getEndDay());
181
    }
182

    
183
    /**
184
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#calendarToPartial(java.util.Calendar)}.
185
     */
186
    @Test
187
    public void testCalendarToPartial() {
188
        Calendar cal = Calendar.getInstance();
189
        cal.set(year, month, day);
190
        Partial part = TimePeriod.calendarToPartial(cal);
191
        Assert.assertEquals("Partial's day should not be equal to calednars day", day, (Integer)part.get(DateTimeFieldType.dayOfMonth()));
192
    }
193

    
194
    /**
195
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#readableInstantToPartial(org.joda.time.ReadableInstant)}.
196
     */
197
    @Test
198
    public void testReadableInstantToPartial() {
199
        ReadableInstant readInst = new MutableDateTime();
200
        Partial part = TimePeriod.readableInstantToPartial(readInst);
201
        part.get(DateTimeFieldType.dayOfMonth());
202
        Assert.assertEquals("Partial's day should not be equal to calednars day", (Integer)part.get(DateTimeFieldType.dayOfMonth()), (Integer)part.get(DateTimeFieldType.dayOfMonth()));
203
    }
204

    
205
    /**
206
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#TimePeriod()}.
207
     */
208
    @Test
209
    public void testTimePeriod() {
210
        TimePeriod tp = new TimePeriod();
211
        Assert.assertNotNull("Time period must be created",tp);
212
    }
213

    
214
//  @Ignore
215
    @Test
216
    public void testSetStart(){
217
        Partial startDate = new Partial().with(DateTimeFieldType.year(), 2010)
218
                .with(DateTimeFieldType.monthOfYear(), 12)
219
                .with(DateTimeFieldType.dayOfMonth(), 16);
220
        Partial newStartDate = new Partial().with(DateTimeFieldType.year(), 1984)
221
            .with(DateTimeFieldType.monthOfYear(), 12)
222
            .with(DateTimeFieldType.dayOfMonth(), 14);
223

    
224
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(startDate);
225
        String startString = tp.toString();
226
        assertNull("Freetext should be not set", tp.getFreeText());
227
        tp.setStart(newStartDate);
228
        String changedString = tp.toString();
229
        Assert.assertTrue("Setting the partial should change the string representation of the TimePeriod", !startString.equals(changedString));
230

    
231
        //
232
        tp = TimePeriodParser.parseStringVerbatim("1752");
233
        assertNull("Freetext should be not set", tp.getFreeText());
234
        startString = tp.toString();
235
        tp.setStart(newStartDate);
236
        changedString = tp.toString();
237
        Assert.assertTrue("Setting a partial for a parsed time period should change the string representation of the TimePeriod ", !startString.equals(changedString));
238

    
239
        //
240
        tp = TimePeriodParser.parseStringVerbatim("any strange date");
241
        assertNotNull("Freetext should be set", tp.getFreeText());
242
        startString = tp.toString();
243
        tp.setStart(newStartDate);
244
        changedString = tp.toString();
245
        Assert.assertEquals("Setting a partial for a time period having the freetext set should not change the string representation of the TimePeriod  ", startString, changedString);
246

    
247

    
248
        //
249
//      tp = TimePeriodParser.parseString("15.12.1730"); //TODO currently not parsed
250
//
251
//      startString = tp.toString();
252
//      tp.setStart(newStartDate);
253
//      changedString = tp.toString();
254
//
255
//      Assert.assertTrue("Setting a partial for a parsed time period should change the string representation of the TimePeriod ", !startString.equals(changedString));
256
    }
257

    
258
    /**
259
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#isPeriod()}.
260
     */
261
    @Test
262
    public void testIsPeriod() {
263
        assertTrue(startAndEndYear.isPeriod());
264
        assertFalse(onlyStartYear.isPeriod());
265
        assertFalse(onlyEndYear.isPeriod());
266
        assertFalse(noStartAndEndYear.isPeriod());
267
        onlyStartYear.setEndDay(14);
268
        assertFalse(onlyStartYear.isPeriod());
269
        onlyStartYear.setEndYear(1988);
270
        assertTrue(onlyStartYear.isPeriod()); //may be discussed
271
    }
272

    
273
    /**
274
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getStart()}.
275
     */
276
    @Test
277
    public void testGetStart() {
278
        TimePeriod tp = new TimePeriod();
279
        Partial start = new Partial(DateTimeFieldType.year(), 1999);
280
        tp.setStart(start);
281
        Assert.assertEquals("Start year should be 1999", Integer.valueOf(1999), tp.getStartYear());
282
        Assert.assertEquals("Start should be 'start'", start, tp.getStart());
283
    }
284

    
285

    
286
    /**
287
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getEnd()}.
288
     */
289
    @Test
290
    public void testGetEnd() {
291
        TimePeriod tp = new TimePeriod();
292
        Partial end = new Partial(DateTimeFieldType.year(), 1999);
293
        tp.setEnd(end);
294
        Assert.assertEquals("End year should be 1999", Integer.valueOf(1999), tp.getEndYear());
295
        Assert.assertEquals("End should be 'end'", end, tp.getEnd());
296
    }
297

    
298
    /**
299
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getYear()}.
300
     */
301
    @Test
302
    public void testGetYear() {
303
        TimePeriod tp = new TimePeriod();
304
        tp.setStartYear(1999);
305
        Assert.assertEquals("Year should be 1999", "1999", tp.getYear());
306
        tp.setEndYear(2002);
307
        Assert.assertEquals("Year should be 1999-2002", "1999-2002", tp.getYear());
308
    }
309

    
310

    
311
    /**
312
     * TODO should be partly moved to a test class for {@link TimePeriodPartialFormatter}
313
     */
314
    @Test
315
    public void testToStringTimePeriod() {
316
        VerbatimTimePeriod tp1 = VerbatimTimePeriod.NewVerbatimInstance(1788,1799);
317
        assertNotNull(tp1);
318
        Assert.assertEquals("1788-1799", tp1.toString());
319
        tp1.setStartDay(3);
320
        Assert.assertEquals("3.xx.1788-1799", tp1.toString());
321
        tp1.setEndMonth(11);
322
        Assert.assertEquals("3.xx.1788-11.1799", tp1.toString());
323
    }
324

    
325

    
326
    /**
327
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#clone()}.
328
     */
329
    @Test
330
    public void testClone() {
331
        Integer startYear = 1788;
332
        Integer startMonth = 6;
333
        Integer startDay = 25;
334
        Integer endDay = 21;
335
        Integer endMonth = 12;
336
        Integer endYear = 1799;
337
        String freeText = "A free period";
338
        VerbatimTimePeriod tp1 = VerbatimTimePeriod.NewVerbatimInstance(startYear,endYear);
339
        tp1.setStartDay(startDay);
340
        tp1.setStartMonth(startMonth);
341
        tp1.setEndDay(endDay);
342
        tp1.setEndMonth(endMonth);
343
        tp1.setFreeText(freeText);
344
        TimePeriod tpClone = (TimePeriod)tp1.clone();
345
        Assert.assertEquals("Start year must be 1788.", startYear, tpClone.getStartYear());
346
        Assert.assertEquals("Start month must be 6.", startMonth, tpClone.getStartMonth());
347
        Assert.assertEquals("Start day must be 25.", startDay, tpClone.getStartDay());
348
        Assert.assertEquals("End year must be 1799.", endYear, tpClone.getEndYear());
349
        Assert.assertEquals("End month must be 12.", endMonth, tpClone.getEndMonth());
350
        Assert.assertEquals("End day must be 21.", endDay, tpClone.getEndDay());
351
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
352
    }
353

    
354
    /**
355
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#clone()}.
356
     */
357
    @Test
358
    public void testEquals() {
359
        Integer startYear = 1788;
360
        Integer startMonth = 6;
361
        Integer endDay = 21;
362
        Integer endYear = 1799;
363
        String freeText = "A free period";
364

    
365
        VerbatimTimePeriod tp1 = VerbatimTimePeriod.NewVerbatimInstance(startYear);
366
        VerbatimTimePeriod tpClone = (VerbatimTimePeriod)tp1.clone();
367
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
368

    
369
        tp1.setStartMonth(startMonth);
370
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
371
        tpClone = (VerbatimTimePeriod)tp1.clone();
372
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
373

    
374

    
375
        tp1.setEndYear(endYear);
376
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
377
        tpClone = (VerbatimTimePeriod)tp1.clone();
378
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
379

    
380
        tp1.setEndDay(endDay);
381
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
382
        tpClone = (VerbatimTimePeriod)tp1.clone();
383
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
384

    
385
        tp1.setFreeText(freeText);
386
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
387
        tpClone = (VerbatimTimePeriod)tp1.clone();
388
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
389

    
390
        tp1 = VerbatimTimePeriod.NewVerbatimInstance();
391
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
392
        VerbatimTimePeriod tp2 = VerbatimTimePeriod.NewVerbatimInstance();
393
        Assert.assertEquals("Empty time periods must be equal", tp1, tp2);
394

    
395
        tp1.setFreeText(freeText);
396
        Assert.assertFalse("Tp2 must not be equal to originial", tp1.equals(tp2));
397
        tp2.setFreeText("jldskjlfi");
398
        Assert.assertFalse("Tp2 must not be equal to originial", tp1.equals(tpClone));
399
        tp2.setFreeText(freeText);
400
        Assert.assertEquals("Tp2 must be equal", tp1, tp2);
401
    }
402

    
403
}
(18-18/18)