You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by si...@apache.org on 2012/01/07 11:47:09 UTC
svn commit: r1228593 - in /incubator/bval/trunk/bval-extras/src:
main/java/org/apache/bval/extras/constraints/checkdigit/
test/java/org/apache/bval/extras/constraints/checkdigit/
Author: simonetripodi
Date: Sat Jan 7 10:47:09 2012
New Revision: 1228593
URL: http://svn.apache.org/viewvc?rev=1228593&view=rev
Log:
added IBAN validation, imported and readapted from commons-validations
Added:
incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBAN.java (with props)
incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBANValidator.java (with props)
incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/IBANCheckDigitTest.java (with props)
Modified:
incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/AbstractCheckDigitTest.java
Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBAN.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBAN.java?rev=1228593&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBAN.java (added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBAN.java Sat Jan 7 10:47:09 2012
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bval.extras.constraints.checkdigit;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+/**
+ * <p>
+ * --
+ * TODO - This class is NOT part of the bean_validation spec and might disappear
+ * as soon as a final version of the specification contains a similar functionality.
+ * --
+ * </p>
+ * Description: annotation to validate a java.io.File is a directory<br/>
+ */
+@Documented
+@Constraint( validatedBy = IBANValidator.class )
+@Target( { FIELD, ANNOTATION_TYPE, PARAMETER } )
+@Retention( RUNTIME )
+public @interface IBAN {
+
+ Class<?>[] groups() default {};
+
+ String message() default "{org.apache.bval.extras.constraints.checkdigit.IBAN.message}";
+
+ Class<? extends Payload>[] payload() default {};
+
+}
Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBAN.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBAN.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBAN.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBANValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBANValidator.java?rev=1228593&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBANValidator.java (added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBANValidator.java Sat Jan 7 10:47:09 2012
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bval.extras.constraints.checkdigit;
+
+import static java.lang.Character.getNumericValue;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * <b>IBAN</b> (International Bank Account Number) Check Digit calculation/validation.
+ * <p>
+ * This rountine is based on the ISO 7064 Mod 97,10 check digit caluclation routine.
+ * <p>
+ * The two check digit characters in a IBAN number are the third and fourth characters
+ * in the code. For <i>check digit</i> calculation/validation the first four characters are moved
+ * to the end of the code.
+ * So <code>CCDDnnnnnnn</code> becomes <code>nnnnnnnCCDD</code> (where
+ * <code>CC</code> is the country code and <code>DD</code> is the check digit). For
+ * check digit calcualtion the check digit value should be set to zero (i.e.
+ * <code>CC00nnnnnnn</code> in this example.
+ * <p>
+ * For further information see
+ * <a href="http://en.wikipedia.org/wiki/International_Bank_Account_Number">Wikipedia -
+ * IBAN number</a>.
+ */
+public final class IBANValidator
+ implements ConstraintValidator<IBAN, String> {
+
+ private static final long MAX = 999999999;
+
+ private static final long MODULUS = 97;
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isValid(String code, ConstraintValidatorContext context) {
+ if (code.length() < 5) {
+ return false;
+ }
+
+ String reformattedCode = code.substring(4) + code.substring(0, 4);
+ long total = 0;
+ for (int i = 0; i < reformattedCode.length(); i++) {
+ int charValue = getNumericValue(reformattedCode.charAt(i));
+ if (charValue < 0 || charValue > 35) {
+ return false;
+ }
+ total = (charValue > 9 ? total * 100 : total * 10) + charValue;
+ if (total > MAX) {
+ total = (total % MODULUS);
+ }
+ }
+
+ return (total % MODULUS) == 1;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void initialize( IBAN iban ) {
+ // not needed
+ }
+
+}
Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBANValidator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBANValidator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/IBANValidator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/AbstractCheckDigitTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/AbstractCheckDigitTest.java?rev=1228593&r1=1228592&r2=1228593&view=diff
==============================================================================
--- incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/AbstractCheckDigitTest.java (original)
+++ incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/AbstractCheckDigitTest.java Sat Jan 7 10:47:09 2012
@@ -35,7 +35,7 @@ import org.junit.Test;
public abstract class AbstractCheckDigitTest {
/** Check digit routine being tested */
- protected int checkDigitLth = 1;
+ private int checkDigitLth;
/** Check digit routine being tested */
private ConstraintValidator<? extends Annotation, String> routine;
@@ -47,11 +47,14 @@ public abstract class AbstractCheckDigit
private String[] invalid;
/** code value which sums to zero */
- protected String zeroSum = "0000000000";
+ private String zeroSum;
/** Prefix for error messages */
- protected String missingMessage = "Code is missing";
+ private String missingMessage;
+ public int getCheckDigitLth() {
+ return 1;
+ }
protected abstract ConstraintValidator<? extends Annotation, String> getConstraint();
@@ -61,11 +64,22 @@ public abstract class AbstractCheckDigit
return new String[] {"12345678A"};
}
+ protected String getZeroSum() {
+ return "0000000000";
+ }
+
+ public String getMissingMessage() {
+ return "Code is missing";
+ }
+
@Before
public void setUp() {
+ checkDigitLth = getCheckDigitLth();
routine = getConstraint();
valid = getValid();
invalid = getInvalid();
+ zeroSum = getZeroSum();
+ missingMessage = getMissingMessage();
}
/**
Added: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/IBANCheckDigitTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/IBANCheckDigitTest.java?rev=1228593&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/IBANCheckDigitTest.java (added)
+++ incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/IBANCheckDigitTest.java Sat Jan 7 10:47:09 2012
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.bval.extras.constraints.checkdigit;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.validation.ConstraintValidator;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * IVAN Check Digit Test.
+ */
+public class IBANCheckDigitTest extends AbstractCheckDigitTest {
+
+ @Override
+ public int getCheckDigitLth() {
+ return 2;
+ }
+
+ @Override
+ protected ConstraintValidator<? extends Annotation, String> getConstraint() {
+ return new IBANValidator();
+ }
+
+ @Override
+ protected String[] getValid() {
+ return new String[] {
+ "AD1200012030200359100100",
+ "AT611904300234573201",
+ "AT611904300234573201",
+ "BE68539007547034",
+ "BE62510007547061",
+ "CY17002001280000001200527600",
+ "CZ6508000000192000145399",
+ "DK5000400440116243",
+ "EE382200221020145685",
+ "FI2112345600000785",
+ "FR1420041010050500013M02606",
+ "DE89370400440532013000",
+ "GI75NWBK000000007099453",
+ "GR1601101250000000012300695",
+ "HU42117730161111101800000000",
+ "IS140159260076545510730339",
+ "IE29AIBK93115212345678",
+ "IT60X0542811101000000123456",
+ "LV80BANK0000435195001",
+ "LT121000011101001000",
+ "LU280019400644750000",
+ "NL91ABNA0417164300",
+ "NO9386011117947",
+ "PL27114020040000300201355387",
+ "PT50000201231234567890154",
+ "SK3112000000198742637541",
+ "SI56191000000123438",
+ "ES8023100001180000012345",
+ "SE3550000000054910000003",
+ "CH3900700115201849173",
+ "GB29NWBK60161331926819"
+ };
+ }
+
+ @Override
+ protected String[] getInvalid() {
+ return new String[] {"510007+47061BE63"};
+ }
+
+ @Override
+ protected String getZeroSum() {
+ return null;
+ }
+
+ @Override
+ public String getMissingMessage() {
+ return "Invalid Code length=0";
+ }
+
+ /**
+ * Test zero sum
+ */
+ @Override
+ @Test
+ @Ignore
+ public void testZeroSum() {
+ // ignore, don't run this test
+ }
+
+ @Override
+ protected String[] createInvalidCodes( String[] codes ) {
+ List<String> list = new ArrayList<String>();
+
+ // create invalid check digit values
+ for (int i = 0; i < codes.length; i++) {
+ String code = removeCheckDigit(codes[i]);
+ String check = checkDigit(codes[i]);
+ for (int j = 0; j < 96; j++) {
+ String curr = j > 9 ? "" + j : "0" + j;
+ if (!curr.equals(check)) {
+ list.add(code.substring(0, 2) + curr + code.substring(4));
+ }
+ }
+ }
+
+ return list.toArray(new String[list.size()]);
+ }
+
+
+
+ /**
+ * Returns a code with the Check Digit (i.e. last character) removed.
+ *
+ * @param code The code
+ * @return The code without the check digit
+ */
+ protected String removeCheckDigit(String code) {
+ return code.substring(0, 2) + "00" + code.substring(4);
+ }
+
+ /**
+ * Returns the check digit (i.e. last character) for a code.
+ *
+ * @param code The code
+ * @return The check digit
+ */
+ protected String checkDigit(String code) {
+ if (code == null || code.length() <= getCheckDigitLth()) {
+ return "";
+ }
+ return code.substring(2, 4);
+ }
+
+}
Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/IBANCheckDigitTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/IBANCheckDigitTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/IBANCheckDigitTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain