01: /*
02: * Copyright 2006 Dan Shellman
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.iscreen.validators;
17:
18: import java.util.Locale;
19:
20: import junit.framework.TestCase;
21:
22: import org.iscreen.SimpleBean;
23: import org.iscreen.ValidationFailure;
24: import org.iscreen.impl.DefaultValidatorContext;
25: import org.iscreen.ognl.OgnlMessage;
26: import org.iscreen.impl.ContextBean;
27:
28: /**
29: * Tests the RegularExpressionValidator validator.
30: *
31: * @author Shellman, Dan
32: */
33: public class RegularExpressionValidatorTest extends TestCase {
34: private RegularExpressionValidator validator;
35: private DefaultValidatorContext context;
36: private SimpleBean bean;
37: private ContextBean root;
38:
39: /**
40: * Default constructor.
41: */
42: public RegularExpressionValidatorTest(String name) {
43: super (name);
44: } //end RegularExpressionValidatorTest()
45:
46: protected void setUp() {
47: validator = new RegularExpressionValidator();
48: validator.setDefaultFailure(new OgnlMessage("regex failure"));
49: bean = new SimpleBean();
50: root = new ContextBean();
51: context = new DefaultValidatorContext(root, Locale.getDefault());
52: } //end setUp()
53:
54: /**
55: * Tests to make sure that a failure is reported when a null
56: * value is used and is shouldn't be.
57: */
58: public void testNull() {
59: validator.setAllowNull(false);
60: validator.validate(context, bean);
61: assertEquals(1, context.getFailureCount());
62: assertEquals("regex failure", ((ValidationFailure) context
63: .getFailures().get(0)).getMessage());
64: } //end testNull()
65:
66: /**
67: * Tests to make sure that a failure is NOT reported when a null
68: * value is used and it's okay.
69: */
70: public void testValidNull() {
71: validator.setAllowNull(true);
72: validator.validate(context, bean);
73: assertEquals(0, context.getFailureCount());
74: } //end testValidNull()
75:
76: /**
77: * Tests to make sure that an invalid regex reports a failure.
78: */
79: public void testBadRegEx() {
80: validator
81: .setRegex("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$");
82: bean.setValue("something@somewhere@com");
83: validator.validate(context, bean);
84: assertEquals(1, context.getFailureCount());
85: assertEquals("regex failure", ((ValidationFailure) context
86: .getFailures().get(0)).getMessage());
87: } //end testBadRegEx()
88:
89: /**
90: * Tests to make sure that a valid regex doesn't report a failure.
91: */
92: public void testGoodRegEx() {
93: validator
94: .setRegex("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$");
95: bean.setValue("something_somewhere.here@somewhere.SomeHow.com");
96: validator.validate(context, bean);
97: assertEquals(0, context.getFailureCount());
98: } //end testGoodRegEx()
99: } //end RegularExpressionValidatorTest
|