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