MethodTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.lang.reflect;
19
20import java.lang.annotation.Annotation;
21import java.lang.annotation.ElementType;
22import java.lang.annotation.Retention;
23import java.lang.annotation.RetentionPolicy;
24import java.lang.annotation.Target;
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27import java.lang.reflect.Modifier;
28import java.lang.reflect.Type;
29import java.lang.reflect.TypeVariable;
30import java.util.HashSet;
31import java.util.Set;
32
33import dalvik.annotation.TestLevel;
34import dalvik.annotation.TestTargetClass;
35import dalvik.annotation.TestTargetNew;
36
37@TestTargetClass(Method.class)
38public class MethodTest extends junit.framework.TestCase {
39
40    static class TestMethod {
41        public TestMethod() {
42        }
43
44        public void voidMethod() throws IllegalArgumentException {
45        }
46
47        public void parmTest(int x, short y, String s, boolean bool, Object o,
48                long l, byte b, char c, double d, float f) {
49        }
50
51        public int intMethod() {
52            return 1;
53        }
54
55        public static final void printTest(int x, short y, String s,
56                boolean bool, Object o, long l, byte b, char c, double d,
57                float f) {
58        }
59
60        public double doubleMethod() {
61            return 1.0;
62        }
63
64        public short shortMethod() {
65            return (short) 1;
66        }
67
68        public byte byteMethod() {
69            return (byte) 1;
70        }
71
72        public float floatMethod() {
73            return 1.0f;
74        }
75
76        public long longMethod() {
77            return 1l;
78        }
79
80        public char charMethod() {
81            return 'T';
82        }
83
84        public Object objectMethod() {
85            return new Object();
86        }
87
88        private static void prstatic() {
89        }
90
91        public static void pustatic() {
92        }
93
94        public static synchronized void pustatsynch() {
95        }
96
97        public static int invokeStaticTest() {
98            return 1;
99        }
100
101        public int invokeInstanceTest() {
102            return 1;
103        }
104
105        private int privateInvokeTest() {
106            return 1;
107        }
108
109        public int invokeExceptionTest() throws NullPointerException {
110            throw new NullPointerException();
111        }
112
113        public static synchronized native void pustatsynchnat();
114
115        public void publicVoidVarargs(Object... param){}
116        public void publicVoidArray(Object[] param){}
117
118        public void annotatedParameter(@TestAnno @Deprecated int a,
119                @Deprecated int b, int c) {
120        }
121
122        @Deprecated
123        @TestAnno
124        public void annotatedMethod(){}
125
126        public void hashCodeTest(int i){}
127        public void hashCodeTest(String s){}
128
129        public void invokeCastTest1(byte param) {
130        }
131
132        public void invokeCastTest1(short param) {
133        }
134
135        public void invokeCastTest1(int param) {
136        }
137
138        public void invokeCastTest1(long param) {
139        }
140
141        public void invokeCastTest1(float param) {
142        }
143
144        public void invokeCastTest1(double param) {
145        }
146
147        public void invokeCastTest1(char param) {
148        }
149
150        public void invokeCastTest1(boolean param) {
151        }
152    }
153
154    @Retention(RetentionPolicy.RUNTIME)
155    @Target({ElementType.PARAMETER, ElementType.METHOD})
156    public static @interface TestAnno{
157        public static final String DEFAULT_VALUE = "DEFAULT_VALUE";
158
159        String value() default DEFAULT_VALUE;
160    }
161
162    abstract class AbstractTestMethod {
163        public abstract void puabs();
164    }
165
166    class TestMethodSub extends TestMethod {
167        public int invokeInstanceTest() {
168            return 0;
169        }
170    }
171
172    static interface IBrigeTest<T>{
173        T m();
174    }
175
176    static class BrigeTest implements IBrigeTest<String> {
177        public String m(){ return null; }
178    }
179
180    static class ExceptionTest<T extends Exception>{
181        @SuppressWarnings("unused")
182        void exceptionTest() throws T{}
183    }
184
185    static class GenericReturnType<T> {
186        T returnGeneric(){return null;}
187    }
188
189    static class GenericString<T> {
190      public static final String GENERIC =
191      "T tests.api.java.lang.reflect.MethodTest$GenericString.genericString(T)";
192        T genericString(T t) {
193            return null;
194        }
195    }
196
197    /**
198     * @tests java.lang.reflect.Method#equals(java.lang.Object)
199     */
200    @TestTargetNew(
201        level = TestLevel.COMPLETE,
202        notes = "",
203        method = "equals",
204        args = {java.lang.Object.class}
205    )
206    public void test_equalsLjava_lang_Object() {
207        // Test for method boolean
208        // java.lang.reflect.Method.equals(java.lang.Object)
209
210        Method m1 = null, m2 = null;
211        try {
212            m1 = TestMethod.class.getMethod("invokeInstanceTest", new Class[0]);
213            m2 = TestMethodSub.class.getMethod("invokeInstanceTest",
214                    new Class[0]);
215        } catch (Exception e) {
216            fail("Exception during equals test : " + e.getMessage());
217        }
218        assertTrue("Overriden method returned equal", !m1.equals(m2));
219        assertTrue("Same method returned not-equal", m1.equals(m1));
220        try {
221            m1 = TestMethod.class.getMethod("invokeStaticTest", new Class[0]);
222            m2 = TestMethodSub.class
223                    .getMethod("invokeStaticTest", new Class[0]);
224        } catch (Exception e) {
225            fail("Exception during equals test : " + e.getMessage());
226        }
227        assertTrue("Inherited method returned not-equal", m1.equals(m2));
228    }
229
230    /**
231     * @tests java.lang.reflect.Method#getDeclaringClass()
232     */
233    @TestTargetNew(
234        level = TestLevel.COMPLETE,
235        notes = "",
236        method = "getDeclaringClass",
237        args = {}
238    )
239    public void test_getDeclaringClass() {
240        // Test for method java.lang.Class
241        // java.lang.reflect.Method.getDeclaringClass()
242
243        Method[] mths;
244
245        try {
246            mths = TestMethod.class.getDeclaredMethods();
247            assertTrue("Returned incorrect declaring class: "
248                    + mths[0].getDeclaringClass().toString(), mths[0]
249                    .getDeclaringClass().equals(TestMethod.class));
250        } catch (Exception e) {
251            fail("Exception during getDeclaringClass test: "
252                    + e.toString());
253        }
254    }
255
256    /**
257     * @tests java.lang.reflect.Method#getExceptionTypes()
258     */
259    @TestTargetNew(
260        level = TestLevel.COMPLETE,
261        notes = "",
262        method = "getExceptionTypes",
263        args = {}
264    )
265    public void test_getExceptionTypes() {
266        // Test for method java.lang.Class []
267        // java.lang.reflect.Method.getExceptionTypes()
268
269        try {
270            Method mth = TestMethod.class.getMethod("voidMethod", new Class[0]);
271            Class[] ex = mth.getExceptionTypes();
272            assertEquals("Returned incorrect number of exceptions",
273                    1, ex.length);
274            assertTrue("Returned incorrect exception type", ex[0]
275                    .equals(IllegalArgumentException.class));
276            mth = TestMethod.class.getMethod("intMethod", new Class[0]);
277            ex = mth.getExceptionTypes();
278            assertEquals("Returned incorrect number of exceptions",
279                    0, ex.length);
280        } catch (Exception e) {
281            fail("Exception during getExceptionTypes: " + e.toString());
282        }
283
284    }
285
286    /**
287     * @tests java.lang.reflect.Method#getModifiers()
288     */
289    @TestTargetNew(
290        level = TestLevel.COMPLETE,
291        notes = "",
292        method = "getModifiers",
293        args = {}
294    )
295    public void test_getModifiers() {
296        // Test for method int java.lang.reflect.Method.getModifiers()
297
298        Class cl = TestMethod.class;
299        int mods = 0;
300        Method mth = null;
301        int mask = 0;
302        try {
303            mth = cl.getMethod("pustatic", new Class[0]);
304            mods = mth.getModifiers();
305        } catch (Exception e) {
306            fail("Exception during getModfiers test: " + e.toString());
307        }
308        mask = Modifier.PUBLIC | Modifier.STATIC;
309        assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
310        try {
311            mth = cl.getDeclaredMethod("prstatic", new Class[0]);
312            mods = mth.getModifiers();
313        } catch (Exception e) {
314            fail("Exception during getModfiers test: " + e.toString());
315        }
316        mask = Modifier.PRIVATE | Modifier.STATIC;
317        assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
318        try {
319            mth = cl.getDeclaredMethod("pustatsynch", new Class[0]);
320            mods = mth.getModifiers();
321        } catch (Exception e) {
322            fail("Exception during getModfiers test: " + e.toString());
323        }
324        mask = (Modifier.PUBLIC | Modifier.STATIC) | Modifier.SYNCHRONIZED;
325        assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
326        try {
327            mth = cl.getDeclaredMethod("pustatsynchnat", new Class[0]);
328            mods = mth.getModifiers();
329        } catch (Exception e) {
330            fail("Exception during getModfiers test: " + e.toString());
331        }
332        mask = ((Modifier.PUBLIC | Modifier.STATIC) | Modifier.SYNCHRONIZED)
333                | Modifier.NATIVE;
334        assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
335        cl = AbstractTestMethod.class;
336        try {
337            mth = cl.getDeclaredMethod("puabs", new Class[0]);
338            mods = mth.getModifiers();
339        } catch (Exception e) {
340            fail("Exception during getModfiers test: " + e.toString());
341        }
342        mask = Modifier.PUBLIC | Modifier.ABSTRACT;
343        assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
344    }
345
346    /**
347     * @tests java.lang.reflect.Method#getName()
348     */
349    @TestTargetNew(
350        level = TestLevel.COMPLETE,
351        notes = "",
352        method = "getName",
353        args = {}
354    )
355    public void test_getName() {
356        // Test for method java.lang.String java.lang.reflect.Method.getName()
357        Method mth = null;
358        try {
359            mth = TestMethod.class.getMethod("voidMethod", new Class[0]);
360        } catch (Exception e) {
361            fail("Exception during getMethodName(): " + e.toString());
362        }
363        assertEquals("Returned incorrect method name",
364                "voidMethod", mth.getName());
365    }
366
367    /**
368     * @tests java.lang.reflect.Method#isVarArgs()
369     */
370    @TestTargetNew(
371        level = TestLevel.COMPLETE,
372        notes = "",
373        method = "isVarArgs",
374        args = {}
375    )
376    public void test_isVarArgs() throws Exception {
377        Method mth = TestMethod.class.getMethod("publicVoidVarargs",
378                Object[].class);
379        assertTrue("Varargs method stated as non vararg.", mth.isVarArgs());
380
381        mth = TestMethod.class.getDeclaredMethod("publicVoidArray",
382                Object[].class);
383        assertFalse("Non varargs method stated as vararg.", mth.isVarArgs());
384    }
385
386    /**
387     * @tests java.lang.reflect.Method#isBridge()
388     */
389    @TestTargetNew(
390        level = TestLevel.COMPLETE,
391        notes = "",
392        method = "isBridge",
393        args = {}
394    )
395    public void test_isBridge() throws Exception {
396        Method[] declaredMethods = BrigeTest.class.getDeclaredMethods();
397        assertEquals("Bridge method not generated.", 2, declaredMethods.length);
398        boolean foundBridgeMethod = false;
399        for (Method method : declaredMethods) {
400            if (method.getReturnType().equals(Object.class)) {
401                assertTrue("Bridge method not stated as bridge.", method
402                        .isBridge());
403                foundBridgeMethod = true;
404            }
405        }
406        assertTrue("Bridge method not found.", foundBridgeMethod);
407    }
408
409    /**
410     * @tests java.lang.reflect.Method#isSynthetic()
411     */
412    @TestTargetNew(
413        level = TestLevel.COMPLETE,
414        notes = "",
415        method = "isSynthetic",
416        args = {}
417    )
418    public void test_isSynthetic() throws Exception {
419        Method[] declaredMethods = BrigeTest.class.getDeclaredMethods();
420        assertEquals("Synthetic method not generated.", 2,
421                declaredMethods.length);
422        boolean foundSyntheticMethod = false;
423        for (Method method : declaredMethods) {
424            if (method.getReturnType().equals(Object.class)) {
425                assertTrue("Synthetic method not stated as synthetic.", method
426                        .isSynthetic());
427                foundSyntheticMethod = true;
428            }
429        }
430        assertTrue("Synthetic method not found.", foundSyntheticMethod);
431    }
432    /**
433     * @tests java.lang.reflect.Method#getParameterAnnotations()
434     */
435    @TestTargetNew(
436        level = TestLevel.COMPLETE,
437        notes = "",
438        method = "getParameterAnnotations",
439        args = {}
440    )
441    public void test_getParameterAnnotations() throws Exception {
442        Method method = TestMethod.class.getDeclaredMethod(
443                "annotatedParameter", new Class[] {
444                        int.class, int.class, int.class});
445        Annotation[][] annotations = method.getParameterAnnotations();
446        assertEquals(3, annotations.length);
447        assertEquals(
448                "Wrong number of annotations returned for first parameter", 2,
449                annotations[0].length);
450        Set<Class<?>> annotationSet = new HashSet<Class<?>>();
451        annotationSet.add(annotations[0][0].annotationType());
452        annotationSet.add(annotations[0][1].annotationType());
453        assertTrue("Missing TestAnno annotation", annotationSet
454                .contains(TestAnno.class));
455        assertTrue("Missing Deprecated annotation", annotationSet
456                .contains(Deprecated.class));
457
458        assertEquals(
459                "Wrong number of annotations returned for second parameter",
460                1, annotations[1].length);
461        annotationSet = new HashSet<Class<?>>();
462        annotationSet.add(annotations[1][0].annotationType());
463        assertTrue("Missing Deprecated annotation", annotationSet
464                .contains(Deprecated.class));
465        assertEquals(
466                "Wrong number of annotations returned for third parameter", 0,
467                annotations[2].length);
468    }
469
470    /**
471     * @tests java.lang.reflect.Method#getDeclaredAnnotations()
472     */
473    @TestTargetNew(
474        level = TestLevel.COMPLETE,
475        notes = "",
476        method = "getDeclaredAnnotations",
477        args = {}
478    )
479    public void test_getDeclaredAnnotations() throws Exception {
480        Method method = TestMethod.class.getDeclaredMethod("annotatedMethod");
481        Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
482        assertEquals(2, declaredAnnotations.length);
483
484        Set<Class<?>> annotationSet = new HashSet<Class<?>>();
485        annotationSet.add(declaredAnnotations[0].annotationType());
486        annotationSet.add(declaredAnnotations[1].annotationType());
487        assertTrue("Missing TestAnno annotation", annotationSet
488                .contains(TestAnno.class));
489        assertTrue("Missing Deprecated annotation", annotationSet
490                .contains(Deprecated.class));
491    }
492
493    /**
494     * @tests java.lang.reflect.Method#getDefaultValue()
495     */
496    @TestTargetNew(
497        level = TestLevel.SUFFICIENT,
498        notes = "Missing tests for TypeNotPresentException",
499        method = "getDefaultValue",
500        args = {}
501    )
502    public void test_getDefaultValue() throws Exception {
503        Method method = TestAnno.class.getDeclaredMethod("value");
504        assertEquals("Wrong default value returned", TestAnno.DEFAULT_VALUE,
505                method.getDefaultValue());
506    }
507
508    /**
509     * @tests java.lang.reflect.Method#getDefaultValue()
510     */
511    @TestTargetNew(
512        level = TestLevel.SUFFICIENT,
513        notes = "Missing tests for GenericSignatureFormatError,TypeNotPresentException, MalformedParameterizedTypeException",
514        method = "getGenericExceptionTypes",
515        args = {}
516    )
517    public void test_getGenericExceptionTypes() throws Exception {
518        Method method = ExceptionTest.class.getDeclaredMethod("exceptionTest");
519        Type[] genericExceptionTypes = method.getGenericExceptionTypes();
520        assertEquals(1, genericExceptionTypes.length);
521        assertTrue(genericExceptionTypes[0] instanceof TypeVariable<?>);
522        @SuppressWarnings("unchecked")
523        TypeVariable<Class<ExceptionTest<?>>> tv =
524            (TypeVariable<Class<ExceptionTest<?>>>) genericExceptionTypes[0];
525        assertEquals("T", tv.getName());
526    }
527
528    /**
529     * @tests java.lang.reflect.Method#getGenericReturnType()
530     */
531    @TestTargetNew(
532        level = TestLevel.SUFFICIENT,
533        notes = "Missing tests for GenericSignatureFormatError,TypeNotPresentException, MalformedParameterizedTypeException",
534        method = "getGenericReturnType",
535        args = {}
536    )
537    public void test_getGenericReturnType() throws Exception {
538        Method method = GenericReturnType.class
539                .getDeclaredMethod("returnGeneric");
540        Type returnType = method.getGenericReturnType();
541        assertNotNull("getGenericReturnType returned null", returnType);
542        assertTrue(returnType instanceof TypeVariable<?>);
543        @SuppressWarnings("unchecked")
544        TypeVariable<Class<ExceptionTest<?>>> tv =
545            (TypeVariable<Class<ExceptionTest<?>>>) returnType;
546        assertEquals("T", tv.getName());
547    }
548
549
550    /**
551     * @tests java.lang.reflect.Method#toGenericString()
552     */
553    @TestTargetNew(
554        level = TestLevel.COMPLETE,
555        notes = "",
556        method = "toGenericString",
557        args = {}
558    )
559    public void test_toGenericString() throws Exception {
560        Method method = GenericString.class.getDeclaredMethod("genericString",
561                Object.class);
562        assertEquals("Wrong generic String returned", GenericString.GENERIC,
563                method.toGenericString());
564    }
565
566
567
568
569
570
571    /**
572     * @tests java.lang.reflect.Method#hashCode()
573     */
574    @TestTargetNew(
575        level = TestLevel.COMPLETE,
576        notes = "",
577        method = "hashCode",
578        args = {}
579    )
580    public void test_hashCode() throws Exception {
581        Method mth0 = TestMethod.class.getMethod("hashCodeTest", String.class);
582        Method mth1 = TestMethod.class.getDeclaredMethod("hashCodeTest",
583                int.class);
584        assertEquals("Methods with same name did not return same hashCode.",
585                mth0.hashCode(), mth1.hashCode());
586    }
587
588    /**
589     * @tests java.lang.reflect.Method#getParameterTypes()
590     */
591    @TestTargetNew(
592        level = TestLevel.COMPLETE,
593        notes = "",
594        method = "getParameterTypes",
595        args = {}
596    )
597    public void test_getParameterTypes() {
598        // Test for method java.lang.Class []
599        // java.lang.reflect.Method.getParameterTypes()
600        Class cl = TestMethod.class;
601        Method mth = null;
602        Class[] parms = null;
603        Method[] methods = null;
604        Class[] plist = { int.class, short.class, String.class, boolean.class,
605                Object.class, long.class, byte.class, char.class, double.class,
606                float.class };
607        try {
608            mth = cl.getMethod("voidMethod", new Class[0]);
609            parms = mth.getParameterTypes();
610        } catch (Exception e) {
611            fail("Exception during getParameterTypes test: "
612                    + e.toString());
613        }
614        assertEquals("Returned incorrect parameterTypes", 0, parms.length);
615        try {
616            mth = cl.getMethod("parmTest", plist);
617            parms = mth.getParameterTypes();
618        } catch (Exception e) {
619            fail("Exception during getParameterTypes test: "
620                    + e.toString());
621        }
622        assertTrue("Invalid number of parameters returned",
623                plist.length == parms.length);
624        for (int i = 0; i < plist.length; i++)
625            assertTrue("Incorrect parameter returned", plist[i]
626                    .equals(parms[i]));
627
628        // Test same method. but this time pull it from the list of methods
629        // rather than asking for it explicitly
630        methods = cl.getDeclaredMethods();
631
632        int i;
633        for (i = 0; i < methods.length; i++)
634            if (methods[i].getName().equals("parmTest")) {
635                mth = methods[i];
636                i = methods.length + 1;
637            }
638        if (i < methods.length) {
639            parms = mth.getParameterTypes();
640            assertTrue("Incorrect number of parameters returned",
641                    parms.length == plist.length);
642            for (i = 0; i < plist.length; i++)
643                assertTrue("Incorrect parameter returned", plist[i]
644                        .equals(parms[i]));
645        }
646    }
647
648    /**
649     * @tests java.lang.reflect.Method#getReturnType()
650     */
651    @TestTargetNew(
652        level = TestLevel.COMPLETE,
653        notes = "",
654        method = "getReturnType",
655        args = {}
656    )
657    public void test_getReturnType() {
658        // Test for method java.lang.Class
659        // java.lang.reflect.Method.getReturnType()
660        Class cl = TestMethod.class;
661        Method mth = null;
662        try {
663            mth = cl.getMethod("charMethod", new Class[0]);
664        } catch (Exception e) {
665            fail("Exception during getReturnType test : " + e.getMessage());
666        }
667        assertTrue("Gave incorrect returne type, wanted char", mth
668                .getReturnType().equals(char.class));
669        try {
670            mth = cl.getMethod("longMethod", new Class[0]);
671        } catch (Exception e) {
672            fail("Exception during getReturnType test : " + e.getMessage());
673        }
674        assertTrue("Gave incorrect returne type, wanted long", mth
675                .getReturnType().equals(long.class));
676        try {
677            mth = cl.getMethod("shortMethod", new Class[0]);
678        } catch (Exception e) {
679            fail("Exception during getReturnType test : " + e.getMessage());
680        }
681        assertTrue("Gave incorrect returne type, wanted short", mth
682                .getReturnType().equals(short.class));
683        try {
684            mth = cl.getMethod("intMethod", new Class[0]);
685        } catch (Exception e) {
686            fail("Exception during getReturnType test : " + e.getMessage());
687        }
688        assertTrue("Gave incorrect returne type, wanted int: "
689                + mth.getReturnType(), mth.getReturnType().equals(int.class));
690        try {
691            mth = cl.getMethod("doubleMethod", new Class[0]);
692        } catch (Exception e) {
693            fail("Exception during getReturnType test : " + e.getMessage());
694        }
695        assertTrue("Gave incorrect returne type, wanted double", mth
696                .getReturnType().equals(double.class));
697        try {
698            mth = cl.getMethod("byteMethod", new Class[0]);
699        } catch (Exception e) {
700            fail("Exception during getReturnType test : " + e.getMessage());
701        }
702        assertTrue("Gave incorrect returne type, wanted byte", mth
703                .getReturnType().equals(byte.class));
704        try {
705            mth = cl.getMethod("byteMethod", new Class[0]);
706        } catch (Exception e) {
707            fail("Exception during getReturnType test:" + e.toString());
708        }
709        assertTrue("Gave incorrect returne type, wanted byte", mth
710                .getReturnType().equals(byte.class));
711        try {
712            mth = cl.getMethod("objectMethod", new Class[0]);
713        } catch (Exception e) {
714            fail("Exception during getReturnType test : " + e.getMessage());
715        }
716        assertTrue("Gave incorrect returne type, wanted Object", mth
717                .getReturnType().equals(Object.class));
718
719        try {
720            mth = cl.getMethod("voidMethod", new Class[0]);
721        } catch (Exception e) {
722            fail("Exception during getReturnType test : " + e.getMessage());
723        }
724        assertTrue("Gave incorrect returne type, wanted void", mth
725                .getReturnType().equals(void.class));
726    }
727
728    /**
729     * @tests java.lang.reflect.Method#invoke(java.lang.Object,
730     *        java.lang.Object[])
731     */
732    @TestTargetNew(
733        level = TestLevel.COMPLETE,
734        notes = "",
735        method = "invoke",
736        args = {java.lang.Object.class, java.lang.Object[].class}
737    )
738    public void test_invokeLjava_lang_Object$Ljava_lang_Object() throws Exception{
739        // Test for method java.lang.Object
740        // java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object
741        // [])
742        Class cl = TestMethod.class;
743        Class[] dcl = new Class[0];
744
745        // Get and invoke a static method
746        Method mth = cl.getDeclaredMethod("invokeStaticTest", dcl);
747        Object ret = mth.invoke(null, new Object[0]);
748        assertEquals("Invoke returned incorrect value", 1, ((Integer) ret)
749                .intValue());
750
751        // Get and invoke an instance method
752        mth = cl.getDeclaredMethod("invokeInstanceTest", dcl);
753        ret = mth.invoke(new TestMethod(), new Object[0]);
754        assertEquals("Invoke returned incorrect value", 1, ((Integer) ret)
755                .intValue());
756
757        // Get and attempt to invoke a private method
758        mth = cl.getDeclaredMethod("privateInvokeTest", dcl);
759        try {
760            ret = mth.invoke(new TestMethod(), new Object[0]);
761        } catch (IllegalAccessException e) {
762            // Correct behaviour
763        } catch (Exception e) {
764            fail("Exception during invoke test : " + e.getMessage());
765        }
766        // Generate an IllegalArgumentException
767        mth = cl.getDeclaredMethod("invokeInstanceTest", dcl);
768
769        try {
770            Object[] args = { Object.class };
771            ret = mth.invoke(new TestMethod(), args);
772        } catch (IllegalArgumentException e) {
773            // Correct behaviour
774        } catch (Exception e) {
775            fail("Exception during invoke test : " + e.getMessage());
776        }
777
778        // Generate a NullPointerException
779        mth = cl.getDeclaredMethod("invokeInstanceTest", dcl);
780
781        try {
782            ret = mth.invoke(null, new Object[0]);
783        } catch (NullPointerException e) {
784            // Correct behaviour
785        } catch (Exception e) {
786            fail("Exception during invoke test : " + e.getMessage());
787        }
788
789        // Generate an InvocationTargetException
790        mth = cl.getDeclaredMethod("invokeExceptionTest", dcl);
791        try {
792            ret = mth.invoke(new TestMethod(), new Object[0]);
793        } catch (InvocationTargetException e) {
794            // Correct behaviour
795        } catch (Exception e) {
796            fail("Exception during invoke test : " + e.getMessage());
797        }
798
799        TestMethod testMethod = new TestMethod();
800        Method methods[] = cl.getMethods();
801        for (int i = 0; i < methods.length; i++) {
802            if (methods[i].getName().startsWith("invokeCastTest1")) {
803                Class param = methods[i].getParameterTypes()[0];
804
805                try {
806                    methods[i].invoke(testMethod, new Object[] { new Byte(
807                            (byte) 1) });
808                    assertTrue("invalid invoke with Byte: " + methods[i],
809                            param == Byte.TYPE || param == Short.TYPE
810                                    || param == Integer.TYPE
811                                    || param == Long.TYPE
812                                    || param == Float.TYPE
813                                    || param == Double.TYPE);
814                } catch (Exception e) {
815                    assertTrue("Byte invalid exception: " + e,
816                            e instanceof IllegalArgumentException);
817                    assertTrue("Byte invalid failure: " + methods[i],
818                            param == Boolean.TYPE || param == Character.TYPE);
819                }
820
821                try {
822                    methods[i].invoke(testMethod, new Object[] { new Short(
823                            (short) 1) });
824                    assertTrue("invalid invoke with Short: " + methods[i],
825                            param == Short.TYPE || param == Integer.TYPE
826                                    || param == Long.TYPE
827                                    || param == Float.TYPE
828                                    || param == Double.TYPE);
829                } catch (Exception e) {
830                    assertTrue("Short invalid exception: " + e,
831                            e instanceof IllegalArgumentException);
832                    assertTrue("Short invalid failure: " + methods[i],
833                            param == Byte.TYPE || param == Boolean.TYPE
834                                    || param == Character.TYPE);
835                }
836
837                try {
838                    methods[i].invoke(testMethod,
839                            new Object[] { new Integer(1) });
840                    assertTrue("invalid invoke with Integer: " + methods[i],
841                            param == Integer.TYPE || param == Long.TYPE
842                                    || param == Float.TYPE
843                                    || param == Double.TYPE);
844                } catch (Exception e) {
845                    assertTrue("Integer invalid exception: " + e,
846                            e instanceof IllegalArgumentException);
847                    assertTrue("Integer invalid failure: " + methods[i],
848                            param == Byte.TYPE || param == Short.TYPE
849                                    || param == Boolean.TYPE
850                                    || param == Character.TYPE);
851                }
852
853                try {
854                    methods[i].invoke(testMethod, new Object[] { new Long(1) });
855                    assertTrue("invalid invoke with Long: " + methods[i],
856                            param == Long.TYPE || param == Float.TYPE
857                                    || param == Double.TYPE);
858                } catch (Exception e) {
859                    assertTrue("Long invalid exception: " + e,
860                            e instanceof IllegalArgumentException);
861                    assertTrue("Long invalid failure: " + methods[i],
862                            param == Byte.TYPE || param == Short.TYPE
863                                    || param == Integer.TYPE
864                                    || param == Boolean.TYPE
865                                    || param == Character.TYPE);
866                }
867
868                try {
869                    methods[i].invoke(testMethod, new Object[] { new Character(
870                            'a') });
871                    assertTrue("invalid invoke with Character: " + methods[i],
872                            param == Character.TYPE || param == Integer.TYPE
873                                    || param == Long.TYPE
874                                    || param == Float.TYPE
875                                    || param == Double.TYPE);
876                } catch (Exception e) {
877                    assertTrue("Character invalid exception: " + e,
878                            e instanceof IllegalArgumentException);
879                    assertTrue("Character invalid failure: " + methods[i],
880                            param == Byte.TYPE || param == Short.TYPE
881                                    || param == Boolean.TYPE);
882                }
883
884                try {
885                    methods[i]
886                            .invoke(testMethod, new Object[] { new Float(1) });
887                    assertTrue("invalid invoke with Float: " + methods[i],
888                            param == Float.TYPE || param == Double.TYPE);
889                } catch (Exception e) {
890                    assertTrue("Float invalid exception: " + e,
891                            e instanceof IllegalArgumentException);
892                    assertTrue("Float invalid failure: " + methods[i],
893                            param == Byte.TYPE || param == Short.TYPE
894                                    || param == Integer.TYPE
895                                    || param == Long.TYPE
896                                    || param == Boolean.TYPE
897                                    || param == Character.TYPE);
898                }
899
900                try {
901                    methods[i].invoke(testMethod,
902                            new Object[] { new Double(1) });
903                    assertTrue("invalid invoke with Double: " + methods[i],
904                            param == Double.TYPE);
905                } catch (Exception e) {
906                    assertTrue("Double invalid exception: " + e,
907                            e instanceof IllegalArgumentException);
908                    assertTrue("Double invalid failure: " + methods[i],
909                            param == Byte.TYPE || param == Short.TYPE
910                                    || param == Integer.TYPE
911                                    || param == Long.TYPE
912                                    || param == Boolean.TYPE
913                                    || param == Character.TYPE
914                                    || param == Float.TYPE);
915                }
916
917                try {
918                    methods[i].invoke(testMethod, new Object[] { new Boolean(
919                            true) });
920                    assertTrue("invalid invoke with Boolean: " + methods[i],
921                            param == Boolean.TYPE);
922                } catch (Exception e) {
923                    assertTrue("Boolean invalid exception: " + e,
924                            e instanceof IllegalArgumentException);
925                    assertTrue("Boolean invalid failure: " + methods[i],
926                            param == Byte.TYPE || param == Short.TYPE
927                                    || param == Integer.TYPE
928                                    || param == Long.TYPE
929                                    || param == Character.TYPE
930                                    || param == Float.TYPE
931                                    || param == Double.TYPE);
932                }
933            }
934        }
935    }
936
937    /**
938     * @tests java.lang.reflect.Method#toString()
939     */
940    @TestTargetNew(
941        level = TestLevel.COMPLETE,
942        notes = "",
943        method = "toString",
944        args = {}
945    )
946    public void test_toString() {
947        // Test for method java.lang.String java.lang.reflect.Method.toString()
948        Method mth = null;
949        Class[] parms = { int.class, short.class, String.class, boolean.class,
950                Object.class, long.class, byte.class, char.class, double.class,
951                float.class };
952        try {
953
954            mth = TestMethod.class.getDeclaredMethod("printTest", parms);
955        } catch (Exception e) {
956            fail("Exception during toString test : " + e.getMessage());
957        }
958
959        assertTrue(
960                "Returned incorrect string for method: " + mth.toString(),
961                mth
962                        .toString()
963                        .equals(
964                                "public static final void tests.api.java.lang.reflect.MethodTest$TestMethod.printTest(int,short,java.lang.String,boolean,java.lang.Object,long,byte,char,double,float)"));
965    }
966
967    /**
968     * Sets up the fixture, for example, open a network connection. This method
969     * is called before a test is executed.
970     */
971    protected void setUp() {
972    }
973
974    /**
975     * Tears down the fixture, for example, close a network connection. This
976     * method is called after a test is executed.
977     */
978    protected void tearDown() {
979    }
980}
981