summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060522/java/sca/containers/container.rhino/src/test/java/org/apache/tuscany/container/rhino/rhino/RhinoE4XScriptTestCase.java
blob: 714814f2cf6dca486d9a151d5fd30dff55609e26 (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
/*
 * 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 javax.xml.namespace.QName;

import junit.framework.TestCase;

import org.apache.tuscany.sdo.helper.XSDHelperImpl;
import org.apache.tuscany.sdo.util.SDOUtil;

import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XSDHelper;

/**
 * Tests for the RhinoE4XScript
 */
public class RhinoE4XScriptTestCase extends TestCase {

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

    private String script;

    private E4XDataBinding dataBinding;

    protected void setUp() throws Exception {
        super.setUp();
        this.script = readResource(scriptName);
        TypeHelper th = SDOUtil.createTypeHelper();
        XSDHelper xsdHelper = new XSDHelperImpl(th);
        URL url = getClass().getResource("helloworld.wsdl");
        xsdHelper.define(url.openStream(), null);

        dataBinding = new E4XDataBinding(getClass().getClassLoader(),th);
        dataBinding.addElementQName("getGreetings", new QName("http://helloworld.samples.tuscany.apache.org", "getGreetings"));
    }

    public void testSimpleInvocation() throws IOException {
        RhinoE4XScript ri = new RhinoE4XScript(scriptName, script, null, null, dataBinding);
        Object x = ri.invoke("getGreetings", new Object[] { "petra" }, null);
        assertEquals(x, "hello petra");
    }

    /**
     * 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);
        }
    }

}