当前位置: 首页>>代码示例>>Python>>正文


Python JavaGateway.getNewExample方法代码示例

本文整理汇总了Python中py4j.java_gateway.JavaGateway.getNewExample方法的典型用法代码示例。如果您正苦于以下问题:Python JavaGateway.getNewExample方法的具体用法?Python JavaGateway.getNewExample怎么用?Python JavaGateway.getNewExample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在py4j.java_gateway.JavaGateway的用法示例。


在下文中一共展示了JavaGateway.getNewExample方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TypeConversionTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class TypeConversionTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        self.gateway = JavaGateway()

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()

    def testLongInt(self):
        ex = self.gateway.getNewExample()
        self.assertEqual(1, ex.method7(1234))
        self.assertEqual(4, ex.method7(2147483648))
        self.assertEqual(4, ex.method7(long(2147483648)))
        self.assertEqual(long(4), ex.method8(3))
        self.assertEqual(4, ex.method8(3))
        self.assertEqual(long(4), ex.method8(long(3)))
        self.assertEqual(long(4), ex.method9(long(3)))

    def testBigDecimal(self):
        ex = self.gateway.getNewExample()
        self.assertEqual(Decimal("2147483.647"), ex.method10(2147483647, 3))
        self.assertEqual(Decimal("-13.456"), ex.method10(Decimal("-14.456")))

    def testFloatConversion(self):
        java_inf = self.gateway.jvm.java.lang.Double.parseDouble("Infinity")
        self.assertEqual(float("inf"), java_inf)
        java_inf = self.gateway.jvm.java.lang.Double.parseDouble("+Infinity")
        self.assertEqual(float("inf"), java_inf)
        java_neg_inf = self.gateway.jvm.java.lang.Double.parseDouble(
            "-Infinity")
        self.assertEqual(float("-inf"), java_neg_inf)
        java_nan = self.gateway.jvm.java.lang.Double.parseDouble("NaN")
        self.assertTrue(math.isnan(java_nan))
开发者ID:18600597055,项目名称:hue,代码行数:36,代码来源:java_gateway_test.py

示例2: TypeConversionTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class TypeConversionTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        # This is to ensure that the server is started before connecting to it!
        time.sleep(1)
        self.gateway = JavaGateway()

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()

    def testLongInt(self):
        ex = self.gateway.getNewExample()
        self.assertEqual(1, ex.method7(1234))
        self.assertEqual(4, ex.method7(2147483648))
        self.assertEqual(4, ex.method7(long(2147483648)))
        self.assertEqual(long(4), ex.method8(3))
        self.assertEqual(4, ex.method8(3))
        self.assertEqual(long(4), ex.method8(long(3)))
        self.assertEqual(long(4), ex.method9(long(3)))

    def testBigDecimal(self):
        ex = self.gateway.getNewExample()
        self.assertEqual(Decimal("2147483.647"), ex.method10(2147483647, 3))
        self.assertEqual(Decimal("-13.456"), ex.method10(Decimal("-14.456")))
开发者ID:Isb1009,项目名称:py4j,代码行数:27,代码来源:java_gateway_test.py

示例3: HelpTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class HelpTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        self.gateway = JavaGateway()

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()

    def testHelpObject(self):
        ex = self.gateway.getNewExample()
        help_page = self.gateway.help(ex, short_name=True, display=False)
        self.assertGreater(len(help_page), 1)

    def testHelpObjectWithPattern(self):
        ex = self.gateway.getNewExample()
        help_page = self.gateway.help(
            ex, pattern="m*", short_name=True, display=False)
        self.assertGreater(len(help_page), 1)

    def testHelpClass(self):
        String = self.gateway.jvm.java.lang.String
        help_page = self.gateway.help(String, short_name=False, display=False)
        self.assertGreater(len(help_page), 1)
        self.assertIn("String", help_page)
开发者ID:bartdag,项目名称:py4j,代码行数:27,代码来源:java_gateway_test.py

