1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
51 invalidStrict = new String[] { null, "", "X", "X12", "12X", "1X2", "1.2" };
52
53
54 invalid = new String[] { null, "", "X", "X12" };
55
56
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
68 localeValue = testStringDE;
69 localePattern = "#.###";
70 testLocale = Locale.GERMANY;
71 localeExpected = testNumber;
72
73 }
74
75
76
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
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
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
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
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 }