Skip to content

Commit 2ac1f8b

Browse files
committed
增加test base case
1 parent 7e80d0e commit 2ac1f8b

File tree

10 files changed

+236
-4
lines changed

10 files changed

+236
-4
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,7 @@ pip-log.txt
163163
.DS_Store
164164

165165
.git
166-
target
166+
target
167+
resin-quercus.iml
168+
resin-quercus.ipr
169+
resin-quercus.iws

pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<artifactId>resin-quercus</artifactId>
1111
<packaging>jar</packaging>
1212
<version>4.0.35.1</version>
13-
<name>Resin Utilities</name>
13+
<name>Resin quercus</name>
1414
<url>http://caucho.com</url>
1515

1616
<licenses>
@@ -46,8 +46,14 @@
4646
<version>3.0</version>
4747
<scope>provided</scope>
4848
</dependency>
49-
50-
</dependencies>
49+
<dependency>
50+
<groupId>junit</groupId>
51+
<artifactId>junit</artifactId>
52+
<version>4.10</version>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
</dependencies>
5157
<build>
5258
<plugins>
5359
<plugin>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.caucho.quercus;
2+
3+
import com.caucho.quercus.env.Env;
4+
import com.caucho.quercus.page.InterpretedPage;
5+
import com.caucho.quercus.parser.QuercusParser;
6+
import com.caucho.quercus.program.QuercusProgram;
7+
import com.caucho.vfs.Path;
8+
import com.caucho.vfs.Vfs;
9+
import com.caucho.vfs.WriteStream;
10+
import junit.framework.Assert;
11+
import org.junit.Before;
12+
13+
import javax.script.Bindings;
14+
import javax.script.ScriptContext;
15+
import javax.script.SimpleBindings;
16+
import javax.script.SimpleScriptContext;
17+
import java.io.*;
18+
import java.net.URISyntaxException;
19+
import java.util.Map;
20+
21+
import static org.junit.Assert.fail;
22+
23+
/**
24+
* User: chao.liuc
25+
* Date: 13-5-8
26+
* Time: ÉÏÎç10:11
27+
*/
28+
public abstract class QuercusBaseTest {
29+
protected Quercus quercus;
30+
protected Env env;
31+
protected int timeout;
32+
protected Path path;
33+
protected String encode = "GBK";
34+
35+
@Before
36+
public void setUp() throws Exception {
37+
quercus = new Quercus();
38+
if (timeout >0) {
39+
quercus.setIni("max_execution_time", String.valueOf(timeout));
40+
}
41+
quercus.setUnicodeSemantics(true);
42+
quercus.init();
43+
quercus.start();
44+
}
45+
46+
protected String eval(InputStream in, Map<String, Object> context) {
47+
try {
48+
QuercusProgram program = QuercusParser.parse(quercus, path, Vfs.openRead(new InputStreamReader(in, encode)));
49+
Writer writer = new StringWriter();
50+
WriteStream writerStream = Vfs.openWrite(writer) ;
51+
writerStream.setEncoding(encode);
52+
env = new Env(quercus, new InterpretedPage(program), writerStream, null, null);
53+
SimpleScriptContext simpleScriptContext = new SimpleScriptContext();
54+
Bindings bindings = new SimpleBindings();
55+
if (context != null ) {
56+
for (Map.Entry<String, Object> entry : context.entrySet()) {
57+
bindings.put(entry.getKey(), entry.getValue());
58+
}
59+
}
60+
simpleScriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
61+
env.setScriptContext(simpleScriptContext);
62+
program.execute(env);
63+
writerStream.flushBuffer();
64+
writerStream.free();
65+
return writer.toString();
66+
}catch (Exception e) {
67+
throw new RuntimeException(e);
68+
}
69+
}
70+
71+
protected String eval(InputStream in) {
72+
return eval(in, null) ;
73+
}
74+
75+
protected String evalString(String php) {
76+
try {
77+
return eval(new ByteArrayInputStream(php.getBytes(encode)), null) ;
78+
} catch (UnsupportedEncodingException e) {
79+
throw new RuntimeException(e);
80+
}
81+
}
82+
83+
84+
protected String evalFile(String path, Map<String, Object> context) {
85+
InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
86+
return eval(in, context);
87+
}
88+
89+
protected String evalFile(String path) {
90+
return evalFile(path, null) ;
91+
}
92+
93+
protected void assertFile(String path) {
94+
String expacted = executeFile(path) ;
95+
expacted = expacted.replaceAll("\r\n", "\n");
96+
String real = evalFile(path);
97+
real = real.replaceAll("\r\n", "\n") + "\n";
98+
Assert.assertEquals(expacted, real);
99+
}
100+
101+
private String executeFile(String path) {
102+
File testFile = null;
103+
try {
104+
testFile = new File(this.getClass().getClassLoader().getResource(path).toURI());
105+
} catch (URISyntaxException e) {
106+
e.printStackTrace();
107+
fail();
108+
109+
}
110+
String cmd = "php " + testFile.getAbsolutePath();
111+
Process process = null;
112+
try {
113+
process = Runtime.getRuntime().exec(cmd);
114+
} catch (IOException e) {
115+
throw new IllegalStateException("please check php in path env", e);
116+
}
117+
try {
118+
process.getOutputStream().close();
119+
process.getErrorStream().close();
120+
BufferedReader ir =new BufferedReader(new InputStreamReader(process.getInputStream())) ;
121+
StringBuffer sb = new StringBuffer();
122+
String line = null;
123+
while ((line = ir.readLine()) != null) {
124+
sb.append(line).append("\n");
125+
}
126+
process.waitFor();
127+
return sb.toString();
128+
} catch (Exception e) {
129+
throw new RuntimeException(e) ;
130+
}
131+
}
132+
133+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.caucho.quercus;
2+
3+
import junit.framework.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* User: chao.liuc
8+
* Date: 13-5-8
9+
* Time: ÉÏÎç10:43
10+
*/
11+
public class SimpleQuercusTest extends QuercusBaseTest {
12+
13+
@Test
14+
public void testEcho () {
15+
String php = "<?php echo 'hello' ?>";
16+
Assert.assertEquals("hello", evalString(php));
17+
}
18+
19+
@Test
20+
public void testEchoStatement() {
21+
String path = "statement/echo.php";
22+
String ret = evalFile(path);
23+
Assert.assertEquals("hello", ret);
24+
}
25+
26+
@Test
27+
public void testEcho_php() {
28+
String path = "statement/echo.php";
29+
assertFile(path);
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.caucho.quercus.module;
2+
3+
import com.caucho.quercus.QuercusBaseTest;
4+
import junit.framework.Assert;
5+
import org.junit.Test;
6+
7+
/**
8+
* User: chao.liuc
9+
* Date: 13-5-8
10+
* Time: ÏÂÎç12:14
11+
*/
12+
public class ExtModuleTest extends QuercusBaseTest{
13+
14+
@Test
15+
public void testExtModule() {
16+
String ret = evalFile("module/ext.php");
17+
Assert.assertEquals("ext:hi", ret);
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.caucho.quercus.module.support;
2+
3+
import com.caucho.quercus.module.AbstractQuercusModule;
4+
5+
/**
6+
* User: chao.liuc
7+
* Date: 13-5-8
8+
* Time: ÏÂÎç12:13
9+
*/
10+
public class ExtModule extends AbstractQuercusModule {
11+
public static String fun_ext (String in) {
12+
return "ext:" + in;
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.caucho.quercus.statement;
2+
3+
import com.caucho.quercus.QuercusBaseTest;
4+
import org.junit.Test;
5+
6+
/**
7+
* User: chao.liuc
8+
* Date: 13-5-8
9+
* Time: ÉÏÎç10:43
10+
*/
11+
public class EchoStatementTest extends QuercusBaseTest {
12+
13+
@Test
14+
public void testEcho_php() {
15+
String path = "statement/echo.php";
16+
assertFile(path);
17+
}
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.caucho.quercus.module.support.ExtModule

src/test/resources/module/ext.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
echo fun_ext('hi');
3+
?>

src/test/resources/statement/echo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
echo 'hello\n\r';
3+
echo 'hello\n';
4+
?>

0 commit comments

Comments
 (0)