1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validation.charlevel;
17
18 import org.junit.Test;
19 import org.kuali.rice.kns.datadictionary.validation.charlevel.CharsetValidationPattern;
20 import org.kuali.rice.test.BaseRiceTestCase;
21
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 public class CharsetValidationPatternTest extends BaseRiceTestCase {
26 private CharsetValidationPattern charsetPattern;
27
28 public void setUp() throws Exception {
29 charsetPattern = new CharsetValidationPattern();
30 }
31
32 @Test public final void testSetBoth_AB() {
33 boolean failedAsExpected = false;
34
35 try {
36 charsetPattern.setMaxLength(5);
37 charsetPattern.setExactLength(5);
38 }
39 catch (IllegalStateException e) {
40 failedAsExpected = true;
41 }
42
43 assertTrue(failedAsExpected);
44 }
45
46 @Test public final void testSetBoth_BA() {
47 boolean failedAsExpected = false;
48
49 try {
50 charsetPattern.setExactLength(5);
51 charsetPattern.setMaxLength(5);
52 }
53 catch (IllegalStateException e) {
54 failedAsExpected = true;
55 }
56
57 assertTrue(failedAsExpected);
58 }
59
60 @Test public final void testMatch_exactLength1() {
61 charsetPattern.setExactLength(3);
62 charsetPattern.setValidChars("abc");
63
64 assertFalse(charsetPattern.matches("aaaa"));
65 }
66
67 @Test public final void testMatch_exactLength2() {
68 charsetPattern.setExactLength(3);
69 charsetPattern.setValidChars("abc");
70
71 assertFalse(charsetPattern.matches("aa"));
72 }
73
74 @Test public final void testMatch_exactLength3() {
75 charsetPattern.setExactLength(3);
76 charsetPattern.setValidChars("abc");
77
78 assertTrue(charsetPattern.matches("aaa"));
79 }
80
81
82 @Test public final void testMatch_maxLength1() {
83 charsetPattern.setMaxLength(3);
84 charsetPattern.setValidChars("abc");
85
86 assertFalse(charsetPattern.matches("aaaa"));
87 }
88
89 @Test public final void testMatch_maxLength2() {
90 charsetPattern.setMaxLength(3);
91 charsetPattern.setValidChars("abc");
92
93 assertTrue(charsetPattern.matches("aa"));
94 }
95
96 @Test public final void testMatch_maxLength3() {
97 charsetPattern.setMaxLength(3);
98 charsetPattern.setValidChars("abc");
99
100 assertTrue(charsetPattern.matches("aaa"));
101 }
102
103
104 @Test public final void testSetValidChars_emptyString() {
105 boolean failedAsExpected = false;
106
107 try {
108 charsetPattern.setValidChars("");
109 }
110 catch (IllegalArgumentException e) {
111 failedAsExpected = true;
112 }
113
114 assertTrue(failedAsExpected);
115 }
116
117
118 @Test public final void testMatch_trailingSlash1() {
119 charsetPattern.setValidChars("abcd\\");
120
121 assertTrue(charsetPattern.matches("a"));
122 }
123
124 @Test public final void testMatch_trailingSlash2() {
125 charsetPattern.setValidChars("abcd\\");
126
127 assertTrue(charsetPattern.matches("c"));
128 }
129
130 @Test public final void testMatch_trailingSlash3() {
131 charsetPattern.setValidChars("abcd\\");
132
133 assertTrue(charsetPattern.matches("\\"));
134 }
135
136
137 @Test public final void testMatch_pseudoSet1() {
138 charsetPattern.setValidChars("[A-Z]");
139
140 assertTrue(charsetPattern.matches("A"));
141 }
142
143 @Test public final void testMatch_pseudoSet2() {
144 charsetPattern.setValidChars("[A-Z]");
145
146 assertTrue(charsetPattern.matches("Z"));
147 }
148
149 @Test public final void testMatch_pseudoSet3() {
150 charsetPattern.setValidChars("[A-Z]");
151
152 assertTrue(charsetPattern.matches("-"));
153 }
154
155 @Test public final void testMatch_pseudoSet4() {
156 charsetPattern.setValidChars("[A-Z]");
157
158 assertTrue(charsetPattern.matches("["));
159 }
160
161 @Test public final void testMatch_pseudoSet5() {
162 charsetPattern.setValidChars("[A-Z]");
163
164 assertFalse(charsetPattern.matches("C"));
165 }
166
167
168 @Test public final void testMatch_partialPseudoSet1() {
169 charsetPattern.setValidChars("[ABC");
170
171 assertTrue(charsetPattern.matches("A"));
172 }
173
174 @Test public final void testMatch_partialPseudoSet2() {
175 charsetPattern.setValidChars("[ABC");
176
177 assertFalse(charsetPattern.matches("Z"));
178 }
179
180
181 @Test public final void testMatch_pseudoSetTrailingSlash1() {
182 charsetPattern.setValidChars("[A-Z]\\");
183
184 assertTrue(charsetPattern.matches("A"));
185 }
186
187 @Test public final void testMatch_pseudoSetTrailingSlash2() {
188 charsetPattern.setValidChars("[A-Z]\\");
189
190 assertTrue(charsetPattern.matches("Z"));
191 }
192
193 @Test public final void testMatch_pseudoSetTrailingSlash3() {
194 charsetPattern.setValidChars("[A-Z]\\");
195
196 assertTrue(charsetPattern.matches("-"));
197 }
198
199 @Test public final void testMatch_pseudoSetTrailingSlash4() {
200 charsetPattern.setValidChars("[A-Z]\\");
201
202 assertTrue(charsetPattern.matches("["));
203 }
204
205 @Test public final void testMatch_pseudoSetTrailingSlash5() {
206 charsetPattern.setValidChars("[A-Z]\\");
207
208 assertFalse(charsetPattern.matches("C"));
209 }
210
211
212 @Test public final void testMatch_pseudoCapture1() {
213 charsetPattern.setValidChars("(ABC)");
214
215 assertTrue(charsetPattern.matches("("));
216 }
217
218 @Test public final void testMatch_pseudoCapture2() {
219 charsetPattern.setValidChars("(ABC)");
220
221 assertTrue(charsetPattern.matches(")"));
222 }
223
224 @Test public final void testMatch_pseudoCapture3() {
225 charsetPattern.setValidChars("(ABC)");
226
227 assertTrue(charsetPattern.matches("B"));
228 }
229
230
231 @Test public final void testMatch_pseudoRange1() {
232 charsetPattern.setValidChars("A-Z");
233
234 assertTrue(charsetPattern.matches("A"));
235 }
236
237 @Test public final void testMatch_pseudoRange2() {
238 charsetPattern.setValidChars("A-Z");
239
240 assertTrue(charsetPattern.matches("-"));
241 }
242
243 @Test public final void testMatch_pseudoRange3() {
244 charsetPattern.setValidChars("A-Z");
245
246 assertFalse(charsetPattern.matches("B"));
247 }
248
249
250 @Test public final void testMatch_pseudoIntersection1() {
251 charsetPattern.setValidChars("A&&Z");
252
253 assertTrue(charsetPattern.matches("A"));
254 }
255
256 @Test public final void testMatch_pseudoIntersection2() {
257 charsetPattern.setValidChars("A&&Z");
258
259 assertTrue(charsetPattern.matches("&"));
260 }
261
262 @Test public final void testMatch_pseudoIntersection3() {
263 charsetPattern.setValidChars("A&&Z");
264
265 assertTrue(charsetPattern.matches("Z"));
266 }
267 }