import static org.junit.Assert.assertEquals; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.logging.Level; import org.junit.Test; import tudelft.utilities.j2p.PyModule; import tudelft.utilities.j2p.PyProgram; import tudelft.utilities.logging.Reporter; import tudelft.utilities.pyrunner.PythonError; import tudelft.utilities.pyrunner.PythonVenv; public class ValueTest { private final static Path TESTPATH = Paths.get("src/test/java"); private final static File FILE = new File("testcode/Value.java"); @Test public void test() throws IOException, ClassNotFoundException { System.out.println(getProgram()); } private PyProgram getProgram() throws FileNotFoundException { return new PyProgram( Arrays.asList(PyModule.fromJavaFile(TESTPATH, FILE))); } @Test public void runFromZip() throws FileNotFoundException, IOException, InterruptedException, PythonError { File zipfile = getProgram().getZip(); System.out.println("zip file at " + zipfile); Reporter reporter = new Reporter() { private List warnings = new LinkedList<>(); @Override public void log(Level level, String msg) { log(level, msg, null); } @Override public void log(Level level, String msg, Throwable thrown) { if (level == Level.WARNING || level == Level.SEVERE) { warnings.add(msg); System.err.println(msg); if (thrown != null) thrown.printStackTrace(); } else { System.out.println(level + ":" + msg); } } }; PythonVenv venv = new PythonVenv(zipfile, reporter); String res = venv.call(Arrays.asList("-m", "testcode.Value"), 10000); System.out.println(res); assertEquals(Arrays.asList("{\"value\":12.0}", "{\"value\":\"hallo\"}"), Arrays.asList(res.replace(" ", "").split("\n"))); // as in Java but true->True and pyson inserts some extra whitespace } }