1 /**
2 * Copyright 2005-2011 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.krad.datadictionary.validation;
17
18 import static org.junit.Assert.fail;
19
20 /**
21 * ValidationTestUtils has test values and a utility method for testing validation patterns
22 *
23 * @author Kuali Rice Team (rice.collab@kuali.org)
24 */
25 public final class ValidationTestUtils {
26 private static final String[] TEST_INPUTS = { "", "!!!", "[a-9]", "^A-Z", "abc", "a bc", "a_bc", "123", "12 3", "12_3", "a1b2c3", "a1b2_c3", "a 1b2c3", "a 1b2_c3", "foo.bar", "foo.bar_baz", ".bar_foo baz" };
27
28 private ValidationTestUtils() {
29 throw new UnsupportedOperationException("do not call");
30 }
31
32 /**
33 * checks that the test values, when matched to the supplied pattern, result in the expected value
34 * @param pattern - a validation pattern
35 * @param expectedValues - an array containing boolean values for each expected result
36 */
37 public static final void assertPatternMatches(ValidationPattern pattern, boolean[] expectedValues) {
38 if (expectedValues.length != TEST_INPUTS.length) {
39 fail("expectedValues length was " + expectedValues.length + ", expected TEST_INPUTS.length of " + TEST_INPUTS.length);
40 }
41
42 for (int i = 0; i < TEST_INPUTS.length; ++i) {
43 String testInput = TEST_INPUTS[i];
44 boolean expectedResult = expectedValues[i];
45
46 boolean actualResult = pattern.matches(testInput);
47 if (actualResult != expectedResult) {
48 fail("for input '" + testInput + "', expected " + expectedResult + " but got " + actualResult);
49 }
50 }
51 }
52 }