1   /*
2    * $Id: BaseNumberValidatorTest.java 371174 2006-01-22 03:24:40Z niallp $
3    * $Revision: 371174 $
4    * $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
5    *
6    * ====================================================================
7    * Copyright 2006 The Apache Software Foundation
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  package org.apache.commons.validator.routines;
22  
23  import junit.framework.TestCase;
24  
25  import java.util.Locale;
26  import java.text.DecimalFormat;
27  import java.math.BigDecimal;
28  /***
29   * Base Number Test Case.
30   * 
31   * @version $Revision: 371174 $ $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
32   */
33  public class BaseNumberValidatorTest 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 BaseNumberValidatorTest(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 }