    1   /*
    2    * Copyright 2002-2006 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.jexl;
    17
    18  import java.io.File;
    19  import java.net.URL;
    20
    21  import junit.framework.TestCase;
    22
    23  /***
    24   * Tests for Script
    25   * @since 1.1
    26   */
    27  public class ScriptTest extends TestCase {
    28
    29      /***
    30       * Create a new test case.
    31       * @param name case name
    32       */
    33      public ScriptTest(String name) {
    34          super(name);
    35      }
    36
    37      /***
    38       * Test creating a script from a string.
    39       */
    40      public void testSimpleScript() throws Exception {
    41          String code = "while (x < 10) x = x + 1;";
    42          Script s = ScriptFactory.createScript(code);
    43          JexlContext jc = JexlHelper.createContext();
    44          jc.getVars().put("x", new Integer(1));
    45
    46          Object o = s.execute(jc);
    47          assertEquals("Result is wrong", new Long(10), o);
    48          assertEquals("getText is wrong", code, s.getText());
    49      }
    50
    51      public void testScriptFromFile() throws Exception {
    52          File testScript = new File("src/test-scripts/test1.jexl");
    53          Script s = ScriptFactory.createScript(testScript);
    54          JexlContext jc = JexlHelper.createContext();
    55          jc.getVars().put("out", System.out);
    56          Object result = s.execute(jc);
    57          assertNotNull("No result", result);
    58          assertEquals("Wrong result", new Long(7), result);
    59      }
    60
    61      public void testScriptFromURL() throws Exception {
    62          URL testUrl = new File("src/test-scripts/test1.jexl").toURL();
    63          Script s = ScriptFactory.createScript(testUrl);
    64          JexlContext jc = JexlHelper.createContext();
    65          jc.getVars().put("out", System.out);
    66          Object result = s.execute(jc);
    67          assertNotNull("No result", result);
    68          assertEquals("Wrong result", new Long(7), result);
    69      }
    70  }