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 junit.framework.TestCase;
20  
21  import java.util.Locale;
22  import java.text.DecimalFormat;
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.ObjectInputStream;
26  import java.io.ObjectOutputStream;
27  import java.math.BigDecimal;
28  /**
29   * Base Number Test Case.
30   * 
31   * @version $Revision$
32   */
33  public abstract class AbstractNumberValidatorTest extends TestCase {
34  
35      protected AbstractNumberValidator validator;
36      protected AbstractNumberValidator strictValidator;
37  
38      protected Number max;
39      protected Number maxPlusOne;
40      protected Number min;
41      protected Number minMinusOne;
42      protected String[] invalid;
43      protected String[] valid;
44      protected Number[] validCompare;
45  
46      protected String[] invalidStrict;
47      protected String[] validStrict;
48      protected Number[] validStrictCompare;
49  
50      protected String testPattern;
51      protected Number testNumber;
52      protected Number testZero;
53      protected String testStringUS;
54      protected String testStringDE;
55  
56      protected String localeValue;
57      protected String localePattern;
58      protected Locale testLocale;
59      protected Number localeExpected;
60  
61      /**
62       * Constructor
63       * @param name test name
64       */
65      public AbstractNumberValidatorTest(String name) {
66          super(name);
67      }
68  
69      @Override
70      protected void setUp() throws Exception {
71          super.setUp();
72  
73          Locale.setDefault(Locale.US);
74  
75      }
76  
77      /**
78       * Tear down
79       * @throws Exception
80       */
81      @Override
82      protected void tearDown() throws Exception {
83          super.tearDown();
84          validator = null;
85          strictValidator = null;
86      }
87  
88      /**
89       * Test Format Type
90       */
91      public void testFormatType() {
92          assertEquals("Format Type A", 0, validator.getFormatType());
93          assertEquals("Format Type B", AbstractNumberValidator.STANDARD_FORMAT, validator.getFormatType());
94      }
95  
96      /**
97       * Test Min/Max values allowed
98       */
99      public void testValidateMinMax() {
100         DecimalFormat fmt = new DecimalFormat("#");
101         if (max != null) {
102             assertEquals("Test Max",   max, validator.parse(fmt.format(max), "#", null));
103             assertNull("Test Max + 1",      validator.parse(fmt.format(maxPlusOne), "#", null));
104             assertEquals("Test Min",   min, validator.parse(fmt.format(min), "#", null));
105             assertNull("Test min - 1",      validator.parse(fmt.format(minMinusOne), "#", null));
106         }
107     }
108 
109     /**
110      * Test Invalid, strict=true
111      */
112     public void testInvalidStrict() {
113         for (int i = 0; i < invalidStrict.length; i++) {
114             String text = "idx=["+i+"] value=[" + invalidStrict[i] + "]";
115             assertNull("(A) "  + text, strictValidator.parse(invalidStrict[i], null, Locale.US));
116             assertFalse("(B) " + text, strictValidator.isValid(invalidStrict[i], null, Locale.US));
117             assertNull("(C) "  + text, strictValidator.parse(invalidStrict[i], testPattern, null));
118             assertFalse("(D) " + text, strictValidator.isValid(invalidStrict[i], testPattern, null));
119         }
120     }
121 
122     /**
123      * Test Invalid, strict=false
124      */
125     public void testInvalidNotStrict() {
126         for (int i = 0; i < invalid.length; i++) {
127             String text = "idx=["+i+"] value=[" + invalid[i] + "]";
128             assertNull("(A) "  + text, validator.parse(invalid[i], null, Locale.US));
129             assertFalse("(B) " + text, validator.isValid(invalid[i], null, Locale.US));
130             assertNull("(C) "  + text, validator.parse(invalid[i], testPattern, null));
131             assertFalse("(D) " + text, validator.isValid(invalid[i], testPattern, null));
132         }
133     }
134 
135     /**
136      * Test Valid, strict=true
137      */
138     public void testValidStrict() {
139         for (int i = 0; i < validStrict.length; i++) {
140             String text = "idx=["+i+"] value=[" + validStrictCompare[i] + "]";
141             assertEquals("(A) "  + text, validStrictCompare[i], strictValidator.parse(validStrict[i], null, Locale.US));
142             assertTrue("(B) "    + text,                        strictValidator.isValid(validStrict[i], null, Locale.US));
143             assertEquals("(C) "  + text, validStrictCompare[i], strictValidator.parse(validStrict[i], testPattern, null));
144             assertTrue("(D) "    + text,                        strictValidator.isValid(validStrict[i], testPattern, null));
145         }
146     }
147 
148     /**
149      * Test Valid, strict=false
150      */
151     public void testValidNotStrict() {
152         for (int i = 0; i < valid.length; i++) {
153             String text = "idx=["+i+"] value=[" + validCompare[i] + "]";
154             assertEquals("(A) "  + text, validCompare[i], validator.parse(valid[i], null, Locale.US));
155             assertTrue("(B) "    + text,                  validator.isValid(valid[i], null, Locale.US));
156             assertEquals("(C) "  + text, validCompare[i], validator.parse(valid[i], testPattern, null));
157             assertTrue("(D) "    + text,                  validator.isValid(valid[i], testPattern, null));
158         }
159     }
160 
161     /**
162      * Test different Locale
163      */
164     public void testValidateLocale() {
165 
166         assertEquals("US Locale, US Format", testNumber, strictValidator.parse(testStringUS, null, Locale.US));
167         assertNull("US Locale, DE Format", strictValidator.parse(testStringDE, null, Locale.US));
168 
169         // Default German Locale
170         assertEquals("DE Locale, DE Format", testNumber, strictValidator.parse(testStringDE, null, Locale.GERMAN));
171         assertNull("DE Locale, US Format", strictValidator.parse(testStringUS, null, Locale.GERMAN));
172 
173         // Default Locale has been set to Locale.US in setup()
174         assertEquals("Default Locale, US Format", testNumber, strictValidator.parse(testStringUS, null, null));
175         assertNull("Default Locale, DE Format", strictValidator.parse(testStringDE, null, null));
176     }
177 
178     /**
179      * Test format() methods
180      */
181     public void testFormat() {
182         Number number = new BigDecimal("1234.5");
183         assertEquals("US Locale, US Format", "1,234.5", strictValidator.format(number, Locale.US));
184         assertEquals("DE Locale, DE Format", "1.234,5", strictValidator.format(number, Locale.GERMAN));
185         assertEquals("Pattern #,#0.00", "12,34.50",  strictValidator.format(number, "#,#0.00"));
186     }
187 
188     /**
189      * Test Range/Min/Max
190      */
191     public void testRangeMinMax() {
192         Number number9 = Integer.valueOf(9);
193         Number number10 = Integer.valueOf(10);
194         Number number11 = Integer.valueOf(11);
195         Number number19 = Integer.valueOf(19);
196         Number number20 = Integer.valueOf(20);
197         Number number21 = Integer.valueOf(21);
198 
199         // Test isInRange()
200         assertFalse("isInRange() < min",   strictValidator.isInRange(number9 ,  number10, number20));
201         assertTrue("isInRange() = min",    strictValidator.isInRange(number10 , number10, number20));
202         assertTrue("isInRange() in range", strictValidator.isInRange(number11 , number10, number20));
203         assertTrue("isInRange() = max",    strictValidator.isInRange(number20 , number10, number20));
204         assertFalse("isInRange() > max",   strictValidator.isInRange(number21 , number10, number20));
205 
206         // Test minValue()
207         assertFalse("minValue() < min",    strictValidator.minValue(number9 ,  number10));
208         assertTrue("minValue() = min",     strictValidator.minValue(number10 , number10));
209         assertTrue("minValue() > min",     strictValidator.minValue(number11 , number10));
210 
211         // Test minValue()
212         assertTrue("maxValue() < max",     strictValidator.maxValue(number19 , number20));
213         assertTrue("maxValue() = max",     strictValidator.maxValue(number20 , number20));
214         assertFalse("maxValue() > max",    strictValidator.maxValue(number21 , number20));
215     }
216 
217     /**
218      * Test validator serialization.
219      */
220     public void testSerialization() {
221         // Serialize the check digit routine
222         ByteArrayOutputStream baos = new ByteArrayOutputStream();
223         try {
224             ObjectOutputStream oos = new ObjectOutputStream(baos);
225             oos.writeObject(validator);
226             oos.flush();
227             oos.close();
228         } catch (Exception e) {
229             fail(validator.getClass().getName() + " error during serialization: " + e);
230         }
231 
232         // Deserialize the test object
233         Object result = null;
234         try {
235             ByteArrayInputStream bais =
236                 new ByteArrayInputStream(baos.toByteArray());
237             ObjectInputStream ois = new ObjectInputStream(bais);
238             result = ois.readObject();
239             bais.close();
240         } catch (Exception e) {
241             fail(validator.getClass().getName() + " error during deserialization: " + e);
242         }
243         assertNotNull(result);
244     }
245 
246 }