← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 336: Added unit test for WeeklyPeriodType. Resolved ambiguity around first week of year and first day...

 

------------------------------------------------------------
revno: 336
committer: Bob Jolliffe <bobjolliffe@xxxxxxxxx
branch nick: dhis2
timestamp: Wed 2009-05-27 12:49:26 +0100
message:
  Added unit test for WeeklyPeriodType.  Resolved ambiguity around first week of year and first day of week.
added:
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/WeeklyPeriodTypeTest.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java

=== 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	2009-03-03 16:46:36 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java	2009-05-27 11:49:26 +0000
@@ -134,9 +134,6 @@
 
             if ( cal.get( Calendar.YEAR ) != cal2.get( Calendar.YEAR ) )
             {
-                // Use saturday as Calendar has sunday as first day of week.
-                cal2.add( Calendar.DAY_OF_WEEK, -1 );
-
                 if ( cal2.get( Calendar.WEEK_OF_YEAR ) == 1 )
                 {
                     cal = cal2;
@@ -149,9 +146,16 @@
         // Generate weeks
         // ---------------------------------------------------------------------
 
+        // Enforce ISO8601 week to match createPeriod, getNextPeriod etc
+        // [Note: perhaps there is need for another weekly type based on locale]
+        // 1st day of week is Monday
+        // 1st week of the year is the first week with a Thursday
+        cal.setMinimalDaysInFirstWeek(4);
+        cal.setFirstDayOfWeek(Calendar.MONDAY);
+        
         cal.set( Calendar.WEEK_OF_YEAR, 1 );
         cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
-
+        
         int firstWeek = cal.get( Calendar.WEEK_OF_YEAR );
 
         ArrayList<Period> weeks = new ArrayList<Period>();

=== added directory 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period'
=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/WeeklyPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/WeeklyPeriodTypeTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/WeeklyPeriodTypeTest.java	2009-05-27 11:49:26 +0000
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2004-2009, 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.
+ */
+package org.hisp.dhis.period;
+
+import static org.junit.Assert.*;
+
+import java.util.Calendar;
+import java.util.List;
+
+import org.junit.Test;
+
+/**
+ * @author Bob Jolliffe
+ * @version $Id$
+ */
+public class WeeklyPeriodTypeTest {
+
+
+	/**
+	 * Test method for {@link org.hisp.dhis.period.WeeklyPeriodType#generatePeriods(org.hisp.dhis.period.Period)}.
+	 */
+	@Test
+	public void testGeneratePeriods() {
+		// get calendar of default locale
+	    Calendar testCal = Calendar.getInstance();
+	    WeeklyPeriodType wpt = new WeeklyPeriodType();
+	    for (int year = 1990; year<2020; year++) {
+	      for (int day=-7; day<7; day++)
+	      	{
+	    	  testCal.set(year, 0, 1); // 1st day of year
+	    	  testCal.add(Calendar.DATE, day);
+	    	  // System.err.println("testing "+testCal.getTime());
+	    	  Period p1 = wpt.createPeriod(testCal.getTime());
+	    	  List<Period> generatedPeriods = wpt.generatePeriods(p1);
+	    	  assertTrue("Period "+p1+" in generated set",generatedPeriods.contains(p1));
+	      	}
+	    }	
+	
+	}
+	
+	/**
+	 * Test method for {@link org.hisp.dhis.period.WeeklyPeriodType#createPeriod(Calendar)}.
+	 */
+	@Test
+	public void testCreatePeriod() {
+		Calendar testCal = Calendar.getInstance();
+		Calendar startCal = Calendar.getInstance();
+		Calendar endCal = Calendar.getInstance();
+		
+		// arbitrary instance - should increase coverage
+		testCal.set(2009, 4, 27); // Wednesday
+		WeeklyPeriodType wpt = new WeeklyPeriodType();
+		
+		assertFalse(startCal.after(testCal));
+		assertFalse(endCal.before(testCal));
+		Period p = wpt.createPeriod(testCal.getTime());
+		startCal.set(Calendar.DAY_OF_MONTH, p.getStartDate().getDate());         
+		assertTrue(startCal.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY);
+		endCal.set(Calendar.DAY_OF_MONTH, p.getEndDate().getDate());         
+		assertTrue(endCal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY);
+		
+		
+	}
+
+}



--
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.