示例4: StreamTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class StreamTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        self.gateway = JavaGateway()

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()

    def testBinarySuccess(self):
        e = self.gateway.getNewExample()

        # not binary - just get the Java object
        v1 = e.getStream()
        self.assertTrue(
            is_instance_of(
                self.gateway, v1, "java.nio.channels.ReadableByteChannel"))

        # pull it as a binary stream
        with e.getStream.stream() as conn:
            self.assertTrue(isinstance(conn, GatewayConnectionGuard))
            expected =\
                "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
            self.assertEqual(expected, smart_decode(conn.read(len(expected))))

    def testBinaryFailure(self):
        e = self.gateway.getNewExample()
        self.assertRaises(Py4JJavaError, lambda: e.getBrokenStream())
        self.assertRaises(Py4JJavaError, lambda: e.getBrokenStream.stream())

    def testNotAStream(self):
        e = self.gateway.getNewExample()
        self.assertEqual(1, e.method1())
        self.assertRaises(Py4JError, lambda: e.method1.stream())
开发者ID:bartdag,项目名称:py4j,代码行数:36,代码来源:java_gateway_test.py

示例5: HelpTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class HelpTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        # This is to ensure that the server is started before connecting to it!
        time.sleep(1)
        self.gateway = JavaGateway()

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()

    def testHelpObject(self):
        ex = self.gateway.getNewExample()
        help_page = self.gateway.help(ex, short_name=True, display=False)
        print(help_page)
        self.assertEqual(939, len(help_page))

    def testHelpObjectWithPattern(self):
        ex = self.gateway.getNewExample()
        help_page = self.gateway.help(ex, pattern='m*', short_name=True,
                display=False)
        print(help_page)
        self.assertEqual(644, len(help_page))

    def testHelpClass(self):
        String = self.gateway.jvm.java.lang.String
        help_page = self.gateway.help(String, short_name=False, display=False)
        print(help_page)
        self.assertEqual(3439, len(help_page))
开发者ID:gdw2,项目名称:py4j,代码行数:31,代码来源:java_gateway_test.py

示例6: IntegrationTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class IntegrationTest(unittest.TestCase):
    def setUp(self):
        self.p = start_echo_server_process()
        # This is to ensure that the server is started before connecting to it!
        time.sleep(1)

    def tearDown(self):
        # Safety check in case there was an exception...
        safe_shutdown(self)
        self.p.join()

    def testIntegration(self):
        try:
            testSocket = get_test_socket()
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('yro0\n'.encode('utf-8'))
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('ysHello World\n'.encode('utf-8'))
#            testSocket.sendall('yo\n'.encode('utf-8')) # No need because getNewExampe is in cache now!
            testSocket.sendall('yro1\n'.encode('utf-8'))
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('ysHello World2\n'.encode('utf-8'))
            testSocket.close()
            time.sleep(1)

            self.gateway = JavaGateway(auto_field=True)
            ex = self.gateway.getNewExample()
            response = ex.method3(1, True)
            self.assertEqual('Hello World', response)
            ex2 = self.gateway.entry_point.getNewExample()
            response = ex2.method3(1, True)
            self.assertEqual('Hello World2', response)
            self.gateway.shutdown()
        except Exception as e:
            print('Error has occurred', e)
            self.fail('Problem occurred')

    def testException(self):
        try:
            testSocket = get_test_socket()
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('yro0\n'.encode('utf-8'))
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall(b'x\n')
            testSocket.close()
            time.sleep(1)

            self.gateway = JavaGateway(auto_field=True)
            ex = self.gateway.getNewExample()

            try:
                ex.method3(1, True)
                self.fail('Should have failed!')
            except Py4JError:
                self.assertTrue(True)
            self.gateway.shutdown()
        except Exception as e:
            print('Error has occurred', e)
            self.fail('Problem occurred')
开发者ID:pellis10asee,项目名称:sharkhunter-shb,代码行数:61,代码来源:java_gateway_test.py

示例7: IntegrationTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class IntegrationTest(unittest.TestCase):
    def setUp(self):
        self.p = start_echo_server_process()
        # This is to ensure that the server is started before connecting to it!

    def tearDown(self):
        # Safety check in case there was an exception...
        safe_shutdown(self)
        self.p.join()

    def testIntegration(self):
        try:
            testSocket = get_socket()
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!yro0\n".encode("utf-8"))
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!ysHello World\n".encode("utf-8"))
            testSocket.sendall("!yro1\n".encode("utf-8"))
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!ysHello World2\n".encode("utf-8"))
            testSocket.close()
            sleep()

            self.gateway = JavaGateway(
                gateway_parameters=GatewayParameters(auto_field=True))
            ex = self.gateway.getNewExample()
            response = ex.method3(1, True)
            self.assertEqual("Hello World", response)
            ex2 = self.gateway.entry_point.getNewExample()
            response = ex2.method3(1, True)
            self.assertEqual("Hello World2", response)
            self.gateway.shutdown()
        except Exception:
            self.fail("Problem occurred")

    def testException(self):
        try:
            testSocket = get_socket()
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!yro0\n".encode("utf-8"))
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall(b"!x\n")
            testSocket.close()
            sleep()

            self.gateway = JavaGateway(
                gateway_parameters=GatewayParameters(auto_field=True))
            ex = self.gateway.getNewExample()

            self.assertRaises(Py4JError, lambda: ex.method3(1, True))
            self.gateway.shutdown()
        except Exception:
            self.fail("Problem occurred")
