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.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.Locale;
25  
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Test Case for ShortValidator.
31   */
32  public class ShortValidatorTest extends AbstractNumberValidatorTest {
33  
34      @Override
35      @BeforeEach
36      protected void setUp() {
37          super.setUp();
38  
39          validator = new ShortValidator(false, 0);
40          strictValidator = new ShortValidator();
41  
42          testPattern = "#,###";
43  
44          // testValidateMinMax()
45          max = Short.valueOf(Short.MAX_VALUE);
46          maxPlusOne = Long.valueOf(max.longValue() + 1);
47          min = Short.valueOf(Short.MIN_VALUE);
48          minMinusOne = Long.valueOf(min.longValue() - 1);
49  
50          // testInvalidStrict()
51          invalidStrict = new String[] { null, "", "X", "X12", "12X", "1X2", "1.2" };
52  
53          // testInvalidNotStrict()
54          invalid = new String[] { null, "", "X", "X12" };
55  
56          // testValid()
57          testNumber = Short.valueOf((short) 1234);
58          testZero = Short.valueOf((short) 0);
59          validStrict = new String[] { "0", "1234", "1,234" };
60          validStrictCompare = new Number[] { testZero, testNumber, testNumber };
61          valid = new String[] { "0", "1234", "1,234", "1,234.5", "1234X" };
62          validCompare = new Number[] { testZero, testNumber, testNumber, testNumber, testNumber };
63  
64          testStringUS = "1,234";
65          testStringDE = "1.234";
66  
67          // Localized Pattern test
68          localeValue = testStringDE;
69          localePattern = "#.###";
70          testLocale = Locale.GERMANY;
71          localeExpected = testNumber;
72  
73      }
74  
75      /**
76       * Test Short Range/Min/Max
77       */
78      @Test
79      public void testShortRangeMinMax() {
80          final ShortValidator validator = (ShortValidator) strictValidator;
81          final Short number9 = validator.validate("9", "#");
82          final Short number10 = validator.validate("10", "#");
83          final Short number11 = validator.validate("11", "#");
84          final Short number19 = validator.validate("19", "#");
85          final Short number20 = validator.validate("20", "#");
86          final Short number21 = validator.validate("21", "#");
87          final short min = (short) 10;
88          final short max = (short) 20;
89  
90          // Test isInRange()
91          assertFalse(validator.isInRange(number9, min, max), "isInRange() < min");
92          assertTrue(validator.isInRange(number10, min, max), "isInRange() = min");
93          assertTrue(validator.isInRange(number11, min, max), "isInRange() in range");
94          assertTrue(validator.isInRange(number20, min, max), "isInRange() = max");
95          assertFalse(validator.isInRange(number21, min, max), "isInRange() > max");
96  
97          // Test minValue()
98          assertFalse(validator.minValue(number9, min), "minValue() < min");
99          assertTrue(validator.minValue(number10, min), "minValue() = min");
100         assertTrue(validator.minValue(number11, min), "minValue() > min");
101 
102         // Test minValue()
103         assertTrue(validator.maxValue(number19, max), "maxValue() < max");
104         assertTrue(validator.maxValue(number20, max), "maxValue() = max");
105         assertFalse(validator.maxValue(number21, max), "maxValue() > max");
106     }
107 
108     /**
109      * Test ShortValidator validate Methods
110      */
111     @Test
112     public void testShortValidatorMethods() {
113         final Locale locale = Locale.GERMAN;
114         final String pattern = "0,00,00";
115         final String patternVal = "1,23,45";
116         final String germanPatternVal = "1.23.45";
117         final String localeVal = "12.345";
118         final String defaultVal = "12,345";
119         final String xxxx = "XXXX";
120         final Short expected = Short.valueOf((short) 12345);
121         assertEquals(expected, ShortValidator.getInstance().validate(defaultVal), "validate(A) default");
122         assertEquals(expected, ShortValidator.getInstance().validate(localeVal, locale), "validate(A) locale");
123         assertEquals(expected, ShortValidator.getInstance().validate(patternVal, pattern), "validate(A) pattern");
124         assertEquals(expected, ShortValidator.getInstance().validate(germanPatternVal, pattern, Locale.GERMAN), "validate(A) both");
125 
126         assertTrue(ShortValidator.getInstance().isValid(defaultVal), "isValid(A) default");
127         assertTrue(ShortValidator.getInstance().isValid(localeVal, locale), "isValid(A) locale");
128         assertTrue(ShortValidator.getInstance().isValid(patternVal, pattern), "isValid(A) pattern");
129         assertTrue(ShortValidator.getInstance().isValid(germanPatternVal, pattern, Locale.GERMAN), "isValid(A) both");
130 
131         assertNull(ShortValidator.getInstance().validate(xxxx), "validate(B) default");
132         assertNull(ShortValidator.getInstance().validate(xxxx, locale), "validate(B) locale");
133         assertNull(ShortValidator.getInstance().validate(xxxx, pattern), "validate(B) pattern");
134         assertNull(ShortValidator.getInstance().validate(patternVal, pattern, Locale.GERMAN), "validate(B) both");
135 
136         assertFalse(ShortValidator.getInstance().isValid(xxxx), "isValid(B) default");
137         assertFalse(ShortValidator.getInstance().isValid(xxxx, locale), "isValid(B) locale");
138         assertFalse(ShortValidator.getInstance().isValid(xxxx, pattern), "isValid(B) pattern");
139         assertFalse(ShortValidator.getInstance().isValid(patternVal, pattern, Locale.GERMAN), "isValid(B) both");
140     }
141 }