001: /*
002: * Copyright 2006 Dan Shellman
003: *
004: * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.iscreen.ognl;
017:
018: import java.util.Locale;
019: import junit.framework.TestCase;
020: import org.iscreen.SimpleBean;
021: import org.iscreen.ValidationFailure;
022: import org.iscreen.impl.*;
023: import org.iscreen.impl.DefaultValidatorContext;
024: import org.iscreen.impl.ContextBean;
025:
026: /**
027: *
028: * @author Shellman, Dan
029: */
030: public class OgnlConfiguredValidatorTest extends TestCase {
031: /**
032: * Default constructor.
033: */
034: public OgnlConfiguredValidatorTest(String name) {
035: super (name);
036: } //end OgnlConfiguredValidatorTest()
037:
038: /**
039: * Test to ensure that the Validator is configured properly.
040: */
041: public void testValidatorConfiguration() {
042: OgnlConfiguredValidator wrapper;
043: MockValidator validator;
044:
045: wrapper = new OgnlConfiguredValidator();
046: wrapper.setClassName("org.iscreen.impl.MockValidator");
047: wrapper.addStaticProperty("testConstraint", "constraint");
048: wrapper.addStaticProperty("testService", "service");
049:
050: validator = (MockValidator) wrapper.getConfiguredValidator();
051: assertEquals("constraint", validator.getTestConstraint());
052: assertEquals("service", validator.getTestService());
053: } //end testValidatorConfiguration()
054:
055: /**
056: * Test to ensure that the mapping is done properly.
057: */
058: public void testMapping() {
059: OgnlConfiguredValidator wrapper;
060: MockValidator validator;
061: DefaultValidatorContext context;
062: ContextBean root;
063: SimpleBean bean = new SimpleBean();
064: SimpleBean obj = new SimpleBean();
065:
066: wrapper = new OgnlConfiguredValidator();
067: wrapper.setClassName("org.iscreen.impl.MockValidator");
068: wrapper.addMapping("value", "value");
069:
070: validator = (MockValidator) wrapper.getConfiguredValidator();
071: validator.defineBeanToValidate(bean);
072:
073: root = new ContextBean();
074: context = new DefaultValidatorContext(root, Locale.getDefault());
075: obj.setValue("foo");
076: wrapper.validate(context, root, obj);
077:
078: assertEquals("foo", bean.getValue());
079: } //end testMapping()
080:
081: /**
082: * Test to ensure that the fields are set on the root properly.
083: */
084: public void testFieldsSetOnRoot() {
085: OgnlConfiguredValidator wrapper;
086: MockValidator validator;
087: DefaultValidatorContext context;
088: ContextBean root;
089: SimpleBean bean = new SimpleBean();
090: SimpleBean obj = new SimpleBean();
091:
092: wrapper = new OgnlConfiguredValidator();
093: wrapper.setClassName("org.iscreen.impl.MockValidator");
094: wrapper.addStaticProperty("testMessage", new OgnlMessage(
095: "error"));
096: wrapper.addMapping("value", "value");
097:
098: validator = (MockValidator) wrapper.getConfiguredValidator();
099: validator.defineBeanToValidate(bean);
100: validator.setReportFailureFlag(true);
101:
102: root = new ContextBean();
103: context = new DefaultValidatorContext(root, Locale.getDefault());
104: obj.setValue("foo");
105: wrapper.validate(context, root, obj);
106:
107: assertEquals("value", root.getFields().iterator().next());
108: assertEquals("value", ((ValidationFailure) context
109: .getFailures().get(0)).getFields().iterator().next());
110: } //end testFieldsSetOnRoot()
111:
112: /**
113: * Test to ensure that the fail fast flag is handled properly.
114: */
115: public void testFailFast() {
116: OgnlConfiguredValidator wrapper;
117: MockValidator validator;
118: DefaultValidatorContext context;
119: ContextBean root;
120: SimpleBean obj = new SimpleBean();
121:
122: wrapper = new OgnlConfiguredValidator();
123: wrapper.setClassName("org.iscreen.impl.MockValidator");
124: wrapper.addStaticProperty("testMessage", new OgnlMessage(
125: "error"));
126: wrapper.setFailFast(true);
127:
128: validator = (MockValidator) wrapper.getConfiguredValidator();
129: validator.setReportFailureFlag(true);
130:
131: root = new ContextBean();
132: context = new DefaultValidatorContext(root, Locale.getDefault());
133: assertTrue(!wrapper.validate(context, root, obj));
134: } //end testFailFast()
135:
136: /**
137: * Test to ensure that the label is set on the root properly.
138: */
139: public void testLabelSetOnRoot() {
140: OgnlConfiguredValidator wrapper;
141: DefaultValidatorContext context;
142: ContextBean root;
143: SimpleBean obj = new SimpleBean();
144:
145: wrapper = new OgnlConfiguredValidator();
146: wrapper.setClassName("org.iscreen.impl.MockValidator");
147: wrapper.setLabel("some label");
148:
149: root = new ContextBean();
150: context = new DefaultValidatorContext(root, Locale.getDefault());
151: wrapper.validate(context, root, obj);
152: assertEquals("some label", root.getLabel());
153: } //end testLabelSetOnRoot()
154:
155: /**
156: * Test to ensure that the Validator is set on the root properly.
157: */
158: public void testValidatorSetOnRoot() {
159: OgnlConfiguredValidator wrapper;
160: MockValidator validator;
161: DefaultValidatorContext context;
162: ContextBean root;
163: SimpleBean obj = new SimpleBean();
164:
165: wrapper = new OgnlConfiguredValidator();
166: wrapper.setClassName("org.iscreen.impl.MockValidator");
167: wrapper.addStaticProperty("testConstraint", "some constraint");
168: wrapper.addStaticProperty("testMessage", new OgnlMessage(
169: "test message with ${validator.testConstraint}"));
170:
171: validator = (MockValidator) wrapper.getConfiguredValidator();
172: validator.setReportFailureFlag(true);
173:
174: root = new ContextBean();
175: context = new DefaultValidatorContext(root, Locale.getDefault());
176: wrapper.validate(context, root, obj);
177: assertEquals("test message with some constraint",
178: ((ValidationFailure) context.getFailures().get(0))
179: .getMessage());
180: } //end testValidatorSetOnRoot()
181: } //end OgnlConfiguredValidatorTest
|