|
From: <ja...@us...> - 2006-10-17 15:33:34
|
Revision: 85
http://svn.sourceforge.net/surveyforge/?rev=85&view=rev
Author: javism
Date: 2006-10-17 08:33:09 -0700 (Tue, 17 Oct 2006)
Log Message:
-----------
BugFix: ValueDomain for dates and its tests.
Issue: 1570019
Modified Paths:
--------------
trunk/surveyforge-db/src/main/resources/hibernate.cfg.xml
Added Paths:
-----------
trunk/surveyforge-core/src/main/java/org/surveyforge/core/metadata/domain/DateValueDomain.java
trunk/surveyforge-core/src/test/java/org/surveyforge/core/metadata/domain/
trunk/surveyforge-core/src/test/java/org/surveyforge/core/metadata/domain/DateValueDomainTest.java
Added: trunk/surveyforge-core/src/main/java/org/surveyforge/core/metadata/domain/DateValueDomain.java
===================================================================
--- trunk/surveyforge-core/src/main/java/org/surveyforge/core/metadata/domain/DateValueDomain.java (rev 0)
+++ trunk/surveyforge-core/src/main/java/org/surveyforge/core/metadata/domain/DateValueDomain.java 2006-10-17 15:33:09 UTC (rev 85)
@@ -0,0 +1,126 @@
+/*
+ * surveyforge-core - Copyright (C) 2006 OPEN input - http://www.openinput.com/
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to
+ * the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+package org.surveyforge.core.metadata.domain;
+
+import java.io.Serializable;
+import java.security.InvalidParameterException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import javax.persistence.Entity;
+
+/**
+ * @author jsegura
+ */
+@Entity
+public class DateValueDomain extends AbstractValueDomain
+ {
+ private static final long serialVersionUID = -7700790723958655600L;
+
+ private SimpleDateFormat simpleDateFormat;
+ private String pattern = "dd/MM/yyyy";
+
+ private Date maxValue = null;
+ private Date minValue = null;
+
+ public DateValueDomain( )
+ {
+ simpleDateFormat = new SimpleDateFormat( pattern );
+ }
+
+ public SimpleDateFormat getFormat( )
+ {
+ return simpleDateFormat;
+ }
+
+ public void setPattern( String pattern )
+ {
+ if( pattern != null )
+ {
+ /* This will throw an exception if pattern is invalid */
+ simpleDateFormat.applyPattern( pattern );
+ this.pattern = pattern;
+ }
+ }
+
+ public String getPattern( )
+ {
+ return this.pattern;
+ }
+
+ public Date getMaxValue( )
+ {
+ return maxValue;
+ }
+
+ public void setMaxValue( Date maxValue )
+ {
+ if( maxValue != null && this.minValue != null )
+ {
+ if( this.minValue.before( maxValue ) )
+ this.maxValue = maxValue;
+ else
+ throw new InvalidParameterException( );
+ }
+ else
+ this.maxValue = maxValue;
+ }
+
+ public Date getMinValue( )
+ {
+ return minValue;
+ }
+
+ public void setMinValue( Date minValue )
+ {
+ if( this.maxValue != null && minValue != null )
+ {
+ if( minValue.before( this.maxValue ) )
+ this.minValue = minValue;
+ else
+ throw new InvalidParameterException( );
+ }
+ else
+ this.minValue = minValue;
+ }
+
+ public boolean isValid( Serializable object )
+ {
+ if( object instanceof Date )
+ {
+ Date date = (Date) object;
+ if( this.getMaxValue( ) != null && date.after( this.getMaxValue( ) ) )
+ return false;
+ else if( this.getMinValue( ) != null && date.before( this.getMinValue( ) ) )
+ return false;
+ else
+ return true;
+ }
+ else
+ return false;
+ }
+
+ public DateValueDomain clone( )
+ {
+ return (DateValueDomain) super.clone( );
+ }
+ }
\ No newline at end of file
Property changes on: trunk/surveyforge-core/src/main/java/org/surveyforge/core/metadata/domain/DateValueDomain.java
___________________________________________________________________
Name: svn:keywords
+ Date Revision Author HeadURL Id
Added: trunk/surveyforge-core/src/test/java/org/surveyforge/core/metadata/domain/DateValueDomainTest.java
===================================================================
--- trunk/surveyforge-core/src/test/java/org/surveyforge/core/metadata/domain/DateValueDomainTest.java (rev 0)
+++ trunk/surveyforge-core/src/test/java/org/surveyforge/core/metadata/domain/DateValueDomainTest.java 2006-10-17 15:33:09 UTC (rev 85)
@@ -0,0 +1,105 @@
+/**
+ *
+ */
+package org.surveyforge.core.metadata.domain;
+
+import java.security.InvalidParameterException;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.testng.Assert;
+import org.testng.annotations.ExpectedExceptions;
+import org.testng.annotations.Test;
+
+/**
+ * @author jsegura
+ */
+public class DateValueDomainTest
+ {
+
+ @Test
+ public void DateValueDomainGettersSetters( )
+ {
+ DateValueDomain domain = new DateValueDomain( );
+ domain.getFormat( );
+ Assert.assertNull( domain.getMaxValue( ) );
+ Assert.assertNull( domain.getMinValue( ) );
+
+ Date before = new Date( Calendar.getInstance( ).getTimeInMillis( ) );
+ Date later = new Date( Calendar.getInstance( ).getTimeInMillis( ) + 10000 );
+
+
+ String pattern = "yyyy";
+ domain.setPattern( pattern );
+ Assert.assertEquals( pattern, domain.getPattern( ) );
+
+ domain.setMaxValue( later );
+ Assert.assertNotNull( domain.getMaxValue( ) );
+ Assert.assertEquals( later, domain.getMaxValue( ) );
+
+ domain.setMinValue( before );
+ Assert.assertNotNull( domain.getMinValue( ) );
+ Assert.assertEquals( before, domain.getMinValue( ) );
+
+ domain.setMaxValue( later );
+ Assert.assertNotNull( domain.getMaxValue( ) );
+ Assert.assertEquals( later, domain.getMaxValue( ) );
+
+ }
+
+ @Test
+ public void DateValueDomainCreation( )
+ {
+ new DateValueDomain( );
+ }
+
+ @Test
+ @ExpectedExceptions( {InvalidParameterException.class})
+ public void DateValueDomainSetMaxValue( )
+ {
+ DateValueDomain domain = new DateValueDomain( );
+ Date before = new Date( Calendar.getInstance( ).getTimeInMillis( ) );
+ Date later = new Date( Calendar.getInstance( ).getTimeInMillis( ) + 10000 );
+
+ domain.setMinValue( later );
+ domain.setMaxValue( before );
+ }
+
+ @Test
+ @ExpectedExceptions( {InvalidParameterException.class})
+ public void DateValueDomainSetMinValue( )
+ {
+ DateValueDomain domain = new DateValueDomain( );
+ Date before = new Date( Calendar.getInstance( ).getTimeInMillis( ) );
+ Date later = new Date( Calendar.getInstance( ).getTimeInMillis( ) + 10000 );
+
+ domain.setMaxValue( before );
+ domain.setMinValue( later );
+ }
+
+ @Test
+ public void validationInRange( )
+ {
+
+ Assert.assertTrue( new DateValueDomain( ).isValid( new Date( ) ) );
+ Assert.assertFalse( new DateValueDomain( ).isValid( new String( ) ) );
+
+ DateValueDomain domain = new DateValueDomain( );
+ Date now = new Date( Calendar.getInstance( ).getTimeInMillis( ) );
+ Date later = new Date( Calendar.getInstance( ).getTimeInMillis( ) + 100000 );
+ domain.setMinValue( later );
+ Assert.assertFalse( domain.isValid( now ) );
+
+ domain = new DateValueDomain( );
+ domain.setMaxValue( now );
+ Assert.assertFalse( domain.isValid( later ) );
+ }
+
+ @Test
+ public void DateValueDomainClone( )
+ {
+ new DateValueDomain( ).clone( );
+ }
+
+
+ }
\ No newline at end of file
Property changes on: trunk/surveyforge-core/src/test/java/org/surveyforge/core/metadata/domain/DateValueDomainTest.java
___________________________________________________________________
Name: svn:keywords
+ Date Revision Author HeadURL Id
Modified: trunk/surveyforge-db/src/main/resources/hibernate.cfg.xml
===================================================================
--- trunk/surveyforge-db/src/main/resources/hibernate.cfg.xml 2006-10-09 14:33:53 UTC (rev 84)
+++ trunk/surveyforge-db/src/main/resources/hibernate.cfg.xml 2006-10-17 15:33:09 UTC (rev 85)
@@ -51,6 +51,7 @@
<mapping class="org.surveyforge.core.metadata.domain.StringValueDomain" />
<mapping class="org.surveyforge.core.metadata.domain.AbstractValueDomain" />
<mapping class="org.surveyforge.core.metadata.domain.StructuredValueDomain" />
+ <mapping class="org.surveyforge.core.metadata.domain.DateValueDomain" />
<!-- package org.surveyforge.core.data -->
<mapping class="org.surveyforge.core.data.RegisterData" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|