开发者ID:bartdag,项目名称:py4j,代码行数:55,代码来源:java_gateway_test.py

示例8: TypeConversionTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class TypeConversionTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        self.gateway = JavaGateway()

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()

    def testLongInt(self):
        ex = self.gateway.getNewExample()
        self.assertEqual(1, ex.method7(1234))
        self.assertEqual(4, ex.method7(2147483648))
        self.assertEqual(4, ex.method7(-2147483649))
        self.assertEqual(4, ex.method7(long(2147483648)))
        self.assertEqual(long(4), ex.method8(3))
        self.assertEqual(4, ex.method8(3))
        self.assertEqual(long(4), ex.method8(long(3)))
        self.assertEqual(long(4), ex.method9(long(3)))
        try:
            ex.method8(3000000000000000000000000000000000000)
            self.fail("Should not be able to convert overflowing long")
        except Py4JError:
            self.assertTrue(True)
        # Check that the connection is not broken (refs #265)
        self.assertEqual(4, ex.method8(3))

    def testBigDecimal(self):
        ex = self.gateway.getNewExample()
        self.assertEqual(Decimal("2147483.647"), ex.method10(2147483647, 3))
        self.assertEqual(Decimal("-13.456"), ex.method10(Decimal("-14.456")))

    def testFloatConversion(self):
        java_inf = self.gateway.jvm.java.lang.Double.parseDouble("Infinity")
        self.assertEqual(float("inf"), java_inf)
        java_inf = self.gateway.jvm.java.lang.Double.parseDouble("+Infinity")
        self.assertEqual(float("inf"), java_inf)
        java_neg_inf = self.gateway.jvm.java.lang.Double.parseDouble(
            "-Infinity")
        self.assertEqual(float("-inf"), java_neg_inf)
        java_nan = self.gateway.jvm.java.lang.Double.parseDouble("NaN")
        self.assertTrue(math.isnan(java_nan))

        python_double = 17.133574204226083
        java_float = self.gateway.jvm.java.lang.Double(python_double)
        self.assertAlmostEqual(python_double, java_float, 15)

    def testUnboxingInt(self):
        ex = self.gateway.getNewExample()
        self.assertEqual(4, ex.getInteger(4))
开发者ID:bartdag,项目名称:py4j,代码行数:52,代码来源:java_gateway_test.py

示例9: MethodTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class MethodTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        # This is to ensure that the server is started before connecting to it!
        time.sleep(1)
        self.gateway = JavaGateway()

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()

    def testNoneArg(self):
        ex = self.gateway.getNewExample()
        try:
            ex.method2(None)
            ex2 = ex.method4(None)
            self.assertEquals(ex2.getField1(), 3)
            self.assertEquals(2, ex.method7(None))
        except Exception:
            print_exc()
            self.fail()

    def testUnicode(self):
        sb = self.gateway.jvm.java.lang.StringBuffer()
        sb.append('\r\n\tHello\r\n\t')
        self.assertEqual('\r\n\tHello\r\n\t', sb.toString())

    def testEscape(self):
        sb = self.gateway.jvm.java.lang.StringBuffer()
        sb.append('\r\n\tHello\r\n\t')
        self.assertEqual('\r\n\tHello\r\n\t', sb.toString())
开发者ID:gdw2,项目名称:py4j,代码行数:33,代码来源:java_gateway_test.py

