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