1 | package tudelft.utilities.j2p;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileNotFoundException;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.logging.Level;
|
---|
12 |
|
---|
13 | import org.junit.Ignore;
|
---|
14 | import org.junit.Test;
|
---|
15 |
|
---|
16 | import tudelft.utilities.logging.Reporter;
|
---|
17 | import tudelft.utilities.pyrunner.PythonError;
|
---|
18 | import tudelft.utilities.pyrunner.PythonVenv;
|
---|
19 |
|
---|
20 | public class PersonJsonTest extends PyTest {
|
---|
21 |
|
---|
22 | private static final String TESTDIR = "src/test/java/";
|
---|
23 | private static final String FULLNAME = "testcode/PersonJson.java";
|
---|
24 |
|
---|
25 | @Test
|
---|
26 | public void test() throws IOException, ClassNotFoundException {
|
---|
27 | System.out.println("TRANSLATION:");
|
---|
28 | System.out.println(translate(TESTDIR + FULLNAME));
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Ignore // this fails because we dont have pyson
|
---|
32 | @Test
|
---|
33 | public void runInPython() throws Exception {
|
---|
34 | List<String> res = runPython(translate(TESTDIR + FULLNAME),
|
---|
35 | FULLNAME.replace(".java", ".py"));
|
---|
36 |
|
---|
37 | assertEquals(Arrays.asList("Person[Kees,25]\n"
|
---|
38 | + "{\"PersonJson\":{\"name\":\"Kees\",\"age\":25}}\n" + "true"),
|
---|
39 | res);
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Test
|
---|
43 | public void runFromZip() throws FileNotFoundException, IOException,
|
---|
44 | InterruptedException, PythonError {
|
---|
45 | File zipfile = zip(translate(TESTDIR + FULLNAME),
|
---|
46 | FULLNAME.split("/")[0]);
|
---|
47 | System.out.println("zip file at " + zipfile);
|
---|
48 | Reporter reporter = new Reporter() {
|
---|
49 | private List<String> warnings = new LinkedList<>();
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void log(Level level, String msg) {
|
---|
53 | log(level, msg, null);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public void log(Level level, String msg, Throwable thrown) {
|
---|
58 | if (level == Level.WARNING || level == Level.SEVERE) {
|
---|
59 | warnings.add(msg);
|
---|
60 | System.err.println(msg);
|
---|
61 | if (thrown != null)
|
---|
62 | thrown.printStackTrace();
|
---|
63 | } else {
|
---|
64 | System.out.println(level + ":" + msg);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | };
|
---|
68 | PythonVenv venv = new PythonVenv(zipfile, reporter);
|
---|
69 | venv.call(Arrays.asList("-c", "from testcode import PersonJson"),
|
---|
70 | 10000);
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|