You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2006/12/08 03:24:08 UTC

svn commit: r483781 - in /jakarta/commons/proper/validator/trunk/src: share/org/apache/commons/validator/routines/checkdigit/ test/org/apache/commons/validator/routines/checkdigit/

Author: niallp
Date: Thu Dec  7 18:24:05 2006
New Revision: 483781

URL: http://svn.apache.org/viewvc?view=rev&rev=483781
Log:
VALIDATOR-213 - Factor out Check Digit logic into separate implementations

Added:
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java   (with props)
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigitException.java   (with props)
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java   (with props)
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java   (with props)
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java   (with props)
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java   (with props)
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java   (with props)
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/package.html   (with props)
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java   (with props)
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java   (with props)
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java   (with props)
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java   (with props)
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java   (with props)
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigitTest.java   (with props)

Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java (added)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,77 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+import org.apache.commons.validator.ISBNValidator;
+
+/**
+ * <b>Check Digit</b> calculation and validation.
+ * <p>
+ * The logic for validating check digits has previously been
+ * embedded within the logic for specific code validation, which
+ * includes other validations such as verifying the format
+ * or length of a code. {@link CheckDigit} provides for separating out
+ * the check digit calculation logic enabling it to be more easily
+ * tested and reused.
+ * <p>
+ * Although Commons Validator is primarily concerned with validation,
+ * {@link CheckDigit} also defines behaviour for calculating/generating check
+ * digits, since it makes sense that users will want to (re-)use the
+ * same logic for both. The {@link ISBNValidator} makes specific use
+ * of this feature by providing the facility to validate ISBN-10 codes
+ * and then convert them to the new ISBN-13 standard.
+ * <p>
+ * <h3>Implementations</h3>
+ * The following check digit implementations are provided as standard:
+ * <ul>
+ *   <li>{@link ModulusCheckDigit} - an abstract class which provides the logic
+ *       for <i>modulus</i> check digit calculation/validation.</li>
+ *   <li>{@link EAN13CheckDigit} - check digit calculation/validation for
+ *       numeric EAN codes (based on the standard EAN-13).</li>
+ *   <li>{@link ISBN10CheckDigit} - check digit calculation/validation for
+ *       numeric ISBN-10 codes (the new ISBN-13 code is actually an EAN-13
+ *       code and uses the same check digit calculation).</li>
+ *   <li>{@link ISBNCheckDigit} - check digit calculation/validation for
+ *       both ISBN-10 and ISBN-13 codes.</li>
+ *   <li>{@link LuhnCheckDigit} - Luhn check digit calculation/validation
+ *       commonly used by credit card numbers.</li>
+ * </ul>
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public interface CheckDigit {
+
+    /**
+     * Calculate the <i>Check Digit</i> for a code.
+     *
+     * @param code The code to calculate the Check Digit for.
+     * @return The calculated Check Digit
+     * @throws CheckDigitException if an error occurs.
+     */
+    public char calculate(String code) throws CheckDigitException;
+
+    /**
+     * Validate the check digit for the code.
+     *
+     * @param code The code to validate.
+     * @return <code>true</code> if the check digit is valid, otherwise
+     * <code>false</code>.
+     */
+    public boolean isValid(String code);
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigitException.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigitException.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigitException.java (added)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigitException.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,52 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+/**
+ * Check Digit calculation/validation error.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public class CheckDigitException extends Exception {
+
+    /**
+     * Construct an Exception with no message.
+     */
+    public CheckDigitException() {
+    }
+
+    /**
+     * Construct an Exception with a message.
+     *
+     * @param msg The error message.
+     */
+    public CheckDigitException(String msg) {
+        super(msg);
+    }
+
+    /**
+     * Construct an Exception with a message and
+     * the underlying cause.
+     *
+     * @param msg The error message.
+     */
+    public CheckDigitException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigitException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigitException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java (added)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,84 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+import java.io.Serializable;
+
+/**
+ * Modulus 10 <b>EAN-13</b>/<b>UPC</b>/<b>ISBN-13</b> Check Digit
+ * calculation/validation.
+ * <p>
+ * The calculation based on the following criteria:
+ *
+ * <ul>
+ *   <li>Modulus 10.</li>
+ *   <li>Odd digits weighted by one (right to left)</li>
+ *   <li>Even digits weighted by three (right to left)</li>
+ * </ul>
+ * <p>
+ * For further information see:
+ * <ul>
+ *   <li>EAN-13 - see 
+ *       <a href="http://en.wikipedia.org/wiki/European_Article_Number">Wikipedia - 
+ *       European Article Number</a>.</li>
+ *   <li>UPC - see
+ *       <a href="http://en.wikipedia.org/wiki/Universal_Product_Code">Wikipedia -
+ *       Universal Product Code</a>.</li>
+ *   <li>ISBN-13 - see 
+ *       <a href="http://en.wikipedia.org/wiki/ISBN">Wikipedia - International
+ *       Standard Book Number (ISBN)</a>.</li>
+ * </ul>
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class EAN13CheckDigit extends ModulusCheckDigit implements Serializable {
+
+    /** Static EAN-13 Check Digit instance */
+    public static final CheckDigit INSTANCE = new EAN13CheckDigit();
+
+    /** weighting given to the 'odd' digits in EAN-13 check digit calculation */
+    private static final int ODD_WEIGHT  = 1;
+
+    /** weighting given to the 'even' digits in EAN-13 check digit calculation */
+    private static final int EVEN_WEIGHT = 3;
+
+    /**
+     * Construct a modulus 10 Check Digit routine for EAN/UPC.
+     */
+    public EAN13CheckDigit() {
+        super(10);
+    }
+
+    /**
+     * <p>Calculates the <i>weighted</i> value of a character in the
+     * code at a specified position.</p>
+     *
+     * <p>For EAN-13 (from right to left) <b>odd</b> digits are weighted
+     * with a factor of <b>one</b> and <b>even</b> digits with a factor
+     * of <b>three</b>.</p>
+     *
+     * @param charValue The numeric value of the character.
+     * @param position The position of a character in the code.
+     * @return The weighted value of the character.
+     */
+    protected int weightedValue(int charValue, int position) {
+        boolean oddPosition = (position % 2 == 1);
+        int weight = (oddPosition  ? ODD_WEIGHT : EVEN_WEIGHT);
+        return (charValue * weight);
+    }
+}

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java (added)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,115 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+import java.io.Serializable;
+
+/**
+ * Modulus 11 <b>ISBN-10</b> Check Digit calculation/validation.
+ * <p>
+ * Calculation of the <b>ISBN-10</b> Check Digit is based on the
+ * following criteria:
+ * <p>
+ * <ul>
+ *   <li>Modulus 11.</li>
+ *   <li>Digits weight by their position (right to left)</li>
+ *   <li>If checkdigit value is 10 --> Character 'X'</li>
+ * </ul>
+ *
+ * <p>
+ * <b>N.B.</b> From 1st January 2007 the book industry will start to use a new 13 digit
+ * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13
+ * (see {@link EAN13CheckDigit}) standard. For more information see:
+ * <p>
+ * For further information see:
+ * <ul>
+ *   <li><a href="http://en.wikipedia.org/wiki/ISBN">Wikipedia - International
+ *       Standard Book Number (ISBN)</a>.</li>
+ *   <li><a href="http://www.isbn.org/standards/home/isbn/transition.asp">ISBN-13
+ *       Transition details</a>.</li>
+ * </ul>
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class ISBN10CheckDigit extends ModulusCheckDigit implements Serializable {
+
+    /** Static ISBN-10 check digit instance */
+    public static final CheckDigit INSTANCE = new ISBN10CheckDigit();
+
+    /**
+     * Construct a modulus 11 Check Digit routine for ISBN-10.
+     */
+    public ISBN10CheckDigit() {
+        super(11);
+    }
+
+    /**
+     * Calculates the <i>weighted</i> value of a charcter in the
+     * code at a specified position.
+     *
+     * <p>For ISBN-10 (from right to left) digits are weighted
+     * by their position.</p>
+     *
+     * @param charValue The numeric value of the character.
+     * @param position The position of a character in the code.
+     * @return The weighted value of the character.
+     */
+    protected int weightedValue(int charValue, int position) {
+        return (charValue * position);
+    }
+
+    /**
+     * <p>Convert a character at a specified position to an
+     * integer value.</p>
+     *
+     * <p>Character 'X' check digit converted to 10.</p>
+     *
+     * @param character The character to convert.
+     * @param position The position of a character in the code.
+     * @return The integer value of the character.
+     * @throws CheckDigitException if an error occurs.
+     */
+    protected int toInt(char character, int position)
+            throws CheckDigitException {
+        if (position == 1 && character == 'X') {
+            return 10;
+        } else {
+            return super.toInt(character, position);
+        }
+    }
+
+    /**
+     * <p>Convert an integer value to a character at a specified position.</p>
+     *
+     * <p>Value '10' for position 1 (check digit) converted to 'X'.</p>
+     *
+     * @param charValue The integer value of the character.
+     * @param position The position of a character in the code.
+     * @return The converted character.
+     * @throws CheckDigitException if an error occurs.
+     */
+    protected char toChar(int charValue, int position)
+            throws CheckDigitException {
+        if (position == 1 && charValue == 10) {
+            return 'X';
+        } else {
+            return super.toChar(charValue, position);
+        }
+    }
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java (added)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,98 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+import java.io.Serializable;
+
+/**
+ * Combined <b>ISBN-10</b>/<b>ISBN-13</b> Check Digit calculation/validation.
+ * <p>
+ * This implementation validates/calculates ISBN check digits
+ * based on the length of the code passed to it - delegating
+ * either to the {@link CheckDigit#ISBN10} or the
+ * {@link CheckDigit#ISBN13} routines to perform the actual
+ * validation/calculation.
+ * 
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class ISBNCheckDigit implements CheckDigit, Serializable {
+
+    /** Static ISBN-10 check digit instance */
+    public static final CheckDigit ISBN10 = ISBN10CheckDigit.INSTANCE;
+
+    /** Static ISBN-13 Check Digit instance */
+    public static final CheckDigit ISBN13 = EAN13CheckDigit.INSTANCE;
+
+    /** Combined static ISBN-10/ISBN-13 check digit instance */
+    public static final CheckDigit ISBN   = new ISBNCheckDigit();
+
+    /**
+     * Calculate an ISBN-10 or ISBN-13 check digit, depending
+     * on the length of the code.
+     * <p>
+     * If the length of the code is 9, it is treated as an ISBN-10
+     * code or if the length of the code is 12, it is treated as an ISBN-13
+     * code.
+     *
+     * @param code The ISBN code to validate (should have a length of
+     * 9 or 12)
+     * @return The ISBN-10 check digit if the length is 9 or an ISBN-13
+     * check digit if the length is 12.
+     * @throws CheckDigitException if the code is missing, or an invalid
+     * length (i.e. not 9 or 12) or if there is an error calculating the
+     * check digit.
+     */
+    public char calculate(String code) throws CheckDigitException {
+        if (code == null || code.length() == 0) {
+            throw new CheckDigitException("ISBN Code is missing");
+        } else if (code.length() == 9) {
+            return ISBN10.calculate(code);
+        } else if (code.length() == 12) {
+            return ISBN13.calculate(code);
+        } else {
+            throw new CheckDigitException("Invalid ISBN Length = " + code.length());
+        }
+    }
+
+    /**
+     * <p>Validate an ISBN-10 or ISBN-13 check digit, depending
+     * on the length of the code.</p>
+     * <p>
+     * If the length of the code is 10, it is treated as an ISBN-10
+     * code or ff the length of the code is 13, it is treated as an ISBN-13
+     * code.
+     *
+     * @param code The ISBN code to validate (should have a length of
+     * 10 or 13)
+     * @return <code>true</code> if the code has a length of 10 and is
+     * a valid ISBN-10 check digit or the code has a length of 13 and is
+     * a valid ISBN-13 check digit - otherwise <code>false</code>.
+     */
+    public boolean isValid(String code) {
+        if (code == null) {
+            return false;
+        } else if (code.length() == 10) {
+            return ISBN10.isValid(code);
+        } else if (code.length() == 13) {
+            return ISBN13.isValid(code);
+        } else {
+            return false;
+        }
+    }
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java (added)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,79 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+import java.io.Serializable;
+
+
+/**
+ * Modulus 10 <b>Luhn</b> Check Digit calculation/validation.
+ * <p>
+ * Luhn check digits are used, for example, by credit card numbers. See
+ * <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Wikipedia - Luhn
+ * algorithm</a> for details.
+ * <p>
+ * Calculation based on the following criteria:
+ * <p>
+ *
+ * <ul>
+ *   <li>Modulus 10.</li>
+ *   <li>Odd digits weighted by one (right to left)</li>
+ *   <li>Even digits weighted by two (right to left)</li>
+ *   <li>Weighted values > 9 have 9 subtracted.</li>
+ * </ul>
+ *
+ * <p>
+ * See <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Wikipedia</a>
+ * for more details.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class LuhnCheckDigit extends ModulusCheckDigit implements Serializable {
+
+    /** Static Luhn Check Digit instance */
+    public static final CheckDigit INSTANCE = new LuhnCheckDigit();
+
+    private static final int ODD_WEIGHT  = 1;
+    private static final int EVEN_WEIGHT = 2;
+
+    /**
+     * Construct a modulus 10 Luhn Check Digit routine.
+     */
+    public LuhnCheckDigit() {
+        super(10);
+    }
+
+    /**
+     * <p>Calculates the <i>weighted</i> value of a charcter in the
+     * code at a specified position.</p>
+     *
+     * <p>For Luhn (from right to left) <b>odd</b> digits are weighted
+     * with a factor of <b>one</b> and <b>even</b> digits with a factor
+     * of <b>two</b>. Weighted values > 9, have 9 subtracted</p>
+     *
+     * @param charValue The numeric value of the character.
+     * @param position The position of a character in the code.
+     * @return The weighted value of the character.
+     */
+    protected int weightedValue(int charValue, int position) {
+        boolean oddPosition = (position % 2 == 1);
+        int weight = (oddPosition  ? ODD_WEIGHT : EVEN_WEIGHT);
+        int weightedValue = (charValue * weight);
+        return (weightedValue > 9 ? (weightedValue - 9) : weightedValue);
+    }
+}

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java (added)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,171 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+/**
+ * Abstract <b>Modulus</b> Check digit calculation/validation.
+ * <p>
+ * Provides a <i>base</i> class for building <i>modulus</i> Check
+ * Digit routines.
+ * <p>
+ * This implementation only handles <i>numeric</i> codes, such as
+ * <b>EAN-13</b>. For <i>alphanumeric</i> codes such as <b>EAN-128</b> you
+ * will need to implement/override the <code>toInt()</code> and
+ * <code>toChar()</code> methods.
+ * <p>
+ *    
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public abstract class ModulusCheckDigit implements CheckDigit {
+
+    private int modulus;
+
+    /**
+     * Construct a {@link CheckDigit} routine for a specified modulus.
+     *
+     * @param modulus The modulus value to use for the check digit calculation
+     */
+    public ModulusCheckDigit(int modulus) {
+        this.modulus = modulus;
+    }
+
+    /**
+     * Validate a modulus check digit for the code.
+     *
+     * @param code The code to validate
+     * @return <code>true</code> if the check digit is valid, otherwise
+     * <code>false</code>
+     */
+    public boolean isValid(String code) {
+        if (code == null || code.length() == 0) {
+            return false;
+        }
+        try {       
+            int remainder = mod(code, true);
+            return (remainder == 0);
+        } catch (CheckDigitException  ex) {
+            return false;
+        }
+    }
+
+    /**
+     * Calculate a modulus <i>Check Digit</i> for a code.
+     *
+     * @param code The code to calculate the Check Digit for
+     * @return The calculated Check Digit
+     * @throws CheckDigitException if an error occurs calculating
+     * the check digit for the specified code
+     */
+    public char calculate(String code) throws CheckDigitException {
+        if (code == null || code.length() == 0) {
+            throw new CheckDigitException("Code is missing");
+        }
+        int remainder = mod(code, false);
+        int charValue = (remainder == 0) ? 0 : (modulus - remainder);
+        return toChar(charValue, 1);
+    }
+
+    /**
+     * Calculate the modulus for a code.
+     *
+     * @param code The code to calculate the modulus for.
+     * @param includesCheckDigit Whether the code includes the Check Digit or not.
+     * @return The modulus value
+     * @throws CheckDigitException if an error occurs calculating the modulus
+     * for the specified code
+     */
+    private int mod(String code, boolean includesCheckDigit) throws CheckDigitException {
+        int total = 0;
+        for (int i = 0; i < code.length(); i++) {
+            int position  = i + (includesCheckDigit ? 1 : 2);
+            int idx = code.length() - (i + 1);
+            int charValue = toInt(code.charAt(idx), position);
+            total += weightedValue(charValue, position);
+        }
+        if (total == 0) {
+            throw new CheckDigitException("Invalid code, sum is zero");
+        }
+        return (total % modulus);
+    }
+
+    /**
+     * Calculates the <i>weighted</i> value of a character in the
+     * code at a specified position.
+     * <p>
+     * Some modulus routines weight the value of a character
+     * depending on its position in the code (e.g. ISBN-10), while
+     * others use different weighting factors for odd/even positions
+     * (e.g. EAN or Luhn). Implement the appropriate mechanism
+     * required by overriding this method.
+     *
+     * @param charValue The numeric value of the character
+     * @param position The position of a character in the code
+     * @return The weighted value of the character
+     * @throws CheckDigitException if an error occurs calculating
+     * the weighted value
+     */
+    protected abstract int weightedValue(int charValue, int position)
+            throws CheckDigitException;
+
+
+    /**
+     * Convert a character at a specified position to an integer value.
+     * <p>
+     * <b>Note:</b> this implementation only handlers numeric values
+     * For non-numeric characters, override this method to provide
+     * character-->integer conversion.
+     *
+     * @param character The character to convert
+     * @param position The position of a character in the code
+     * @return The integer value of the character
+     * @throws CheckDigitException if character is non-numeric
+     */
+    protected int toInt(char character, int position)
+            throws CheckDigitException {
+        if (Character.isDigit(character)) {
+            return Character.getNumericValue(character);
+        } else {
+            throw new CheckDigitException("Invalid Character[" + 
+                    position + "] = '" + character + "'");
+        }
+    }
+
+    /**
+     * Convert an integer value to a character at a specified position.
+     * <p>
+     * <b>Note:</b> this implementation only handles numeric values
+     * For non-numeric characters, override this method to provide
+     * integer-->character conversion.
+     *
+     * @param charValue The integer value of the character
+     * @param position The position of a character in the code
+     * @return The converted character
+     * @throws CheckDigitException if integer character value
+     * doesn't represent a numeric character
+     */
+    protected char toChar(int charValue, int position)
+            throws CheckDigitException {
+        if (charValue >= 0 && charValue <= 9) {
+            return Character.forDigit(charValue, 10);
+        } else {
+            throw new CheckDigitException("Invalid Value[" + 
+                    position + "] = " + charValue);
+        }
+    }
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/package.html
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/package.html?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/package.html (added)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/package.html Thu Dec  7 18:24:05 2006
@@ -0,0 +1,26 @@
+<!--
+ 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.
+-->
+<html>
+<head>
+<title>Package Documentation for org.apache.commons.validator.routines.checkdigit Package</title>
+</head>
+<body bgcolor="white">
+
+    <p>This package contains <i>Check Digit</i> validation/calculation routines.</p>
+
+</body>
+</html>

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/package.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/package.html
------------------------------------------------------------------------------
    svn:mime-type = text/html

Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,198 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+import java.util.List;
+import java.util.ArrayList;
+
+
+import junit.framework.TestCase;
+
+/**
+ * Luhn Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class CheckDigitTestBase extends TestCase {
+
+    /** Check digit routine being tested */
+    protected CheckDigit routine;
+
+    /** Array of valid code values */
+    protected String[] valid;
+
+    /** Array of invalid code values */
+    protected String[] invalid = new String[] {"12345678A"};
+
+    /** code value which sums to zero */
+    protected String zeroSum = "0000000000";
+
+    /** Prefix for error messages */
+    protected String msgPrefix = "";
+
+    /**
+     * Constructor
+     * @param name test name
+     */
+    public CheckDigitTestBase(String name) {
+        super(name);
+    }
+
+    /**
+     * Tear Down - clears routine and valid codes.
+     */
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        valid = null;
+        routine = null;
+    }
+
+    /**
+     * Test method for {@link CheckDigit#isValid(java.lang.String)}.
+     */
+    public void testIsValid() {
+
+        // test valid values
+        for (int i = 0; i < valid.length; i++) {
+            assertTrue("valid[" + i +"]: " + valid[i], routine.isValid(valid[i]));
+        }
+
+        // test invalid code values
+        for (int i = 0; i < invalid.length; i++) {
+            assertFalse("invalid[" + i +"]: " + invalid[i], routine.isValid(invalid[i]));
+        }
+
+        // test invalid check digit values
+        String[] invalidCheckDigits = createInvalidCodes(valid);
+        for (int i = 0; i < invalidCheckDigits.length; i++) {
+            assertFalse("invalid check digit[" + i +"]: " + invalidCheckDigits[i], routine.isValid(invalidCheckDigits[i]));
+        }
+
+        // test null
+        assertFalse("Test Null", routine.isValid(null));
+
+        // test zero length
+        assertFalse("Test Zero Length", routine.isValid(""));
+        
+        // test zero sum
+        assertFalse("Test Zero Sum", routine.isValid(zeroSum));
+
+    }
+
+    /**
+     * Test method for {@link CheckDigit#calculate(java.lang.String)}.
+     */
+    public void testCalculate() {
+
+        // test valid values
+        for (int i = 0; i < valid.length; i++) {
+            String code = removeCheckDigit(valid[i]);
+            char expected = checkDigit(valid[i]);
+            try {
+                assertEquals("valid[" + i +"]: " + valid[i], expected, routine.calculate(code));
+            } catch (Exception e) {
+                fail("valid[" + i +"] threw " + e);
+            }
+        }
+
+        // test null
+        try {
+            routine.calculate(null);
+            fail("Null - expected exception");
+        } catch (Exception e) {
+            assertEquals("Null Test", msgPrefix +"Code is missing", e.getMessage());
+        }
+
+        // test zero length
+        try {
+            routine.calculate("");
+            fail("Zero Length - expected exception");
+        } catch (Exception e) {
+            assertEquals("Zero Length",  msgPrefix +"Code is missing", e.getMessage());
+        }
+
+        // test invalid code values
+        for (int i = 0; i < invalid.length; i++) {
+            try {
+                routine.calculate(invalid[i]);
+                fail("Invalid Characters[" + i + "] - expected exception");
+            } catch (Exception e) {
+                assertTrue("Invalid Character[" +i +"]", e.getMessage().startsWith("Invalid Character["));
+            }
+        }
+
+        // test zero sum
+        try {
+            routine.calculate(zeroSum);
+            fail("Zero Sum - expected exception");
+        } catch (Exception e) {
+            assertEquals("Zero Length",  "Invalid code, sum is zero", e.getMessage());
+        }
+    }
+
+    /**
+     * Returns an array of codes with invalid check digits.
+     *
+     * @param codes Codes with valid check digits
+     * @return Codes with invalid check digits
+     */
+    protected String[] createInvalidCodes(String[] codes) {
+        List list = new ArrayList();
+
+        // create invalid check digit values
+        for (int i = 0; i < codes.length; i++) {
+            String code = removeCheckDigit(codes[i]);
+            char check  = checkDigit(codes[i]);
+            for (int j = 0; j < 10; j++) {
+                char curr =  Character.forDigit(j, 10);
+                if (curr != check) {
+                    list.add(code + curr);
+                }
+            }
+        }
+        
+        return (String[])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) {
+        if (code == null || code.length() <= 1) {
+            return null;
+        }
+        return code.substring(0, code.length() -1);
+    }
+
+    /**
+     * Returns the check digit (i.e. last character) for a code.
+     *
+     * @param code The code
+     * @return The check digit
+     */
+    protected char checkDigit(String code) {
+        if (code == null || code.length() <= 1) {
+            return '?';
+        }
+        return code.charAt(code.length() -1);
+    }
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,63 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Test suite for <code>org.apache.commons.validator.routines.checkdigit</code>
+ * package.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class CheckDigitTestSuite extends TestCase {
+
+    /** 
+     * Construct an instance with the specified name
+     * @param name name of the test
+     */
+    public CheckDigitTestSuite(String name) {
+        super(name);
+    }
+
+    /** 
+     * Create a Test Suite
+     * @return the test suite.
+     */
+    public static Test suite() {
+       TestSuite suite = new TestSuite();
+
+       suite.addTestSuite(EAN13CheckDigitTest.class);
+       suite.addTestSuite(ISBN10CheckDigitTest.class);
+       suite.addTestSuite(ISBNCheckDigitTest.class);
+       suite.addTestSuite(LuhnCheckDigitTest.class);
+
+       return suite;
+    }
+
+    /** 
+     * Static main.
+     * @param args arguments
+     */
+    public static void main(String args[]) {
+        junit.textui.TestRunner.run(suite());
+    }
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,45 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+
+/**
+ * EAN-13 Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class EAN13CheckDigitTest extends CheckDigitTestBase {
+
+    /**
+     * Constructor
+     * @param name test name
+     */
+    public EAN13CheckDigitTest(String name) {
+        super(name);
+    }
+
+    /**
+     * Set up routine & valid codes.
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+        routine = EAN13CheckDigit.INSTANCE;
+        valid = new String[] {"9780072129519", "9780764558313"};
+    }
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,45 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+
+/**
+ * ISBN-10 Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class ISBN10CheckDigitTest extends CheckDigitTestBase {
+
+    /**
+     * Constructor
+     * @param name test name
+     */
+    public ISBN10CheckDigitTest(String name) {
+        super(name);
+    }
+
+    /**
+     * Set up routine & valid codes.
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+        routine = ISBN10CheckDigit.INSTANCE;
+        valid = new String[] {"1930110995", "020163385X"};
+    }
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,86 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+
+/**
+ * ISBN-10/ISBN-13 Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class ISBNCheckDigitTest extends CheckDigitTestBase {
+    
+    /**
+     * Constructor
+     * @param name test name
+     */
+    public ISBNCheckDigitTest(String name) {
+        super(name);
+    }
+
+    /**
+     * Set up routine & valid codes.
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+        routine = ISBNCheckDigit.ISBN;
+        valid = new String[] {"9780072129519", "9780764558313", "1930110995", "020163385X"};
+        msgPrefix = "ISBN ";
+        zeroSum = "000000000000";
+    }
+
+    /**
+     * Set up routine & valid codes.
+     */
+    public void testInvalidLength() {
+        assertFalse("isValid() Lth 9 ", routine.isValid("123456789"));
+        assertFalse("isValid() Lth 11", routine.isValid("12345678901"));
+        assertFalse("isValid() Lth 12", routine.isValid("123456789012"));
+        assertFalse("isValid() Lth 14", routine.isValid("12345678901234"));
+
+        try {
+            routine.calculate("12345678");
+            fail("calculate() Lth 8 - expected exception");
+        } catch (Exception e) {
+            assertEquals("calculate() Lth 8", "Invalid ISBN Length = 8", e.getMessage());
+        }
+
+        try {
+            routine.calculate("1234567890");
+            fail("calculate() Lth 10 - expected exception");
+        } catch (Exception e) {
+            assertEquals("calculate() Lth 10", "Invalid ISBN Length = 10", e.getMessage());
+        }
+
+        try {
+            routine.calculate("12345678901");
+            fail("calculate() Lth 11 - expected exception");
+        } catch (Exception e) {
+            assertEquals("calculate() Lth 11", "Invalid ISBN Length = 11", e.getMessage());
+        }
+
+        try {
+            routine.calculate("1234567890123");
+            fail("calculate() Lth 13 - expected exception");
+        } catch (Exception e) {
+            assertEquals("calculate() Lth 13", "Invalid ISBN Length = 13", e.getMessage());
+        }
+    }
+    
+
+}

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigitTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigitTest.java?view=auto&rev=483781
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigitTest.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigitTest.java Thu Dec  7 18:24:05 2006
@@ -0,0 +1,59 @@
+/*
+ * 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.commons.validator.routines.checkdigit;
+
+
+/**
+ * Luhn Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class LuhnCheckDigitTest extends CheckDigitTestBase {
+
+    private static final String VALID_VISA       = "4417123456789113";
+    private static final String VALID_SHORT_VISA = "4222222222222";
+    private static final String VALID_AMEX       = "378282246310005";
+    private static final String VALID_MASTERCARD = "5105105105105100";
+    private static final String VALID_DISCOVER   = "6011000990139424";
+    private static final String VALID_DINERS     = "30569309025904";
+
+    /**
+     * Constructor
+     * @param name test name
+     */
+    public LuhnCheckDigitTest(String name) {
+        super(name);
+    }
+
+    /**
+     * Set up routine & valid codes.
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        routine = LuhnCheckDigit.INSTANCE;
+
+        valid = new String[] {
+                VALID_VISA,
+                VALID_SHORT_VISA,
+                VALID_AMEX,
+                VALID_MASTERCARD,
+                VALID_DISCOVER,
+                VALID_DINERS};
+    }
+}

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigitTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigitTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org