Author: jcouteau Date: 2011-08-19 15:18:58 +0200 (Fri, 19 Aug 2011) New Revision: 2170 Url: http://nuiton.org/repositories/revision/nuiton-utils/2170 Log: Vat and Siret validators Added: trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/field/SiretFieldValidator.java trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/field/VATIntraFieldValidator.java trunk/nuiton-validator/src/test/java/org/nuiton/validator/model/Company.java trunk/nuiton-validator/src/test/java/org/nuiton/validator/xwork2/field/SiretFieldValidatorTest.java trunk/nuiton-validator/src/test/java/org/nuiton/validator/xwork2/field/VatIntraFieldValidatorTest.java trunk/nuiton-validator/src/test/resources/org/nuiton/validator/model/Company-error-validation.xml Modified: trunk/nuiton-validator/src/test/resources/validators.xml Added: trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/field/SiretFieldValidator.java =================================================================== --- trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/field/SiretFieldValidator.java (rev 0) +++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/field/SiretFieldValidator.java 2011-08-19 13:18:58 UTC (rev 2170) @@ -0,0 +1,68 @@ +/* + * #%L + * Nuiton Utils :: Nuiton Validator + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.validator.xwork2.field; + +import com.opensymphony.xwork2.validator.ValidationException; +import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Validator for French SIRET numbers + */ +public class SiretFieldValidator extends FieldValidatorSupport { + + protected static String LAST_NAME_REGEXP = "[0-9]{14}"; + protected static Pattern p = Pattern.compile(LAST_NAME_REGEXP); + + //TODO JC-19082011- Need to validate the numbers too cf: http://fr.wikipedia.org/wiki/SIRET + + @Override + public void validate(Object object) throws ValidationException { + + String fieldName = getFieldName(); + Object value = getFieldValue(fieldName, object); + + if (value == null) { + // no value defined + return; + } + if (value instanceof String) { + Matcher m = p.matcher((String) value); + if (!m.matches()) { + addFieldError(fieldName, object); + return; + } + } else { + addFieldError(fieldName, object); + return; + } + } + + @Override + public String getValidatorType() { + return "siret"; + } +} Added: trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/field/VATIntraFieldValidator.java =================================================================== --- trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/field/VATIntraFieldValidator.java (rev 0) +++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/field/VATIntraFieldValidator.java 2011-08-19 13:18:58 UTC (rev 2170) @@ -0,0 +1,66 @@ +/* + * #%L + * Nuiton Utils :: Nuiton Validator + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.validator.xwork2.field; + +import com.opensymphony.xwork2.validator.ValidationException; +import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Validator for EU VAT number + */ +public class VATIntraFieldValidator extends FieldValidatorSupport { + + protected static String VAT_INTRA_REGEXP = "^(RO\\d{2,10}|GB\\d{5}|(ATU|DK|FI|HU|LU|MT|CZ|SI)\\d{8}|IE[A-Z\\d]{8}|(DE|BG|EE|EL|LT|BE0|PT|CZ)\\d{9}|CY\\d{8}[A-Z]|(ES|GB)[A-Z\\d]{9}|(BE0|PL|SK|CZ)\\d{10}|(FR|IT|LV)\\d{11}|(LT|SE)\\d{12}|(NL|GB)[A-Z\\d]{12})$"; + protected static Pattern p = Pattern.compile(VAT_INTRA_REGEXP); + + @Override + public void validate(Object object) throws ValidationException { + + String fieldName = getFieldName(); + Object value = getFieldValue(fieldName, object); + + if (value == null) { + // no value defined + return; + } + if (value instanceof String) { + Matcher m = p.matcher((String) value); + if (!m.matches()) { + addFieldError(fieldName, object); + return; + } + } else { + addFieldError(fieldName, object); + return; + } + } + + @Override + public String getValidatorType() { + return "VATIntra"; + } +} Added: trunk/nuiton-validator/src/test/java/org/nuiton/validator/model/Company.java =================================================================== --- trunk/nuiton-validator/src/test/java/org/nuiton/validator/model/Company.java (rev 0) +++ trunk/nuiton-validator/src/test/java/org/nuiton/validator/model/Company.java 2011-08-19 13:18:58 UTC (rev 2170) @@ -0,0 +1,55 @@ +/* + * #%L + * Nuiton Utils :: Nuiton Validator + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.validator.model; + +/** + * + */ +public class Company { + + public static final String PROPERTY_VAT_NUMBER = "vat"; + + public static final String PROPERTY_SIRET = "siret"; + + protected String vat; + + protected String siret; + + public String getVat() { + return vat; + } + + public void setVat(String vat) { + this.vat = vat; + } + + public String getSiret() { + return siret; + } + + public void setSiret(String siret) { + this.siret = siret; + } +} Added: trunk/nuiton-validator/src/test/java/org/nuiton/validator/xwork2/field/SiretFieldValidatorTest.java =================================================================== --- trunk/nuiton-validator/src/test/java/org/nuiton/validator/xwork2/field/SiretFieldValidatorTest.java (rev 0) +++ trunk/nuiton-validator/src/test/java/org/nuiton/validator/xwork2/field/SiretFieldValidatorTest.java 2011-08-19 13:18:58 UTC (rev 2170) @@ -0,0 +1,66 @@ +/* + * #%L + * Nuiton Utils :: Nuiton Validator + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.validator.xwork2.field; + +import org.junit.Test; +import org.nuiton.validator.model.Company; + +/** + * User: couteau + * Date: 19/08/11 + */ +public class SiretFieldValidatorTest extends AbstractFieldValidatorTest<Company> { + + public SiretFieldValidatorTest() { + super(Company.class); + } + + @Test + @Override + public void testValidator() throws Exception { + + assertNull(bean.getSiret()); + + // Valid siret + bean.setSiret("44211670300038"); + assertFieldInError(Company.PROPERTY_SIRET, "company.siret.format", + false); + + // Not valid siret + bean.setSiret("4421167030003"); + assertFieldInError(Company.PROPERTY_SIRET, "company.siret.format", + true); + + // Not valid siret + bean.setSiret("442116703000389"); + assertFieldInError(Company.PROPERTY_SIRET, "company.siret.format", + true); + + // Not valid siret + bean.setSiret("4421bf1670300038"); + assertFieldInError(Company.PROPERTY_SIRET, "company.siret.format", + true); + } +} Added: trunk/nuiton-validator/src/test/java/org/nuiton/validator/xwork2/field/VatIntraFieldValidatorTest.java =================================================================== --- trunk/nuiton-validator/src/test/java/org/nuiton/validator/xwork2/field/VatIntraFieldValidatorTest.java (rev 0) +++ trunk/nuiton-validator/src/test/java/org/nuiton/validator/xwork2/field/VatIntraFieldValidatorTest.java 2011-08-19 13:18:58 UTC (rev 2170) @@ -0,0 +1,65 @@ +/* + * #%L + * Nuiton Utils :: Nuiton Validator + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.validator.xwork2.field; + +import org.junit.Test; +import org.nuiton.validator.model.Company; + +/** + * + */ +public class VatIntraFieldValidatorTest extends AbstractFieldValidatorTest<Company> { + + public VatIntraFieldValidatorTest() { + super(Company.class); + } + + @Test + @Override + public void testValidator() throws Exception { + + assertNull(bean.getVat()); + + // Valid vat + bean.setVat("FR57442116703"); + assertFieldInError(Company.PROPERTY_VAT_NUMBER, "company.vat.format", + false); + + // Not valid vat + bean.setVat("FR 57 44211670332"); + assertFieldInError(Company.PROPERTY_VAT_NUMBER, "company.vat.format", + true); + + // Not valid vat + bean.setVat("442116703000389"); + assertFieldInError(Company.PROPERTY_VAT_NUMBER, "company.vat.format", + true); + + // Not valid vat, spaces in it + bean.setVat("FR 57 442116703"); + assertFieldInError(Company.PROPERTY_VAT_NUMBER, "company.vat.format", + true); + } +} Added: trunk/nuiton-validator/src/test/resources/org/nuiton/validator/model/Company-error-validation.xml =================================================================== --- trunk/nuiton-validator/src/test/resources/org/nuiton/validator/model/Company-error-validation.xml (rev 0) +++ trunk/nuiton-validator/src/test/resources/org/nuiton/validator/model/Company-error-validation.xml 2011-08-19 13:18:58 UTC (rev 2170) @@ -0,0 +1,42 @@ +<!-- + #%L + Nuiton Utils :: Nuiton Validator + + $Id$ + $HeadURL$ + %% + Copyright (C) 2011 CodeLutin + %% + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. + + You should have received a copy of the GNU General Lesser Public + License along with this program. If not, see + <http://www.gnu.org/licenses/lgpl-3.0.html>. + #L% + --> +<!DOCTYPE validators PUBLIC + "-//OpenSymphony Group//XWork Validator 1.0.2//EN" + "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> +<validators> + + <field name="siret"> + <field-validator type="siret"> + <message>company.siret.format</message> + </field-validator> + </field> + + <field name="vat"> + <field-validator type="vat"> + <message>company.vat.format</message> + </field-validator> + </field> + +</validators> \ No newline at end of file Modified: trunk/nuiton-validator/src/test/resources/validators.xml =================================================================== --- trunk/nuiton-validator/src/test/resources/validators.xml 2011-08-19 07:35:44 UTC (rev 2169) +++ trunk/nuiton-validator/src/test/resources/validators.xml 2011-08-19 13:18:58 UTC (rev 2170) @@ -61,5 +61,7 @@ <validator name="cityName" class="org.nuiton.validator.xwork2.field.CityNameFieldValidator"/> <validator name="lastName" class="org.nuiton.validator.xwork2.field.LastNameFieldValidator"/> <validator name="postCode" class="org.nuiton.validator.xwork2.field.PostCodeFieldValidator"/> + <validator name="siret" class="org.nuiton.validator.xwork2.field.SiretFieldValidator"/> + <validator name="vat" class="org.nuiton.validator.xwork2.field.VATIntraFieldValidator"/> </validators> \ No newline at end of file