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