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.ognl;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Locale;
022: import junit.framework.TestCase;
023: import org.iscreen.*;
024: import org.iscreen.impl.DefaultValidationService;
025:
026: /**
027: * This set of unit tests are designed to test integration of
028: * the XML configuration together with the underlying object model.
029: *
030: * @author Shellman, Dan
031: */
032: public class OgnlIntegrationTest extends TestCase {
033: public OgnlIntegrationTest(String name) {
034: super (name);
035: } //end OgnlIntegrationTest()
036:
037: /**
038: * Tests the use of a simple validation.
039: */
040: public void testSimpleStringValidation() {
041: ValidationFactory factory;
042: ValidationService service;
043:
044: factory = ValidationFactory.buildFactory(
045: ValidationFactory.FACTORY_OGNL_XML,
046: "org/iscreen/ognl/integration_test.xml", new HashMap());
047: service = factory
048: .getValidationService("org.iscreen.test.simple_string");
049: assertNotNull(service);
050:
051: try {
052: service.validate("this is way too long");
053: fail("Expected validation failure!");
054: } catch (ValidationException e) {
055: }
056: } //end testSimpleStringValidation()
057:
058: /**
059: * Tests to verify that if there's no mapping, the default mapping is used.
060: */
061: public void testNoMapping() {
062: ValidationFactory factory;
063: ValidationService service;
064:
065: factory = ValidationFactory.buildFactory(
066: ValidationFactory.FACTORY_OGNL_XML,
067: "org/iscreen/ognl/integration_test.xml", new HashMap());
068: service = factory
069: .getValidationService("org.iscreen.test.no_mapping");
070: assertNotNull(service);
071:
072: try {
073: service.validate(null);
074: fail("Expected validation failure!");
075: } catch (ValidationException e) {
076: }
077: } //end testNoMapping()
078:
079: /**
080: * Tests the fail fast feature on a validation set.
081: */
082: public void testFailFastOnSet() {
083: ValidationFactory factory;
084: ValidationService service;
085:
086: factory = ValidationFactory.buildFactory(
087: ValidationFactory.FACTORY_OGNL_XML,
088: "org/iscreen/ognl/integration_test.xml", new HashMap());
089: service = factory
090: .getValidationService("org.iscreen.test.fail_fast");
091: assertNotNull(service);
092:
093: try {
094: service.validate("too short and too long");
095: fail("Expected validation failure!");
096: } catch (ValidationException e) {
097: assertEquals("Expected only one validation failure.", 1, e
098: .getFailures().size());
099: }
100: } //end testFailFastOnSet()
101:
102: /**
103: * Tests the default values on the mapping element for a use-validator.
104: */
105: public void testDefaultMappingsOnSet() {
106: ValidationFactory factory;
107: ValidationService service;
108:
109: factory = ValidationFactory.buildFactory(
110: ValidationFactory.FACTORY_OGNL_XML,
111: "org/iscreen/ognl/integration_test.xml", new HashMap());
112: service = factory
113: .getValidationService("org.iscreen.test.default_mapping");
114: assertNotNull(service);
115:
116: try {
117: service.validate("this is way too long");
118: fail("Expected validation failure!");
119: } catch (ValidationException e) {
120: }
121: } //end testDefaultMappingsOnSet()
122:
123: /**
124: * Tests the setting of a service on a validator.
125: */
126: public void testSettingService() {
127: ValidationFactory factory;
128: ValidationService service;
129: HashMap serviceMap = new HashMap();
130: List wrappers;
131: OgnlConfiguredValidator validatorWrapper;
132: MockValidator validator;
133:
134: serviceMap.put("mockService", "the service");
135: factory = ValidationFactory.buildFactory(
136: ValidationFactory.FACTORY_OGNL_XML,
137: "org/iscreen/ognl/integration_test.xml", serviceMap);
138: service = factory
139: .getValidationService("org.iscreen.test.service_test");
140: assertNotNull(service);
141:
142: wrappers = ((DefaultValidationService) service)
143: .getAllWrappers();
144: validatorWrapper = (OgnlConfiguredValidator) wrappers.get(0);
145: validator = (MockValidator) validatorWrapper
146: .getConfiguredValidator();
147:
148: assertEquals("The service was not set properly.",
149: "the service", validator.getTestService());
150: } //end testSettingService()
151:
152: /**
153: * Tests the setting of failure objects and their proper usage.
154: */
155: public void testSettingFailure() {
156: ValidationFactory factory;
157: ValidationService service;
158:
159: factory = ValidationFactory.buildFactory(
160: ValidationFactory.FACTORY_OGNL_XML,
161: "org/iscreen/ognl/integration_test.xml", new HashMap());
162: service = factory
163: .getValidationService("org.iscreen.test.failure_test");
164: assertNotNull(service);
165:
166: try {
167: service.validate("this is way too long");
168: fail("Expected validation failure!");
169: } catch (ValidationException e) {
170: String failure;
171:
172: failure = ((ValidationFailure) e.getFailures().get(0))
173: .getMessage();
174: assertEquals(
175: "Failure message is invalid.",
176: "Failure with label My Test and max characters of 10.",
177: failure);
178: }
179: } //end testSettingFailure()
180:
181: /**
182: * Tests the usage of an embedded validation set within a set. This
183: * test will not use any 'if', 'iterate', or 'mapping' settings.
184: */
185: public void testIncludeValidationSet() {
186: ValidationFactory factory;
187: ValidationService service;
188:
189: factory = ValidationFactory.buildFactory(
190: ValidationFactory.FACTORY_OGNL_XML,
191: "org/iscreen/ognl/integration_test.xml", new HashMap());
192: service = factory
193: .getValidationService("org.iscreen.test.include_set_test");
194: assertNotNull(service);
195:
196: try {
197: service.validate("this is way too long");
198: fail("Expected validation failure!");
199: } catch (ValidationException e) {
200: }
201: } //end testIncludeValidationSet()
202:
203: /**
204: * Tests the usage of an embedded validation set within a set. This
205: * test will use the 'iterate' setting.
206: */
207: public void testIterateValidationSet() {
208: ValidationFactory factory;
209: ValidationService service;
210: List strings = new ArrayList();
211:
212: factory = ValidationFactory.buildFactory(
213: ValidationFactory.FACTORY_OGNL_XML,
214: "org/iscreen/ognl/integration_test.xml", new HashMap());
215: service = factory
216: .getValidationService("org.iscreen.test.set_iterate_test");
217: assertNotNull(service);
218:
219: strings.add("this is too long 1");
220: strings.add("this is too long 2");
221: strings.add("this is too long 3");
222: strings.add("this is too long 4");
223:
224: try {
225: service.validate(strings);
226: fail("Expected validation failure!");
227: } catch (ValidationException e) {
228: assertEquals(4, e.getFailureMessages().size());
229: }
230: } //end testIterateValidationSet()
231:
232: /**
233: * Tests the usage of an embedded validation set within a set. This
234: * test will use the 'if' setting.
235: */
236: public void testIfValidationSet() {
237: ValidationFactory factory;
238: ValidationService service;
239: MockBean bean = new MockBean();
240:
241: factory = ValidationFactory.buildFactory(
242: ValidationFactory.FACTORY_OGNL_XML,
243: "org/iscreen/ognl/integration_test.xml", new HashMap());
244: service = factory
245: .getValidationService("org.iscreen.test.set_if_test");
246: assertNotNull(service);
247:
248: bean.setSomeFlag(false);
249: bean.setSomeString("this is way too long");
250:
251: try {
252: service.validate(bean);
253: } catch (ValidationException e) {
254: fail("Expected no validation failures!");
255: }
256: } //end testIfValidationSet()
257:
258: /**
259: * Tests the usage of an embedded validation set within a set. This
260: * test will use the 'map' setting.
261: */
262: public void testMapValidationSet() {
263: ValidationFactory factory;
264: ValidationService service;
265: MockBean bean = new MockBean();
266:
267: factory = ValidationFactory.buildFactory(
268: ValidationFactory.FACTORY_OGNL_XML,
269: "org/iscreen/ognl/integration_test.xml", new HashMap());
270: service = factory
271: .getValidationService("org.iscreen.test.set_map_test");
272: assertNotNull(service);
273:
274: bean.setSomeFlag(true);
275: bean.setSomeString("this is way too long");
276:
277: try {
278: service.validate(bean);
279: fail("Expected validation failure!");
280: } catch (ValidationException e) {
281: }
282: } //end testMapValidationSet()
283:
284: /**
285: * Tests the usage of an embedded validation set within a set. This
286: * test will use the 'if', 'iterate', and 'map' settings.
287: */
288: public void testIfIterateMapValidationSet() {
289: ValidationFactory factory;
290: ValidationService service;
291: List strings = new ArrayList();
292: MockBean bean = new MockBean();
293:
294: factory = ValidationFactory.buildFactory(
295: ValidationFactory.FACTORY_OGNL_XML,
296: "org/iscreen/ognl/integration_test.xml", new HashMap());
297: service = factory
298: .getValidationService("org.iscreen.test.set_if_it_map_test");
299: assertNotNull(service);
300:
301: strings.add("this is way too long 1");
302: strings.add("short");
303: strings.add("very, very, very, very long");
304:
305: bean.setSomeFlag(true);
306: bean.setSomeString("this is way too long");
307: bean.setSomeObject(strings);
308:
309: try {
310: service.validate(bean);
311: fail("Expected validation failure!");
312: } catch (ValidationException e) {
313: assertEquals(2, e.getFailureMessages().size());
314: }
315: } //end testIfIterateMapValidationSet()
316:
317: /**
318: * Tests the usage of an embedded validation set within a set. This
319: * test will use the 'fail-fast' setting.
320: */
321: public void testFailFastOnValidationSet() {
322: ValidationFactory factory;
323: ValidationService service;
324: List strings = new ArrayList();
325:
326: factory = ValidationFactory.buildFactory(
327: ValidationFactory.FACTORY_OGNL_XML,
328: "org/iscreen/ognl/integration_test.xml", new HashMap());
329: service = factory
330: .getValidationService("org.iscreen.test.set_failfast_test");
331: assertNotNull(service);
332:
333: strings.add("this is too long 1");
334: strings.add("this is too long 2");
335: strings.add("this is too long 3");
336: strings.add("this is too long 4");
337:
338: try {
339: service.validate(strings);
340: fail("Expected validation failure!");
341: } catch (ValidationException e) {
342: assertEquals(4, e.getFailureMessages().size());
343: }
344: } //end testFailFastOnValidationSet()
345:
346: /**
347: * Tests the usage of an embedded validation set within a set. This
348: * test will use the 'fail-fast' setting within a validator that's within
349: * an embedded set.
350: */
351: public void testFailFastOnEmbeddedValidator() {
352: ValidationFactory factory;
353: ValidationService service;
354: MockBean bean = new MockBean();
355:
356: factory = ValidationFactory.buildFactory(
357: ValidationFactory.FACTORY_OGNL_XML,
358: "org/iscreen/ognl/integration_test.xml", new HashMap());
359: service = factory
360: .getValidationService("org.iscreen.test.set_embedded_failfast_test");
361: assertNotNull(service);
362:
363: bean.setSomeString("this is way too long");
364:
365: try {
366: service.validate(bean);
367: fail("Expected validation failure!");
368: } catch (ValidationException e) {
369: assertEquals(2, e.getFailureMessages().size());
370: }
371: } //end testFailFastOnEmbeddedValidator()
372:
373: /**
374: * Tests to ensure that the failure message is reported correctly (that is,
375: * that the text of the message is correct).
376: */
377: public void testFailureMessage() {
378: ValidationFactory factory;
379: ValidationService service;
380:
381: factory = ValidationFactory.buildFactory(
382: ValidationFactory.FACTORY_OGNL_XML,
383: "org/iscreen/ognl/integration_test.xml", new HashMap());
384: service = factory
385: .getValidationService("org.iscreen.test.simple_string");
386: assertNotNull(service);
387:
388: try {
389: service.validate("this is way too long");
390: fail("Expected validation failure!");
391: } catch (ValidationException e) {
392: assertEquals(
393: "My Test must not be greater than 10 characters long.",
394: e.getFailureMessages().get(0));
395: }
396: } //end testFailureMessage()
397:
398: /**
399: * Tests to ensure that the failure message has the appropriate name.
400: */
401: public void testFailureMessageName() {
402: ValidationFactory factory;
403: ValidationService service;
404:
405: factory = ValidationFactory.buildFactory(
406: ValidationFactory.FACTORY_OGNL_XML,
407: "org/iscreen/ognl/integration_test.xml", new HashMap());
408: service = factory
409: .getValidationService("org.iscreen.test.simple_string");
410: assertNotNull(service);
411:
412: try {
413: service.validate("this is way too long");
414: fail("Expected validation failure!");
415: } catch (ValidationException e) {
416: assertEquals("testField", ((ValidationFailure) e
417: .getFailures().get(0)).getName());
418: }
419: } //end testFailureMessageName()
420:
421: /**
422: * Tests to ensure that the failure message has the appropriate name.
423: */
424: public void testFailureMessageNameFromSet() {
425: ValidationFactory factory;
426: ValidationService service;
427:
428: factory = ValidationFactory.buildFactory(
429: ValidationFactory.FACTORY_OGNL_XML,
430: "org/iscreen/ognl/integration_test.xml", new HashMap());
431: service = factory
432: .getValidationService("org.iscreen.test.set_call");
433: assertNotNull(service);
434:
435: try {
436: service.validate("this is way too long");
437: fail("Expected validation failure!");
438: } catch (ValidationException e) {
439: assertEquals("validationField", ((ValidationFailure) e
440: .getFailures().get(0)).getName());
441: }
442: } //end testFailureMessageNameFromSet()
443:
444: /**
445: * Tests to ensure that the failure message has the appropriate name.
446: */
447: public void testFailureMessageNameFromSetNoName() {
448: ValidationFactory factory;
449: ValidationService service;
450:
451: factory = ValidationFactory.buildFactory(
452: ValidationFactory.FACTORY_OGNL_XML,
453: "org/iscreen/ognl/integration_test.xml", new HashMap());
454: service = factory
455: .getValidationService("org.iscreen.test.set_call_no_name");
456: assertNotNull(service);
457:
458: try {
459: service.validate("this is way too long");
460: fail("Expected validation failure!");
461: } catch (ValidationException e) {
462: assertEquals("testField", ((ValidationFailure) e
463: .getFailures().get(0)).getName());
464: }
465: } //end testFailureMessageNameFromSetNoName()
466:
467: /**
468: * Tests the email address validator to ensure that it catches and reports
469: * failures properly.
470: */
471: public void testEmailAddressValidatorFailure() {
472: ValidationFactoryConfig factory;
473: ValidationService service;
474:
475: factory = new ValidationFactoryConfig(
476: "org/iscreen/ognl/integration_test.xml");
477: service = new ValidationServiceWrapper(factory,
478: "org.iscreen.test.email_validation");
479:
480: try {
481: service.validate("this.is.an_invalid@email.address@com");
482: fail("Expected validation failure!");
483: } catch (ValidationException e) {
484: assertEquals(
485: "The address this.is.an_invalid@email.address@com given for the label is not in a valid email address format.",
486: e.getFailureMessages().get(0));
487: }
488: } //end testEmailAddressValidatorFailure()
489:
490: /**
491: * Tests the email address validator to ensure that a valid email address
492: * is accepted and not reported.
493: */
494: public void testEmailAddressValidatorSuccess() {
495: ValidationFactoryConfig factory;
496: ValidationService service;
497:
498: factory = new ValidationFactoryConfig(
499: "org/iscreen/ognl/integration_test.xml");
500: service = new ValidationServiceWrapper(factory,
501: "org.iscreen.test.email_validation");
502:
503: try {
504: service.validate("this.is.an_invalid@email.address.com");
505: } catch (ValidationException e) {
506: fail("No failure should have been reported: "
507: + e.getFailureMessages().get(0));
508: }
509: } //end testEmailAddressValidatorSuccess()
510:
511: /**
512: * Tests to ensure that a mapping is inherited from a configured Validator
513: * to a validator reference within a validation set.
514: */
515: public void testMappingInheritance() {
516: ValidationFactoryConfig factory;
517: ValidationService service;
518: MockBean bean = new MockBean();
519:
520: factory = new ValidationFactoryConfig(
521: "org/iscreen/ognl/integration_test.xml");
522: service = new ValidationServiceWrapper(factory,
523: "org.iscreen.test.inherited_mapping");
524:
525: bean.setSomeString("this is valid");
526:
527: try {
528: service.validate(bean);
529: } catch (ValidationException e) {
530: fail("No failure should have been reported: "
531: + e.getFailureMessages().get(0));
532: }
533: } //end testMappingInheritance()
534:
535: /**
536: * Tests the use of internationalization in failure message and the locale
537: * provided during call to the validate() method.
538: */
539: public void testLocalizedFailureMessage() {
540: ValidationFactoryConfig factory;
541: ValidationService service;
542:
543: factory = new ValidationFactoryConfig(
544: "org/iscreen/ognl/integration_test.xml");
545: service = new ValidationServiceWrapper(factory,
546: "org.iscreen.test.international_test");
547:
548: try {
549: service.validate(null, Locale.FRENCH);
550: fail("Failure should have been reported.");
551: } catch (ValidationException e) {
552: assertEquals("French Test Value 2", e.getFailureMessages()
553: .get(0));
554: }
555: } //end testLocalizedFailureMessage()
556:
557: /**
558: * Tests the use of internationalization in failure message and the locale
559: * provided during call to the validate() method.
560: */
561: public void testLocalizedFailureMessageWithFactory() {
562: ValidationFactoryConfig factory;
563: ValidationService service;
564:
565: factory = new ValidationFactoryConfig(
566: "org/iscreen/ognl/integration_test.xml");
567: factory.setDefaultLocale(Locale.FRENCH);
568: service = new ValidationServiceWrapper(factory,
569: "org.iscreen.test.international_test");
570:
571: try {
572: service.validate(null);
573: fail("Failure should have been reported.");
574: } catch (ValidationException e) {
575: assertEquals("French Test Value 2", e.getFailureMessages()
576: .get(0));
577: }
578: } //end testLocalizedFailureMessageWithFactory()
579: } //end OgnlIntegrationTest
|