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.FilenameFilter;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.nio.file.Path;
|
---|
10 | import java.nio.file.Paths;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.logging.Level;
|
---|
15 |
|
---|
16 | import org.junit.Test;
|
---|
17 |
|
---|
18 | import tudelft.utilities.logging.Reporter;
|
---|
19 | import tudelft.utilities.pyrunner.PythonError;
|
---|
20 | import tudelft.utilities.pyrunner.PythonVenv;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * A more complex package involving multiple files.
|
---|
24 | */
|
---|
25 | public class ActionsUseTest {
|
---|
26 |
|
---|
27 | private static final String TESTDIR = "src/test/java/";
|
---|
28 | private static final Path TESTPATH = Paths.get(TESTDIR);
|
---|
29 | private static final String FULLNAME = "testcode/actions/ActionsUse.java";
|
---|
30 |
|
---|
31 | @Test
|
---|
32 | public void test() throws IOException, ClassNotFoundException {
|
---|
33 | System.out.println(getProgram());
|
---|
34 | }
|
---|
35 |
|
---|
36 | private PyProgram getProgram() throws FileNotFoundException {
|
---|
37 | // get all java files under TESTDIR/testcode/actions
|
---|
38 | File dir = TESTPATH.resolve("testcode/actions").toFile();
|
---|
39 | File[] javafiles = dir.listFiles(new FilenameFilter() {
|
---|
40 | @Override
|
---|
41 | public boolean accept(File dir, String name) {
|
---|
42 | return name.endsWith(".java");
|
---|
43 | }
|
---|
44 | });
|
---|
45 |
|
---|
46 | List<PyModule> modules = new LinkedList<>();
|
---|
47 | for (File file : javafiles) {
|
---|
48 | // re-relativize
|
---|
49 | file = TESTPATH.relativize(file.toPath()).toFile();
|
---|
50 | modules.add(PyModule.fromJavaFile(TESTPATH, file));
|
---|
51 | }
|
---|
52 |
|
---|
53 | return new PyProgram(modules);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Test
|
---|
57 | public void runFromZip() throws FileNotFoundException, IOException,
|
---|
58 | InterruptedException, PythonError {
|
---|
59 | File zipfile = getProgram().getZip();
|
---|
60 | // File zipfile = zip(translate(TESTDIR + FULLNAME), new File(FULLNAME));
|
---|
61 | System.out.println("zip file at " + zipfile);
|
---|
62 | Reporter reporter = new Reporter() {
|
---|
63 | private List<String> warnings = new LinkedList<>();
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void log(Level level, String msg) {
|
---|
67 | log(level, msg, null);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public void log(Level level, String msg, Throwable thrown) {
|
---|
72 | if (level == Level.WARNING || level == Level.SEVERE) {
|
---|
73 | warnings.add(msg);
|
---|
74 | System.err.println(msg);
|
---|
75 | if (thrown != null)
|
---|
76 | thrown.printStackTrace();
|
---|
77 | } else {
|
---|
78 | System.out.println(level + ":" + msg);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | };
|
---|
82 | PythonVenv venv = new PythonVenv(zipfile, reporter);
|
---|
83 | String res = venv.call(
|
---|
84 | Arrays.asList("-m", "testcode.actions.ActionsUse"), 10000);
|
---|
85 | System.out.println(res);
|
---|
86 | assertEquals("serializeAcceptTest\n"
|
---|
87 | + "{\"Accept\":{\"actor\":\"party1\",\"bid\":\"issuevalues\"}}\n"
|
---|
88 | + "deserializeAcceptTest\n" + "Accept[party1,issuevalues]\n"
|
---|
89 | + "AlltestsOK\n", res.replace(" ", ""));
|
---|
90 | // as in Java but true->True and pyson inserts some extra whitespace
|
---|
91 | }
|
---|
92 |
|
---|
93 | }
|
---|