From 60ee14ed6fff0d82cc26ae83ccd4053cf2c0483b Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Sat, 18 Aug 2012 12:59:15 +0200 Subject: [PATCH] add the previous tests from python-for-android/javawrapper branch. + fix findclass (need cleaning) + fix array return with chars + export reflect module by default (it's already used in jnius so..) --- Makefile | 1 + jnius/__init__.py | 6 +- jnius/jnius.pyx | 19 +++++- jnius/jnius_conversion.pxi | 2 +- jnius/jnius_jvm_desktop.pxi | 14 +++- jnius/reflect.py | 5 +- tests/org/jnius/BasicsTest.java | 115 ++++++++++++++++++++++++++++++++ tests/test_basics.py | 79 ++++++++++++++++++++++ tests/test_cast.py | 2 +- tests/test_simple.py | 2 +- 10 files changed, 234 insertions(+), 11 deletions(-) create mode 100644 tests/org/jnius/BasicsTest.java create mode 100644 tests/test_basics.py diff --git a/Makefile b/Makefile index 928a7be..cf9a76f 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,5 @@ build_ext: tests: build_ext cd tests && javac org/jnius/HelloWorld.java + cd tests && javac org/jnius/BasicsTest.java cd tests && env PYTHONPATH=..:$(PYTHONPATH) nosetests -v diff --git a/jnius/__init__.py b/jnius/__init__.py index 5386691..dfdb719 100644 --- a/jnius/__init__.py +++ b/jnius/__init__.py @@ -1,4 +1,2 @@ -from .jnius import (JavaObject, JavaMethod, JavaStaticMethod, - JavaField, JavaStaticField, JavaClass, MetaJavaClass, - JavaException, cast) - +from .jnius import * +from .reflect import * diff --git a/jnius/jnius.pyx b/jnius/jnius.pyx index 34a208b..7b97e4b 100644 --- a/jnius/jnius.pyx +++ b/jnius/jnius.pyx @@ -86,7 +86,8 @@ Python:: ''' __all__ = ('JavaObject', 'JavaClass', 'JavaMethod', 'JavaStaticMethod', - 'JavaField', 'JavaStaticField', 'MetaJavaClass', 'JavaException', 'cast') + 'JavaField', 'JavaStaticField', 'MetaJavaClass', 'JavaException', 'cast', + 'find_javaclass') from libc.stdlib cimport malloc, free @@ -929,3 +930,19 @@ def cast(destclass, obj): jc.instanciate_from(jobj.j_self) return jc +def find_javaclass(bytes name): + from reflect import Class + cdef JavaClass cls + cdef jclass jc + cdef JNIEnv *j_env = get_jnienv() + + name = name.replace('.', '/') + + jc = j_env[0].FindClass(j_env, name) + if jc == NULL: + raise JavaException('Class not found {0!r}'.format(name)) + + cls = Class(noinstance=True) + cls.instanciate_from(create_local_ref(j_env, jc)) + return cls + diff --git a/jnius/jnius_conversion.pxi b/jnius/jnius_conversion.pxi index 83b6c56..cda924f 100644 --- a/jnius/jnius_conversion.pxi +++ b/jnius/jnius_conversion.pxi @@ -75,7 +75,7 @@ cdef convert_jarray_to_python(JNIEnv *j_env, definition, jobject j_object): elif r == 'C': j_chars = j_env[0].GetCharArrayElements( j_env, j_object, &iscopy) - ret = [(j_chars[i]) for i in range(array_size)] + ret = [chr(j_chars[i]) for i in range(array_size)] if iscopy: j_env[0].ReleaseCharArrayElements( j_env, j_object, j_chars, 0) diff --git a/jnius/jnius_jvm_desktop.pxi b/jnius/jnius_jvm_desktop.pxi index 9d30401..2208eb9 100644 --- a/jnius/jnius_jvm_desktop.pxi +++ b/jnius/jnius_jvm_desktop.pxi @@ -9,15 +9,27 @@ cdef extern from "jni.h": jint version jint nOptions jboolean ignoreUnrecognized + JavaVMOption *options + ctypedef struct JavaVMOption: + char *optionString + void *extraInfo cdef JNIEnv *default_env = NULL cdef void create_jnienv(): cdef JavaVM* jvm cdef JavaVMInitArgs args + cdef JavaVMOption options[1] + cdef bytes py_bytes + + from os.path import realpath + py_bytes = ('-Djava.class.path={0}'.format(realpath('.'))) + options[0].optionString = py_bytes + options[0].extraInfo = NULL args.version = JNI_VERSION_1_4 - args.nOptions = 0 + args.options = options + args.nOptions = 1 args.ignoreUnrecognized = JNI_FALSE JNI_CreateJavaVM(&jvm, &default_env, &args) diff --git a/jnius/reflect.py b/jnius/reflect.py index e5c2099..e6b32de 100644 --- a/jnius/reflect.py +++ b/jnius/reflect.py @@ -1,7 +1,7 @@ __all__ = ('autoclass', 'ensureclass') from jnius import JavaClass, MetaJavaClass, JavaMethod, JavaStaticMethod, \ - JavaField, JavaStaticField, JavaMethodMultiple + JavaField, JavaStaticField, JavaMethodMultiple, find_javaclass class Class(JavaClass): __metaclass__ = MetaJavaClass @@ -100,7 +100,8 @@ def autoclass(clsname): classDict = {} - c = Class.forName(clsname) + #c = Class.forName(clsname) + c = find_javaclass(clsname) if c is None: raise Exception('Java class {0} not found'.format(c)) return None diff --git a/tests/org/jnius/BasicsTest.java b/tests/org/jnius/BasicsTest.java new file mode 100644 index 0000000..091e71c --- /dev/null +++ b/tests/org/jnius/BasicsTest.java @@ -0,0 +1,115 @@ +package org.jnius; + +import java.lang.String; + +public class BasicsTest { + static public boolean methodStaticZ() { return true; }; + static public byte methodStaticB() { return 127; }; + static public char methodStaticC() { return 'k'; }; + static public short methodStaticS() { return 32767; }; + static public int methodStaticI() { return 2147483467; }; + static public long methodStaticJ() { return 2147483467; }; + static public float methodStaticF() { return 1.23456789f; }; + static public double methodStaticD() { return 1.23456789; }; + static public String methodStaticString() { return new String("helloworld"); } + + public boolean methodZ() { return true; }; + public byte methodB() { return 127; }; + public char methodC() { return 'k'; }; + public short methodS() { return 32767; }; + public int methodI() { return 2147483467; }; + public long methodJ() { return 2147483467; }; + public float methodF() { return 1.23456789f; }; + public double methodD() { return 1.23456789; }; + public String methodString() { return new String("helloworld"); } + + static public boolean fieldStaticZ = true; + static public byte fieldStaticB = 127; + static public char fieldStaticC = 'k'; + static public short fieldStaticS = 32767; + static public int fieldStaticI = 2147483467; + static public long fieldStaticJ = 2147483467; + static public float fieldStaticF = 1.23456789f; + static public double fieldStaticD = 1.23456789; + static public String fieldStaticString = new String("helloworld"); + + public boolean fieldZ = true; + public byte fieldB = 127; + public char fieldC = 'k'; + public short fieldS = 32767; + public int fieldI = 2147483467; + public long fieldJ = 2147483467; + public float fieldF = 1.23456789f; + public double fieldD = 1.23456789; + public String fieldString = new String("helloworld"); + + public boolean[] methodArrayZ() { + boolean[] x = new boolean[3]; + x[0] = x[1] = x[2] = true; + return x; + }; + public byte[] methodArrayB() { + byte[] x = new byte[3]; + x[0] = x[1] = x[2] = 127; + return x; + }; + public char[] methodArrayC() { + char[] x = new char[3]; + x[0] = x[1] = x[2] = 'k'; + return x; + }; + public short[] methodArrayS() { + short[] x = new short[3]; + x[0] = x[1] = x[2] = 32767; + return x; + }; + public int[] methodArrayI() { + int[] x = new int[3]; + x[0] = x[1] = x[2] = 2147483467; + return x; + }; + public long[] methodArrayJ() { + long[] x = new long[3]; + x[0] = x[1] = x[2] = 2147483467; + return x; + }; + public float[] methodArrayF() { + float[] x = new float[3]; + x[0] = x[1] = x[2] = 1.23456789f; + return x; + }; + public double[] methodArrayD() { + double[] x = new double[3]; + x[0] = x[1] = x[2] = 1.23456789; + return x; + }; + public String[] methodArrayString() { + String[] x = new String[3]; + x[0] = x[1] = x[2] = new String("helloworld"); + return x; + }; + + + public boolean methodParamsZBCSIJFD(boolean x1, byte x2, char x3, short x4, + int x5, long x6, float x7, double x8) { + // ADD float / double, but dunno how to do with approx + return (x1 == true && x2 == 127 && x3 == 'k' && x4 == 32767 && + x5 == 2147483467 && x6 == 2147483467); + } + + public boolean methodParamsString(String s) { + return (s.equals("helloworld")); + } + + public boolean methodParamsArrayI(int[] x) { + if (x.length != 3) + return false; + return (x[0] == 1 && x[1] == 2 && x[2] == 3); + } + + public boolean methodParamsArrayString(String[] x) { + if (x.length != 2) + return false; + return (x[0].equals("hello") && x[1].equals("world")); + } +} diff --git a/tests/test_basics.py b/tests/test_basics.py new file mode 100644 index 0000000..fb81f60 --- /dev/null +++ b/tests/test_basics.py @@ -0,0 +1,79 @@ +import unittest +from jnius.reflect import autoclass + +class BasicsTest(unittest.TestCase): + + def test_static_methods(self): + Test = autoclass('org.jnius.BasicsTest') + self.assertEquals(Test.methodStaticZ(), True) + self.assertEquals(Test.methodStaticB(), 127) + self.assertEquals(Test.methodStaticC(), 'k') + self.assertEquals(Test.methodStaticS(), 32767) + self.assertEquals(Test.methodStaticI(), 2147483467) + self.assertEquals(Test.methodStaticJ(), 2147483467) + self.assertAlmostEquals(Test.methodStaticF(), 1.23456789) + self.assertEquals(Test.methodStaticD(), 1.23456789) + self.assertEquals(Test.methodStaticString(), 'helloworld') + + def test_static_fields(self): + Test = autoclass('org.jnius.BasicsTest') + self.assertEquals(Test.fieldStaticZ, True) + self.assertEquals(Test.fieldStaticB, 127) + self.assertEquals(Test.fieldStaticC, 'k') + self.assertEquals(Test.fieldStaticS, 32767) + self.assertEquals(Test.fieldStaticI, 2147483467) + self.assertEquals(Test.fieldStaticJ, 2147483467) + self.assertAlmostEquals(Test.fieldStaticF, 1.23456789) + self.assertEquals(Test.fieldStaticD, 1.23456789) + self.assertEquals(Test.fieldStaticString, 'helloworld') + + def test_instance_methods(self): + test = autoclass('org.jnius.BasicsTest')() + self.assertEquals(test.methodZ(), True) + self.assertEquals(test.methodB(), 127) + self.assertEquals(test.methodC(), 'k') + self.assertEquals(test.methodS(), 32767) + self.assertEquals(test.methodI(), 2147483467) + self.assertEquals(test.methodJ(), 2147483467) + self.assertAlmostEquals(test.methodF(), 1.23456789) + self.assertEquals(test.methodD(), 1.23456789) + self.assertEquals(test.methodString(), 'helloworld') + + def test_instance_fields(self): + test = autoclass('org.jnius.BasicsTest')() + self.assertEquals(test.fieldZ, True) + self.assertEquals(test.fieldB, 127) + self.assertEquals(test.fieldC, 'k') + self.assertEquals(test.fieldS, 32767) + self.assertEquals(test.fieldI, 2147483467) + self.assertEquals(test.fieldJ, 2147483467) + self.assertAlmostEquals(test.fieldF, 1.23456789) + self.assertEquals(test.fieldD, 1.23456789) + self.assertEquals(test.fieldString, 'helloworld') + + def test_instances_methods_array(self): + test = autoclass('org.jnius.BasicsTest')() + self.assertEquals(test.methodArrayZ(), [True] * 3) + self.assertEquals(test.methodArrayB(), [127] * 3) + self.assertEquals(test.methodArrayC(), ['k'] * 3) + self.assertEquals(test.methodArrayS(), [32767] * 3) + self.assertEquals(test.methodArrayI(), [2147483467] * 3) + self.assertEquals(test.methodArrayJ(), [2147483467] * 3) + + ret = test.methodArrayF() + ref = [1.23456789] * 3 + self.assertAlmostEquals(ret[0], ref[0]) + self.assertAlmostEquals(ret[1], ref[1]) + self.assertAlmostEquals(ret[2], ref[2]) + + self.assertEquals(test.methodArrayD(), [1.23456789] * 3) + self.assertEquals(test.methodArrayString(), ['helloworld'] * 3) + + def test_instances_methods_params(self): + test = autoclass('org.jnius.BasicsTest')() + self.assertEquals(test.methodParamsZBCSIJFD( + True, 127, 'k', 32767, 2147483467, 2147483467, 1.23456789, 1.23456789), True) + self.assertEquals(test.methodParamsString('helloworld'), True) + self.assertEquals(test.methodParamsArrayI([1, 2, 3]), True) + self.assertEquals(test.methodParamsArrayString([ + 'hello', 'world']), True) diff --git a/tests/test_cast.py b/tests/test_cast.py index ef33746..6498821 100644 --- a/tests/test_cast.py +++ b/tests/test_cast.py @@ -2,7 +2,7 @@ import unittest from jnius.reflect import autoclass from jnius import cast -class MultipleSignature(unittest.TestCase): +class MultipleSignatureTest(unittest.TestCase): def test_multiple_constructors(self): String = autoclass('java.lang.String') diff --git a/tests/test_simple.py b/tests/test_simple.py index 8628d76..2601562 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -3,7 +3,7 @@ from jnius import JavaClass, MetaJavaClass, JavaMethod class HelloWorldTest(unittest.TestCase): - def test(self): + def test_helloworld(self): class HelloWorld(JavaClass): __metaclass__ = MetaJavaClass