1 package club.wpia.gigi;
3 import static org.junit.Assert.*;
5 import java.util.Calendar;
6 import java.util.TimeZone;
10 import club.wpia.gigi.util.CalendarUtil;
11 import club.wpia.gigi.util.DayDate;
13 public class TestCalendarUtil {
16 public void testGetDateFromComponents() {
18 Calendar now = Calendar.getInstance();
19 now.setTimeZone(TimeZone.getTimeZone("UTC"));
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);
27 DayDate dob = CalendarUtil.getDateFromComponents(year, month, days);
28 DayDate d = new DayDate(now.getTimeInMillis());
30 assertEquals(d.getTime(), dob.getTime());
31 dob = CalendarUtil.getDateFromComponents(year + 1, month, days);
33 assertNotEquals(d.getTime(), dob.getTime());
38 public void testIsOfAge() {
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);
45 DayDate dob = CalendarUtil.getDateFromComponents(year - 14, month, days);
47 assertTrue(CalendarUtil.isOfAge(dob, 13));
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));
60 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
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));
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));
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));