001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary.validation.charlevel;
017
018import org.junit.Test;
019import org.kuali.rice.kns.datadictionary.validation.charlevel.CharsetValidationPattern;
020import org.kuali.rice.test.BaseRiceTestCase;
021
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertTrue;
024
025public class CharsetValidationPatternTest extends BaseRiceTestCase {
026    private CharsetValidationPattern charsetPattern;
027
028    public void setUp() throws Exception {
029        charsetPattern = new CharsetValidationPattern();
030    }
031
032    @Test public final void testSetBoth_AB() {
033        boolean failedAsExpected = false;
034
035        try {
036            charsetPattern.setMaxLength(5);
037            charsetPattern.setExactLength(5);
038        }
039        catch (IllegalStateException e) {
040            failedAsExpected = true;
041        }
042
043        assertTrue(failedAsExpected);
044    }
045
046    @Test public final void testSetBoth_BA() {
047        boolean failedAsExpected = false;
048
049        try {
050            charsetPattern.setExactLength(5);
051            charsetPattern.setMaxLength(5);
052        }
053        catch (IllegalStateException e) {
054            failedAsExpected = true;
055        }
056
057        assertTrue(failedAsExpected);
058    }
059
060    @Test public final void testMatch_exactLength1() {
061        charsetPattern.setExactLength(3);
062        charsetPattern.setValidChars("abc");
063
064        assertFalse(charsetPattern.matches("aaaa"));
065    }
066
067    @Test public final void testMatch_exactLength2() {
068        charsetPattern.setExactLength(3);
069        charsetPattern.setValidChars("abc");
070
071        assertFalse(charsetPattern.matches("aa"));
072    }
073
074    @Test public final void testMatch_exactLength3() {
075        charsetPattern.setExactLength(3);
076        charsetPattern.setValidChars("abc");
077
078        assertTrue(charsetPattern.matches("aaa"));
079    }
080
081
082    @Test public final void testMatch_maxLength1() {
083        charsetPattern.setMaxLength(3);
084        charsetPattern.setValidChars("abc");
085
086        assertFalse(charsetPattern.matches("aaaa"));
087    }
088
089    @Test public final void testMatch_maxLength2() {
090        charsetPattern.setMaxLength(3);
091        charsetPattern.setValidChars("abc");
092
093        assertTrue(charsetPattern.matches("aa"));
094    }
095
096    @Test public final void testMatch_maxLength3() {
097        charsetPattern.setMaxLength(3);
098        charsetPattern.setValidChars("abc");
099
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}