View Javadoc

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