001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.krad.datadictionary.validation.charlevel;
017    
018    import org.junit.Test;
019    import org.kuali.rice.kns.datadictionary.validation.charlevel.AlphaNumericValidationPattern;
020    import org.kuali.rice.krad.datadictionary.validation.ValidationTestUtils;
021    import org.kuali.rice.test.BaseRiceTestCase;
022    
023    /**
024     * AlphaNumericValidationPatternTest tests {@link AlphaNumericValidationPattern}
025     *
026     * @author Kuali Rice Team (rice.collab@kuali.org)
027     */
028    public class AlphaNumericValidationPatternTest extends BaseRiceTestCase {
029        private AlphaNumericValidationPattern pattern;
030    
031        @Override
032        public void setUp() throws Exception {
033            super.setUp();
034            pattern = new AlphaNumericValidationPattern();
035        }
036    
037        /**
038         * tests that only numbers and letters of the alphabet are allowed
039         */
040        @Test public final void testMatch_allowDefault() {
041            boolean[] expected = { true, // ""
042                    false, // "!!!"
043                    false, // "[a-9]"
044                    false, // "^A-Z"
045                    true, // "abc"
046                    false, // "a bc"
047                    false, // "a_bc"
048                    true, // "123"
049                    false, // "12 3"
050                    false, // "12_3"
051                    true, // "a1b2c3"
052                    false, // "a1b2_c3"
053                    false, // "a 1b2c3"
054                    false, // "a 1b2_c3"
055                    false, //"foo.bar"
056                    false, //"foo.bar_baz"
057                    false, //".bar_foo baz"
058            };
059    
060            ValidationTestUtils.assertPatternMatches(pattern, expected);
061        }
062    
063        /**
064         * tests that underscores, numbers and letters of the alphabet are allowed
065         */
066        @Test public final void testMatch_allowUnderscore() {
067            boolean[] expected = { true, // ""
068                    false, // "!!!"
069                    false, // "[a-9]"
070                    false, // "^A-Z"
071                    true, // "abc"
072                    false, // "a bc"
073                    true, // "a_bc"
074                    true, // "123"
075                    false, // "12 3"
076                    true, // "12_3"
077                    true, // "a1b2c3"
078                    true, // "a1b2_c3"
079                    false, // "a 1b2c3"
080                    false, // "a 1b2_c3"
081                    false, //"foo.bar"
082                    false, //"foo.bar_baz"
083                    false, //".bar_foo baz"
084            };
085    
086            pattern.setAllowUnderscore(true);
087            ValidationTestUtils.assertPatternMatches(pattern, expected);
088        }
089    
090        /**
091         * tests that white space, numbers and letters of the alphabet are allowed
092         */
093        @Test public final void testMatch_allowWhitespace() {
094            boolean[] expected = { true, // ""
095                    false, // "!!!"
096                    false, // "[a-9]"
097                    false, // "^A-Z"
098                    true, // "abc"
099                    true, // "a bc"
100                    false, // "a_bc"
101                    true, // "123"
102                    true, // "12 3"
103                    false, // "12_3"
104                    true, // "a1b2c3"
105                    false, // "a1b2_c3"
106                    true, // "a 1b2c3"
107                    false, // "a 1b2_c3"
108                    false, //"foo.bar"
109                    false, //"foo.bar_baz"
110                    false, //".bar_foo baz"
111            };
112    
113            pattern.setAllowWhitespace(true);
114            ValidationTestUtils.assertPatternMatches(pattern, expected);
115        }
116    
117        /**
118         * tests that white space, underscores, numbers and letters of the alphabet are allowed
119         */
120        @Test public final void testMatch_allowUnderScoreAndWhiteSpace() {
121            boolean[] expected = { true, // ""
122                    false, // "!!!"
123                    false, // "[a-9]"
124                    false, // "^A-Z"
125                    true, // "abc"
126                    true, // "a bc"
127                    true, // "a_bc"
128                    true, // "123"
129                    true, // "12 3"
130                    true, // "12_3"
131                    true, // "a1b2c3"
132                    true, // "a1b2_c3"
133                    true, // "a 1b2c3"
134                    true, // "a 1b2_c3"
135                    false, //"foo.bar"
136                    false, //"foo.bar_baz"
137                    false, //".bar_foo baz"
138            };
139    
140            pattern.setAllowUnderscore(true);
141            pattern.setAllowWhitespace(true);
142    
143            ValidationTestUtils.assertPatternMatches(pattern, expected);
144        }
145    
146        /**
147         * tests that periods, numbers and letters of the alphabet are allowed
148         */
149        @Test public final void testMatch_allowPeriod() {
150            boolean[] expected = { true, // ""
151                    false, // "!!!"
152                    false, // "[a-9]"
153                    false, // "^A-Z"
154                    true, // "abc"
155                    false, // "a bc"
156                    false, // "a_bc"
157                    true, // "123"
158                    false, // "12 3"
159                    false, // "12_3"
160                    true, // "a1b2c3"
161                    false, // "a1b2_c3"
162                    false, // "a 1b2c3"
163                    false, // "a 1b2_c3"
164                    true, //"foo.bar"
165                    false, //"foo.bar_baz"
166                    false, //".bar_foo baz"
167            };
168    
169            pattern.setAllowPeriod(true);
170    
171            ValidationTestUtils.assertPatternMatches(pattern, expected);
172        }
173    
174        /**
175         * tests that periods, underscores, numbers and letters of the alphabet are allowed
176         */
177        @Test public final void testMatch_allowPeriodAndUnderscore() {
178            boolean[] expected = { true, // ""
179                    false, // "!!!"
180                    false, // "[a-9]"
181                    false, // "^A-Z"
182                    true, // "abc"
183                    false, // "a bc"
184                    true, // "a_bc"
185                    true, // "123"
186                    false, // "12 3"
187                    true, // "12_3"
188                    true, // "a1b2c3"
189                    true, // "a1b2_c3"
190                    false, // "a 1b2c3"
191                    false, // "a 1b2_c3"
192                    true, //"foo.bar"
193                    true, //"foo.bar_baz"
194                    false, //".bar_foo baz"
195            };
196    
197            pattern.setAllowPeriod(true);
198            pattern.setAllowUnderscore(true);
199    
200            ValidationTestUtils.assertPatternMatches(pattern, expected);
201        }
202    
203        /**
204         * tests that periods, underscores, white space, numbers and letters of the alphabet are allowed
205         */
206        @Test public final void testMatch_allowPeriodAndUnderscoreAndSpace() {
207            boolean[] expected = { true, // ""
208                    false, // "!!!"
209                    false, // "[a-9]"
210                    false, // "^A-Z"
211                    true, // "abc"
212                    true, // "a bc"
213                    true, // "a_bc"
214                    true, // "123"
215                    true, // "12 3"
216                    true, // "12_3"
217                    true, // "a1b2c3"
218                    true, // "a1b2_c3"
219                    true, // "a 1b2c3"
220                    true, // "a 1b2_c3"
221                    true, //"foo.bar"
222                    true, //"foo.bar_baz"
223                    true, //".bar_foo baz"
224            };
225    
226            pattern.setAllowPeriod(true);
227            pattern.setAllowUnderscore(true);
228            pattern.setAllowWhitespace(true);
229    
230            ValidationTestUtils.assertPatternMatches(pattern, expected);
231        }
232    }