001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml.env.jsp;
018:
019: import java.net.URL;
020:
021: import javax.servlet.jsp.JspContext;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import junit.textui.TestRunner;
027:
028: import org.apache.commons.scxml.SCXMLExecutor;
029: import org.apache.commons.scxml.SCXMLTestHelper;
030:
031: /**
032: * Unit tests {@link org.apache.commons.scxml.env.RootContext}.
033: */
034: public class RootContextTest extends TestCase {
035: /**
036: * Construct a new instance of ActionsTest with
037: * the specified name
038: */
039: public RootContextTest(String name) {
040: super (name);
041: }
042:
043: public static Test suite() {
044: TestSuite suite = new TestSuite(RootContextTest.class);
045: suite
046: .setName("SCXML Env RootContext (wraps JSP Context) Tests");
047: return suite;
048: }
049:
050: // Test data
051: private URL rootCtxSample;
052: private ELEvaluator evaluator;
053: private JspContext jspCtx;
054: private RootContext rootCtx;
055: private SCXMLExecutor exec;
056:
057: /**
058: * Set up instance variables required by this test case.
059: */
060: public void setUp() {
061: rootCtxSample = this
062: .getClass()
063: .getClassLoader()
064: .getResource(
065: "org/apache/commons/scxml/env/jsp/jsp-rootctx-test.xml");
066: evaluator = new ELEvaluator();
067: jspCtx = new MockJspContext();
068: jspCtx.setAttribute("foo", "1");
069: rootCtx = new RootContext(jspCtx);
070: }
071:
072: /**
073: * Tear down instance variables required by this test case.
074: */
075: public void tearDown() {
076: rootCtxSample = null;
077: evaluator = null;
078: jspCtx = null;
079: rootCtx = null;
080: exec = null;
081: }
082:
083: /**
084: * Test the implementation
085: */
086: public void testRootContext() {
087: assertEquals("1", String.valueOf(rootCtx.get("foo")));
088: exec = SCXMLTestHelper.getExecutor(rootCtxSample, rootCtx,
089: evaluator);
090: assertEquals("1", String.valueOf(jspCtx.getAttribute("foo")));
091: assertEquals("2", String.valueOf(rootCtx.get("foo")));
092: assertNull(jspCtx.getAttribute("bar"));
093: ELContext ctx = (ELContext) SCXMLTestHelper.lookupContext(exec,
094: "rootCtxTest");
095: assertNotNull(ctx);
096: assertNotNull(ctx.get("bar"));
097: try {
098: assertNull(jspCtx.getVariableResolver().resolveVariable(
099: "bar"));
100: assertNotNull(ctx.resolveVariable("bar"));
101: assertEquals(ctx.resolveVariable("bar"),
102: "a brand new value");
103: } catch (Exception e) {
104: fail(e.getMessage());
105: }
106: assertNotNull(ctx.getVars());
107: }
108:
109: public static void main(String args[]) {
110: TestRunner.run(suite());
111: }
112:
113: }
|