1 package org.cacert.gigi;
3 import static org.junit.Assert.*;
5 import java.util.Calendar;
6 import java.util.TimeZone;
8 import org.cacert.gigi.util.CalendarUtil;
9 import org.cacert.gigi.util.DayDate;
10 import org.junit.Test;
12 public class TestCalendarUtil {
15 public void testGetDateFromComponents() {
17 Calendar now = Calendar.getInstance();
18 now.setTimeZone(TimeZone.getTimeZone("UTC"));
20 int year = now.get(Calendar.YEAR);
21 int month = now.get(Calendar.MONTH) + 1;
22 int days = now.get(Calendar.DATE);
23 now.setTimeInMillis(0);
24 now.set(year, month - 1, days, 0, 0, 0);
26 DayDate dob = CalendarUtil.getDateFromComponents(year, month, days);
27 DayDate d = new DayDate(now.getTimeInMillis());
29 assertEquals(d.getTime(), dob.getTime());
30 dob = CalendarUtil.getDateFromComponents(year + 1, month, days);
32 assertNotEquals(d.getTime(), dob.getTime());
37 public void testIsOfAge() {
39 Calendar now = Calendar.getInstance();
40 int year = now.get(Calendar.YEAR);
41 int month = now.get(Calendar.MONTH) + 1;
42 int days = now.get(Calendar.DATE);
44 DayDate dob = CalendarUtil.getDateFromComponents(year - 14, month, days);
46 assertTrue(CalendarUtil.isOfAge(dob, 13));
48 assertTrue(CalendarUtil.isOfAge(dob, 14));
50 dob = CalendarUtil.getDateFromComponents(year - 14, month, days + 1);
51 assertFalse(CalendarUtil.isOfAge(dob, 14));
56 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
60 public void testIsDateValid() {
61 assertTrue(CalendarUtil.isDateValid(2016, 2, 28));
62 assertTrue(CalendarUtil.isDateValid(2016, 2, 29));
63 assertFalse(CalendarUtil.isDateValid(2016, 2, 30));
64 assertFalse(CalendarUtil.isDateValid(2016, 4, 31));
66 assertTrue(CalendarUtil.isDateValid(2000, 2, 28));
67 assertTrue(CalendarUtil.isDateValid(2000, 2, 29));
68 assertFalse(CalendarUtil.isDateValid(2000, 2, 30));
69 assertFalse(CalendarUtil.isDateValid(2000, 4, 31));
71 assertTrue(CalendarUtil.isDateValid(2015, 2, 28));
72 assertFalse(CalendarUtil.isDateValid(2015, 2, 29));
73 assertFalse(CalendarUtil.isDateValid(2015, 2, 30));
74 assertFalse(CalendarUtil.isDateValid(2015, 4, 31));