source: java2python/jackson-t/src/test/java/tudelft/utilities/j2p/ActionsUseTest.java@ 404

Last change on this file since 404 was 404, checked in by wouter, 2 years ago

#122 refactoring, creating separate packages for java core and jackson-translator (jackson-t)

File size: 2.7 KB
Line 
1package tudelft.utilities.j2p;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.File;
6import java.io.FileNotFoundException;
7import java.io.FilenameFilter;
8import java.io.IOException;
9import java.nio.file.Path;
10import java.util.Arrays;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.logging.Level;
14
15import org.junit.Test;
16
17import tudelft.utilities.logging.Reporter;
18import tudelft.utilities.pyrunner.PythonError;
19import tudelft.utilities.pyrunner.PythonVenv;
20
21/**
22 * A more complex package involving multiple files.
23 */
24public 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}
Note: See TracBrowser for help on using the repository browser.