01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.bpmscript;
18:
19: import java.io.FileReader;
20:
21: import junit.framework.TestCase;
22:
23: import org.mozilla.javascript.Context;
24: import org.mozilla.javascript.Function;
25: import org.mozilla.javascript.JavaScriptException;
26: import org.mozilla.javascript.Script;
27: import org.mozilla.javascript.Scriptable;
28: import org.mozilla.javascript.ScriptableObject;
29:
30: public class E4XTest extends TestCase {
31: public void testE4X() throws Exception {
32: Context cx = Context.enter();
33: try {
34: // create scripting environment (i.e. load everything)
35: Scriptable scope = cx.initStandardObjects();
36: cx.setOptimizationLevel(-1);
37: Object wrappedOut = Context.javaToJS(System.out, scope);
38: ScriptableObject.putProperty(scope, "out", wrappedOut);
39: Script script = cx.compileReader(new FileReader(
40: "./src/test/java/org/bpmscript/e4xtest.js"),
41: "e4xtest.js", 0, null);
42: script.exec(cx, scope);
43:
44: // get the main function and execute it
45: Object object = scope.get("main", scope);
46: if (object instanceof Function) {
47: Function function = (Function) object;
48: Object result = null;
49: try {
50: result = function.call(cx, scope, scope,
51: new Object[] {});
52: System.out.println(result);
53: } catch (JavaScriptException ex) {
54: // if we have an exception and it's a continuation stash it away
55: ex.printStackTrace();
56: }
57: }
58: } finally {
59: Context.exit();
60: }
61: }
62: }
|