1 | package tudelft.utilities.j2p.t;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertTrue;
|
---|
4 |
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileNotFoundException;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.nio.file.Paths;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.logging.Level;
|
---|
14 |
|
---|
15 | import org.eclipse.jdt.annotation.NonNull;
|
---|
16 | import org.junit.Test;
|
---|
17 |
|
---|
18 | import tudelft.utilities.j2p.PyProgram;
|
---|
19 | import tudelft.utilities.logging.Reporter;
|
---|
20 | import tudelft.utilities.pyrunner.PythonError;
|
---|
21 | import tudelft.utilities.pyrunner.PythonVenv;
|
---|
22 |
|
---|
23 | public class ReporterTest {
|
---|
24 |
|
---|
25 | private static final String TESTDIR = "src/test/java/";
|
---|
26 | private static final String FULLNAME = "testcode/Reporter1.java";
|
---|
27 |
|
---|
28 | @Test
|
---|
29 | public void test()
|
---|
30 | throws IOException, ClassNotFoundException, TranslationException {
|
---|
31 | System.out.println("TRANSLATION:");
|
---|
32 | final @NonNull PyProgram program = PyProgram.fromModules(
|
---|
33 | Paths.get(TESTDIR), Arrays.asList(new File(FULLNAME)),
|
---|
34 | Paths.get(TESTDIR), Collections.emptyList());
|
---|
35 | System.out.println(program.toString());
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Test
|
---|
39 | public void runFromZip() throws FileNotFoundException, IOException,
|
---|
40 | InterruptedException, PythonError, TranslationException {
|
---|
41 | final @NonNull PyProgram program = PyProgram.fromModules(
|
---|
42 | Paths.get(TESTDIR), Arrays.asList(new File(FULLNAME)),
|
---|
43 | Paths.get(TESTDIR), Collections.emptyList());
|
---|
44 | File zipfile = program.getZip();
|
---|
45 |
|
---|
46 | System.out.println("zip file at " + zipfile);
|
---|
47 | Reporter reporter = new Reporter() {
|
---|
48 | private List<String> warnings = new LinkedList<>();
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public void log(Level level, String msg) {
|
---|
52 | log(level, msg, null);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void log(Level level, String msg, Throwable thrown) {
|
---|
57 | if (level == Level.WARNING || level == Level.SEVERE) {
|
---|
58 | warnings.add(msg);
|
---|
59 | System.err.println(msg);
|
---|
60 | if (thrown != null)
|
---|
61 | thrown.printStackTrace();
|
---|
62 | } else {
|
---|
63 | System.out.println(level + ":" + msg);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | };
|
---|
67 | PythonVenv venv = new PythonVenv(zipfile, reporter);
|
---|
68 | String res = venv.call(Arrays.asList("-m", "testcode.Reporter1"),
|
---|
69 | 10000);
|
---|
70 | System.out.println(res);
|
---|
71 | assertTrue(res.contains("ok"));
|
---|
72 | assertTrue(res.contains("INFO"));
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|