1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml.env.jsp;
18  
19  import java.net.URL;
20  
21  import javax.servlet.jsp.JspContext;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  import junit.textui.TestRunner;
27  
28  import org.apache.commons.scxml.SCXMLExecutor;
29  import org.apache.commons.scxml.SCXMLTestHelper;
30  
31  /***
32   * Unit tests {@link org.apache.commons.scxml.env.RootContext}.
33   */
34  public class RootContextTest extends TestCase {
35      /***
36       * Construct a new instance of ActionsTest with
37       * the specified name
38       */
39      public RootContextTest(String name) {
40          super(name);
41      }
42  
43      public static Test suite() {
44          TestSuite suite = new TestSuite(RootContextTest.class);
45          suite.setName("SCXML Env RootContext (wraps JSP Context) Tests");
46          return suite;
47      }
48  
49      // Test data
50      private URL rootCtxSample;
51      private ELEvaluator evaluator;
52      private JspContext jspCtx;
53      private RootContext rootCtx;
54      private SCXMLExecutor exec;
55  
56      /***
57       * Set up instance variables required by this test case.
58       */
59      public void setUp() {
60          rootCtxSample = this.getClass().getClassLoader().
61              getResource("org/apache/commons/scxml/env/jsp/jsp-rootctx-test.xml");
62          evaluator = new ELEvaluator();
63          jspCtx = new MockJspContext();
64          jspCtx.setAttribute("foo", "1");
65          rootCtx = new RootContext(jspCtx);
66      }
67  
68      /***
69       * Tear down instance variables required by this test case.
70       */
71      public void tearDown() {
72          rootCtxSample = null;
73          evaluator = null;
74          jspCtx = null;
75          rootCtx = null;
76          exec = null;
77      }
78  
79      /***
80       * Test the implementation
81       */
82      public void testRootContext() {
83          assertEquals("1", String.valueOf(rootCtx.get("foo")));
84          exec = SCXMLTestHelper.getExecutor(rootCtxSample, rootCtx, evaluator);
85          assertEquals("1", String.valueOf(jspCtx.getAttribute("foo")));
86          assertEquals("2", String.valueOf(rootCtx.get("foo")));
87          assertNull(jspCtx.getAttribute("bar"));
88          ELContext ctx = (ELContext) SCXMLTestHelper.lookupContext(exec,
89              "rootCtxTest");
90          assertNotNull(ctx);
91          assertNotNull(ctx.get("bar"));
92          try {
93              assertNull(jspCtx.getVariableResolver().resolveVariable("bar"));
94              assertNotNull(ctx.resolveVariable("bar"));
95              assertEquals(ctx.resolveVariable("bar"), "a brand new value");
96          } catch (Exception e) {
97              fail(e.getMessage());
98          }
99          assertNotNull(ctx.getVars());
100     }
101 
102      public static void main(String args[]) {
103         TestRunner.run(suite());
104     }
105 
106 }
107