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: 1739356 $
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      @Override
44      protected void setUp() throws Exception {
45          super.setUp();
46      }
47  
48      /**
49       * @see junit.framework.TestCase#tearDown()
50       */
51      @Override
52      protected void tearDown() throws Exception {
53          super.tearDown();
54      }
55  
56      /**
57       * Test Check Digit.
58       */
59      public void testCheckDigit() {
60          CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
61          String invalidEAN = "9781930110992";
62          String validEAN   = "9781930110991";
63  
64          // Test no CheckDigit (i.e. null)
65          assertNull("No CheckDigit", validator.getCheckDigit());
66          assertEquals("No CheckDigit invalid", invalidEAN, validator.validate(invalidEAN));
67          assertEquals("No CheckDigit valid",     validEAN, validator.validate(validEAN));
68          assertEquals("No CheckDigit (is) invalid",  true, validator.isValid(invalidEAN));
69          assertEquals("No CheckDigit (is) valid",    true, validator.isValid(validEAN));
70  
71          // Use the EAN-13 check digit routine
72          validator = new CodeValidator((String)null, -1, EAN13CheckDigit.EAN13_CHECK_DIGIT);
73  
74          assertNotNull("EAN CheckDigit", validator.getCheckDigit());
75          assertEquals("EAN CheckDigit invalid",       null, validator.validate(invalidEAN));
76          assertEquals("EAN CheckDigit valid",     validEAN, validator.validate(validEAN));
77          assertEquals("EAN CheckDigit (is) invalid", false, validator.isValid(invalidEAN));
78          assertEquals("EAN CheckDigit (is) valid",    true, validator.isValid(validEAN));
79          assertEquals("EAN CheckDigit ex",            null, validator.validate("978193011099X"));
80      }
81  
82      /**
83       * Test the minimum/maximum length
84       */
85      public void testLength() {
86          CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
87          String length_10  = "1234567890";
88          String length_11  = "12345678901";
89          String length_12  = "123456789012";
90          String length_20  = "12345678901234567890";
91          String length_21  = "123456789012345678901";
92          String length_22  = "1234567890123456789012";
93  
94          assertEquals("No min", -1, validator.getMinLength());
95          assertEquals("No max", -1, validator.getMaxLength());
96  
97          assertEquals("No Length 10", length_10, validator.validate(length_10));
98          assertEquals("No Length 11", length_11, validator.validate(length_11));
99          assertEquals("No Length 12", length_12, validator.validate(length_12));
100         assertEquals("No Length 20", length_20, validator.validate(length_20));
101         assertEquals("No Length 21", length_21, validator.validate(length_21));
102         assertEquals("No Length 22", length_22, validator.validate(length_22));
103         
104         validator = new CodeValidator((String)null, 11, -1, (CheckDigit)null);
105         assertEquals("Min 11 - min", 11, validator.getMinLength());
106         assertEquals("Min 11 - max", -1, validator.getMaxLength());
107         assertEquals("Min 11 - 10", null,      validator.validate(length_10));
108         assertEquals("Min 11 - 11", length_11, validator.validate(length_11));
109         assertEquals("Min 11 - 12", length_12, validator.validate(length_12));
110         assertEquals("Min 11 - 20", length_20, validator.validate(length_20));
111         assertEquals("Min 11 - 21", length_21, validator.validate(length_21));
112         assertEquals("Min 11 - 22", length_22, validator.validate(length_22));
113         
114         validator = new CodeValidator((String)null, -1, 21, (CheckDigit)null);
115         assertEquals("Max 21 - min", -1, validator.getMinLength());
116         assertEquals("Max 21 - max", 21, validator.getMaxLength());
117         assertEquals("Max 21 - 10", length_10, validator.validate(length_10));
118         assertEquals("Max 21 - 11", length_11, validator.validate(length_11));
119         assertEquals("Max 21 - 12", length_12, validator.validate(length_12));
120         assertEquals("Max 21 - 20", length_20, validator.validate(length_20));
121         assertEquals("Max 21 - 21", length_21, validator.validate(length_21));
122         assertEquals("Max 21 - 22", null,      validator.validate(length_22));
123         
124         validator = new CodeValidator((String)null, 11, 21, (CheckDigit)null);
125         assertEquals("Min 11 / Max 21 - min", 11, validator.getMinLength());
126         assertEquals("Min 11 / Max 21 - max", 21, validator.getMaxLength());
127         assertEquals("Min 11 / Max 21 - 10", null,      validator.validate(length_10));
128         assertEquals("Min 11 / Max 21 - 11", length_11, validator.validate(length_11));
129         assertEquals("Min 11 / Max 21 - 12", length_12, validator.validate(length_12));
130         assertEquals("Min 11 / Max 21 - 20", length_20, validator.validate(length_20));
131         assertEquals("Min 11 / Max 21 - 21", length_21, validator.validate(length_21));
132         assertEquals("Min 11 / Max 21 - 22", null,      validator.validate(length_22));
133 
134         validator = new CodeValidator((String)null, 11, 11, (CheckDigit)null);
135         assertEquals("Exact 11 - min", 11, validator.getMinLength());
136         assertEquals("Exact 11 - max", 11, validator.getMaxLength());
137         assertEquals("Exact 11 - 10", null,      validator.validate(length_10));
138         assertEquals("Exact 11 - 11", length_11, validator.validate(length_11));
139         assertEquals("Exact 11 - 12", null,      validator.validate(length_12));
140     }
141 
142     /**
143      * Test Regular Expression.
144      */
145     public void testRegex() {
146         CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
147 
148         String value2  = "12";
149         String value3  = "123";
150         String value4  = "1234";
151         String value5  = "12345";
152         String invalid = "12a4";
153 
154         // No Regular Expression
155         assertNull("No Regex", validator.getRegexValidator());
156         assertEquals("No Regex 2", value2, validator.validate(value2));
157         assertEquals("No Regex 3", value3, validator.validate(value3));
158         assertEquals("No Regex 4", value4, validator.validate(value4));
159         assertEquals("No Regex 5", value5, validator.validate(value5));
160         assertEquals("No Regex invalid", invalid, validator.validate(invalid));
161 
162         // Regular Expression
163         String regex = "^([0-9]{3,4})$";
164         validator = new CodeValidator(regex, -1, -1, (CheckDigit)null);
165         assertNotNull("No Regex", validator.getRegexValidator());
166         assertEquals("Regex 2", null,   validator.validate(value2));
167         assertEquals("Regex 3", value3, validator.validate(value3));
168         assertEquals("Regex 4", value4, validator.validate(value4));
169         assertEquals("Regex 5", null,   validator.validate(value5));
170         assertEquals("Regex invalid", null, validator.validate(invalid));
171 
172         // Reformatted
173         regex = "^([0-9]{3})(?:[-\\s])([0-9]{3})$";
174         validator = new CodeValidator(new RegexValidator(regex), 6, (CheckDigit)null);
175         assertEquals("Reformat 123-456", "123456", validator.validate("123-456"));
176         assertEquals("Reformat 123 456", "123456", validator.validate("123 456"));
177         assertEquals("Reformat 123456",  null,     validator.validate("123456"));
178         assertEquals("Reformat 123.456", null,     validator.validate("123.456"));
179 
180         regex = "^(?:([0-9]{3})(?:[-\\s])([0-9]{3}))|([0-9]{6})$";
181         validator = new CodeValidator(new RegexValidator(regex), 6, (CheckDigit)null);
182         assertEquals("Reformat 2 Regex",  "RegexValidator{" + regex + "}", validator.getRegexValidator().toString());
183         assertEquals("Reformat 2 123-456", "123456", validator.validate("123-456"));
184         assertEquals("Reformat 2 123 456", "123456", validator.validate("123 456"));
185         assertEquals("Reformat 2 123456",  "123456", validator.validate("123456"));
186 
187     }
188 
189     /**
190      * Test Regular Expression.
191      */
192     public void testNoInput() {
193         CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
194         assertEquals("Null",         null, validator.validate(null));
195         assertEquals("Zero Length",  null, validator.validate(""));
196         assertEquals("Spaces",       null, validator.validate("   "));
197         assertEquals("Trimmed",      "A",  validator.validate(" A  "));
198     }
199 
200     public void testValidator294_1() {
201         CodeValidator validator = new CodeValidator((String)null, 0, -1, (CheckDigit)null);
202         assertEquals("Null",         null, validator.validate(null));
203         validator = new CodeValidator((String)null, -1, 0, (CheckDigit)null);
204         assertEquals("Null",         null, validator.validate(null));
205     }
206 
207     public void testValidator294_2() {
208         CodeValidator validator = new CodeValidator((String)null, -1, 0, (CheckDigit)null);
209         assertEquals("Null",         null, validator.validate(null));
210     }
211 
212     /**
213      * Test Regular Expression.
214      */
215     public void testConstructors() {
216         CodeValidator validator = null;
217         RegexValidator regex = new RegexValidator("^[0-9]*$");
218 
219         // Constructor 1
220         validator = new CodeValidator(regex, EAN13CheckDigit.EAN13_CHECK_DIGIT);
221         assertEquals("Constructor 1 - regex",      regex, validator.getRegexValidator());
222         assertEquals("Constructor 1 - min length", -1, validator.getMinLength());
223         assertEquals("Constructor 1 - max length", -1, validator.getMaxLength());
224         assertEquals("Constructor 1 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
225 
226         // Constructor 2
227         validator = new CodeValidator(regex, 13, EAN13CheckDigit.EAN13_CHECK_DIGIT);
228         assertEquals("Constructor 2 - regex",      regex, validator.getRegexValidator());
229         assertEquals("Constructor 2 - min length", 13, validator.getMinLength());
230         assertEquals("Constructor 2 - max length", 13, validator.getMaxLength());
231         assertEquals("Constructor 2 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
232 
233         // Constructor 3
234         validator = new CodeValidator(regex, 10, 20, EAN13CheckDigit.EAN13_CHECK_DIGIT);
235         assertEquals("Constructor 3 - regex",      regex, validator.getRegexValidator());
236         assertEquals("Constructor 3 - min length", 10, validator.getMinLength());
237         assertEquals("Constructor 3 - max length", 20, validator.getMaxLength());
238         assertEquals("Constructor 3 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
239 
240         // Constructor 4
241         validator = new CodeValidator("^[0-9]*$", EAN13CheckDigit.EAN13_CHECK_DIGIT);
242         assertEquals("Constructor 4 - regex",      "RegexValidator{^[0-9]*$}", validator.getRegexValidator().toString());
243         assertEquals("Constructor 4 - min length", -1, validator.getMinLength());
244         assertEquals("Constructor 4 - max length", -1, validator.getMaxLength());
245         assertEquals("Constructor 4 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
246 
247         // Constructor 5
248         validator = new CodeValidator("^[0-9]*$", 13, EAN13CheckDigit.EAN13_CHECK_DIGIT);
249         assertEquals("Constructor 5 - regex",      "RegexValidator{^[0-9]*$}", validator.getRegexValidator().toString());
250         assertEquals("Constructor 5 - min length", 13, validator.getMinLength());
251         assertEquals("Constructor 5 - max length", 13, validator.getMaxLength());
252         assertEquals("Constructor 5 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
253 
254         // Constructor 6
255         validator = new CodeValidator("^[0-9]*$", 10, 20, EAN13CheckDigit.EAN13_CHECK_DIGIT);
256         assertEquals("Constructor 6 - regex",      "RegexValidator{^[0-9]*$}", validator.getRegexValidator().toString());
257         assertEquals("Constructor 6 - min length", 10, validator.getMinLength());
258         assertEquals("Constructor 6 - max length", 20, validator.getMaxLength());
259         assertEquals("Constructor 6 - check digit", EAN13CheckDigit.EAN13_CHECK_DIGIT, validator.getCheckDigit());
260     }
261 
262 }