Author: fdesbois Date: 2010-03-01 18:10:07 +0100 (Mon, 01 Mar 2010) New Revision: 1781 Added: trunk/src/test/java/org/nuiton/util/PeriodDatesTest.java Modified: trunk/src/main/java/org/nuiton/util/PeriodDates.java Log: Ano #334 : infinite loop on PeriodDates getMonths method Modified: trunk/src/main/java/org/nuiton/util/PeriodDates.java =================================================================== --- trunk/src/main/java/org/nuiton/util/PeriodDates.java 2010-03-01 13:20:13 UTC (rev 1780) +++ trunk/src/main/java/org/nuiton/util/PeriodDates.java 2010-03-01 17:10:07 UTC (rev 1781) @@ -177,10 +177,21 @@ return months; } - Calendar current = (Calendar) fromCalendar.clone(); - current.set(Calendar.DAY_OF_MONTH, 1); - Calendar end = (Calendar) thruCalendar.clone(); - end.set(Calendar.DAY_OF_MONTH, 1); +// Calendar current = (Calendar) fromCalendar.clone(); +// current.set(Calendar.DAY_OF_MONTH, 1); +// Calendar end = (Calendar) thruCalendar.clone(); +// end.set(Calendar.DAY_OF_MONTH, 1); + + // Prepare calendars for while condition : + // set first day of month + reset time to 00:00:00.000 + Date tmp = DateUtils.setFirstDayOfMonth(fromCalendar.getTime()); + tmp = DateUtils.setMinTimeOfDay(tmp); + Calendar current = DateUtils.getDefaultCalendar(tmp); + + tmp = DateUtils.setFirstDayOfMonth(thruCalendar.getTime()); + tmp = DateUtils.setMinTimeOfDay(tmp); + Calendar end = DateUtils.getDefaultCalendar(tmp); + while (!current.equals(end)) { months.add(current.getTime()); current.add(Calendar.MONTH, 1); Added: trunk/src/test/java/org/nuiton/util/PeriodDatesTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/PeriodDatesTest.java (rev 0) +++ trunk/src/test/java/org/nuiton/util/PeriodDatesTest.java 2010-03-01 17:10:07 UTC (rev 1781) @@ -0,0 +1,230 @@ + +package org.nuiton.util; + +import java.text.DateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * + * @author fdesbois + */ +public class PeriodDatesTest { + + public PeriodDatesTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of createMonthsPeriodFromToday method, of class PeriodDates. + */ + //@Test + public void testCreateMonthsPeriodFromToday() { + System.out.println("createMonthsPeriodFromToday"); + } + + /** + * Test of getPattern method, of class PeriodDates. + */ + //@Test + public void testGetPattern() { + System.out.println("getPattern"); + } + + /** + * Test of setPattern method, of class PeriodDates. + */ + //@Test + public void testSetPattern() { + System.out.println("setPattern"); + } + + /** + * Test of initDayOfMonthExtremities method, of class PeriodDates. + */ + //@Test + public void testInitDayOfMonthExtremities() { + System.out.println("initDayOfMonthExtremities"); + } + + /** + * Test of setFromDate method, of class PeriodDates. + */ + //@Test + public void testSetFromDate() { + System.out.println("setFromDate"); + } + + /** + * Test of getFromDate method, of class PeriodDates. + */ + //@Test + public void testGetFromDate() { + System.out.println("getFromDate"); + } + + /** + * Test of getFromMonth method, of class PeriodDates. + */ + //@Test + public void testGetFromMonth() { + System.out.println("getFromMonth"); + } + + /** + * Test of setThruDate method, of class PeriodDates. + */ + //@Test + public void testSetThruDate() { + System.out.println("setThruDate"); + } + + /** + * Test of getThruDate method, of class PeriodDates. + */ + //@Test + public void testGetThruDate() { + System.out.println("getThruDate"); + } + + /** + * Test of getMonths method, of class PeriodDates. + */ + @Test + public void testGetMonths() { + System.out.println("getMonths"); + + // Prepare two calendars with time not equals to 0 + Calendar cal1 = DateUtils.getDefaultCalendar(new Date()); + cal1.set(Calendar.DAY_OF_MONTH, 3); + cal1.set(Calendar.MONTH, 2); + cal1.set(Calendar.YEAR, 2009); + cal1.set(Calendar.HOUR, 8); + + DateFormat timeFormat = DateFormat.getTimeInstance(); + System.out.println("getMonths:: time for calendarFrom : " + + timeFormat.format(cal1.getTime())); + + Calendar cal2 = DateUtils.getDefaultCalendar(new Date()); + cal2.set(Calendar.DAY_OF_MONTH, 22); + cal2.set(Calendar.MONTH, 6); + cal2.set(Calendar.YEAR, 2009); + cal2.set(Calendar.HOUR, 4); + + System.out.println("getMonths:: time for calendarThru : " + + timeFormat.format(cal2.getTime())); + + PeriodDates period = new PeriodDates(cal1, cal2); + + List<Date> months = period.getMonths(); + Assert.assertEquals(5, months.size()); + for (int i = 0; i < 5; i++) { + Date monthDate = months.get(i); + int month = DateUtils.getDefaultCalendar(monthDate).get(Calendar.MONTH); + Assert.assertEquals(i+2, month); // 2, 3, 4, 5, 6 + } + } + + /** + * Test of getFormatedMonths method, of class PeriodDates. + */ + //@Test + public void testGetFormatedMonths() { + System.out.println("getFormatedMonths"); + } + + /** + * Test of beforeEnd method, of class PeriodDates. + */ + //@Test + public void testBeforeEnd_Calendar() { + System.out.println("beforeEnd"); + } + + /** + * Test of afterEnd method, of class PeriodDates. + */ + //@Test + public void testAfterEnd_Calendar() { + System.out.println("afterEnd"); + } + + /** + * Test of afterBegin method, of class PeriodDates. + */ + //@Test + public void testAfterBegin_Calendar() { + System.out.println("afterBegin"); + } + + /** + * Test of between method, of class PeriodDates. + */ + //@Test + public void testBetween_Calendar() { + System.out.println("between"); + } + + /** + * Test of beforeEnd method, of class PeriodDates. + */ + //@Test + public void testBeforeEnd_Date() { + System.out.println("beforeEnd"); + } + + /** + * Test of afterEnd method, of class PeriodDates. + */ + //@Test + public void testAfterEnd_Date() { + System.out.println("afterEnd"); + } + + /** + * Test of afterBegin method, of class PeriodDates. + */ + //@Test + public void testAfterBegin_Date() { + System.out.println("afterBegin"); + } + + /** + * Test of between method, of class PeriodDates. + */ + //@Test + public void testBetween_Date() { + System.out.println("between"); + } + + /** + * Test of toString method, of class PeriodDates. + */ + //@Test + public void testToString() { + System.out.println("toString"); + } + +} \ No newline at end of file Property changes on: trunk/src/test/java/org/nuiton/util/PeriodDatesTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"