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