]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/TestCalendarUtil.java
add: ensure that for Org Administrator actions certificate login is used
[gigi.git] / tests / club / wpia / gigi / TestCalendarUtil.java
1 package club.wpia.gigi;
2
3 import static org.junit.Assert.*;
4
5 import java.util.Calendar;
6 import java.util.TimeZone;
7
8 import org.junit.Test;
9
10 import club.wpia.gigi.util.CalendarUtil;
11 import club.wpia.gigi.util.DayDate;
12
13 public class TestCalendarUtil {
14
15     @Test
16     public void testGetDateFromComponents() {
17
18         Calendar now = Calendar.getInstance();
19         now.setTimeZone(TimeZone.getTimeZone("UTC"));
20
21         int year = now.get(Calendar.YEAR);
22         int month = now.get(Calendar.MONTH) + 1;
23         int days = now.get(Calendar.DATE);
24         now.setTimeInMillis(0);
25         now.set(year, month - 1, days, 0, 0, 0);
26
27         DayDate dob = CalendarUtil.getDateFromComponents(year, month, days);
28         DayDate d = new DayDate(now.getTimeInMillis());
29
30         assertEquals(d.getTime(), dob.getTime());
31         dob = CalendarUtil.getDateFromComponents(year + 1, month, days);
32
33         assertNotEquals(d.getTime(), dob.getTime());
34
35     }
36
37     @Test
38     public void testIsOfAge() {
39
40         Calendar now = Calendar.getInstance();
41         int year = now.get(Calendar.YEAR);
42         int month = now.get(Calendar.MONTH) + 1;
43         int days = now.get(Calendar.DATE);
44
45         DayDate dob = CalendarUtil.getDateFromComponents(year - 14, month, days);
46
47         assertTrue(CalendarUtil.isOfAge(dob, 13));
48
49         assertTrue(CalendarUtil.isOfAge(dob, 14));
50         // We need one day as safety margin. Between 10:00 and 23:59 UTC there
51         // is a place on earth (UTC+1 to UTC+14) where a person having
52         // birthday "tomorrow" is already of that age. So we need the day after
53         // tomorrow for doing this check the easy way.
54         dob = CalendarUtil.getDateFromComponents(year - 14, month, days + 2);
55         assertFalse(CalendarUtil.isOfAge(dob, 14));
56
57     }
58
59     static {
60         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
61     }
62
63     @Test
64     public void testIsDateValid() {
65         assertTrue(CalendarUtil.isDateValid(2016, 2, 28));
66         assertTrue(CalendarUtil.isDateValid(2016, 2, 29));
67         assertFalse(CalendarUtil.isDateValid(2016, 2, 30));
68         assertFalse(CalendarUtil.isDateValid(2016, 4, 31));
69
70         assertTrue(CalendarUtil.isDateValid(2000, 2, 28));
71         assertTrue(CalendarUtil.isDateValid(2000, 2, 29));
72         assertFalse(CalendarUtil.isDateValid(2000, 2, 30));
73         assertFalse(CalendarUtil.isDateValid(2000, 4, 31));
74
75         assertTrue(CalendarUtil.isDateValid(2015, 2, 28));
76         assertFalse(CalendarUtil.isDateValid(2015, 2, 29));
77         assertFalse(CalendarUtil.isDateValid(2015, 2, 30));
78         assertFalse(CalendarUtil.isDateValid(2015, 4, 31));
79
80     }
81
82 }