View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator.routines;
18  
19  import org.apache.commons.validator.routines.checkdigit.CheckDigit;
20  import org.apache.commons.validator.routines.checkdigit.EAN13CheckDigit;
21  
22  import junit.framework.TestCase;
23  
24  /**
25   * CodeValidatorTest.java.
26   *    
27   * @version $Revision: 1094799 $ $Date: 2011-04-19 00:40:24 +0200 (Di, 19 Apr 2011) $
28   * @since Validator 1.4
29   */
30  public class CodeValidatorTest extends TestCase {
31  
32      /**
33       * Construct a test with the specified name.
34       * @param name The name of the test
35       */
36      public CodeValidatorTest(String name) {
37          super(name);
38      }
39  
40      /**
41       * @see junit.framework.TestCase#setUp()
42       */
43      protected void setUp() throws Exception {
44          super.setUp();
45      }
46  
47      /**
48       * @see junit.framework.TestCase#tearDown()
49       */
50      protected void tearDown() throws Exception {
51          super.tearDown();
52      }
53  
54      /**
55       * Test Check Digit.
56       */
57      public void testCheckDigit() {
58          CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
59          String invalidEAN = "9781930110992";
60          String validEAN   = "9781930110991";
61  
62          // Test no CheckDigit (i.e. null)
63          assertNull("No CheckDigit", validator.getCheckDigit());
64          assertEquals("No CheckDigit invalid", invalidEAN, validator.validate(invalidEAN));
65          assertEquals("No CheckDigit valid",     validEAN, validator.validate(validEAN));
66          assertEquals("No CheckDigit (is) invalid",  true, validator.isValid(invalidEAN));
67          assertEquals("No CheckDigit (is) valid",    true, validator.isValid(validEAN));
68  
69          // Use the EAN-13 check digit routine
70          validator = new CodeValidator((String)null, -1, EAN13CheckDigit.EAN13_CHECK_DIGIT);
71  
72          assertNotNull("EAN CheckDigit", validator.getCheckDigit());
73          assertEquals("EAN CheckDigit invalid",       null, validator.validate(invalidEAN));
74          assertEquals("EAN CheckDigit valid",     validEAN, validator.validate(validEAN));
75          assertEquals("EAN CheckDigit (is) invalid", false, validator.isValid(invalidEAN));
76          assertEquals("EAN CheckDigit (is) valid",    true, validator.isValid(validEAN));
77          assertEquals("EAN CheckDigit ex",            null, validator.validate("978193011099X"));
78      }
79  
80      /**
81       * Test the minimum/maximum length
82       */
83      public void testLength() {
84          CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
85          String length_10  = "1234567890";
86          String length_11  = "12345678901";
87          String length_12  = "123456789012";
88          String length_20  = "12345678901234567890";
89          String length_21  = "123456789012345678901";
90          String length_22  = "1234567890123456789012";
91  
92          assertEquals("No min", -1, validator.getMinLength());
93          assertEquals("No max", -1, validator.getMaxLength());
94  
95          assertEquals("No Length 10", length_10, validator.validate(length_10));
96          assertEquals("No Length 11", length_11, validator.validate(length_11));
97          assertEquals("No Length 12", length_12, validator.validate(length_12));
98          assertEquals("No Length 20", length_20, validator.validate(length_20));
99          assertEquals("No Length 21", length_21, validator.validate(length_21));
100         assertEquals("No Length 22", length_22, validator.validate(length_22));
101         
102         validator = new CodeValidator((String)null, 11, -1, (CheckDigit)null);
103         assertEquals("Min 11 - min", 11, validator.getMinLength());
104         assertEquals("Min 11 - max", -1, validator.getMaxLength());
105         assertEquals("Min 11 - 10", null,      validator.validate(length_10));
106         assertEquals("Min 11 - 11", length_11, validator.validate(length_11));
107         assertEquals("Min 11 - 12", length_12, validator.validate(length_12));
108         assertEquals("Min 11 - 20", length_20, validator.validate(length_20));
109         assertEquals("Min 11 - 21", length_21, validator.validate(length_21));
110         assertEquals("Min 11 - 22", length_22, validator.validate(length_22));
111         
112         validator = new CodeValidator((String)null, -1, 21, (CheckDigit)null);
113         assertEquals("Max 21 - min", -1, validator.getMinLength());
114         assertEquals("Max 21 - max", 21, validator.getMaxLength());
115         assertEquals("Max 21 - 10", length_10, validator.validate(length_10));
116         assertEquals("Max 21 - 11", length_11, validator.validate(length_11));
117         assertEquals("Max 21 - 12", length_12, validator.validate(length_12));
118         assertEquals("Max 21 - 20", length_20, validator.validate(length_20));
119         assertEquals("Max 21 - 21", length_21, validator.validate(length_21));
120         assertEquals("Max 21 - 22", null,      validator.validate(length_22));
121         
122         validator = new CodeValidator((String)null, 11, 21, (CheckDigit)null);
123         assertEquals("Min 11 / Max 21 - min", 11, validator.getMinLength());
124         assertEquals("Min 11 / Max 21 - max", 21, validator.getMaxLength());
125         assertEquals("Min 11 / Max 21 - 10", null,      validator.validate(length_10));
126         assertEquals("Min 11 / Max 21 - 11", length_11, validator.validate(length_11));
127         assertEquals("Min 11 / Max 21 - 12", length_12, validator.validate(length_12));
128         assertEquals("Min 11 / Max 21 - 20", length_20, validator.validate(length_20));
129         assertEquals("Min 11 / Max 21 - 21", length_21, validator.validate(length_21));
130         assertEquals("Min 11 / Max 21 - 22", null,      validator.validate(length_22));
131 
132         validator = new CodeValidator((String)null, 11, 11, (CheckDigit)null);
133         assertEquals("Exact 11 - min", 11, validator.getMinLength());
134         assertEquals("Exact 11 - max", 11, validator.getMaxLength());
135         assertEquals("Exact 11 - 10", null,      validator.validate(length_10));
136         assertEquals("Exact 11 - 11", length_11, validator.validate(length_11));
137         assertEquals("Exact 11 - 12", null,      validator.validate(length_12));
138     }
139 
140     /**
141      * Test Regular Expression.
142      */
143     public void testRegex() {
144         CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
145 
146         String value2  = "12";
147         String value3  = "123";
148         String value4  = "1234";
149         String value5  = "12345";
150         String invalid = "12a4";
151 
152         // No Regular Expression
153         assertNull("No Regex", validator.getRegexValidator());
154         assertEquals("No Regex 2", value2, validator.validate(value2));
155         assertEquals("No Regex 3", value3, validator.validate(value3));
156         assertEquals("No Regex 4", value4, validator.validate(value4));
157         assertEquals("No Regex 5", value5, validator.validate(value5));
158         assertEquals("No Regex invalid", invalid, validator.validate(invalid));
159 
160         // Regular Expression
161         String regex = "^([0-9]{3,4})$";
162         validator = new CodeValidator(regex, -1, -1, (CheckDigit)null);
163         assertNotNull("No Regex", validator.getRegexValidator());
164         assertEquals("Regex 2", null,   validator.validate(value2));
165         assertEquals("Regex 3", value3, validator.validate(value3));
166         assertEquals("Regex 4", value4, validator.validate(value4));
167         assertEquals("Regex 5", null,   validator.validate(value5));
168         assertEquals("Regex invalid", null, validator.validate(invalid));
169 
170         // Reformatted
171         regex = "^([0-9]{3})(?:[-\\s])([0-9]{3})$";
172         validator = new CodeValidator(new RegexValidator(regex), 6, (CheckDigit)null);
173         assertEquals("Reformat 123-456", "123456", validator.validate("123-456"));
174         assertEquals("Reformat 123 456", "123456", validator.validate("123 456"));
175         assertEquals("Reformat 123456",  null,     validator.validate("123456"));
176         assertEquals("Reformat 123.456", null,     validator.validate("123.456"));
177 
178         regex = "^(?:([0-9]{3})(?:[-\\s])([0-9]{3}))|([0-9]{6})$";
179         validator = new CodeValidator(new RegexValidator(regex), 6, (CheckDigit)null);
180         assertEquals("Reformat 2 Regex",  "RegexValidator{" + regex + "}", validator.getRegexValidator().toString());
181         assertEquals("Reformat 2 123-456", "123456", validator.validate("123-456"));
182         assertEquals("Reformat 2 123 456", "123456", validator.validate("123 456"));
183         assertEquals("Reformat 2 123456",  "123456", validator.validate("123456"));
184 
185     }
186 
187     /**
188      * Test Regular Expression.
189      */
190     public void testNoInput() {
191         CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
192         assertEquals("Null",         null, validator.validate(null));
193         assertEquals("Zero Length",  null, validator.validate(""));
194         assertEquals("Spaces",       null, validator.validate("   "));
195         assertEquals("Trimmed",      "A",  validator.validate(" A  "));
196     }
197 
198     public void testValidator294_1() {
199         CodeValidator validator = new CodeValidator((String)null, 0, -1, (CheckDigit)null);
200         assertEquals("Null",         null, validator.validate(null));
201         validator = new CodeValidator((String)null, -1, 0, (CheckDigit)null);
202         assertEquals("Null",         null, validator.validate(null));
203     }
204 
205     public void testValidator294_2() {
206         CodeValidator validator = new CodeValidator((String)null, -1, 0, (CheckDigit)null);
207         assertEquals("Null",         null, validator.validate(null));
208     }
209 
210     /**
211      * Test Regular Expression.
212      */
213     public void testConstructors() {
214         CodeValidator validator = null;
215         RegexValidator regex = new RegexValidator("^[0-9]*$");
216 
217         // Constructor 1
218         validator = new CodeValidator(regex, EAN13CheckDigit.EAN13_CHECK_DIGIT);
219         assertEquals("Constructor 1 - regex",      regex, validator.getRegexValidator());
220         assertEquals("Constructor 1 - min length", -1, validator.getMinLength());
221         assertEquals("Constructor 1 - max length", -1, validator.getMaxLength());
222         assertEquals("Constructor 1 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
223 
224         // Constructor 2
225         validator = new CodeValidator(regex, 13, EAN13CheckDigit.EAN13_CHECK_DIGIT);
226         assertEquals("Constructor 2 - regex",      regex, validator.getRegexValidator());
227         assertEquals("Constructor 2 - min length", 13, validator.getMinLength());
228         assertEquals("Constructor 2 - max length", 13, validator.getMaxLength());
229         assertEquals("Constructor 2 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
230 
231         // Constructor 3
232         validator = new CodeValidator(regex, 10, 20, EAN13CheckDigit.EAN13_CHECK_DIGIT);
233         assertEquals("Constructor 3 - regex",      regex, validator.getRegexValidator());
234         assertEquals("Constructor 3 - min length", 10, validator.getMinLength());
235         assertEquals("Constructor 3 - max length", 20, validator.getMaxLength());
236         assertEquals("Constructor 3 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
237 
238         // Constructor 4
239         validator = new CodeValidator("^[0-9]*$", EAN13CheckDigit.EAN13_CHECK_DIGIT);
240         assertEquals("Constructor 4 - regex",      "RegexValidator{^[0-9]*$}", validator.getRegexValidator().toString());
241         assertEquals("Constructor 4 - min length", -1, validator.getMinLength());
242         assertEquals("Constructor 4 - max length", -1, validator.getMaxLength());
243         assertEquals("Constructor 4 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
244 
245         // Constructor 5
246         validator = new CodeValidator("^[0-9]*$", 13, EAN13CheckDigit.EAN13_CHECK_DIGIT);
247         assertEquals("Constructor 5 - regex",      "RegexValidator{^[0-9]*$}", validator.getRegexValidator().toString());
248         assertEquals("Constructor 5 - min length", 13, validator.getMinLength());
249         assertEquals("Constructor 5 - max length", 13, validator.getMaxLength());
250         assertEquals("Constructor 5 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
251 
252         // Constructor 6
253         validator = new CodeValidator("^[0-9]*$", 10, 20, EAN13CheckDigit.EAN13_CHECK_DIGIT);
254         assertEquals("Constructor 6 - regex",      "RegexValidator{^[0-9]*$}", validator.getRegexValidator().toString());
255         assertEquals("Constructor 6 - min length", 10, validator.getMinLength());
256         assertEquals("Constructor 6 - max length", 20, validator.getMaxLength());
257         assertEquals("Constructor 6 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
258     }
259 
260 }