1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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