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.Locale;
019:
020: import junit.framework.TestCase;
021:
022: import org.iscreen.ValidationFailure;
023: import org.iscreen.ognl.OgnlMessage;
024:
025: /**
026: * Tests the DefaultValidatorContext class.
027: *
028: * @author Shellman, Dan
029: */
030: public class DefaultValidatorContextTest extends TestCase {
031: public DefaultValidatorContextTest(String name) {
032: super (name);
033: } //end DefaultValidatorContextTest()
034:
035: /**
036: * Tests the reporting of a failure.
037: */
038: public void testFailureReport() {
039: ContextBean root;
040: DefaultValidatorContext context;
041: OgnlMessage failureMessage;
042:
043: root = new ContextBean();
044: context = new DefaultValidatorContext(root, Locale.getDefault());
045:
046: failureMessage = new OgnlMessage("bogus template");
047: context.reportFailure(failureMessage);
048:
049: assertEquals("Invalid number of failures reported.", 1, context
050: .getFailures().size());
051: assertEquals("Invalid failure message.", "bogus template",
052: ((ValidationFailure) context.getFailures().get(0))
053: .getMessage());
054: } //end testFailureReport()
055:
056: /**
057: * Tests the use of a failure object when reporting a failure.
058: */
059: public void testFailureObject() {
060: ContextBean root;
061: DefaultValidatorContext context;
062: OgnlMessage failureMessage;
063:
064: root = new ContextBean();
065: context = new DefaultValidatorContext(root, Locale.getDefault());
066:
067: failureMessage = new OgnlMessage(
068: "The value of the object is ${failure}");
069: context.reportFailure(failureMessage, new Integer(3));
070:
071: assertEquals("Invalid number of failures reported.", 1, context
072: .getFailures().size());
073: assertEquals("Invalid failure message.",
074: "The value of the object is 3",
075: ((ValidationFailure) context.getFailures().get(0))
076: .getMessage());
077: } //end testFailureObject()
078:
079: /**
080: * Tests the reporting of multiple failures.
081: */
082: public void testMultipleReports() {
083: ContextBean root;
084: DefaultValidatorContext context;
085: OgnlMessage failureMessage;
086:
087: root = new ContextBean();
088: context = new DefaultValidatorContext(root, Locale.getDefault());
089:
090: failureMessage = new OgnlMessage("bogus template");
091: context.reportFailure(failureMessage);
092: failureMessage = new OgnlMessage("another bogus template");
093: context.reportFailure(failureMessage);
094:
095: assertEquals("Invalid number of failures reported.", 2, context
096: .getFailures().size());
097: assertEquals("Invalid failure message.", "bogus template",
098: ((ValidationFailure) context.getFailures().get(0))
099: .getMessage());
100: assertEquals("Invalid failure message.",
101: "another bogus template", ((ValidationFailure) context
102: .getFailures().get(1)).getMessage());
103: } //end testMultipleReports()
104: } //end DefaultValidatorContextTest
|