Skip to content

Commit

Permalink
Adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfelgamal committed Aug 8, 2016
1 parent c2e2066 commit 0f73387
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
6 changes: 5 additions & 1 deletion beam/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void close() {
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();
}
Expand All @@ -51,7 +51,7 @@ public InterpreterResult interpret(String st, InterpreterContext context) {
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());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
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;

Expand Down Expand Up @@ -86,14 +88,20 @@ public static String execute(String className, String code) throws Exception {
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();
Expand All @@ -120,8 +128,19 @@ public static String execute(String className, String code) throws Exception {
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());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 0f73387

Please sign in to comment.