Adding some tests

This commit is contained in:
mahmoudelgamal 2016-08-08 10:37:52 +02:00
parent 3c5038f779
commit 7cf25fb727
4 changed files with 110 additions and 7 deletions

View file

@ -21,7 +21,11 @@
<dependencies>
<dependency>
<groupId>com.javax0</groupId>
<artifactId>jscc</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>

View file

@ -36,7 +36,7 @@ public class BeamInterpreter extends Interpreter {
File dir = new File(".");
for (int i = 0; i < dir.list().length; i++) {
File f = dir.listFiles()[i];
System.out.println(f.getAbsolutePath());
// System.out.println(f.getAbsolutePath());
if (f.getAbsolutePath().contains(".class"))
f.delete();
}
@ -51,7 +51,7 @@ public class BeamInterpreter extends Interpreter {
String msg = CompileSourceInMemory.execute(className, st);
return new InterpreterResult(InterpreterResult.Code.SUCCESS, msg);
} catch (Exception e) {
e.printStackTrace();
// e.printStackTrace();
return new InterpreterResult(InterpreterResult.Code.ERROR, e.getMessage());
}

View file

@ -8,18 +8,20 @@ import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaSource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
@ -86,14 +88,20 @@ public class CompileSourceInMemory {
boolean success = task.call();
if (!success) {
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
System.out.println(diagnostic.getMessage(null));
if (diagnostic.getLineNumber() == -1) continue;
System.out.println("line "+ diagnostic.getLineNumber()+ " : " +diagnostic.getMessage(null));
}
}
if (success) {
try {
Class.forName(className).getDeclaredMethod("main", new Class[] { String[].class })
.invoke(null, new Object[] { null });
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
//URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
Class.forName(className, true, classLoader).getDeclaredMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { null });
// Class.forName(className).getDeclaredMethod("main", new Class[] { String[].class })
// .invoke(null, new Object[] { null });
System.out.flush();
System.err.flush();
@ -120,8 +128,19 @@ public class CompileSourceInMemory {
e.printStackTrace(newErr);
System.err.println("Invocation target: " + e);
throw new Exception(baosErr.toString());
} finally{
System.out.flush();
System.err.flush();
System.setOut(oldOut);
System.setErr(oldErr);
}
} else {
System.out.flush();
System.err.flush();
System.setOut(oldOut);
System.setErr(oldErr);
throw new Exception(baosOut.toString());
}
}

View file

@ -0,0 +1,80 @@
package org.apache.zeppelin.beam;
import static org.junit.Assert.assertEquals;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.zeppelin.beam.BeamInterpreter;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class BeamInterpreterTest {
private static BeamInterpreter beam;
private static InterpreterContext context;
@BeforeClass
public static void setUp() {
Properties p = new Properties();
beam = new BeamInterpreter(p);
beam.open();
context = new InterpreterContext(null, null, null, null, null, null,
null, null, null, null, null);
}
@AfterClass
public static void tearDown() {
beam.close();
}
@Test
public void testStaticRepl() {
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.println(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
InterpreterResult res = beam.interpret(writer.toString(), context);
assertEquals(InterpreterResult.Code.SUCCESS, res.code());
}
@Test
public void testStaticReplWithoutMain() {
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public String hello() { return \"hello\"; }");
sourceCode.append("}");
InterpreterResult res = beam.interpret(sourceCode.toString(), context);
assertEquals(InterpreterResult.Code.ERROR, res.code());
}
@Test
public void testStaticReplWithSyntaxError() {
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.prin(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
InterpreterResult res = beam.interpret(writer.toString(), context);
assertEquals(InterpreterResult.Code.ERROR, res.code());
}
}