From 427a1ab389a1a28f1a74a0c83140b80025f265c9 Mon Sep 17 00:00:00 2001 From: hlq07 Date: Wed, 9 May 2018 22:39:28 +0800 Subject: [PATCH] add a test unit of CalendarUtil --- .../cutecalendar/util/CalendarUtilTest.java | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 app/src/test/java/org/cutem/cutecalendar/util/CalendarUtilTest.java diff --git a/app/src/test/java/org/cutem/cutecalendar/util/CalendarUtilTest.java b/app/src/test/java/org/cutem/cutecalendar/util/CalendarUtilTest.java new file mode 100644 index 0000000..46a195b --- /dev/null +++ b/app/src/test/java/org/cutem/cutecalendar/util/CalendarUtilTest.java @@ -0,0 +1,131 @@ +package org.cutem.cutecalendar.util; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Calendar; + +import static org.junit.Assert.*; + +public class CalendarUtilTest { + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + + @Test + public void roundToMinute() { + Calendar calendar = Calendar.getInstance(); + Calendar rounded = CalendarUtil.roundToMinute(calendar); + int[] res1 = CalendarUtil.splitCalendar(calendar); + int len = res1.length; + res1[len - 1] = res1[len - 2] = 0; + int[] res2 = CalendarUtil.splitCalendar(rounded); + + assertArrayEquals(res1, res2); + } + + @Test + public void getWholeDayPeriod() { + Calendar calendar = Calendar.getInstance(); + calendar = CalendarUtil.roundToMinute(calendar); + + Calendar[] whole = CalendarUtil.getWholeDayPeriod(calendar); + int[] res1 = CalendarUtil.splitCalendar(calendar); + res1[3] = res1[4] = 0; + int[] res2 = CalendarUtil.splitCalendar(whole[0]); + assertArrayEquals(res1, res2); + + res1[3] = 23; + res1[4] = 59; + res2 = CalendarUtil.splitCalendar(whole[1]); + assertArrayEquals(res1, res2); + } + + @Test + public void getMonthBeginAndEnd() { + Calendar calendar = Calendar.getInstance(); + calendar = CalendarUtil.roundToMinute(calendar); + calendar.set(Calendar.MONTH, Calendar.JANUARY); + + int[] res1 = CalendarUtil.splitCalendar(calendar); + res1[2] = 1; + res1[3] = 0; + res1[4] = 0; + + Calendar bgn = CalendarUtil.getMonthBegin(calendar); + + int[] res2 = CalendarUtil.splitCalendar(bgn); + + assertArrayEquals(res1, res2); + + Calendar end = CalendarUtil.getMonthEnd(calendar); + res2 = CalendarUtil.splitCalendar(end); + res1[2] = 31; + res1[3] = 23; + res1[4] = 59; + assertArrayEquals(res1, res2); + + } + + @Test + public void isSameMonth() { + Calendar c1 = Calendar.getInstance(); + Calendar c2; + c1 = CalendarUtil.roundToMinute(c1); + c2 = CalendarUtil.roundToMinute(c1); + + assertTrue(CalendarUtil.isSameMonth(c1, c2)); + c1.set(Calendar.MONTH, Calendar.MARCH); + c2.set(Calendar.MONTH, Calendar.JANUARY); + assertFalse(CalendarUtil.isSameMonth(c1, c2)); + } + + @Test + public void isValidDateStringAndTimeString() { + String s1 = "2018-2-28"; + String s2 = "2018-2-29"; + String s3 = "2016-2-29"; + String s4 = "2016-2-30"; + + assertTrue(CalendarUtil.isValidDateString(s1)); + assertFalse(CalendarUtil.isValidDateString(s2)); + assertTrue(CalendarUtil.isValidDateString(s3)); + assertFalse(CalendarUtil.isValidDateString(s4)); + + s1 = "23:59"; + s2 = "24:59"; + s3 = "23:50"; + s4 = "3:50"; + assertTrue(CalendarUtil.isValidTimeString(s1)); + assertFalse(CalendarUtil.isValidTimeString(s2)); + assertTrue(CalendarUtil.isValidTimeString(s3)); + assertTrue(CalendarUtil.isValidTimeString(s4)); + + s1 = "2018-5-2"; + s2 = "11:30"; + s3 = "2018-2-29"; + + Calendar cal = CalendarUtil.constructCalendar(s1); + assertNotNull(cal); + int[] res = CalendarUtil.splitCalendar(cal); + int[] exp = new int[]{2018, 4, 2, 0, 0, 0, 0}; + assertArrayEquals(exp, res); + + cal = CalendarUtil.constructCalendar(s1, s2); + assertNotNull(cal); + res = CalendarUtil.splitCalendar(cal); + exp = new int[]{2018, 4, 2, 11, 30, 0, 0}; + assertArrayEquals(exp, res); + + cal = CalendarUtil.constructCalendar(s3); + assertNull(cal); + } + +} \ No newline at end of file