| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import jpype |
| 19 |
from jpype import java, JProxy |
| 20 |
from jpype.types import * |
| 21 |
import common |
| 22 |
|
| 23 |
|
| 24 |
def throwIOException(): |
| 25 |
raise java.io.IOException("Test throw") |
| 26 |
|
| 27 |
|
| 28 |
def throwByJavaException(): |
| 29 |
JClass('jpype.exc.ExceptionTest').throwIOException() |
| 30 |
|
| 31 |
|
| 32 |
class ExceptionTestCase(common.JPypeTestCase): |
| 33 |
def testExceptionThrown(self): |
| 34 |
ext = JClass('jpype.exc.ExceptionTest') |
| 35 |
try: |
| 36 |
ext.throwRuntime() |
| 37 |
self.fail() |
| 38 |
except JException as ex: |
| 39 |
self.assertIs(type(ex), java.lang.RuntimeException) |
| 40 |
self.assertEqual('Foo', ex.message()) |
| 41 |
trace = ex.stacktrace() |
| 42 |
self.assertTrue(str(trace).startswith( |
| 43 |
'java.lang.RuntimeException: Foo')) |
| 44 |
|
| 45 |
def testExceptionByJavaClass(self): |
| 46 |
ext = JClass('jpype.exc.ExceptionTest') |
| 47 |
try: |
| 48 |
ext.throwRuntime() |
| 49 |
self.fail() |
| 50 |
except java.lang.RuntimeException as ex: |
| 51 |
self.assertIs(type(ex), java.lang.RuntimeException) |
| 52 |
self.assertEqual('Foo', ex.message()) |
| 53 |
trace = ex.stacktrace() |
| 54 |
self.assertTrue(str(trace).startswith( |
| 55 |
'java.lang.RuntimeException: Foo')) |
| 56 |
|
| 57 |
def testThrowException(self): |
| 58 |
exthrow = JClass('jpype.exc.ExceptionThrower') |
| 59 |
extest = JClass('jpype.exc.ExceptionTest') |
| 60 |
d = {"throwIOException": throwIOException, } |
| 61 |
p = JProxy(exthrow, dict=d) |
| 62 |
self.assertTrue(extest.delegateThrow(p)) |
| 63 |
|
| 64 |
def testThrowException3(self): |
| 65 |
exthrow = JClass('jpype.exc.ExceptionThrower') |
| 66 |
extest = JClass('jpype.exc.ExceptionTest') |
| 67 |
d = {"throwIOException": throwByJavaException, } |
| 68 |
p = JProxy(exthrow, dict=d) |
| 69 |
|
| 70 |
self.assertTrue(extest.delegateThrow(p)) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
def testExceptionInstanceof(self): |
| 79 |
e = self.jpype.exc.ChildTestException() |
| 80 |
self.assertIsInstance(e, self.jpype.exc.ParentTestException) |
| 81 |
|
| 82 |
def testExceptionPYEXCInstanceof(self): |
| 83 |
e = self.jpype.exc.ChildTestException |
| 84 |
self.assertTrue(issubclass(e, self.jpype.exc.ParentTestException)) |
| 85 |
|
| 86 |
def testThrowChildExceptionFromCatchJExceptionParentClass(self): |
| 87 |
try: |
| 88 |
self.jpype.exc.ExceptionTest.throwChildTestException() |
| 89 |
self.fail() |
| 90 |
except self.jpype.exc.ParentTestException as ex: |
| 91 |
self.assertIsInstance(ex, self.jpype.exc.ChildTestException) |
| 92 |
|
| 93 |
def testCause(self): |
| 94 |
cls = jpype.JClass("jpype.exc.ExceptionTest") |
| 95 |
try: |
| 96 |
cls.throwChain() |
| 97 |
except Exception as ex: |
| 98 |
ex1 = ex |
| 99 |
|
| 100 |
self.assertEqual(str(ex1.__cause__), "Java Exception") |
| 101 |
frame = ex1.__cause__.__traceback__ |
| 102 |
expected = [ |
| 103 |
'jpype.exc.ExceptionTest.throwChain', |
| 104 |
'jpype.exc.ExceptionTest.method1', |
| 105 |
'jpype.exc.ExceptionTest.method2', |
| 106 |
] |
| 107 |
i = 0 |
| 108 |
while (frame): |
| 109 |
self.assertEqual(frame.tb_frame.f_code.co_name, expected[i]) |
| 110 |
frame = frame.tb_next |
| 111 |
i += 1 |
| 112 |
|
| 113 |
def testIndexError(self): |
| 114 |
with self.assertRaises(IndexError): |
| 115 |
raise java.lang.IndexOutOfBoundsException("From Java") |
| 116 |
|
| 117 |
def testValueError(self): |
| 118 |
js = JObject(None, JString) |
| 119 |
with self.assertRaises(ValueError): |
| 120 |
js.substring(0) |
| 121 |
|
| 122 |
def testExcCtor(self): |
| 123 |
WE = jpype.JClass("jpype.exc.WierdException") |
| 124 |
with self.assertRaises(WE): |
| 125 |
WE.testThrow() |
| 126 |
try: |
| 127 |
WE.testThrow() |
| 128 |
except Exception as ex: |
| 129 |
ex1 = ex |
| 130 |
self.assertEqual(ex1.args, ("Got it",)) |
| 131 |
|
| 132 |
def testExcCauseChained1(self): |
| 133 |
import jpype.imports |
| 134 |
try: |
| 135 |
from org.jpype.fail import BadInitializer |
| 136 |
except Exception as ex: |
| 137 |
ex1 = ex |
| 138 |
self.assertIsInstance(ex1, ImportError) |
| 139 |
self.assertIsInstance(ex1.__cause__, JClass( |
| 140 |
"java.lang.ExceptionInInitializerError")) |
| 141 |
self.assertIsInstance(ex1.__cause__.__cause__, JClass( |
| 142 |
"java.lang.ArrayIndexOutOfBoundsException")) |
| 143 |
self.assertTrue(ex1.__cause__.__traceback__ is not None) |
| 144 |
self.assertTrue(ex1.__cause__.__cause__.__traceback__ is not None) |
| 145 |
|
| 146 |
def testExcCauseChained2(self): |
| 147 |
try: |
| 148 |
JClass('org.jpype.fail.BadInitializer2') |
| 149 |
except Exception as ex: |
| 150 |
ex1 = ex |
| 151 |
self.assertIsInstance(ex1, JClass( |
| 152 |
'java.lang.ExceptionInInitializerError')) |
| 153 |
self.assertIsInstance(ex1.__cause__.__cause__, JClass( |
| 154 |
"java.lang.ArrayIndexOutOfBoundsException")) |
| 155 |
self.assertTrue(ex1.__cause__.__traceback__ is not None) |
| 156 |
self.assertTrue(ex1.__cause__.__cause__.__traceback__ is not None) |
| 157 |
|
| 158 |
def testExpandStacktrace(self): |
| 159 |
Th = jpype.JClass('java.lang.Throwable') |
| 160 |
null = jpype.JObject(None, Th) |
| 161 |
|
| 162 |
Th._expandStacktrace(null) |
| 163 |
|
| 164 |
def testException(self): |
| 165 |
Th = jpype.JClass('java.lang.Throwable')('abc') |
| 166 |
self.assertEqual(str(Th), 'java.lang.Throwable: abc') |