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