forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestInvocablePhpScriptEngine.java
More file actions
157 lines (141 loc) · 4.45 KB
/
Copy pathTestInvocablePhpScriptEngine.java
File metadata and controls
157 lines (141 loc) · 4.45 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package php.java.test;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import junit.framework.TestCase;
public class TestInvocablePhpScriptEngine extends TestCase {
private ScriptEngine e;
private Bindings b;
private String script;
private ScriptEngineManager m;
private String invocableScript;
public TestInvocablePhpScriptEngine(String name) {
super(name);
}
protected void setUp() throws Exception {
super.setUp();
m = new ScriptEngineManager();
e = m.getEngineByName("php-invocable");
b = new SimpleBindings();
script = "<?php function f($arg) {return 1 + (string)$arg;}; exit(1+2); ?>";
invocableScript = "<?php function f($arg) {return 1 + (string)$arg;}; ?>"; // no exit()
}
protected void tearDown() throws Exception {
super.tearDown();
((Closeable)e).close();
}
public void testEvalReader() {
try {
Reader r = new StringReader(script);
assertTrue("3".equals(String.valueOf(e.eval(r))));
r.close();
} catch (Exception e) {
fail(String.valueOf(e));
}
}
public void testEvalReaderBindings() {
try {
Reader r = new StringReader(script);
assertTrue("3".equals(String.valueOf(e.eval(r, b))));
r.close();
} catch (Exception e) {
fail(String.valueOf(e));
}
}
public void testEvalString() {
try {
assertTrue("3".equals(String.valueOf(e.eval(script))));
} catch (ScriptException e) {
fail(String.valueOf(e));
}
}
public void testEvalStringBindings() {
try {
assertTrue("3".equals(String.valueOf(e.eval(script,b))));
} catch (ScriptException e) {
fail(String.valueOf(e));
}
}
public void testEvalCompilableString() {
try {
ScriptEngine e = m.getEngineByName("php-invocable");
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
e.getContext().setWriter(writer);e.getContext().getWriter();
((java.io.FileFilter)e).accept(new File(System.getProperty("java.io.tmpdir", "/tmp")+File.separator+"test.php"));
CompiledScript s = ((Compilable)e).compile("<?php echo 1+2;?>");
long t1 = System.currentTimeMillis();
for (int i=0; i<100; i++) {
s.eval();
((Closeable)e).close();
assertTrue("3".equals(out.toString()));
out.reset();
}
long t2 = System.currentTimeMillis();
System.out.println("testEvalInvocableCompilableString time:" + (t2-t1));
} catch (Exception e) {
fail(String.valueOf(e));
}
}
public void testInvokeFunction() {
try {
ScriptEngine e = m.getEngineByName("php-invocable");
e.eval(invocableScript);
assertTrue(6==(Integer)((Invocable)e).invokeFunction("f",new Object[]{"5"}));
((Closeable)e).close();
} catch (Exception e) {
fail(String.valueOf(e));
}
}
public void testInvokeFunctionCompiled() {
try {
ScriptEngine e = m.getEngineByName("php-invocable");
((java.io.FileFilter)e).accept(new File(System.getProperty("java.io.tmpdir", "/tmp")+File.separator+"test.php"));
CompiledScript c = ((Compilable)e).compile(invocableScript);
c.eval();
assertTrue(6==(Integer)((Invocable)e).invokeFunction("f",new Object[]{"5"}));
c.eval();
assertTrue(6==(Integer)((Invocable)e).invokeFunction("f",new Object[]{"5"}));
((Closeable)e).close();
} catch (Exception e) {
fail(String.valueOf(e));
}
}
// public void testInvokeMethod() {
// fail("Not yet implemented");
// }
//
// public void testGetInterfaceClass() {
// fail("Not yet implemented");
// }
//
// public void testGetInterfaceObjectClass() {
// fail("Not yet implemented");
// }
// public void testInvokeFunctionCompiled() {
// fail("Not yet implemented");
// }
//
// public void testInvokeMethodCompiled() {
// fail("Not yet implemented");
// }
//
// public void testGetInterfaceClassCompiled() {
// fail("Not yet implemented");
// }
//
// public void testGetInterfaceObjectClassCompiled() {
// fail("Not yet implemented");
// }
}