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.ByteArrayOutputStream;
20: import java.io.FileReader;
21:
22: import junit.framework.TestCase;
23:
24: import org.mozilla.javascript.Context;
25: import org.mozilla.javascript.EvaluatorException;
26: import org.mozilla.javascript.Function;
27: import org.mozilla.javascript.Script;
28: import org.mozilla.javascript.Scriptable;
29: import org.mozilla.javascript.ScriptableObject;
30: import org.mozilla.javascript.continuations.Continuation;
31: import org.mozilla.javascript.serialize.ScriptableOutputStream;
32:
33: public class ContinuationDebuggingTest extends TestCase {
34:
35: public void testSerialization() throws Exception {
36: Context cx = Context.enter();
37: try {
38: // create scripting environment (i.e. load everything)
39: Scriptable scope = cx.initStandardObjects();
40: cx.setOptimizationLevel(-1);
41: Object wrappedOut = Context.javaToJS(System.out, scope);
42: ScriptableObject.putProperty(scope, "out", wrappedOut);
43: ScriptableObject.putProperty(scope, "continuationThrower",
44: Context.javaToJS(new ContinuationErrorThrower(),
45: scope));
46: Script script = cx
47: .compileReader(
48: new FileReader(
49: "./src/test/java/org/bpmscript/continuationdebugging.js"),
50: "continuationdebugging.js", 0, null);
51: script.exec(cx, scope);
52:
53: // get the main function and execute it
54: Object object = scope.get("main", scope);
55: if (object instanceof Function) {
56: Function function = (Function) object;
57: Object result = null;
58: try {
59: result = function.call(cx, scope, scope,
60: new Object[] {});
61: System.out.println(result);
62: } catch (ContinuationError ex) {
63: Continuation cont = (Continuation) ex
64: .getContinuation();
65: Object implementation = cont.getImplementation();
66: System.out.println(implementation.getClass());
67: ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
68:
69: ScriptableOutputStream outputStream = new ScriptableOutputStream(
70: byteOut, scope);
71:
72: outputStream.writeObject(cont);
73: outputStream.flush();
74: outputStream.close();
75: byte[] bytes = byteOut.toByteArray();
76: System.out.println(bytes.length);
77: System.out.println(new String(bytes));
78: }
79: }
80: } finally {
81: Context.exit();
82: }
83: }
84: }
|