2015-05-01 21:57:22 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import absolute_import
|
2012-08-15 14:31:22 +00:00
|
|
|
import unittest
|
|
|
|
from jnius import JavaException, JavaClass
|
|
|
|
from jnius.reflect import autoclass
|
|
|
|
|
|
|
|
class BadDeclarationTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_class_not_found(self):
|
2012-08-15 17:16:39 +00:00
|
|
|
#self.assertRaises(JavaException, autoclass, 'org.unknow.class')
|
|
|
|
#self.assertRaises(JavaException, autoclass, 'java/lang/String')
|
|
|
|
pass
|
2012-08-15 14:31:22 +00:00
|
|
|
|
|
|
|
def test_invalid_attribute(self):
|
|
|
|
Stack = autoclass('java.util.Stack')
|
|
|
|
self.assertRaises(AttributeError, getattr, Stack, 'helloworld')
|
|
|
|
|
|
|
|
def test_invalid_static_call(self):
|
|
|
|
Stack = autoclass('java.util.Stack')
|
|
|
|
self.assertRaises(JavaException, Stack.push, 'hello')
|
|
|
|
|
|
|
|
def test_with_too_much_arguments(self):
|
|
|
|
Stack = autoclass('java.util.Stack')
|
|
|
|
stack = Stack()
|
|
|
|
self.assertRaises(JavaException, stack.push, 'hello', 'world', 123)
|
2014-05-16 00:12:12 +00:00
|
|
|
|
|
|
|
def test_java_exception_handling(self):
|
|
|
|
Stack = autoclass('java.util.Stack')
|
|
|
|
stack = Stack()
|
|
|
|
try:
|
|
|
|
stack.pop()
|
|
|
|
self.fail("Expected exception to be thrown")
|
|
|
|
except JavaException as je:
|
2014-05-18 22:22:36 +00:00
|
|
|
# print "Got JavaException: " + str(je)
|
|
|
|
# print "Got Exception Class: " + je.classname
|
|
|
|
# print "Got stacktrace: \n" + '\n'.join(je.stacktrace)
|
2020-04-03 21:48:26 +00:00
|
|
|
self.assertEqual("java.util.EmptyStackException", je.classname)
|
2014-05-18 22:22:36 +00:00
|
|
|
|
|
|
|
def test_java_exception_chaining(self):
|
|
|
|
BasicsTest = autoclass('org.jnius.BasicsTest')
|
|
|
|
basics = BasicsTest()
|
|
|
|
try:
|
|
|
|
basics.methodExceptionChained()
|
|
|
|
self.fail("Expected exception to be thrown")
|
|
|
|
except JavaException as je:
|
|
|
|
# print "Got JavaException: " + str(je)
|
|
|
|
# print "Got Exception Class: " + je.classname
|
|
|
|
# print "Got Exception Message: " + je.innermessage
|
|
|
|
# print "Got stacktrace: \n" + '\n'.join(je.stacktrace)
|
2020-04-03 21:48:26 +00:00
|
|
|
self.assertEqual("java.lang.IllegalArgumentException", je.classname)
|
|
|
|
self.assertEqual("helloworld2", je.innermessage)
|
2014-05-18 22:22:36 +00:00
|
|
|
self.assertIn("Caused by:", je.stacktrace)
|
2020-04-03 21:48:26 +00:00
|
|
|
self.assertEqual(11, len(je.stacktrace))
|