001: /*
002: * Copyright 2006-2007 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.impl;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020: import java.util.Locale;
021:
022: import junit.framework.TestCase;
023:
024: import org.iscreen.MockBean;
025: import org.iscreen.ValidationFailure;
026: import org.iscreen.ognl.OgnlConfiguredValidator;
027: import org.iscreen.ognl.OgnlMessage;
028: import org.iscreen.ognl.OgnlValidationServiceValidator;
029:
030: /**
031: * Tests the ValidationServiceValidator.
032: *
033: * @author Shellman, Dan
034: */
035: public class ValidationServiceValidatorTest extends TestCase {
036: private DefaultValidationService wrappedService;
037: private OgnlConfiguredValidator configuredValidator;
038:
039: public ValidationServiceValidatorTest(String name) {
040: super (name);
041: } //end ValidationServiceValidatorTest()
042:
043: protected void setUp() {
044: configuredValidator = new OgnlConfiguredValidator();
045: configuredValidator
046: .setClassName("org.iscreen.impl.MockValidator");
047: configuredValidator.addStaticProperty("testMessage",
048: new OgnlMessage("error"));
049:
050: //Build wrapped service.
051: wrappedService = new DefaultValidationService("someId", Locale
052: .getDefault());
053: wrappedService.addValidatorWrapper(configuredValidator);
054: } //end setUp()
055:
056: /**
057: * Tests to ensure that a "true" if expression works properly.
058: */
059: public void testIfSuccess() {
060: OgnlValidationServiceValidator serviceValidator;
061: DefaultValidatorContext context;
062: ContextBean contextBean = new ContextBean();
063: MockBean bean = new MockBean();
064:
065: serviceValidator = new OgnlValidationServiceValidator(
066: wrappedService);
067: serviceValidator.setIfExpression("someFlag");
068:
069: bean.setSomeFlag(true);
070: contextBean.setBean(bean);
071: context = new DefaultValidatorContext(contextBean, Locale
072: .getDefault());
073: ((MockValidator) configuredValidator.getConfiguredValidator())
074: .setReportFailureFlag(true);
075:
076: serviceValidator.validate(context, contextBean, bean);
077: assertEquals(1, context.getFailureCount());
078: } //end testIfSuccess()
079:
080: /**
081: * Tests to ensure that a "false" if expression works properly.
082: */
083: public void testIfFailure() {
084: OgnlValidationServiceValidator serviceValidator;
085: DefaultValidatorContext context;
086: ContextBean contextBean = new ContextBean();
087: MockBean bean = new MockBean();
088:
089: serviceValidator = new OgnlValidationServiceValidator(
090: wrappedService);
091: serviceValidator.setIfExpression("someFlag");
092:
093: bean.setSomeFlag(false);
094: contextBean.setBean(bean);
095: context = new DefaultValidatorContext(contextBean, Locale
096: .getDefault());
097: ((MockValidator) configuredValidator.getConfiguredValidator())
098: .setReportFailureFlag(true);
099:
100: serviceValidator.validate(context, contextBean, bean);
101: assertEquals(0, context.getFailureCount());
102: } //end testIfFailure()
103:
104: /**
105: * Tests to ensure that if iterate is true, it works properly.
106: */
107: public void testIterate() {
108: OgnlValidationServiceValidator serviceValidator;
109: DefaultValidatorContext context;
110: ContextBean contextBean = new ContextBean();
111: List bean = new ArrayList();
112:
113: serviceValidator = new OgnlValidationServiceValidator(
114: wrappedService);
115: serviceValidator.setIterateExpression("true");
116:
117: bean.add("one");
118: bean.add("two");
119: bean.add("three");
120:
121: contextBean.setBean(bean);
122: context = new DefaultValidatorContext(contextBean, Locale
123: .getDefault());
124: ((MockValidator) configuredValidator.getConfiguredValidator())
125: .setReportFailureFlag(true);
126:
127: serviceValidator.validate(context, contextBean, bean);
128: assertEquals(3, context.getFailureCount());
129: } //end testIterate()
130:
131: /**
132: * Tests to ensure that if iterate is true, the indexing of the failures
133: * works properly.
134: */
135: public void testIterateIndexing() {
136: OgnlValidationServiceValidator serviceValidator;
137: DefaultValidatorContext context;
138: ContextBean contextBean = new ContextBean();
139: List bean = new ArrayList();
140:
141: serviceValidator = new OgnlValidationServiceValidator(
142: wrappedService);
143: serviceValidator.setIterateExpression("true");
144:
145: bean.add("one");
146: bean.add("two");
147: bean.add("three");
148:
149: contextBean.setBean(bean);
150: context = new DefaultValidatorContext(contextBean, Locale
151: .getDefault());
152: ((MockValidator) configuredValidator.getConfiguredValidator())
153: .setReportFailureFlag(true);
154:
155: serviceValidator.validate(context, contextBean, bean);
156: assertEquals(0, ((ValidationFailure) context.getFailures().get(
157: 0)).getIndex().intValue());
158: assertEquals(1, ((ValidationFailure) context.getFailures().get(
159: 1)).getIndex().intValue());
160: assertEquals(2, ((ValidationFailure) context.getFailures().get(
161: 2)).getIndex().intValue());
162: } //end testIterateIndexing()
163:
164: /**
165: * Tests the mapping of the object to validate prior to forwarding
166: * it (or a property) on to individual validators.
167: */
168: public void testMap() {
169: OgnlValidationServiceValidator serviceValidator;
170: DefaultValidatorContext context;
171: ContextBean contextBean = new ContextBean();
172: MockBean bean = new MockBean();
173: List list = new ArrayList();
174:
175: serviceValidator = new OgnlValidationServiceValidator(
176: wrappedService);
177: serviceValidator.setMapExpression("someObject");
178: serviceValidator.setIterateExpression("true");
179:
180: list.add("one");
181: list.add("two");
182:
183: bean.setSomeObject(list);
184: contextBean.setBean(bean);
185: context = new DefaultValidatorContext(contextBean, Locale
186: .getDefault());
187: ((MockValidator) configuredValidator.getConfiguredValidator())
188: .setReportFailureFlag(true);
189:
190: serviceValidator.validate(context, contextBean, bean);
191: assertEquals(2, context.getFailureCount());
192: } //end testMap()
193: } //end ValidationServiceValidatorTest
|