summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/containers/container.rhino/src/test/java/org/apache/tuscany/container/rhino/rhino/RhinoScriptTestCase.java
blob: 19c0d9bcf6198b81f8c7b4eaa89f585ca4394fe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright 2004,2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tuscany.container.rhino.rhino;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.tuscany.container.rhino.rhino.RhinoScript;
import org.mozilla.javascript.EcmaError;

/**
 * Tests for the RhinoScript
 */
public class RhinoScriptTestCase extends TestCase {

    private static final String scriptName = "RhinoScriptTestCase.js";

    private String script;

    protected void setUp() throws Exception {
        super.setUp();
        this.script = readResource(scriptName);
    }

    public void testSimpleInvocation() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        Object x = ri.invoke("echo", new Object[] { "petra" }, null);
        assertEquals("petra", x);
    }

    public void testCopy() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        Object x = ri.invoke("echo", new Object[] { "petra" }, null);
        assertEquals("petra", x);

        ri = ri.copy();
        x = ri.invoke("echo", new Object[] { "sue" }, null);
        assertEquals("sue", x);

    }

    public void testContexts1() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        Map<String, Object> contexts = new HashMap<String, Object>();
        contexts.put("a", "petra");
        Object x = ri.invoke("getA", null, contexts);
        assertEquals("petra", x);
    }

    /**
     * Tests context not accessable across invocations
     */
    public void testContexts2() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        Map<String, Object> contexts = new HashMap<String, Object>();
        contexts.put("a", "petra");
        Object x = ri.invoke("getA", null, contexts);
        assertEquals("petra", x);

        try {
            x = ri.invoke("getA", null, null);
            assertTrue("expected ReferenceError", false);
        } catch (EcmaError e) {
            assertEquals("ReferenceError", e.getName());
        }
    }

    /**
     * Tests shared scope is accessable across invocations
     */
    public void testScopes1() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        ri.invoke("setGlobalVarY", new Object[] { "petra" }, null);

        Object x = ri.invoke("getGlobalVarY", null, null);
        assertEquals("petra", x);
    }

    /**
     * Tests local vars are NOT accessable across invocations
     */
    public void testScopes2() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        ri.invoke("setLocalVarY", new Object[] { "petra" }, null);

        try {
            ri.invoke("getGlobalVarY", null, null);
            assertTrue("expected ReferenceError", false);
        } catch (EcmaError e) {
            assertEquals("ReferenceError", e.getName());
        }
    }

    /**
     * Tests shared scope is accessable when using contexts (ie an wire scope)
     */
    public void testScopes3() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        ri.invoke("setGlobalVarY", new Object[] { "petra" }, null);

        Map<String, Object> contexts = new HashMap<String, Object>();
        contexts.put("a", "sue");
        Object x = ri.invoke("getGlobalVarY", null, contexts);
        assertEquals("petra", x);

        x = ri.invoke("getA", null, contexts);
        assertEquals("sue", x);

    }

    /**
     * Tests a copy only retains the script scope not the shared scope
     */
    public void testScopes4() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        ri.invoke("setGlobalVarY", new Object[] { "petra" }, null);

        ri = ri.copy();
        try {
            ri.invoke("getGlobalVarY", null, null);
            assertTrue("expected ReferenceError", false);
        } catch (EcmaError e) {
            assertEquals("ReferenceError", e.getName());
        }
        try {
            ri.invoke("getA", null, null);
            assertTrue("expected ReferenceError", false);
        } catch (EcmaError e) {
            assertEquals("ReferenceError", e.getName());
        }

    }

    public void testGetInt() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        Object x = ri.invoke("getInt", null, Integer.TYPE, null);
        assertEquals(Integer.class, x.getClass());
    }

    /**
     * Read a resource into a String
     */
    private String readResource(String name) {
        try {
            URL url = getClass().getResource(name);
            if (url == null) {
                throw new RuntimeException("resource not found: " + name);
            }
            InputStream inputStream = url.openStream();

            StringBuffer resource = new StringBuffer();
            int n = 0;

            while ((n = inputStream.read()) != -1) {
                resource.append((char) n);
            }

            inputStream.close();

            String s = resource.toString();
            return s;

        } catch (IOException e) {
            throw new RuntimeException("IOException reading resource " + name, e);
        }
    }

}