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 NullValidator validator.
30: *
31: * @author Shellman, Dan
32: */
33: public class NullValidatorTest extends TestCase {
34: private NullValidator validator;
35: private DefaultValidatorContext context;
36: private SimpleBean bean;
37: private ContextBean root;
38:
39: /**
40: * Default constructor.
41: */
42: public NullValidatorTest(String name) {
43: super (name);
44: } //end NullValidatorTest()
45:
46: protected void setUp() {
47: validator = new NullValidator();
48: validator.setDefaultFailure(new OgnlMessage("null value"));
49: bean = new SimpleBean();
50: root = new ContextBean();
51: context = new DefaultValidatorContext(root, Locale.getDefault());
52: } //end setUp()
53:
54: /**
55: * Tests to ensure that a null value gets a failure reported.
56: */
57: public void testNullCheck() {
58: bean.setValue(null);
59: validator.validate(context, bean);
60: assertEquals(1, context.getFailureCount());
61: assertEquals("null value", ((ValidationFailure) context
62: .getFailures().get(0)).getMessage());
63: } //end testNullCheck()
64:
65: /**
66: * Tests to ensure that a non-null value doesn't get a failure reported.
67: */
68: public void testNonNullCheck() {
69: bean.setValue("");
70: validator.validate(context, bean);
71: assertEquals(0, context.getFailureCount());
72: } //end testNonNullCheck()
73: } //end NullValidatorTest
|