1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.scriptengine.jython;
23
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.Properties;
27
28 import net.grinder.scriptengine.jython.JythonScriptExecutionException;
29 import net.grinder.testutility.AbstractFileTestCase;
30 import static net.grinder.testutility.AssertUtilities.assertContains;
31
32 import org.python.core.Py;
33 import org.python.core.PyException;
34 import org.python.core.PyString;
35 import org.python.core.PySystemState;
36
37
38 public class TestJythonScriptExecutionException extends AbstractFileTestCase {
39
40 protected void setUp() throws Exception {
41 super.setUp();
42 final Properties properties = new Properties();
43 properties.put("python.cachedir", getDirectory());
44 PySystemState.initialize(properties, null, null);
45 }
46
47 public void testWithSimpleJythonException() throws Exception {
48 final PyException pe = new PyException();
49 final JythonScriptExecutionException e =
50 new JythonScriptExecutionException("Hello", pe);
51
52 assertNull(e.getCause());
53 assertContains(e.getShortMessage(), "Jython exception");
54 }
55
56 public void testWithJythonStringException() throws Exception {
57 final PyException pe = new PyException(new PyString("lah"));
58 final JythonScriptExecutionException e =
59 new JythonScriptExecutionException("Hello", pe);
60
61 assertNull(e.getCause());
62 assertContains(e.getShortMessage(), "Jython exception");
63 }
64
65 public void testWithJythonClassException() throws Exception {
66 final PyException pe = new PyException(Py.RuntimeError, "Its all wrong");
67 final JythonScriptExecutionException e =
68 new JythonScriptExecutionException("Hello", pe);
69
70 assertNull(e.getCause());
71 assertContains(e.getShortMessage(), "Jython exception");
72 }
73
74 public void testWithWrappedJavaException() throws Exception {
75 final Throwable wrapped = new Throwable();
76 final PyException pe = Py.JavaError(wrapped);
77 final JythonScriptExecutionException e =
78 new JythonScriptExecutionException("Hello", pe);
79
80 assertNotSame(pe, e.getCause());
81 assertSame(wrapped, e.getCause());
82
83 final StringWriter writer = new StringWriter();
84 e.printStackTrace(new PrintWriter(writer));
85 final String stack = writer.toString();
86
87 assertContains(stack, "Hello");
88 assertContains(stack, "java.lang.Throwable");
89 assertSame(wrapped, e.getCause());
90 assertContains(e.getShortMessage(), "Java exception");
91 }
92
93
94 public void testWithNullTraceBack() {
95 final PyException pe = new PyException();
96 pe.traceback = null;
97
98 final JythonScriptExecutionException e =
99 new JythonScriptExecutionException("Hello", pe);
100
101 assertContains(e.getShortMessage(), "Jython");
102
103 final StringWriter writer = new StringWriter();
104 e.printStackTrace(new PrintWriter(writer));
105 final String stack = writer.toString();
106
107 assertContains(stack, "None");
108 }
109 }