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