Skip to content
Snippets Groups Projects
Commit 3e36de76 authored by Alexandre Vanini's avatar Alexandre Vanini
Browse files

[TESTS] - TRY WT RESOURCES

parent ae2865f8
Branches
Tags
No related merge requests found
import io.javalin.Javalin; import io.javalin.Javalin;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.commons.lang.StringEscapeUtils;
public class app { public class app {
public static void main(String[] args) { public static void main(String[] args) {
Javalin app = Javalin.create().enableCorsForAllOrigins().start(6999); Javalin app = Javalin.create().enableCorsForAllOrigins().start(6999);
app.post("/", ctx -> { app.post("/", ctx -> {
compilation cpl = new compilation();
JSONObject input = new JSONObject(ctx.body()); ctx.json(cpl.execute_kata(new JSONObject(ctx.body())));
PrintWriter writer = null;
File file = null;
String filename = "";
String cmd = "";
if (input.get("language").equals("python")) {
// Create a PrintWriter object targeting sample.py
filename = "sample.py";
writer = new PrintWriter(filename, "UTF-8");
cmd = "python " + filename;
} else if (input.get("language").equals("java")) {
// Create a PrintWriter object targeting sample.py
filename = "app.java";
writer = new PrintWriter(filename, "UTF-8");
cmd = "java " + filename;
}
// For delete purpose, create a File object also targeting sample.py
file = new File(filename);
// Complex file with tabulation structure for test purpose
writer.println(input.get("stream"));
// Close the writer, the file has been filled and is ready to be executed
writer.close();
long start = System.currentTimeMillis();
Process cmdProc = Runtime.getRuntime().exec(cmd);
cmdProc.waitFor();
long elapsed = System.currentTimeMillis() - start;
// Create Array lists that will contain output and error values of the executed program
/**************************/
String line, output = "", error = "";
BufferedReader stdoutReaderl = new BufferedReader(new InputStreamReader(cmdProc.getInputStream()));
while ((line = stdoutReader.readLine()) != null)
output += line + "\n";
BufferedReader stderrReader = new BufferedReader(new InputStreamReader(cmdProc.getErrorStream()));
while ((line = stderrReader.readLine()) != null)
error += line + "\n";
/**************************/
HashMap<String, Object> json = new HashMap<>();
//exit value of the ran program
json.put("exit", cmdProc.exitValue());
json.put("output", output);
json.put("error", error);
json.put("time", elapsed);
ctx.json(json);
file.delete();
}); });
} }
} }
import org.json.JSONObject;
import java.io.*;
import java.util.HashMap;
public class compilation {
public HashMap<String, Object> execute_kata(JSONObject input) {
String filename = "", cmd = "", line, output = "", error = "";
switch (input.get("language").toString()) {
case "python":
filename = "sample.py";
cmd = "python " + filename;
break;
case "java":
filename = "app.java";
cmd = "java " + filename;
break;
}
try (PrintWriter writer = new PrintWriter(filename, "UTF-8")) {
writer.println(input.get("stream"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
long start,elapsed = 0;
Process cmdProc = null;
try {
start = System.currentTimeMillis();
cmdProc = Runtime.getRuntime().exec(cmd);
cmdProc.waitFor();
// Compute the execute time
elapsed = System.currentTimeMillis() - start;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Create Array lists that will contain output and error values of the executed program
/**************************/
try (BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(cmdProc.getInputStream()))) {
while ((line = stdoutReader.readLine()) != null)
output += line + "\n";
} catch (IOException e) {
e.printStackTrace();
}
try (BufferedReader stderrReader = new BufferedReader(new InputStreamReader(cmdProc.getErrorStream()))) {
while ((line = stderrReader.readLine()) != null)
error += line + "\n";
} catch (IOException e) {
e.printStackTrace();
}
/**************************/
HashMap<String, Object> json = new HashMap<>();
//exit value of the ran program
json.put("exit", cmdProc.exitValue());
json.put("output", output);
json.put("error", error);
json.put("time", elapsed);
// For delete purpose, create a File object also targeting sample.py
File file = new File(filename);
file.delete();
return json;
}
}
No preview for this file type
File added
...@@ -26,7 +26,7 @@ public class app { ...@@ -26,7 +26,7 @@ public class app {
String code = StringEscapeUtils.escapeJava(ctx.queryParams("code").get(0)); String code = StringEscapeUtils.escapeJava(ctx.queryParams("code").get(0));
wr.writeBytes("{\"language\":\""+type+"\",\"stream\":\""+code+"\"}"); wr.writeBytes("{\"language\":\""+type+"\",\"stream\":\""+code+"\"}");
//wr.writeBytes("{\"language\":\"python\",\"stream\":\"def avg(marks):\\n assert len(marks) != 0\\n return sum(marks)/len(marks)\\nmark1 = []\\nprint('Average of mark1:',avg(mark1))\"}");
wr.close(); wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment