Skip to content

Commit 7cf25fb

Browse files
committed
Adding some tests
1 parent 3c5038f commit 7cf25fb

File tree

4 files changed

+110
-7
lines changed

4 files changed

+110
-7
lines changed

beam/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
<dependencies>
2222

2323

24-
24+
<dependency>
25+
<groupId>com.javax0</groupId>
26+
<artifactId>jscc</artifactId>
27+
<version>1.0.1</version>
28+
</dependency>
2529
<dependency>
2630
<groupId>org.apache.spark</groupId>
2731
<artifactId>spark-core_2.10</artifactId>

beam/src/main/java/org/apache/zeppelin/beam/BeamInterpreter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void close() {
3636
File dir = new File(".");
3737
for (int i = 0; i < dir.list().length; i++) {
3838
File f = dir.listFiles()[i];
39-
System.out.println(f.getAbsolutePath());
39+
// System.out.println(f.getAbsolutePath());
4040
if (f.getAbsolutePath().contains(".class"))
4141
f.delete();
4242
}
@@ -51,7 +51,7 @@ public InterpreterResult interpret(String st, InterpreterContext context) {
5151
String msg = CompileSourceInMemory.execute(className, st);
5252
return new InterpreterResult(InterpreterResult.Code.SUCCESS, msg);
5353
} catch (Exception e) {
54-
e.printStackTrace();
54+
// e.printStackTrace();
5555
return new InterpreterResult(InterpreterResult.Code.ERROR, e.getMessage());
5656

5757
}

beam/src/main/java/org/apache/zeppelin/beam/CompileSourceInMemory.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@
88
import javax.tools.SimpleJavaFileObject;
99
import javax.tools.ToolProvider;
1010

11-
1211
import com.thoughtworks.qdox.JavaProjectBuilder;
1312
import com.thoughtworks.qdox.model.JavaClass;
1413
import com.thoughtworks.qdox.model.JavaSource;
1514

1615
import java.io.ByteArrayOutputStream;
16+
import java.io.File;
1717
import java.io.PrintStream;
1818
import java.io.PrintWriter;
1919
import java.io.StringReader;
2020
import java.io.StringWriter;
2121
import java.lang.reflect.InvocationTargetException;
2222
import java.net.URI;
23+
import java.net.URL;
24+
import java.net.URLClassLoader;
2325
import java.util.Arrays;
2426
import java.util.List;
2527

@@ -86,14 +88,20 @@ public static String execute(String className, String code) throws Exception {
8688
boolean success = task.call();
8789
if (!success) {
8890
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
89-
System.out.println(diagnostic.getMessage(null));
91+
if (diagnostic.getLineNumber() == -1) continue;
92+
System.out.println("line "+ diagnostic.getLineNumber()+ " : " +diagnostic.getMessage(null));
9093
}
9194
}
9295
if (success) {
9396
try {
9497

95-
Class.forName(className).getDeclaredMethod("main", new Class[] { String[].class })
96-
.invoke(null, new Object[] { null });
98+
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
99+
//URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
100+
Class.forName(className, true, classLoader).getDeclaredMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { null });
101+
102+
103+
// Class.forName(className).getDeclaredMethod("main", new Class[] { String[].class })
104+
// .invoke(null, new Object[] { null });
97105

98106
System.out.flush();
99107
System.err.flush();
@@ -120,8 +128,19 @@ public static String execute(String className, String code) throws Exception {
120128
e.printStackTrace(newErr);
121129
System.err.println("Invocation target: " + e);
122130
throw new Exception(baosErr.toString());
131+
} finally{
132+
System.out.flush();
133+
System.err.flush();
134+
135+
System.setOut(oldOut);
136+
System.setErr(oldErr);
123137
}
124138
} else {
139+
System.out.flush();
140+
System.err.flush();
141+
142+
System.setOut(oldOut);
143+
System.setErr(oldErr);
125144
throw new Exception(baosOut.toString());
126145
}
127146
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.apache.zeppelin.beam;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.PrintWriter;
6+
import java.io.StringWriter;
7+
import java.util.Properties;
8+
9+
import org.apache.zeppelin.beam.BeamInterpreter;
10+
import org.apache.zeppelin.interpreter.InterpreterContext;
11+
import org.apache.zeppelin.interpreter.InterpreterResult;
12+
import org.junit.AfterClass;
13+
import org.junit.BeforeClass;
14+
import org.junit.Test;
15+
16+
public class BeamInterpreterTest {
17+
18+
private static BeamInterpreter beam;
19+
private static InterpreterContext context;
20+
21+
@BeforeClass
22+
public static void setUp() {
23+
Properties p = new Properties();
24+
beam = new BeamInterpreter(p);
25+
beam.open();
26+
context = new InterpreterContext(null, null, null, null, null, null,
27+
null, null, null, null, null);
28+
}
29+
30+
@AfterClass
31+
public static void tearDown() {
32+
beam.close();
33+
}
34+
35+
@Test
36+
public void testStaticRepl() {
37+
38+
StringWriter writer = new StringWriter();
39+
PrintWriter out = new PrintWriter(writer);
40+
out.println("public class HelloWorld {");
41+
out.println(" public static void main(String args[]) {");
42+
out.println(" System.out.println(\"This is in another java file\");");
43+
out.println(" }");
44+
out.println("}");
45+
out.close();
46+
47+
InterpreterResult res = beam.interpret(writer.toString(), context);
48+
49+
assertEquals(InterpreterResult.Code.SUCCESS, res.code());
50+
}
51+
52+
@Test
53+
public void testStaticReplWithoutMain() {
54+
55+
StringBuffer sourceCode = new StringBuffer();
56+
sourceCode.append("package org.mdkt;\n");
57+
sourceCode.append("public class HelloClass {\n");
58+
sourceCode.append(" public String hello() { return \"hello\"; }");
59+
sourceCode.append("}");
60+
InterpreterResult res = beam.interpret(sourceCode.toString(), context);
61+
assertEquals(InterpreterResult.Code.ERROR, res.code());
62+
}
63+
64+
@Test
65+
public void testStaticReplWithSyntaxError() {
66+
67+
StringWriter writer = new StringWriter();
68+
PrintWriter out = new PrintWriter(writer);
69+
out.println("public class HelloWorld {");
70+
out.println(" public static void main(String args[]) {");
71+
out.println(" System.out.prin(\"This is in another java file\");");
72+
out.println(" }");
73+
out.println("}");
74+
out.close();
75+
InterpreterResult res = beam.interpret(writer.toString(), context);
76+
77+
assertEquals(InterpreterResult.Code.ERROR, res.code());
78+
}
79+
80+
}

0 commit comments

Comments
 (0)