forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestExceptionInvocable2.java
More file actions
67 lines (59 loc) · 1.81 KB
/
Copy pathTestExceptionInvocable2.java
File metadata and controls
67 lines (59 loc) · 1.81 KB
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
package php.java.test;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import junit.framework.TestCase;
public class TestExceptionInvocable2 extends TestCase {
public TestExceptionInvocable2(String name) {
super(name);
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void test() throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine e = manager.getEngineByName("php-invocable");
OutputStream out = new ByteArrayOutputStream();
Writer w = new OutputStreamWriter(out);
e.getContext().setWriter(w);
e.getContext().setErrorWriter(w);
e.eval("<?php function f() { return 'f ok';}; \n" +
"class c {function p() {return 'c::p ok';}}; \n" +
"java_context()->setAttribute('c', java_closure(new c()), 100); " +
"?>");
Invocable i = (Invocable) e;
try {
assertTrue("f ok".equals(i.invokeFunction("f", new Object[] {})));
} catch (Throwable ex) {
fail("2");
}
try {
i.invokeFunction("g", new Object[] {});
System.err.println("test failed");
fail("3");
} catch (NoSuchMethodException ex) {
// ex.printStackTrace(System.out);
}
try {
assertTrue("c::p ok".equals(i.invokeMethod(e.get("c"), "p", new Object[] {})));
} catch (NoSuchMethodException ex) {
fail("4");
}
try {
i.invokeMethod(e.get("c"), "g", new Object[] {});
fail("5");
} catch (NoSuchMethodException ex) {
//ex.printStackTrace(System.out);
}
((Closeable)e).close();
if (out.toString().length() != 0) throw new Exception("test failed");
}
}