示例10: ProtocolTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class ProtocolTest(unittest.TestCase):
    def tearDown(self):
        # Safety check in case there was an exception...
        safe_shutdown(self)

    def testEscape(self):
        self.assertEqual("Hello\t\rWorld\n\\", unescape_new_line(escape_new_line("Hello\t\rWorld\n\\")))
        self.assertEqual("Hello\t\rWorld\n\\", unescape_new_line(escape_new_line("Hello\t\rWorld\n\\")))

    def testProtocolSend(self):
        testConnection = TestConnection()
        self.gateway = JavaGateway()

        # Replace gateway client by test connection
        self.gateway.set_gateway_client(testConnection)

        e = self.gateway.getExample()
        self.assertEqual("c\nt\ngetExample\ne\n", testConnection.last_message)
        e.method1(1, True, "Hello\nWorld", e, None, 1.5)
        self.assertEqual("c\no0\nmethod1\ni1\nbTrue\nsHello\\nWorld\nro0\nn\nd1.5\ne\n", testConnection.last_message)
        del (e)

    def testProtocolReceive(self):
        p = start_echo_server_process()
        try:
            testSocket = get_socket()
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!yro0\n".encode("utf-8"))
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!ysHello World\n".encode("utf-8"))
            # No extra echange (method3) because it is already cached.
            testSocket.sendall("!yi123\n".encode("utf-8"))
            testSocket.sendall("!yd1.25\n".encode("utf-8"))
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!yn\n".encode("utf-8"))
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!ybTrue\n".encode("utf-8"))
            testSocket.sendall("!yo\n".encode("utf-8"))
            testSocket.sendall("!yL123\n".encode("utf-8"))
            testSocket.sendall("!ydinf\n".encode("utf-8"))
            testSocket.close()
            sleep()

            self.gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_field=True))
            ex = self.gateway.getNewExample()
            self.assertEqual("Hello World", ex.method3(1, True))
            self.assertEqual(123, ex.method3())
            self.assertAlmostEqual(1.25, ex.method3())
            self.assertTrue(ex.method2() is None)
            self.assertTrue(ex.method4())
            self.assertEqual(long(123), ex.method8())
            self.assertEqual(float("inf"), ex.method8())
            self.gateway.shutdown()

        except Exception:
            print_exc()
            self.fail("Problem occurred")
        p.join()
开发者ID:bartdag,项目名称:py4j,代码行数:60,代码来源:java_gateway_test.py

示例11: ProtocolTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class ProtocolTest(unittest.TestCase):
    def tearDown(self):
        # Safety check in case there was an exception...
        safe_shutdown(self)

    def testEscape(self):
        self.assertEqual("Hello\t\rWorld\n\\", unescape_new_line(
            escape_new_line("Hello\t\rWorld\n\\")))
        self.assertEqual("Hello\t\rWorld\n\\", unescape_new_line(
            escape_new_line("Hello\t\rWorld\n\\")))

    def testProtocolSend(self):
        testConnection = TestConnection()
        self.gateway = JavaGateway(testConnection, False)
        e = self.gateway.getExample()
        self.assertEqual('c\nt\ngetExample\ne\n', testConnection.last_message)
        e.method1(1, True, 'Hello\nWorld', e, None, 1.5)
        self.assertEqual(
                'c\no0\nmethod1\ni1\nbTrue\nsHello\\nWorld\nro0\nn\nd1.5\ne\n',
                testConnection.last_message)
        del(e)

    def testProtocolReceive(self):
        p = start_echo_server_process()
        time.sleep(1)
        try:
            testSocket = get_socket()
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('yro0\n'.encode('utf-8'))
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('ysHello World\n'.encode('utf-8'))
            # No extra echange (method3) because it is already cached.
            testSocket.sendall('yi123\n'.encode('utf-8'))
            testSocket.sendall('yd1.25\n'.encode('utf-8'))
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('yn\n'.encode('utf-8'))
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('ybTrue\n'.encode('utf-8'))
            testSocket.sendall('yo\n'.encode('utf-8'))
            testSocket.sendall('yL123\n'.encode('utf-8'))
            testSocket.close()
            time.sleep(1)

            self.gateway = JavaGateway(auto_field=True)
            ex = self.gateway.getNewExample()
            self.assertEqual('Hello World', ex.method3(1, True))
            self.assertEqual(123, ex.method3())
            self.assertAlmostEqual(1.25, ex.method3())
            self.assertTrue(ex.method2() is None)
            self.assertTrue(ex.method4())
            self.assertEqual(long(123), ex.method8())
            self.gateway.shutdown()

        except Exception as e:
            print('Error has occurred', e)
            print_exc()
            self.fail('Problem occurred')
        p.join()
开发者ID:Isb1009,项目名称:py4j,代码行数:60,代码来源:java_gateway_test.py

示例12: AutoConvertTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class AutoConvertTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        self.gateway = JavaGateway(
            gateway_parameters=GatewayParameters(auto_convert=True))

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()
        sleep()

    def testAutoConvert(self):
        ex = self.gateway.getNewExample()
        pList = get_list(3)
        jList = ex.getList(3)
        self.assertTrue(jList.equals(pList))
