r101 - in trunk/coser-business/src: main/java/fr/ifremer/coser/validators main/resources/fr/ifremer/coser/bean test/java/fr/ifremer/coser/services
Author: chatellier Date: 2010-10-25 16:22:54 +0000 (Mon, 25 Oct 2010) New Revision: 101 Log: Manage NA as valid double value Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/validators/CoserCheckDoubleValidator.java trunk/coser-business/src/main/resources/fr/ifremer/coser/bean/Catch-error-validation.xml trunk/coser-business/src/main/resources/fr/ifremer/coser/bean/Length-error-validation.xml trunk/coser-business/src/test/java/fr/ifremer/coser/services/ValidationServiceTest.java Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/validators/CoserCheckDoubleValidator.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/validators/CoserCheckDoubleValidator.java 2010-10-25 15:44:33 UTC (rev 100) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/validators/CoserCheckDoubleValidator.java 2010-10-25 16:22:54 UTC (rev 101) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2010 Codelutin, Chatellier Eric + * Copyright (C) 2010 Ifremer, Codelutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -29,7 +29,7 @@ import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; /** - * TODO add comment here. + * Validateur de double de coser. * * @author chatellier * @version $Revision$ @@ -39,25 +39,16 @@ */ public class CoserCheckDoubleValidator extends FieldValidatorSupport { - protected Double max = null; - protected Double min = null; + protected String notAvailable; - public void setMax(Double max) { - this.max = max; + public String getNotAvailable() { + return notAvailable; } - public Double getMax() { - return max; + public void setNotAvailable(String notAvailable) { + this.notAvailable = notAvailable; } - public void setMin(Double min) { - this.min = min; - } - - public Double getMin() { - return min; - } - public void validate(Object object) throws ValidationException { Object obj = getFieldValue(getFieldName(), object); String value = (String) obj; @@ -68,22 +59,16 @@ return; } - double doubleValue = 0; + // If NA value = object it's ok too + if (notAvailable != null && notAvailable.equals(value)) { + return; + } + try { - doubleValue = Double.parseDouble(value); + Double.parseDouble(value); } catch (NumberFormatException ex) { addFieldError(getFieldName(), object); } - - // only check for a minimum value if the min parameter is set - if (min != null && doubleValue < min) { - addFieldError(getFieldName(), object); - } - - // only check for a maximum value if the max parameter is set - if (max != null && doubleValue > max) { - addFieldError(getFieldName(), object); - } } } Modified: trunk/coser-business/src/main/resources/fr/ifremer/coser/bean/Catch-error-validation.xml =================================================================== --- trunk/coser-business/src/main/resources/fr/ifremer/coser/bean/Catch-error-validation.xml 2010-10-25 15:44:33 UTC (rev 100) +++ trunk/coser-business/src/main/resources/fr/ifremer/coser/bean/Catch-error-validation.xml 2010-10-25 16:22:54 UTC (rev 101) @@ -52,11 +52,13 @@ </field> <field name="number"> <field-validator type="checkDouble" short-circuit="true"> + <param name="notAvailable">NA</param> <message>Number attribute is not a valid double</message> </field-validator> </field> <field name="weight"> <field-validator type="checkDouble" short-circuit="true"> + <param name="notAvailable">NA</param> <message>Weight attribute is not a valid double</message> </field-validator> </field> Modified: trunk/coser-business/src/main/resources/fr/ifremer/coser/bean/Length-error-validation.xml =================================================================== --- trunk/coser-business/src/main/resources/fr/ifremer/coser/bean/Length-error-validation.xml 2010-10-25 15:44:33 UTC (rev 100) +++ trunk/coser-business/src/main/resources/fr/ifremer/coser/bean/Length-error-validation.xml 2010-10-25 16:22:54 UTC (rev 101) @@ -67,11 +67,13 @@ </field> <field name="number"> <field-validator type="checkDouble" short-circuit="true"> + <param name="notAvailable">NA</param> <message>number attribute is not a valid double</message> </field-validator> </field> <field name="weight"> <field-validator type="checkDouble" short-circuit="true"> + <param name="notAvailable">NA</param> <message>Weight attribute is not a valid double</message> </field-validator> </field> Modified: trunk/coser-business/src/test/java/fr/ifremer/coser/services/ValidationServiceTest.java =================================================================== --- trunk/coser-business/src/test/java/fr/ifremer/coser/services/ValidationServiceTest.java 2010-10-25 15:44:33 UTC (rev 100) +++ trunk/coser-business/src/test/java/fr/ifremer/coser/services/ValidationServiceTest.java 2010-10-25 16:22:54 UTC (rev 101) @@ -36,7 +36,7 @@ import fr.ifremer.coser.control.ValidationError; /** - * TODO add comment here. + * Test abour validation. * * @author chatellier * @version $Revision$ @@ -50,25 +50,42 @@ protected ValidationService validationService = new ValidationService(config); + /** + * Test les validations sur les champs vide. + */ @Test public void testCatchValidation() { Catch myCatch = new Catch(); - myCatch.setData(new String[]{"","","","","",""}); + myCatch.setData(new String[]{"1", "","","","","",""}); List<ValidationError> errors = validationService.validate(myCatch); + if (log.isDebugEnabled()) { + log.debug("Validation errors = " + errors); + } Assert.assertNotNull(errors); Assert.assertFalse(errors.isEmpty()); + + myCatch.setData(new String[]{"1", "Toto","","","","999",""}); + errors = validationService.validate(myCatch); if (log.isDebugEnabled()) { log.debug("Validation errors = " + errors); } - - myCatch.setData(new String[]{"Toto","","","","999",""}); - errors = validationService.validate(myCatch); Assert.assertNotNull(errors); Assert.assertFalse(errors.isEmpty()); + } + + /** + * Test la validation des valeurs NA pour les doubles. + */ + @Test + public void testDoubleValidation() { + Catch myCatch = new Catch(); + myCatch.setData(new String[]{"1", "Test survey","1999","Test trait","Test sp","NA","12"}); + List<ValidationError> errors = validationService.validate(myCatch); if (log.isDebugEnabled()) { log.debug("Validation errors = " + errors); } + Assert.assertNull(errors); } }
participants (1)
-
chatellierï¼ users.labs.libre-entreprise.org