source: josm/trunk/test/unit/org/openstreetmap/josm/data/validation/routines/RegexValidatorTest.java@ 17275

Last change on this file since 17275 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 11.9 KB
Line 
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 */
17package org.openstreetmap.josm.data.validation.routines;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNull;
22import static org.junit.Assert.assertTrue;
23import static org.junit.jupiter.api.Assertions.fail;
24
25import java.util.Arrays;
26import java.util.regex.PatternSyntaxException;
27
28import org.junit.jupiter.api.Test;
29import org.openstreetmap.josm.tools.Logging;
30
31/**
32 * Test Case for RegexValidatorTest.
33 *
34 * @version $Revision: 1741724 $
35 * @since Validator 1.4
36 */
37class RegexValidatorTest {
38
39 private static final String REGEX = "^([abc]*)(?:\\-)([DEF]*)(?:\\-)([123]*)$";
40
41 private static final String COMPONENT_1 = "([abc]{3})";
42 private static final String COMPONENT_2 = "([DEF]{3})";
43 private static final String COMPONENT_3 = "([123]{3})";
44 private static final String SEPARATOR_1 = "(?:\\-)";
45 private static final String SEPARATOR_2 = "(?:\\s)";
46 private static final String REGEX_1 = "^" + COMPONENT_1 + SEPARATOR_1 + COMPONENT_2 + SEPARATOR_1 + COMPONENT_3 + "$";
47 private static final String REGEX_2 = "^" + COMPONENT_1 + SEPARATOR_2 + COMPONENT_2 + SEPARATOR_2 + COMPONENT_3 + "$";
48 private static final String REGEX_3 = "^" + COMPONENT_1 + COMPONENT_2 + COMPONENT_3 + "$";
49 private static final String[] MULTIPLE_REGEX = new String[] {REGEX_1, REGEX_2, REGEX_3};
50
51 // CHECKSTYLE.OFF: SingleSpaceSeparator
52
53 /**
54 * Test instance methods with single regular expression.
55 */
56 @Test
57 void testSingle() {
58 RegexValidator sensitive = new RegexValidator(REGEX);
59 RegexValidator insensitive = new RegexValidator(REGEX, false);
60
61 // isValid()
62 assertTrue("Sensitive isValid() valid", sensitive.isValid("ac-DE-1"));
63 assertFalse("Sensitive isValid() invalid", sensitive.isValid("AB-de-1"));
64 assertTrue("Insensitive isValid() valid", insensitive.isValid("AB-de-1"));
65 assertFalse("Insensitive isValid() invalid", insensitive.isValid("ABd-de-1"));
66
67 // validate()
68 assertEquals("Sensitive validate() valid", "acDE1", sensitive.validate("ac-DE-1"));
69 assertNull("Sensitive validate() invalid", sensitive.validate("AB-de-1"));
70 assertEquals("Insensitive validate() valid", "ABde1", insensitive.validate("AB-de-1"));
71 assertNull("Insensitive validate() invalid", insensitive.validate("ABd-de-1"));
72
73 // match()
74 checkArray("Sensitive match() valid", new String[] {"ac", "DE", "1"}, sensitive.match("ac-DE-1"));
75 checkArray("Sensitive match() invalid", null, sensitive.match("AB-de-1"));
76 checkArray("Insensitive match() valid", new String[] {"AB", "de", "1"}, insensitive.match("AB-de-1"));
77 checkArray("Insensitive match() invalid", null, insensitive.match("ABd-de-1"));
78 assertEquals("validate one", "ABC", (new RegexValidator("^([A-Z]*)$")).validate("ABC"));
79 checkArray("match one", new String[] {"ABC"}, (new RegexValidator("^([A-Z]*)$")).match("ABC"));
80 }
81
82 /**
83 * Test with multiple regular expressions (case sensitive).
84 */
85 @Test
86 void testMultipleSensitive() {
87
88 // ------------ Set up Sensitive Validators
89 RegexValidator multiple = new RegexValidator(MULTIPLE_REGEX);
90 RegexValidator single1 = new RegexValidator(REGEX_1);
91 RegexValidator single2 = new RegexValidator(REGEX_2);
92 RegexValidator single3 = new RegexValidator(REGEX_3);
93
94 // ------------ Set up test values
95 String value = "aac FDE 321";
96 String expect = "aacFDE321";
97 String[] array = new String[] {"aac", "FDE", "321"};
98
99 // isValid()
100 assertTrue("Sensitive isValid() Multiple", multiple.isValid(value));
101 assertFalse("Sensitive isValid() 1st", single1.isValid(value));
102 assertTrue("Sensitive isValid() 2nd", single2.isValid(value));
103 assertFalse("Sensitive isValid() 3rd", single3.isValid(value));
104
105 // validate()
106 assertEquals("Sensitive validate() Multiple", expect, multiple.validate(value));
107 assertNull("Sensitive validate() 1st", single1.validate(value));
108 assertEquals("Sensitive validate() 2nd", expect, single2.validate(value));
109 assertNull("Sensitive validate() 3rd", single3.validate(value));
110
111 // match()
112 checkArray("Sensitive match() Multiple", array, multiple.match(value));
113 checkArray("Sensitive match() 1st", null, single1.match(value));
114 checkArray("Sensitive match() 2nd", array, single2.match(value));
115 checkArray("Sensitive match() 3rd", null, single3.match(value));
116
117 // All invalid
118 value = "AAC*FDE*321";
119 assertFalse("isValid() Invalid", multiple.isValid(value));
120 assertNull("validate() Invalid", multiple.validate(value));
121 assertNull("match() Multiple", multiple.match(value));
122 }
123
124 /**
125 * Test with multiple regular expressions (case in-sensitive).
126 */
127 @Test
128 void testMultipleInsensitive() {
129
130 // ------------ Set up In-sensitive Validators
131 RegexValidator multiple = new RegexValidator(MULTIPLE_REGEX, false);
132 RegexValidator single1 = new RegexValidator(REGEX_1, false);
133 RegexValidator single2 = new RegexValidator(REGEX_2, false);
134 RegexValidator single3 = new RegexValidator(REGEX_3, false);
135
136 // ------------ Set up test values
137 String value = "AAC FDE 321";
138 String expect = "AACFDE321";
139 String[] array = new String[] {"AAC", "FDE", "321"};
140
141 // isValid()
142 assertTrue("isValid() Multiple", multiple.isValid(value));
143 assertFalse("isValid() 1st", single1.isValid(value));
144 assertTrue("isValid() 2nd", single2.isValid(value));
145 assertFalse("isValid() 3rd", single3.isValid(value));
146
147 // validate()
148 assertEquals("validate() Multiple", expect, multiple.validate(value));
149 assertNull("validate() 1st", single1.validate(value));
150 assertEquals("validate() 2nd", expect, single2.validate(value));
151 assertNull("validate() 3rd", single3.validate(value));
152
153 // match()
154 checkArray("match() Multiple", array, multiple.match(value));
155 checkArray("match() 1st", null, single1.match(value));
156 checkArray("match() 2nd", array, single2.match(value));
157 checkArray("match() 3rd", null, single3.match(value));
158
159 // All invalid
160 value = "AAC*FDE*321";
161 assertFalse("isValid() Invalid", multiple.isValid(value));
162 assertNull("validate() Invalid", multiple.validate(value));
163 assertNull("match() Multiple", multiple.match(value));
164 }
165
166 /**
167 * Test Null value
168 */
169 @Test
170 void testNullValue() {
171 RegexValidator validator = new RegexValidator(REGEX);
172 assertFalse("Instance isValid()", validator.isValid(null));
173 assertNull("Instance validate()", validator.validate(null));
174 assertNull("Instance match()", validator.match(null));
175 }
176
177 // CHECKSTYLE.ON: SingleSpaceSeparator
178
179 /**
180 * Test exceptions
181 */
182 @Test
183 void testMissingRegex() {
184
185 // Single Regular Expression - null
186 try {
187 new RegexValidator((String) null);
188 fail("Single Null - expected IllegalArgumentException");
189 } catch (IllegalArgumentException e) {
190 assertEquals("Single Null", "Regular expression[0] is missing", e.getMessage());
191 }
192
193 // Single Regular Expression - Zero Length
194 try {
195 new RegexValidator("");
196 fail("Single Zero Length - expected IllegalArgumentException");
197 } catch (IllegalArgumentException e) {
198 assertEquals("Single Zero Length", "Regular expression[0] is missing", e.getMessage());
199 }
200
201 // Multiple Regular Expression - Null array
202 try {
203 new RegexValidator((String[]) null);
204 fail("Null Array - expected IllegalArgumentException");
205 } catch (IllegalArgumentException e) {
206 assertEquals("Null Array", "Regular expressions are missing", e.getMessage());
207 }
208
209 // Multiple Regular Expression - Zero Length array
210 try {
211 new RegexValidator(new String[0]);
212 fail("Zero Length Array - expected IllegalArgumentException");
213 } catch (IllegalArgumentException e) {
214 assertEquals("Zero Length Array", "Regular expressions are missing", e.getMessage());
215 }
216
217 // Multiple Regular Expression - Array has Null
218 String[] expressions = new String[] {"ABC", null};
219 try {
220 new RegexValidator(expressions);
221 fail("Array has Null - expected IllegalArgumentException");
222 } catch (IllegalArgumentException e) {
223 assertEquals("Array has Null", "Regular expression[1] is missing", e.getMessage());
224 }
225
226 // Multiple Regular Expression - Array has Zero Length
227 expressions = new String[] {"", "ABC"};
228 try {
229 new RegexValidator(expressions);
230 fail("Array has Zero Length - expected IllegalArgumentException");
231 } catch (IllegalArgumentException e) {
232 assertEquals("Array has Zero Length", "Regular expression[0] is missing", e.getMessage());
233 }
234 }
235
236 /**
237 * Test exceptions
238 */
239 @Test
240 void testExceptions() {
241 String invalidRegex = "^([abCD12]*$";
242 try {
243 new RegexValidator(invalidRegex);
244 } catch (PatternSyntaxException e) {
245 // expected
246 Logging.debug(e.getMessage());
247 }
248 }
249
250 /**
251 * Test toString() method
252 */
253 @Test
254 void testToString() {
255 RegexValidator single = new RegexValidator(REGEX);
256 assertEquals("Single", "RegexValidator{" + REGEX + "}", single.toString());
257
258 RegexValidator multiple = new RegexValidator(new String[] {REGEX, REGEX});
259 assertEquals("Multiple", "RegexValidator{" + REGEX + "," + REGEX + "}", multiple.toString());
260 }
261
262 /**
263 * Unit test of {@link RegexValidator#getValidatorName}.
264 */
265 @Test
266 void testValidatorName() {
267 assertNull(new RegexValidator(".*").getValidatorName());
268 }
269
270 /**
271 * Compare two arrays
272 * @param label Label for the test
273 * @param expect Expected array
274 * @param result Actual array
275 */
276 private void checkArray(String label, String[] expect, String[] result) {
277
278 // Handle nulls
279 if (expect == null || result == null) {
280 if (expect == null && result == null) {
281 return; // valid, both null
282 } else {
283 fail(label + " Null expect=" + Arrays.toString(expect) + " result=" + Arrays.toString(result));
284 }
285 return; // not strictly necessary, but prevents possible NPE below
286 }
287
288 // Check Length
289 if (expect.length != result.length) {
290 fail(label + " Length expect=" + expect.length + " result=" + result.length);
291 }
292
293 // Check Values
294 for (int i = 0; i < expect.length; i++) {
295 assertEquals(label +" value[" + i + "]", expect[i], result[i]);
296 }
297 }
298}
Note: See TracBrowser for help on using the repository browser.