开发者ID:DawnScience,项目名称:py4j,代码行数:18,代码来源:java_list_test.py

示例13: AutoConvertTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class AutoConvertTest(unittest.TestCase):
    def setUp(self):
#        logger = logging.getLogger("py4j")
#        logger.setLevel(logging.DEBUG)
#        logger.addHandler(logging.StreamHandler())
        self.p = start_example_app_process()
        time.sleep(0.5)
        self.gateway = JavaGateway(auto_convert=True)

    def tearDown(self):
        self.p.terminate()
        self.gateway.shutdown()
        time.sleep(0.5)

    def testAutoConvert(self):
        ex = self.gateway.getNewExample()
        pList = get_list(3)
        jList = ex.getList(3)
        self.assertTrue(jList.equals(pList))
开发者ID:pellis10asee,项目名称:sharkhunter-shb,代码行数:21,代码来源:java_list_test.py

示例14: AutoConvertTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class AutoConvertTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        self.gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_convert=True))

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()
        sleep()

    def testAutoConvert(self):
        ex = self.gateway.getNewExample()
        python_list = get_list(3)
        java_list = ex.getList(3)
        self.assertTrue(java_list.equals(python_list))

    def testAutoConvertConstructor(self):
        python_list = get_list(3)
        java_list = self.gateway.jvm.java.util.ArrayList(python_list)
        self.assertTrue(java_list.equals(python_list))

    def testAutoConvertNotByteArray(self):
        self.gateway.jvm.java.nio.ByteBuffer.wrap(bytearray(range(255)))
开发者ID:mrfaheemkhan,项目名称:py4j,代码行数:25,代码来源:java_list_test.py

示例15: FieldTest

# 需要导入模块: from py4j.java_gateway import JavaGateway [as 别名]
# 或者: from py4j.java_gateway.JavaGateway import getNewExample [as 别名]
class FieldTest(unittest.TestCase):
    def setUp(self):
        self.p = start_example_app_process()
        # This is to ensure that the server is started before connecting to it!
        time.sleep(1)

    def tearDown(self):
        safe_shutdown(self)
        self.p.join()

    def testAutoField(self):
        self.gateway = JavaGateway(auto_field=True)
        ex = self.gateway.getNewExample()
        self.assertEqual(ex.field10, 10)
        sb = ex.field20
        sb.append('Hello')
        self.assertEqual('Hello', sb.toString())
        self.assertTrue(ex.field21 == None)

    def testNoField(self):
        self.gateway = JavaGateway(auto_field=True)
        ex = self.gateway.getNewExample()
        member = ex.field50
        self.assertTrue(isinstance(member, JavaMember))

    def testNoAutoField(self):
        self.gateway = JavaGateway(auto_field=False)
        ex = self.gateway.getNewExample()
        self.assertTrue(isinstance(ex.field10, JavaMember))
        self.assertTrue(isinstance(ex.field50, JavaMember))
        self.assertEqual(10, get_field(ex, 'field10'))

        try:
            get_field(ex, 'field50')
            self.fail()
        except Exception:
            self.assertTrue(True)

        ex._auto_field = True
        sb = ex.field20
        sb.append('Hello')
        self.assertEqual('Hello', sb.toString())

        try:
            get_field(ex, 'field20')
            self.fail()
        except Exception:
            self.assertTrue(True)

    def testSetField(self):
        self.gateway = JavaGateway(auto_field=False)
        ex = self.gateway.getNewExample()

        set_field(ex, 'field10', 2334)
        self.assertEquals(get_field(ex, 'field10'), 2334)

        sb = self.gateway.jvm.java.lang.StringBuffer('Hello World!')
        set_field(ex, 'field21', sb)
        self.assertEquals(get_field(ex, 'field21').toString(), 'Hello World!')

        try:
            set_field(ex, 'field1', 123)
            self.fail()
        except Exception:
            self.assertTrue(True)

    def testGetMethod(self):
        # This is necessary if a field hides a method...
        self.gateway = JavaGateway()
        ex = self.gateway.getNewExample()
        self.assertEqual(1, get_method(ex, 'method1')())
开发者ID:gdw2,项目名称:py4j,代码行数:73,代码来源:java_gateway_test.py


注:本文中的py4j.java_gateway.JavaGateway.getNewExample方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。