dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #30046
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15238: added simple tests for DateUnit, inject CalendarService into PeriodTypes (but not in use yet)
------------------------------------------------------------
revno: 15238
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-05-12 18:41:54 +0200
message:
added simple tests for DateUnit, inject CalendarService into PeriodTypes (but not in use yet)
added:
dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/
dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/DateUnitTest.java
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateIntervalType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/calendar/DefaultCalendarService.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/calendar/DateIntervalType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateIntervalType.java 2014-05-09 18:53:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateIntervalType.java 2014-05-12 16:41:54 +0000
@@ -34,7 +34,7 @@
public enum DateIntervalType
{
/**
- * Gives interval for a year, month or week based on ISO 8601.
+ * Gives interval for a year, month, week or day based on ISO 8601.
* @see <a href="http://en.wikipedia.org/wiki/ISO_8601">http://en.wikipedia.org/wiki/ISO_8601</a>
*/
ISO8601_YEAR, ISO8601_MONTH, ISO8601_WEEK, ISO8601_DAY
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java 2014-05-09 21:24:47 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java 2014-05-12 16:41:54 +0000
@@ -33,6 +33,7 @@
import org.joda.time.chrono.ISOChronology;
import javax.validation.constraints.NotNull;
+import java.util.Date;
import java.util.GregorianCalendar;
/**
@@ -153,10 +154,15 @@
public static DateUnit fromJdkCalendar( java.util.Calendar calendar )
{
- return new DateUnit( calendar.get( java.util.Calendar.YEAR ), calendar.get( java.util.Calendar.MONTH ),
+ return new DateUnit( calendar.get( java.util.Calendar.YEAR ), calendar.get( java.util.Calendar.MONTH ) + 1,
calendar.get( java.util.Calendar.DAY_OF_MONTH ), calendar.get( java.util.Calendar.DAY_OF_WEEK ) );
}
+ public static DateUnit fromJdkDate( Date date )
+ {
+ return fromDateTime( new DateTime( date.getTime() ) );
+ }
+
@Override
public String toString()
{
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java 2014-05-12 12:33:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java 2014-05-12 16:41:54 +0000
@@ -29,7 +29,9 @@
*/
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import org.hisp.dhis.calendar.CalendarService;
import org.hisp.dhis.common.DxfNamespaces;
+import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import java.text.ParseException;
@@ -55,6 +57,14 @@
*/
private static final long serialVersionUID = 2402122626196305083L;
+ private CalendarService calendarService;
+
+ @Autowired
+ public void setCalendarService( CalendarService calendarService )
+ {
+ this.calendarService = calendarService;
+ }
+
// -------------------------------------------------------------------------
// Available PeriodTypes
// -------------------------------------------------------------------------
=== added directory 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar'
=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/DateUnitTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/DateUnitTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/DateUnitTest.java 2014-05-12 16:41:54 +0000
@@ -0,0 +1,64 @@
+package org.hisp.dhis.calendar;
+
+/*
+ * Copyright (c) 2004-2014, 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 org.junit.Test;
+
+import java.util.*;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class DateUnitTest
+{
+ @Test
+ public void fromJdkDateTest()
+ {
+ java.util.Calendar cal = new GregorianCalendar( 2014, 2, 20 );
+ Date date = cal.getTime();
+
+ DateUnit dateUnit = DateUnit.fromJdkDate( date );
+ assertEquals( 2014, dateUnit.getYear() );
+ assertEquals( 3, dateUnit.getMonth() );
+ assertEquals( 20, dateUnit.getDay() );
+ }
+
+ @Test
+ public void fromJdkCalendarTest()
+ {
+ java.util.Calendar cal = new GregorianCalendar( 2014, 2, 20 );
+
+ DateUnit dateUnit = DateUnit.fromJdkCalendar( cal );
+ assertEquals( 2014, dateUnit.getYear() );
+ assertEquals( 3, dateUnit.getMonth() );
+ assertEquals( 20, dateUnit.getDay() );
+ }
+}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/calendar/DefaultCalendarService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/calendar/DefaultCalendarService.java 2014-04-29 10:27:58 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/calendar/DefaultCalendarService.java 2014-05-12 16:41:54 +0000
@@ -32,6 +32,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.hisp.dhis.calendar.impl.Iso8601Calendar;
+import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.setting.SystemSettingManager;
import org.springframework.beans.factory.annotation.Autowired;
@@ -66,6 +67,11 @@
{
calendarMap.put( calendar.name(), calendar );
}
+
+ for ( PeriodType periodType : PeriodType.PERIOD_TYPES )
+ {
+ periodType.setCalendarService( this );
+ }
}
@Override