dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #27147
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13467: Improved ValidationUtils
------------------------------------------------------------
revno: 13467
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2013-12-29 22:11:38 +0100
message:
Improved ValidationUtils
modified:
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java
dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java 2013-10-11 12:58:30 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java 2013-12-29 21:11:38 +0000
@@ -34,6 +34,7 @@
import java.util.Random;
import java.util.regex.Pattern;
+import org.hisp.dhis.datavalue.DataValue;
import org.hisp.dhis.expression.Operator;
import org.nfunk.jep.JEP;
@@ -328,8 +329,7 @@
public static boolean isPositiveInteger( String value )
{
return value != null && POSITIVE_INT_PATTERN.matcher( value ).matches();
- }
-
+ }
/**
* Returns true if the provided string argument is to be considered a positive
@@ -344,7 +344,6 @@
return value != null && POSITIVE_OR_ZERO_INT_PATTERN.matcher( value ).matches();
}
-
/**
* Returns true if the provided string argument is to be considered a negative
* integer.
@@ -370,6 +369,18 @@
}
/**
+ * Indicates if the provided string argument is to be considered as a boolean,
+ * more specifically if it equals "true" or "false".
+ *
+ * @param value the value.
+ * @return if the provided string argument is to be considered as a boolean.
+ */
+ public static boolean isBool( String value )
+ {
+ return value != null && ( value.equals( DataValue.TRUE ) || value.equals( DataValue.FALSE ) );
+ }
+
+ /**
* Tests whether the two decimal numbers are equal with a tolerance of 0.01.
* If one or both of the numbers are null, false is returned.
*
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java 2013-11-01 08:11:31 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java 2013-12-29 21:11:38 +0000
@@ -32,6 +32,7 @@
import org.apache.commons.validator.routines.EmailValidator;
import org.apache.commons.validator.routines.UrlValidator;
import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.datavalue.DataValue;
import java.awt.geom.Point2D;
import java.util.Arrays;
@@ -249,6 +250,9 @@
* <li>value_not_positive_integer</li>
* <li>value_not_negative_integer</li>
* <li>value_is_zero_and_not_zero_significant</li>
+ * <li>value_not_bool</li>
+ * <li>value_not_true_only</li>
+ * <li>value_not_valid_date</li>
* </ul>
*
* @param value the data value.
@@ -268,7 +272,7 @@
}
List<String> types = Arrays.asList( VALUE_TYPE_STRING, VALUE_TYPE_INT, VALUE_TYPE_NUMBER,
- VALUE_TYPE_POSITIVE_INT, VALUE_TYPE_NEGATIVE_INT, VALUE_TYPE_ZERO_OR_POSITIVE_INT);
+ VALUE_TYPE_POSITIVE_INT, VALUE_TYPE_NEGATIVE_INT, VALUE_TYPE_ZERO_OR_POSITIVE_INT );
String type = dataElement.getDetailedNumberType();
@@ -301,7 +305,22 @@
{
return "value_not_zero_or_positive_integer";
}
+
+ if ( VALUE_TYPE_BOOL.equals( type ) && !MathUtils.isBool( value ) )
+ {
+ return "value_not_bool";
+ }
+ if ( VALUE_TYPE_TRUE_ONLY.equals( type ) && !DataValue.TRUE.equals( value ) )
+ {
+ return "value_not_true_only";
+ }
+
+ if ( VALUE_TYPE_DATE.equals( type ) && !DateUtils.dateIsValid( value ) )
+ {
+ return "value_not_valid_date";
+ }
+
if ( VALUE_TYPE_INT.equals( dataElement.getType() ) && MathUtils.isZero( value ) &&
!dataElement.isZeroIsSignificant() && !AGGREGATION_OPERATOR_AVERAGE.equals( dataElement.getAggregationOperator() ) )
{
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java 2013-09-20 15:13:05 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java 2013-12-29 21:11:38 +0000
@@ -136,14 +136,12 @@
assertNull( dataValueIsValid( "3", de ) );
assertNotNull( dataValueIsValid( "-4", de ) );
-
de.setNumberType( DataElement.VALUE_TYPE_NEGATIVE_INT );
assertNull( dataValueIsValid( "-3", de ) );
assertNotNull( dataValueIsValid( "4", de ) );
- de.setNumberType( DataElement.VALUE_TYPE_INT );
-
+ de.setNumberType( DataElement.VALUE_TYPE_INT );
assertNotNull( dataValueIsValid( "0", de ) );
de.setAggregationOperator( DataElement.AGGREGATION_OPERATOR_AVERAGE );
@@ -151,8 +149,24 @@
assertNull( dataValueIsValid( "0", de ) );
de.setAggregationOperator( DataElement.AGGREGATION_OPERATOR_SUM );
+
de.setType( DataElement.VALUE_TYPE_TEXT );
assertNull( dataValueIsValid( "0", de ) );
+
+ de.setType( DataElement.VALUE_TYPE_BOOL );
+
+ assertNull( dataValueIsValid( "true", de ) );
+ assertNotNull( dataValueIsValid( "yes", de ) );
+
+ de.setType( DataElement.VALUE_TYPE_TRUE_ONLY );
+
+ assertNull( dataValueIsValid( "true", de ) );
+ assertNotNull( dataValueIsValid( "false", de ) );
+
+ de.setType( DataElement.VALUE_TYPE_DATE );
+ assertNull( dataValueIsValid( "2013-04-01", de ) );
+ assertNotNull( dataValueIsValid( "2012304-01", de ) );
+ assertNotNull( dataValueIsValid( "Date", de ) );
}
}