1 |
|
---|
2 | import static org.junit.Assert.assertEquals;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.nio.file.Path;
|
---|
8 | import java.nio.file.Paths;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.logging.Level;
|
---|
13 |
|
---|
14 | import org.junit.Test;
|
---|
15 |
|
---|
16 | import tudelft.utilities.j2p.PyModule;
|
---|
17 | import tudelft.utilities.j2p.PyProgram;
|
---|
18 | import tudelft.utilities.logging.Reporter;
|
---|
19 | import tudelft.utilities.pyrunner.PythonError;
|
---|
20 | import tudelft.utilities.pyrunner.PythonVenv;
|
---|
21 |
|
---|
22 | public class ValueTest {
|
---|
23 |
|
---|
24 | private final static Path TESTPATH = Paths.get("src/test/java");
|
---|
25 | private final static File FILE = new File("testcode/Value.java");
|
---|
26 |
|
---|
27 | @Test
|
---|
28 | public void test() throws IOException, ClassNotFoundException {
|
---|
29 | System.out.println(getProgram());
|
---|
30 | }
|
---|
31 |
|
---|
32 | private PyProgram getProgram() throws FileNotFoundException {
|
---|
33 | return new PyProgram(
|
---|
34 | Arrays.asList(PyModule.fromJavaFile(TESTPATH, FILE)));
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Test
|
---|
38 | public void runFromZip() throws FileNotFoundException, IOException,
|
---|
39 | InterruptedException, PythonError {
|
---|
40 | File zipfile = getProgram().getZip();
|
---|
41 | System.out.println("zip file at " + zipfile);
|
---|
42 | Reporter reporter = new Reporter() {
|
---|
43 | private List<String> warnings = new LinkedList<>();
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void log(Level level, String msg) {
|
---|
47 | log(level, msg, null);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public void log(Level level, String msg, Throwable thrown) {
|
---|
52 | if (level == Level.WARNING || level == Level.SEVERE) {
|
---|
53 | warnings.add(msg);
|
---|
54 | System.err.println(msg);
|
---|
55 | if (thrown != null)
|
---|
56 | thrown.printStackTrace();
|
---|
57 | } else {
|
---|
58 | System.out.println(level + ":" + msg);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | };
|
---|
62 | PythonVenv venv = new PythonVenv(zipfile, reporter);
|
---|
63 | String res = venv.call(
|
---|
64 | Arrays.asList("-m", "testcode.actions.ActionsUse"), 10000);
|
---|
65 | System.out.println(res);
|
---|
66 | assertEquals("{\"value\":12.0}\n" + "{\"value\":\"hallo\"}",
|
---|
67 | res.replace(" ", ""));
|
---|
68 | // as in Java but true->True and pyson inserts some extra whitespace
|
---|
69 | }
|
---|
70 | } |
---|