KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > scriptrunner > RhinoScriptRunnerTest


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.modules.scriptrunner;
14
15 import junit.framework.TestCase;
16
17 import com.openedit.BaseTestCase;
18 import com.openedit.WebPageRequest;
19
20
21 /**
22  * DOCUMENT ME!
23  *
24  * @author avery To change this generated comment edit the template variable "typecomment":
25  * Window>Preferences>Java>Templates.
26  */

27 public class RhinoScriptRunnerTest extends BaseTestCase
28 {
29     public static final Script SCRIPTS = new Script(
30             "function foo() { /* do nothing */ }" + "function returnArray()\n" + "{\n" +
31             " return new Array( 2, 'hi', true, null, new Array(), new Array( 'mom' ) );\n" +
32             "}", "The wonderful amazing script");
33     protected ScriptRunner fieldScriptRunner;
34
35     /**
36      * Constructor for RhinoScriptRunnerTest.
37      *
38      * @param arg0
39      */

40     public RhinoScriptRunnerTest(String JavaDoc arg0)
41     {
42         super(arg0);
43     }
44
45     /**
46      * DOCUMENT ME!
47      *
48      * @throws Exception
49      */

50     public void testExecFunction() throws Exception JavaDoc
51     {
52         fieldScriptRunner.init();
53
54         Object JavaDoc result = fieldScriptRunner.execFunction("returnArray");
55         assertNotNull(result);
56         assertTrue("returnArray() should have returned an Object[]", result instanceof Object JavaDoc[]);
57
58         Object JavaDoc[] array = (Object JavaDoc[]) result;
59         assertEquals("Array length", 6, array.length);
60         assertEquals(2, ((Number JavaDoc) array[0]).intValue());
61         assertEquals("hi", (String JavaDoc) array[1]);
62         assertEquals(true, ((Boolean JavaDoc) array[2]).booleanValue());
63         assertNull(array[3]);
64
65         Object JavaDoc[] innerArray = (Object JavaDoc[]) array[4];
66         assertEquals("First inner array length", 0, innerArray.length);
67
68         innerArray = (Object JavaDoc[]) array[5];
69         assertEquals("Second inner array length", 1, innerArray.length);
70         assertEquals("mom", (String JavaDoc) innerArray[0]);
71     }
72
73     /**
74      * @see TestCase#setUp()
75      */

76     protected void setUp() throws Exception JavaDoc
77     {
78         fieldScriptRunner = new RhinoScriptRunner(SCRIPTS);
79     }
80     
81     public void testRunModule() throws Exception JavaDoc
82     {
83         WebPageRequest req = getFixture().createPageRequest("/script/run.html");
84         getFixture().getEngine().executePageActions(req);
85         
86         ScriptLogger logger = (ScriptLogger)req.getPageValue("log");
87         assertEquals(logger.getLogs().size(), 1);
88         assertEquals(logger.getLogs().get(0),"debug: Hello World!");
89     }
90 }
91
Popular Tags