summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-stable-20060304/sca/container.js/src/test/java/org/apache/tuscany/container/js/rhino/RhinoInvokerTestCase.java
blob: 9269c1cf7e4d5b314102f5225e12b2e6c03782f1 (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
/*
 * 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.js.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.mozilla.javascript.EcmaError;

/**
 * Tests for the RhinoInvoker
 */
public class RhinoInvokerTestCase extends TestCase {

    private static final String scriptName = "RhinoInvokerTestCase.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", "petra", null);
        assertEquals("petra", x);
    }

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

        ri = ri.copy();
        x = ri.invoke("echo", "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", "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", "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 invocation scope)
     */
    public void testScopes3() {
        RhinoScript ri = new RhinoScript(scriptName, script);
        ri.invoke("setGlobalVarY", "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", "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);
        }
    }

}