001: /*
002: * Copyright 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.ognl;
017:
018: import java.util.HashSet;
019: import java.util.Locale;
020: import java.util.Set;
021:
022: import junit.framework.*;
023:
024: import org.iscreen.MockValidator;
025: import org.iscreen.Validator;
026: import org.iscreen.impl.ConfiguredResource;
027: import org.iscreen.impl.DefaultValidationService;
028: import org.iscreen.impl.ValidationServiceValidator;
029: import org.iscreen.impl.xml.XmlConfigConstraint;
030: import org.iscreen.impl.xml.XmlConfigFailure;
031: import org.iscreen.impl.xml.XmlConfigLabel;
032: import org.iscreen.impl.xml.XmlConfigMessage;
033: import org.iscreen.validators.StringValidator;
034:
035: /**
036: * These unit tests check to ensure that the OgnlXmlServiceFactory's API
037: * to the parser are correct and generate the internal model of the
038: * validators and validations properly.
039: *
040: *
041: * @author Shellman, Dan
042: */
043: public class OgnlXmlServiceFactoryTest extends TestCase {
044: OgnlXmlServiceFactory factory;
045:
046: public OgnlXmlServiceFactoryTest(String name) {
047: super (name);
048: } //end OgnlXmlServiceFactoryTest()
049:
050: protected void setUp() throws Exception {
051: factory = new OgnlXmlServiceFactory();
052: } //end setUp()
053:
054: /**
055: * Tests the creation of a configured validator by registering one.
056: */
057: public void testCreatingConfiguredValidator() {
058: assertNotNull(registerMockValidator(null, null, null));
059: } //end testCreatingConfiguredValidator()
060:
061: /**
062: * Tests the override of a failure when doing a 'use-validator' that references
063: * an existing validator.
064: */
065: public void testFailureOverride() {
066: Set failures = new HashSet();
067: XmlConfigFailure failure;
068: OgnlConfiguredValidator configuredValidator;
069: Validator validator;
070:
071: failure = new XmlConfigFailure();
072: failure.setMessage("MyValidator's failure message");
073: failure.setProperty("testFailure");
074: failures.add(failure);
075:
076: //First, register a validator to be referenced later.
077: registerMockValidator(null, null, failures);
078:
079: //Setup the configuration for the "reference" to the validator.
080: failures.clear();
081: failure = new XmlConfigFailure();
082: failure.setMessage("Override message");
083: failure.setProperty("testFailure");
084: failures.add(failure);
085:
086: //Register a validation set, and then add a validator to it (that
087: //will reference the previously registered validator).
088: configuredValidator = registerSetAndValidator(null, null, null,
089: failures);
090: validator = configuredValidator.getConfiguredValidator();
091: assertEquals("Override message", ((MockValidator) validator)
092: .getTestFailure().getMessage(null, Locale.getDefault()));
093: } //end testFailureOverride()
094:
095: /**
096: * Tests the override of a constraint when doing a 'use-validator' that references
097: * an existing validator.
098: */
099: public void testConstraintOverride() {
100: Set constraints = new HashSet();
101: XmlConfigConstraint constraint;
102: OgnlConfiguredValidator configuredValidator;
103: Validator validator;
104:
105: constraint = new XmlConfigConstraint();
106: constraint.setValue("constraint value");
107: constraint.setProperty("testConstraint");
108: constraints.add(constraint);
109:
110: //First, register a validator to be referenced later.
111: registerMockValidator(null, constraints, null);
112:
113: //Setup the configuration for the "reference" to the validator.
114: constraints.clear();
115: constraint = new XmlConfigConstraint();
116: constraint.setValue("Override constraint");
117: constraint.setProperty("testConstraint");
118: constraints.add(constraint);
119:
120: //Register a validation set, and then add a validator to it (that
121: //will reference the previously registered validator).
122: configuredValidator = registerSetAndValidator(null, null,
123: constraints, null);
124: validator = configuredValidator.getConfiguredValidator();
125: assertEquals("Override constraint", ((MockValidator) validator)
126: .getTestConstraint());
127: } //end testConstraintOverride()
128:
129: /**
130: * Tests the override of a service when doing a 'use-validator' that references
131: * an existing validator.
132: */
133: public void testServiceOverride() {
134: Set services = new HashSet();
135: XmlConfigConstraint service;
136: OgnlConfiguredValidator configuredValidator;
137: Validator validator;
138:
139: factory.registerService("service1", "service 1");
140: factory.registerService("service2", "service 2");
141:
142: service = new XmlConfigConstraint();
143: service.setServiceId("service1");
144: service.setProperty("testService");
145: services.add(service);
146:
147: //First, register a validator to be referenced later.
148: registerMockValidator(null, services, null);
149:
150: //Setup the configuration for the "reference" to the validator.
151: services.clear();
152: service = new XmlConfigConstraint();
153: service.setServiceId("service2");
154: service.setProperty("testService");
155: services.add(service);
156:
157: //Register a validation set, and then add a validator to it (that
158: //will reference the previously registered validator).
159: configuredValidator = registerSetAndValidator(null, null,
160: services, null);
161: validator = configuredValidator.getConfiguredValidator();
162: assertEquals("service 2", ((MockValidator) validator)
163: .getTestService());
164: } //end testServiceOverride()
165:
166: /**
167: * Tests to ensure that a resource key can be overridden when a resource
168: * references another.
169: */
170: public void testResourceOverride() {
171: Set messages = new HashSet();
172: XmlConfigMessage msg;
173: ConfiguredResource resourceParent;
174: ConfiguredResource resourceChild;
175:
176: msg = new XmlConfigMessage();
177: msg.setKey("someKey");
178: msg.setValue("some value");
179: messages.add(msg);
180:
181: factory.registerResource("child", "parent", messages,
182: new HashSet());
183: resourceChild = factory.getResource("child");
184:
185: messages = new HashSet();
186: msg = new XmlConfigMessage();
187: msg.setKey("someKey");
188: msg.setValue("some other value");
189: messages.add(msg);
190: msg = new XmlConfigMessage();
191: msg.setKey("someOtherKey");
192: msg.setValue("some value");
193: messages.add(msg);
194: factory.registerResource("parent", null, messages,
195: new HashSet());
196:
197: assertEquals("some value", resourceChild.getValue("someKey",
198: Locale.getDefault()));
199: assertEquals("some value", resourceChild.getValue(
200: "someOtherKey", Locale.getDefault()));
201: } //end testResourceOverride()
202:
203: /**
204: * Tests the registration of resources.
205: */
206: public void testResourceRegistration() {
207: Set messages = new HashSet();
208: XmlConfigMessage msg;
209: ConfiguredResource resource;
210:
211: msg = new XmlConfigMessage();
212: msg.setKey("someKey");
213: msg.setValue("some value");
214: messages.add(msg);
215:
216: factory.registerResource("my_resource", null, messages,
217: new HashSet());
218: resource = factory.getResource("my_resource");
219: assertNotNull(resource);
220: assertEquals("some value", resource.getValue("someKey", Locale
221: .getDefault()));
222: } //end testResourceRegistration()
223:
224: /**
225: * Tests the adding of a validation set reference to a validation set.
226: */
227: public void testAddingSetToSet() {
228: factory.registerValidationSet("theSet");
229: factory.addValidationSetToSet("theSet", "someOtherSet", false,
230: null, null, null, null);
231: assertNotNull(((DefaultValidationService) factory
232: .getValidationService("theSet")).getAllWrappers()
233: .get(0));
234: assertTrue(((DefaultValidationService) factory
235: .getValidationService("theSet")).getAllWrappers()
236: .get(0) instanceof ValidationServiceValidator);
237: } //end testAddingSetToSet()
238:
239: /**
240: * Tests to ensure an exception is thrown when no class name or ref is
241: * given when a validator is registered.
242: */
243: public void testValidatorRegWithNoClassOrRef() {
244: try {
245: factory.registerValidator(null, "some id", null, null,
246: null, null, null, new HashSet(), new HashSet(),
247: new HashSet());
248: fail("Exception should have been thrown when no class name or ref given for validator registration.");
249: } catch (Exception e) {
250: }
251: } //end testValidatorRegWithNoClassOrRef()
252:
253: /**
254: * Tests to ensure that a validator with a class name overrides a referenced
255: * validator with its own class name.
256: */
257: public void testClassNameOverride() {
258: factory.registerValidator(null, "parent", null,
259: "org.iscreen.validators.StringValidator", null, null,
260: null, new HashSet(), new HashSet(), new HashSet());
261: factory.registerValidator(null, "child", "parent",
262: "org.iscreen.MockValidator", null, null, null,
263: new HashSet(), new HashSet(), new HashSet());
264: factory.registerValidator(null, "another child", "parent",
265: null, null, null, null, new HashSet(), new HashSet(),
266: new HashSet());
267:
268: assertTrue(factory.getValidator("child")
269: .getConfiguredValidator() instanceof MockValidator);
270: assertTrue(factory.getValidator("another child")
271: .getConfiguredValidator() instanceof StringValidator);
272: } //end testClassNameOverride()
273:
274: /**
275: * Test to ensure that creating a set within a set works properly.
276: */
277: public void testCreatingSetInSet() {
278: factory.registerValidationSet("some set");
279: factory.registerValidationSet("container set");
280: factory.addValidationSetToSet("container set", "some set",
281: false, null, null, null, null);
282:
283: assertTrue(((DefaultValidationService) factory
284: .getValidationService("container set"))
285: .getAllWrappers().get(0) instanceof OgnlValidationServiceValidator);
286: } //end testCreatingSetInSet()
287:
288: /**
289: * Tests to ensure that a validator's id is set on the OgnlConfiguredValidator.
290: */
291: public void testIdSetOnValidator() {
292: factory.registerValidator(null, "id1", null,
293: "org.iscreen.MockValidator", null, null, null,
294: new HashSet(), new HashSet(), new HashSet());
295: assertEquals("id1", factory.getValidator("id1").getId());
296: assertEquals("id2", factory.getValidator("id2").getId());
297: } //end testIdSetOnValidator()
298:
299: // ***
300: // Private methods
301: // ***
302:
303: private OgnlConfiguredValidator registerMockValidator(Set mappings,
304: Set constraints, Set failures) {
305: Set mappingSet = new HashSet();
306: Set constraintSet = new HashSet();
307: Set failureSet = new HashSet();
308:
309: if (mappings != null) {
310: mappingSet.addAll(mappings);
311: }
312:
313: if (constraints != null) {
314: constraintSet.addAll(constraints);
315: }
316:
317: if (failures != null) {
318: failureSet.addAll(failures);
319: }
320:
321: factory.registerValidator(null, "MockValidator", null,
322: "org.iscreen.MockValidator", null, null, null,
323: mappingSet, constraintSet, failureSet);
324:
325: return factory.getValidator("MockValidator");
326: } //end registerMockValidator()
327:
328: private OgnlConfiguredValidator registerSetAndValidator(
329: String label, Set mappings, Set constraints, Set failures) {
330: Set mappingSet = new HashSet();
331: Set constraintSet = new HashSet();
332: Set failureSet = new HashSet();
333: XmlConfigLabel configLabel;
334:
335: if (label == null) {
336: configLabel = null;
337: } else {
338: configLabel = new XmlConfigLabel();
339: configLabel.setValue(label);
340: }
341:
342: if (mappings != null) {
343: mappingSet.addAll(mappings);
344: }
345:
346: if (constraints != null) {
347: constraintSet.addAll(constraints);
348: }
349:
350: if (failures != null) {
351: failureSet.addAll(failures);
352: }
353:
354: factory.registerValidationSet("MockSet");
355: factory.addValidatorToSet("MockSet", null, null,
356: "MockValidator", false, null, configLabel, null,
357: mappingSet, constraintSet, failureSet);
358: return (OgnlConfiguredValidator) ((DefaultValidationService) factory
359: .getValidationService("MockSet")).getAllWrappers().get(
360: 0);
361: } //end registerSetAndValidator()
362: } //end OgnlXmlServiceFactoryTest
|