diff options
author | jeffhao <jeffhao@google.com> | 2011-09-29 17:41:15 -0700 |
---|---|---|
committer | jeffhao <jeffhao@google.com> | 2011-09-29 17:41:15 -0700 |
commit | 5d1ac920fdaef5d4ec8f66bb734488cd9660b024 (patch) | |
tree | dd372f306ab70f4c86759869b1f74eca62ff6f2b /test/046-reflect | |
parent | c31664f3d82e6cd68275a529a8a73f067a52e8be (diff) | |
download | art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.zip art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.gz art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.bz2 |
Adding old unit tests to test suite.
These tests are copied straight over. They'll still run, but they're
using the old system.
Change-Id: If494519e52ddf858a9febfc55bdae830468cb3c8
Diffstat (limited to 'test/046-reflect')
-rw-r--r-- | test/046-reflect/expected.txt | 97 | ||||
-rw-r--r-- | test/046-reflect/info.txt | 6 | ||||
-rw-r--r-- | test/046-reflect/src/Main.java | 441 |
3 files changed, 544 insertions, 0 deletions
diff --git a/test/046-reflect/expected.txt b/test/046-reflect/expected.txt new file mode 100644 index 0000000..55b0eca --- /dev/null +++ b/test/046-reflect/expected.txt @@ -0,0 +1,97 @@ +Method name is myMethod + Declaring class is Target + Arg 0: int + Exc 0: java.lang.NullPointerException + Exc 1: java.io.IOException + Return type is int + Access flags are 0x1 +Method name is myMethod + Declaring class is SuperTarget + Arg 0: float + Return type is int + Access flags are 0x1 +Method name is myNoargMethod + Declaring class is Target + Return type is void + Access flags are 0x9 +Method name is myMethod + Declaring class is Target + Arg 0: [Ljava.lang.String; + Arg 1: float + Arg 2: char + Return type is int + Access flags are 0x1 +SuperTarget constructor ()V +Target constructor ()V +Before, float is 3.1415925 +myMethod: hi there 3.1415925 Q ! +Result of invoke: 7 +Calling no-arg void-return method +myNoargMethod ()V +throwingMethod +Invoke got expected exception: +java.lang.reflect.InvocationTargetException +java.lang.NullPointerException: gratuitous throw! + +Field name is string1 + Declaring class is Target + Field type is java.lang.String + Access flags are 0x1 + string1 value is 'hey' + ::: hey:yo:there + string1 value is now 'a new string' + ::: a new string:yo:there + got expected illegal obj store exc + got the other expected access exc + got expected arg exc +pubLong initial value is 1122334455667788 +pubLong new value is 9988776655443322 +Field name is superInt + Declaring class is SuperTarget + Field type is int + Access flags are 0x1 + superInt value is 1010101 + superInt boxed is 1010101 + superInt value is now 20202 + superInt value (from short) is now 30303 + superInt value is now 40404 + got expected long->int failure + got expected long->int failure + got expected string->int failure + got expected int->short failure +Field name is superClassInt + Declaring class is SuperTarget + Field type is int + Access flags are 0x9 + superClassInt value is 1010102 +Field name is staticDouble + Declaring class is Target + Field type is double + Access flags are 0x9 + staticDoubleVal value is 3.3 + got expected double->long failure +as expected: aPrivateInt not found +Field name is constantString + Declaring class is Target + Field type is java.lang.String + Access flags are 0x19 + Constant test value is a constant string +Field name is cantTouchThis + Declaring class is Target + Field type is int + Access flags are 0x11 + cantTouchThis is 77 + got expected set-final failure + cantTouchThis is now 77 + cantTouchThis is now 88 +cons modifiers=1 +SuperTarget constructor ()V +Target constructor (IF)V : ii=7 ff=3.3333 +myMethod (I)I + arg=17 anInt=7 +ReflectTest done! +checkType invoking null +checkType got expected exception +got methods +NoisyInitUser is initializing +NoisyInit is initializing diff --git a/test/046-reflect/info.txt b/test/046-reflect/info.txt new file mode 100644 index 0000000..08127da --- /dev/null +++ b/test/046-reflect/info.txt @@ -0,0 +1,6 @@ +This is a miscellaneous test that was imported into the new-at-the-time +runtime test framework. The test is intended to exercise basic features, +and as such cannot be build on top of junit, since failure of such basic +features might disrupt junit. + +TODO: Real description goes here. diff --git a/test/046-reflect/src/Main.java b/test/046-reflect/src/Main.java new file mode 100644 index 0000000..e604979 --- /dev/null +++ b/test/046-reflect/src/Main.java @@ -0,0 +1,441 @@ +// Copyright 2006 The Android Open Source Project + +import java.lang.reflect.*; +import java.io.IOException; +import java.util.Collections; + +/** + * Reflection test. + */ +public class Main { + void printMethodInfo(Method meth) { + Class[] params, exceptions; + int i; + + System.out.println("Method name is " + meth.getName()); + System.out.println(" Declaring class is " + + meth.getDeclaringClass().getName()); + params = meth.getParameterTypes(); + for (i = 0; i < params.length; i++) + System.out.println(" Arg " + i + ": " + params[i].getName()); + exceptions = meth.getExceptionTypes(); + for (i = 0; i < exceptions.length; i++) + System.out.println(" Exc " + i + ": " + exceptions[i].getName()); + System.out.println(" Return type is " + meth.getReturnType().getName()); + System.out.println(" Access flags are 0x" + + Integer.toHexString(meth.getModifiers())); + //System.out.println(" GenericStr is " + meth.toGenericString()); + } + + void printFieldInfo(Field field) { + System.out.println("Field name is " + field.getName()); + System.out.println(" Declaring class is " + + field.getDeclaringClass().getName()); + System.out.println(" Field type is " + field.getType().getName()); + System.out.println(" Access flags are 0x" + + Integer.toHexString(field.getModifiers())); + } + + private void showStrings(Target instance) + throws NoSuchFieldException, IllegalAccessException { + + Class target = Target.class; + String one, two, three, four; + Field field = null; + + field = target.getField("string1"); + one = (String) field.get(instance); + + field = target.getField("string2"); + two = (String) field.get(instance); + + field = target.getField("string3"); + three = (String) field.get(instance); + + System.out.println(" ::: " + one + ":" + two + ":" + three); + } + + public void run() { + Class target = Target.class; + Method meth = null; + Field field = null; + boolean excep; + + try { + meth = target.getMethod("myMethod", new Class[] { int.class }); + + if (meth.getDeclaringClass() != target) + throw new RuntimeException(); + printMethodInfo(meth); + + meth = target.getMethod("myMethod", new Class[] { float.class }); + printMethodInfo(meth); + + meth = target.getMethod("myNoargMethod", (Class[]) null); + printMethodInfo(meth); + + meth = target.getMethod("myMethod", + new Class[] { String[].class, float.class, char.class }); + printMethodInfo(meth); + + Target instance = new Target(); + Object[] argList = new Object[] { + new String[] { "hi there" }, + new Float(3.1415926f), + new Character('Q') + }; + System.out.println("Before, float is " + + ((Float)argList[1]).floatValue()); + + Integer boxval; + boxval = (Integer) meth.invoke(instance, argList); + System.out.println("Result of invoke: " + boxval.intValue()); + + System.out.println("Calling no-arg void-return method"); + meth = target.getMethod("myNoargMethod", (Class[]) null); + meth.invoke(instance, (Object[]) null); + + /* try invoking a method that throws an exception */ + meth = target.getMethod("throwingMethod", (Class[]) null); + try { + meth.invoke(instance, (Object[]) null); + System.out.println("GLITCH: didn't throw"); + } catch (InvocationTargetException ite) { + System.out.println("Invoke got expected exception:"); + System.out.println(ite.getClass().getName()); + System.out.println(ite.getCause()); + } + catch (Exception ex) { + System.out.println("GLITCH: invoke got wrong exception:"); + ex.printStackTrace(); + } + System.out.println(""); + + + field = target.getField("string1"); + if (field.getDeclaringClass() != target) + throw new RuntimeException(); + printFieldInfo(field); + String strVal = (String) field.get(instance); + System.out.println(" string1 value is '" + strVal + "'"); + + showStrings(instance); + + field.set(instance, new String("a new string")); + strVal = (String) field.get(instance); + System.out.println(" string1 value is now '" + strVal + "'"); + + showStrings(instance); + + try { + field.set(instance, new Object()); + System.out.println("WARNING: able to store Object into String"); + } + catch (IllegalArgumentException iae) { + System.out.println(" got expected illegal obj store exc"); + } + + + try { + String four; + field = target.getField("string4"); + four = (String) field.get(instance); + System.out.println("WARNING: able to access string4: " + + four); + } + catch (IllegalAccessException iae) { + System.out.println(" got expected access exc"); + } + catch (NoSuchFieldException nsfe) { + System.out.println(" got the other expected access exc"); + } + try { + String three; + field = target.getField("string3"); + three = (String) field.get(this); + System.out.println("WARNING: able to get string3 in wrong obj: " + + three); + } + catch (IllegalArgumentException iae) { + System.out.println(" got expected arg exc"); + } + + /* + * Try setting a field to null. + */ + String four; + field = target.getDeclaredField("string3"); + field.set(instance, null); + + /* + * Do some stuff with long. + */ + long longVal; + field = target.getField("pubLong"); + longVal = field.getLong(instance); + System.out.println("pubLong initial value is " + + Long.toHexString(longVal)); + field.setLong(instance, 0x9988776655443322L); + longVal = field.getLong(instance); + System.out.println("pubLong new value is " + + Long.toHexString(longVal)); + + + field = target.getField("superInt"); + if (field.getDeclaringClass() == target) + throw new RuntimeException(); + printFieldInfo(field); + int intVal = field.getInt(instance); + System.out.println(" superInt value is " + intVal); + Integer boxedIntVal = (Integer) field.get(instance); + System.out.println(" superInt boxed is " + boxedIntVal); + + field.set(instance, new Integer(20202)); + intVal = field.getInt(instance); + System.out.println(" superInt value is now " + intVal); + field.setShort(instance, (short)30303); + intVal = field.getInt(instance); + System.out.println(" superInt value (from short) is now " +intVal); + field.setInt(instance, 40404); + intVal = field.getInt(instance); + System.out.println(" superInt value is now " + intVal); + try { + field.set(instance, new Long(123)); + System.out.println("FAIL: expected exception not thrown"); + } + catch (IllegalArgumentException iae) { + System.out.println(" got expected long->int failure"); + } + try { + field.setLong(instance, 123); + System.out.println("FAIL: expected exception not thrown"); + } + catch (IllegalArgumentException iae) { + System.out.println(" got expected long->int failure"); + } + try { + field.set(instance, new String("abc")); + System.out.println("FAIL: expected exception not thrown"); + } + catch (IllegalArgumentException iae) { + System.out.println(" got expected string->int failure"); + } + + try { + field.getShort(instance); + System.out.println("FAIL: expected exception not thrown"); + } + catch (IllegalArgumentException iae) { + System.out.println(" got expected int->short failure"); + } + + field = target.getField("superClassInt"); + printFieldInfo(field); + int superClassIntVal = field.getInt(instance); + System.out.println(" superClassInt value is " + superClassIntVal); + + field = target.getField("staticDouble"); + printFieldInfo(field); + double staticDoubleVal = field.getDouble(null); + System.out.println(" staticDoubleVal value is " + staticDoubleVal); + + try { + field.getLong(instance); + System.out.println("FAIL: expected exception not thrown"); + } + catch (IllegalArgumentException iae) { + System.out.println(" got expected double->long failure"); + } + + excep = false; + try { + field = target.getField("aPrivateInt"); + printFieldInfo(field); + } + catch (NoSuchFieldException nsfe) { + System.out.println("as expected: aPrivateInt not found"); + excep = true; + } + if (!excep) + System.out.println("BUG: got aPrivateInt"); + + + field = target.getField("constantString"); + printFieldInfo(field); + String val = (String) field.get(instance); + System.out.println(" Constant test value is " + val); + + + field = target.getField("cantTouchThis"); + printFieldInfo(field); + intVal = field.getInt(instance); + System.out.println(" cantTouchThis is " + intVal); + try { + field.setInt(instance, 99); + System.out.println("ERROR: set-final succeeded"); + } catch (IllegalAccessException iae) { + System.out.println(" got expected set-final failure"); + } + intVal = field.getInt(instance); + System.out.println(" cantTouchThis is now " + intVal); + + field.setAccessible(true); + field.setInt(instance, 87); // exercise int version + field.set(instance, 88); // exercise Object version + intVal = field.getInt(instance); + System.out.println(" cantTouchThis is now " + intVal); + + Constructor<Target> cons; + Target targ; + Object[] args; + + cons = target.getConstructor(new Class[] { int.class,float.class }); + args = new Object[] { new Integer(7), new Float(3.3333) }; + System.out.println("cons modifiers=" + cons.getModifiers()); + targ = cons.newInstance(args); + targ.myMethod(17); + + } + catch (Exception ex) { + System.out.println("----- unexpected exception -----"); + ex.printStackTrace(); + } + + System.out.println("ReflectTest done!"); + } + + public static void checkType() { + Method m; + + try { + m = Collections.class.getDeclaredMethod("checkType", + Object.class, Class.class); + } catch (NoSuchMethodException nsme) { + nsme.printStackTrace(); + return; + } + + m.setAccessible(true); + try { + m.invoke(null, new Object(), Object.class); + } catch (IllegalAccessException iae) { + iae.printStackTrace(); + return; + } catch (InvocationTargetException ite) { + ite.printStackTrace(); + return; + } + + try { + System.out.println("checkType invoking null"); + m.invoke(null, new Object(), int.class); + System.out.println("ERROR: should throw InvocationTargetException"); + } catch (InvocationTargetException ite) { + System.out.println("checkType got expected exception"); + } catch (IllegalAccessException iae) { + iae.printStackTrace(); + return; + } + } + + public static void checkInit() { + Class niuClass = NoisyInitUser.class; + Method[] methods; + + methods = niuClass.getDeclaredMethods(); + System.out.println("got methods"); + /* neither NoisyInit nor NoisyInitUser should be initialized yet */ + NoisyInitUser niu = new NoisyInitUser(); + NoisyInit ni = new NoisyInit(); + } + + public static void main(String[] args) { + Main test = new Main(); + test.run(); + + checkType(); + checkInit(); + } +} + + +class SuperTarget { + public SuperTarget() { + System.out.println("SuperTarget constructor ()V"); + superInt = 1010101; + superClassInt = 1010102; + } + + public int myMethod(float floatArg) { + System.out.println("myMethod (F)I " + floatArg); + return 6; + } + + public int superInt; + public static int superClassInt; +} + +class Target extends SuperTarget { + public Target() { + System.out.println("Target constructor ()V"); + } + + public Target(int ii, float ff) { + System.out.println("Target constructor (IF)V : ii=" + + ii + " ff=" + ff); + anInt = ii; + } + + public int myMethod(int intarg) throws NullPointerException, IOException { + System.out.println("myMethod (I)I"); + System.out.println(" arg=" + intarg + " anInt=" + anInt); + return 5; + } + + public int myMethod(String[] strarg, float f, char c) { + System.out.println("myMethod: " + strarg[0] + " " + f + " " + c + " !"); + return 7; + } + + public static void myNoargMethod() { + System.out.println("myNoargMethod ()V"); + } + + public void throwingMethod() { + System.out.println("throwingMethod"); + throw new NullPointerException("gratuitous throw!"); + } + + public void misc() { + System.out.println("misc"); + } + + public int anInt; + public String string1 = "hey"; + public String string2 = "yo"; + public String string3 = "there"; + private String string4 = "naughty"; + public static final String constantString = "a constant string"; + private int aPrivateInt; + + public final int cantTouchThis = 77; + + public long pubLong = 0x1122334455667788L; + + public static double staticDouble = 3.3; +} + +class NoisyInit { + static { + System.out.println("NoisyInit is initializing"); + //Throwable th = new Throwable(); + //th.printStackTrace(); + } +} + +class NoisyInitUser { + static { + System.out.println("NoisyInitUser is initializing"); + } + public void createNoisyInit(NoisyInit ni) {} +} |