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.Function;
26: import org.mozilla.javascript.JavaScriptException;
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 ContinuationSerializationTest extends TestCase {
34: public void testSerialization() throws Exception {
35: Context cx = Context.enter();
36: try {
37: // create scripting environment (i.e. load everything)
38: Scriptable scope = cx.initStandardObjects();
39: cx.setOptimizationLevel(-1);
40: Object wrappedOut = Context.javaToJS(System.out, scope);
41: ScriptableObject.putProperty(scope, "out", wrappedOut);
42: Script script = cx
43: .compileReader(
44: new FileReader(
45: "./src/test/java/org/bpmscript/continuationserialization.js"),
46: "continuationserialization.js", 0, null);
47: script.exec(cx, scope);
48:
49: // get the main function and execute it
50: Object object = scope.get("main", scope);
51: if (object instanceof Function) {
52: Function function = (Function) object;
53: Object result = null;
54: try {
55: result = function.call(cx, scope, scope,
56: new Object[] {});
57: System.out.println(result);
58: } catch (JavaScriptException ex) {
59: Object k = ex.getValue();
60: if (k instanceof Continuation) {
61: Continuation cont = (Continuation) k;
62: ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
63:
64: ScriptableOutputStream outputStream = new ScriptableOutputStream(
65: byteOut, scope);
66:
67: // Object[] ids = scope.getIds();
68: // for(Object id: ids) {
69: // outputStream.addExcludedName(id.toString());
70: // }
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: }
81: } finally {
82: Context.exit();
83: }
84: }
85: }
|