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

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

#123 fix pom. Fix typo in utils package name. Change Path.of to Paths.from because jhava8 compatibility

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.nio.file.Paths;
11import java.util.Arrays;
12import java.util.LinkedList;
13import java.util.List;
14import java.util.logging.Level;
15
16import org.junit.Test;
17
18import tudelft.utilities.logging.Reporter;
19import tudelft.utilities.pyrunner.PythonError;
20import tudelft.utilities.pyrunner.PythonVenv;
21
22/**
23 * A more complex package involving multiple files.
24 */
25public 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}
Note: See TracBrowser for help on using the repository browser.