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