dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #10629
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2911: Simplified implementation of PeriodType.getNextPeriod and getPreviousPeriod. Improved testing cov...
------------------------------------------------------------
revno: 2911
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-02-24 19:52:47 +0100
message:
Simplified implementation of PeriodType.getNextPeriod and getPreviousPeriod. Improved testing coverage for PeriodTypes.
added:
dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/QuarterlyPeriodTypeTest.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/SixMonthlyPeriodTypeTest.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/YearlyPeriodTypeTest.java
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/FinancialAprilPeriodTypeTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java 2011-02-24 16:28:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java 2011-02-24 18:52:47 +0000
@@ -101,26 +101,16 @@
public Period getNextPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
-
- cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) - cal.get( Calendar.YEAR ) % 2 + 12 );
-
- Date startDate = cal.getTime();
cal.add( Calendar.YEAR, 1 );
- cal.add( Calendar.DAY_OF_YEAR, -2 );
- return new Period( this, startDate, cal.getTime() );
+ return createPeriod( cal );
}
@Override
public Period getPreviousPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) - cal.get( Calendar.YEAR ) % 2 + 2 );
- Date startDate = cal.getTime();
-
- cal.add( Calendar.YEAR, 1 );
- cal.add( Calendar.DAY_OF_YEAR, -2 );
-
- return new Period( this, startDate, cal.getTime() );
+ cal.add( Calendar.YEAR, -1 );
+ return createPeriod( cal );
}
/**
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java 2011-02-15 06:22:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java 2011-02-24 18:52:47 +0000
@@ -96,32 +96,21 @@
// -------------------------------------------------------------------------
// CalendarPeriodType functionality
// -------------------------------------------------------------------------
+
@Override
public Period getNextPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.DAY_OF_MONTH, 1 );
cal.add( Calendar.MONTH, 1 );
-
- Date startDate = cal.getTime();
-
- cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
-
- return new Period( this, startDate, cal.getTime() );
+ return createPeriod( cal );
}
@Override
public Period getPreviousPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.DAY_OF_MONTH, 1 );
cal.add( Calendar.MONTH, -1 );
-
- Date startDate = cal.getTime();
-
- cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
-
- return new Period( this, startDate, cal.getTime() );
+ return createPeriod( cal );
}
/**
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java 2011-02-15 06:22:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java 2011-02-24 18:52:47 +0000
@@ -99,30 +99,16 @@
public Period getNextPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.MONTH, cal.get( Calendar.MONTH ) - cal.get( Calendar.MONTH ) % 3 + 3 );
- cal.set( Calendar.DAY_OF_MONTH, 1 );
-
- Date startDate = cal.getTime();
-
- cal.add( Calendar.MONTH, 2 );
- cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
-
- return new Period( this, startDate, cal.getTime() );
+ cal.add( Calendar.MONTH, 3 );
+ return createPeriod( cal );
}
@Override
public Period getPreviousPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.MONTH, cal.get( Calendar.MONTH ) - cal.get( Calendar.MONTH ) % 3 - 3 );
- cal.set( Calendar.DAY_OF_MONTH, 1 );
-
- Date startDate = cal.getTime();
-
- cal.add( Calendar.MONTH, 2 );
- cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
-
- return new Period( this, startDate, cal.getTime() );
+ cal.add( Calendar.MONTH, -3 );
+ return createPeriod( cal );
}
/**
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java 2011-02-15 06:22:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java 2011-02-24 18:52:47 +0000
@@ -99,30 +99,16 @@
public Period getNextPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.MONTH, cal.get( Calendar.MONTH ) - cal.get( Calendar.MONTH ) % 6 + 6 );
- cal.set( Calendar.DAY_OF_MONTH, 1 );
-
- Date startDate = cal.getTime();
-
- cal.add( Calendar.MONTH, 5 );
- cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
-
- return new Period( this, startDate, cal.getTime() );
+ cal.add( Calendar.MONTH, 6 );
+ return createPeriod( cal );
}
@Override
public Period getPreviousPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.MONTH, cal.get( Calendar.MONTH ) - cal.get( Calendar.MONTH ) % 6 - 6 );
- cal.set( Calendar.DAY_OF_MONTH, 1 );
-
- Date startDate = cal.getTime();
-
- cal.add( Calendar.MONTH, 5 );
- cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
-
- return new Period( this, startDate, cal.getTime() );
+ cal.add( Calendar.MONTH, -6 );
+ return createPeriod( cal );
}
/**
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java 2011-02-15 06:22:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java 2011-02-24 18:52:47 +0000
@@ -98,28 +98,16 @@
public Period getNextPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
cal.add( Calendar.WEEK_OF_YEAR, 1 );
-
- Date startDate = cal.getTime();
-
- cal.add( Calendar.DAY_OF_YEAR, 6 );
-
- return new Period( this, startDate, cal.getTime() );
+ return createPeriod( cal );
}
@Override
public Period getPreviousPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
cal.add( Calendar.WEEK_OF_YEAR, -1 );
-
- Date startDate = cal.getTime();
-
- cal.add( Calendar.DAY_OF_YEAR, 6 );
-
- return new Period( this, startDate, cal.getTime() );
+ return createPeriod( cal );
}
@Override
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java 2011-02-15 06:22:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java 2011-02-24 18:52:47 +0000
@@ -96,28 +96,16 @@
public Period getNextPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.DAY_OF_YEAR, 1 );
cal.add( Calendar.YEAR, 1 );
-
- Date startDate = cal.getTime();
-
- cal.set( Calendar.DAY_OF_YEAR, cal.getActualMaximum( Calendar.DAY_OF_YEAR ) );
-
- return new Period( this, startDate, cal.getTime() );
+ return createPeriod( cal );
}
@Override
public Period getPreviousPeriod( Period period )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
- cal.set( Calendar.DAY_OF_YEAR, 1 );
cal.add( Calendar.YEAR, -1 );
-
- Date startDate = cal.getTime();
-
- cal.set( Calendar.DAY_OF_YEAR, cal.getActualMaximum( Calendar.DAY_OF_YEAR ) );
-
- return new Period( this, startDate, cal.getTime() );
+ return createPeriod( cal );
}
/**
=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/FinancialAprilPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/FinancialAprilPeriodTypeTest.java 2011-02-24 16:28:30 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/FinancialAprilPeriodTypeTest.java 2011-02-24 18:52:47 +0000
@@ -1,3 +1,5 @@
+package org.hisp.dhis.period;
+
/*
* Copyright (c) 2004-2010, University of Oslo
* All rights reserved.
@@ -25,8 +27,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-package org.hisp.dhis.period;
-
import static junit.framework.Assert.assertEquals;
import java.util.Calendar;
@@ -38,25 +38,25 @@
/**
* @author Lars Helge Overland
*/
-
public class FinancialAprilPeriodTypeTest
{
private Calendar startCal;
private Calendar endCal;
- private FinancialAprilPeriodType periodType;
+ private Calendar testCal;
+ private CalendarPeriodType periodType;
@Before
public void before()
{
startCal = PeriodType.createCalendarInstance();
endCal = PeriodType.createCalendarInstance();
+ testCal = PeriodType.createCalendarInstance();
periodType = new FinancialAprilPeriodType();
}
@Test
public void testCreatePeriod()
{
- Calendar testCal = PeriodType.createCalendarInstance();
testCal.set( 2009, Calendar.FEBRUARY, 15 );
startCal.set( 2008, Calendar.APRIL, 1 );
@@ -79,9 +79,40 @@
}
@Test
+ public void testGetNextPeriod()
+ {
+ testCal.set( 2009, Calendar.FEBRUARY, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getNextPeriod( period );
+
+ startCal.set( 2009, Calendar.APRIL, 1 );
+ endCal.set( 2010, Calendar.MARCH, 31 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetPreviousPeriod()
+ {
+ testCal.set( 2009, Calendar.FEBRUARY, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getPreviousPeriod( period );
+
+ startCal.set( 2007, Calendar.APRIL, 1 );
+ endCal.set( 2008, Calendar.MARCH, 31 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
public void testGeneratePeriods()
{
- Calendar testCal = PeriodType.createCalendarInstance();
testCal.set( 2009, 1, 15 );
List<Period> periods = periodType.generatePeriods( testCal.getTime() );
=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java 2011-02-24 18:52:47 +0000
@@ -0,0 +1,110 @@
+package org.hisp.dhis.period;
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static junit.framework.Assert.assertEquals;
+
+import java.util.Calendar;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class MonthlyPeriodTypeTest
+{
+ private Calendar startCal;
+ private Calendar endCal;
+ private Calendar testCal;
+ private CalendarPeriodType periodType;
+
+ @Before
+ public void before()
+ {
+ startCal = PeriodType.createCalendarInstance();
+ endCal = PeriodType.createCalendarInstance();
+ testCal = PeriodType.createCalendarInstance();
+ periodType = new MonthlyPeriodType();
+ }
+
+ @Test
+ public void testCreatePeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ startCal.set( 2009, Calendar.AUGUST, 1 );
+ endCal.set( 2009, Calendar.AUGUST, 31 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+
+ testCal.set( 2009, Calendar.JUNE, 15 );
+
+ startCal.set( 2009, Calendar.JUNE, 1 );
+ endCal.set( 2009, Calendar.JUNE, 30 );
+
+ period = periodType.createPeriod( testCal.getTime() );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetNextPeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getNextPeriod( period );
+
+ startCal.set( 2009, Calendar.SEPTEMBER, 1 );
+ endCal.set( 2009, Calendar.SEPTEMBER, 30 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetPreviousPeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getPreviousPeriod( period );
+
+ startCal.set( 2009, Calendar.JULY, 1 );
+ endCal.set( 2009, Calendar.JULY, 31 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+}
=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/QuarterlyPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/QuarterlyPeriodTypeTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/QuarterlyPeriodTypeTest.java 2011-02-24 18:52:47 +0000
@@ -0,0 +1,111 @@
+package org.hisp.dhis.period;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static junit.framework.Assert.assertEquals;
+
+import java.util.Calendar;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class QuarterlyPeriodTypeTest
+{
+ private Calendar startCal;
+ private Calendar endCal;
+ private Calendar testCal;
+ private CalendarPeriodType periodType;
+
+ @Before
+ public void before()
+ {
+ startCal = PeriodType.createCalendarInstance();
+ endCal = PeriodType.createCalendarInstance();
+ testCal = PeriodType.createCalendarInstance();
+ periodType = new QuarterlyPeriodType();
+ }
+
+ @Test
+ public void testCreatePeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ startCal.set( 2009, Calendar.JULY, 1 );
+ endCal.set( 2009, Calendar.SEPTEMBER, 30 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+
+ testCal.set( 2009, Calendar.APRIL, 15 );
+
+ startCal.set( 2009, Calendar.APRIL, 1 );
+ endCal.set( 2009, Calendar.JUNE, 30 );
+
+ period = periodType.createPeriod( testCal.getTime() );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetNextPeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getNextPeriod( period );
+
+ startCal.set( 2009, Calendar.OCTOBER, 1 );
+ endCal.set( 2009, Calendar.DECEMBER, 31 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetPreviousPeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getPreviousPeriod( period );
+
+ startCal.set( 2009, Calendar.APRIL, 1 );
+ endCal.set( 2009, Calendar.JUNE, 30 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+}
=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/SixMonthlyPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/SixMonthlyPeriodTypeTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/SixMonthlyPeriodTypeTest.java 2011-02-24 18:52:47 +0000
@@ -0,0 +1,111 @@
+package org.hisp.dhis.period;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static junit.framework.Assert.assertEquals;
+
+import java.util.Calendar;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class SixMonthlyPeriodTypeTest
+{
+ private Calendar startCal;
+ private Calendar endCal;
+ private Calendar testCal;
+ private CalendarPeriodType periodType;
+
+ @Before
+ public void before()
+ {
+ startCal = PeriodType.createCalendarInstance();
+ endCal = PeriodType.createCalendarInstance();
+ testCal = PeriodType.createCalendarInstance();
+ periodType = new SixMonthlyPeriodType();
+ }
+
+ @Test
+ public void testCreatePeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ startCal.set( 2009, Calendar.JULY, 1 );
+ endCal.set( 2009, Calendar.DECEMBER, 31 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+
+ testCal.set( 2009, Calendar.APRIL, 15 );
+
+ startCal.set( 2009, Calendar.JANUARY, 1 );
+ endCal.set( 2009, Calendar.JUNE, 30 );
+
+ period = periodType.createPeriod( testCal.getTime() );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetNextPeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getNextPeriod( period );
+
+ startCal.set( 2010, Calendar.JANUARY, 1 );
+ endCal.set( 2010, Calendar.JUNE, 30 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetPreviousPeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getPreviousPeriod( period );
+
+ startCal.set( 2009, Calendar.JANUARY, 1 );
+ endCal.set( 2009, Calendar.JUNE, 30 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+}
=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/YearlyPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/YearlyPeriodTypeTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/YearlyPeriodTypeTest.java 2011-02-24 18:52:47 +0000
@@ -0,0 +1,108 @@
+package org.hisp.dhis.period;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static junit.framework.Assert.assertEquals;
+
+import java.util.Calendar;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class YearlyPeriodTypeTest
+{
+ private Calendar startCal;
+ private Calendar endCal;
+ private Calendar testCal;
+ private CalendarPeriodType periodType;
+
+ @Before
+ public void before()
+ {
+ startCal = PeriodType.createCalendarInstance();
+ endCal = PeriodType.createCalendarInstance();
+ testCal = PeriodType.createCalendarInstance();
+ periodType = new YearlyPeriodType();
+ }
+
+ @Test
+ public void testCreatePeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ startCal.set( 2009, Calendar.JANUARY, 1 );
+ endCal.set( 2009, Calendar.DECEMBER, 31 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+
+ testCal.set( 2009, Calendar.APRIL, 15 );
+
+ period = periodType.createPeriod( testCal.getTime() );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetNextPeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getNextPeriod( period );
+
+ startCal.set( 2010, Calendar.JANUARY, 1 );
+ endCal.set( 2010, Calendar.DECEMBER, 31 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+
+ @Test
+ public void testGetPreviousPeriod()
+ {
+ testCal.set( 2009, Calendar.AUGUST, 15 );
+
+ Period period = periodType.createPeriod( testCal.getTime() );
+
+ period = periodType.getPreviousPeriod( period );
+
+ startCal.set( 2008, Calendar.JANUARY, 1 );
+ endCal.set( 2008, Calendar.DECEMBER, 31 );
+
+ assertEquals( startCal.getTime(), period.getStartDate() );
+ assertEquals( endCal.getTime(), period.getEndDate() );
+ }
+}