Debian Codegraph @

src/test/python/jpy_exception_test.py

raw symbols SCIP
1 # This file was modified by Deephaven Data Labs.
2 import unittest
3
4 import jpyutil
5 import string
6
7 jpyutil.init_jvm(jvm_maxmem='512M', jvm_classpath=['target/test-classes'])
8 import jpy
9
10
11 class TestExceptions(unittest.TestCase):
12
13 def setUp(self):
14 self.Fixture = jpy.get_type('org.jpy.fixtures.ExceptionTestFixture')
15 self.assertIsNotNone(self.Fixture)
16
17 def test_NullPointerException(self):
18 fixture = self.Fixture()
19
20 self.assertEqual(fixture.throwNpeIfArgIsNull("123456"), 6)
21
22 with self.assertRaises(RuntimeError, msg='Java NullPointerException expected') as e:
23 fixture.throwNpeIfArgIsNull(None)
24 self.assertTrue(str(e.exception).startswith( 'java.lang.NullPointerException'))
25
26 def test_ArrayIndexOutOfBoundsException(self):
27 fixture = self.Fixture()
28 self.assertEqual(fixture.throwAioobeIfIndexIsNotZero(0), 101)
29
30 def check_exception(ex_msg: str, expected_idx: int, expected_len: int):
31 """
32 Verify an ArrayIndexOutOfBoundsException. The exception message can vary by JVM.
33 :param ex_msg: The exception message
34 :param expected_idx: The invalid index expected in the exception message
35 :param expected_len: The expected length of the array that we attempted to access
36 """
37 valid_ex_msg_1 = f'java.lang.ArrayIndexOutOfBoundsException: {expected_idx}'
38 valid_ex_msg_2 = f'java.lang.ArrayIndexOutOfBoundsException: Index {expected_idx} out of bounds for length {expected_len}'
39 ex_msg_correct = (ex_msg == valid_ex_msg_1 or ex_msg == valid_ex_msg_2)
40
41 self.assertTrue(ex_msg_correct,
42 f'Exception message \'{ex_msg}\' does not match expectations: either \'{valid_ex_msg_1}\' or \'{valid_ex_msg_2}\'')
43
44 with self.assertRaises(RuntimeError, msg='Java ArrayIndexOutOfBoundsException expected') as e:
45 fixture.throwAioobeIfIndexIsNotZero(1)
46 check_exception(str(e.exception), 1, 1)
47
48 with self.assertRaises(RuntimeError, msg='Java ArrayIndexOutOfBoundsException expected') as e:
49 fixture.throwAioobeIfIndexIsNotZero(-1)
50 check_exception(str(e.exception), -1, 1)
51
52 def test_RuntimeException(self):
53 fixture = self.Fixture()
54 fixture.throwRteIfMessageIsNotNull(None)
55
56 with self.assertRaises(RuntimeError, msg='Java RuntimeException expected') as e:
57 fixture.throwRteIfMessageIsNotNull("Evil!")
58 self.assertEqual(str(e.exception), 'java.lang.RuntimeException: Evil!')
59
60 def test_IOException(self):
61 fixture = self.Fixture()
62 fixture.throwIoeIfMessageIsNotNull(None)
63
64 with self.assertRaises(RuntimeError, msg='Java IOException expected') as e:
65 fixture.throwIoeIfMessageIsNotNull("Evil!")
66 self.assertEqual(str(e.exception), 'java.io.IOException: Evil!')
67
68 # Checking the exceptions for differences (e.g. in white space) can be a huge pain, this helps)
69 def hexdump(self, s):
70 for i in range(0, len(s), 32):
71 sl = s[i:min(i + 32, len(s))]
72 fsl = map(lambda x: x if (x in string.printable and not x in "\n\t\r") else ".", sl)
73 print("%08d %s %s %s" % (i, " ".join("{:02x}".format(ord(c)) for c in sl),
74 (" ".join(map(lambda x : "", range(32 - len(sl))))), sl))
75
76 def test_VerboseException(self):
77 fixture = self.Fixture()
78
79 jpy.VerboseExceptions.enabled = True
80
81 self.assertEqual(fixture.throwNpeIfArgIsNull("123456"), 6)
82
83 with self.assertRaises(RuntimeError) as e:
84 fixture.throwNpeIfArgIsNullNested(None)
85 actual_message = str(e.exception)
86 expected_message = "java.lang.RuntimeException: Nested exception\n\t" \
87 "at org.jpy.fixtures.ExceptionTestFixture." \
88 "throwNpeIfArgIsNullNested(ExceptionTestFixture.java:43)\n" \
89 "caused by java.lang.NullPointerException\n\t" \
90 "at org.jpy.fixtures.ExceptionTestFixture." \
91 "throwNpeIfArgIsNull(ExceptionTestFixture.java:32)\n\t" \
92 "at org.jpy.fixtures.ExceptionTestFixture." \
93 "throwNpeIfArgIsNullNested(ExceptionTestFixture.java:41)\n"
94
95 # self.hexdump(actual_message)
96 # self.hexdump(expected_message)
97 # print [i for i in xrange(min(len(expected_message), len(actual_message))) if actual_message[i] != expected_message[i]]
98
99 self.assertIn("java.lang.NullPointerException", actual_message)
100
101 with self.assertRaises(RuntimeError) as e:
102 fixture.throwNpeIfArgIsNullNested3(None)
103 actual_message = str(e.exception)
104 expected_message = "java.lang.RuntimeException: Nested exception 3\n\t" \
105 "at org.jpy.fixtures.ExceptionTestFixture." \
106 "throwNpeIfArgIsNullNested3(ExceptionTestFixture.java:55)\n" \
107 "caused by java.lang.RuntimeException: Nested exception\n\t" \
108 "at org.jpy.fixtures.ExceptionTestFixture." \
109 "throwNpeIfArgIsNullNested(ExceptionTestFixture.java:43)\n\t" \
110 "at org.jpy.fixtures.ExceptionTestFixture." \
111 "throwNpeIfArgIsNullNested2(ExceptionTestFixture.java:48)\n\t" \
112 "at org.jpy.fixtures.ExceptionTestFixture." \
113 "throwNpeIfArgIsNullNested3(ExceptionTestFixture.java:53)\n" \
114 "caused by java.lang.NullPointerException\n\t" \
115 "at org.jpy.fixtures.ExceptionTestFixture." \
116 "throwNpeIfArgIsNull(ExceptionTestFixture.java:32)\n\t" \
117 "at org.jpy.fixtures.ExceptionTestFixture." \
118 "throwNpeIfArgIsNullNested(ExceptionTestFixture.java:41)\n\t... 2 more\n"
119
120 # self.hexdump(actual_message)
121 # self.hexdump(expected_message)
122 # print [i for i in xrange(min(len(expected_message), len(actual_message))) if actual_message[i] != expected_message[i]]
123
124 self.assertIn("java.lang.NullPointerException", actual_message)
125
126 jpy.VerboseExceptions.enabled = False
127
128
129 if __name__ == '__main__':
130 print('\nRunning ' + __file__)
131 unittest.main()