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

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

cleanup unit tests

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