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.impl;
017:
018: import java.util.Locale;
019:
020: import junit.framework.TestCase;
021:
022: import org.iscreen.ValidationException;
023: import org.iscreen.impl.DefaultValidationService;
024:
025: /**
026: *
027: * @author Shellman, Dan
028: */
029: public class DefaultValidationServiceTest extends TestCase {
030: /**
031: * Default constructor.
032: */
033: public DefaultValidationServiceTest(String name) {
034: super (name);
035: } //end DefaultValidationServiceTest()
036:
037: /**
038: * Tests the ensure that all validators are called.
039: */
040: public void testCallingValidators() throws Exception {
041: DefaultValidationService service;
042: MockValidatorWrapper wrapper1;
043: MockValidatorWrapper wrapper2;
044:
045: service = new DefaultValidationService("some id", Locale
046: .getDefault());
047: wrapper1 = new MockValidatorWrapper(false, false);
048: wrapper2 = new MockValidatorWrapper(false, false);
049: service.addValidatorWrapper(wrapper1);
050: service.addValidatorWrapper(wrapper2);
051:
052: service.validate("foo");
053:
054: assertTrue(wrapper1.wasValidateCalled());
055: assertTrue(wrapper2.wasValidateCalled());
056: } //end testCallingValidators()
057:
058: /**
059: * Tests to ensure that a fail-fast check is made.
060: */
061: public void testFailFastCheck() throws Exception {
062: DefaultValidationService service;
063: MockValidatorWrapper wrapper1;
064: MockValidatorWrapper wrapper2;
065:
066: service = new DefaultValidationService("some id", Locale
067: .getDefault());
068: wrapper1 = new MockValidatorWrapper(false, true);
069: wrapper2 = new MockValidatorWrapper(false, false);
070: service.addValidatorWrapper(wrapper1);
071: service.addValidatorWrapper(wrapper2);
072:
073: service.validate("foo");
074:
075: assertTrue(wrapper1.wasValidateCalled());
076: assertTrue(!wrapper2.wasValidateCalled());
077: } //end testFailFastCheck()
078:
079: /**
080: * Tests to ensure that an exception is thrown when a failure is found.
081: */
082: public void testThrowException() {
083: DefaultValidationService service;
084: MockValidatorWrapper wrapper1;
085: MockValidatorWrapper wrapper2;
086:
087: service = new DefaultValidationService("some id", Locale
088: .getDefault());
089: wrapper1 = new MockValidatorWrapper(true, false);
090: wrapper2 = new MockValidatorWrapper(false, false);
091: service.addValidatorWrapper(wrapper1);
092: service.addValidatorWrapper(wrapper2);
093:
094: try {
095: service.validate("foo");
096: fail("Expected a validation failure");
097: } catch (ValidationException e) {
098: }
099:
100: assertTrue(wrapper1.wasValidateCalled());
101: assertTrue(wrapper2.wasValidateCalled());
102: } //end testThrowException()
103: } //end DefaultValidationServiceTest
|