forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestGetInterface.java
More file actions
57 lines (44 loc) · 1.66 KB
/
Copy pathTestGetInterface.java
File metadata and controls
57 lines (44 loc) · 1.66 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
package php.java.test;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.OutputStreamWriter;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import junit.framework.TestCase;
public class TestGetInterface extends TestCase {
public TestGetInterface(String name) {
super(name);
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
private ScriptEngine scriptEngine;
String classA = "class A{function toString(){return '::A';} function invokeA($b){$b->invokeB();}}\n";
String classB = "class B{function toString(){return '::B';} function invokeB(){echo '::B';}}\n";
String test = "<?php "+classA+classB+" $thiz=java_context()->getAttribute('thiz');\n$thiz->call(java_closure(new A()), java_closure(new B())); ?>";
public void test() throws Exception {
scriptEngine = new ScriptEngineManager().getEngineByName("php-invocable");
scriptEngine.put("thiz", this);
ByteArrayOutputStream out;
OutputStreamWriter writer;
scriptEngine.getContext().setWriter(writer = new OutputStreamWriter(out = new ByteArrayOutputStream()));
scriptEngine.eval(test);
((Closeable)scriptEngine).close();
writer.close();
if(!"::B".equals(out.toString())) {
fail("test failed");
}
return;
}
interface IA { public void invokeA(IB ccb); };
interface IB { public void invokeB(); };
public void call(Object $cca, Object $ccb) {
IA cca = (IA) ((Invocable)scriptEngine).getInterface($cca, IA.class);
IB ccb = (IB) ((Invocable)scriptEngine).getInterface($ccb, IB.class);
cca.invokeA(ccb);
}
}