View Javadoc

1   // Copyright (C) 2005 - 2010 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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    // Bug 2988755
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 }