Home | History | Annotate | Download | only in reflect
      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 
     18 package org.apache.harmony.tests.java.lang.reflect;
     19 
     20 import java.io.ByteArrayOutputStream;
     21 import java.io.CharArrayWriter;
     22 import java.io.PrintStream;
     23 import java.io.PrintWriter;
     24 import java.lang.reflect.Constructor;
     25 import java.lang.reflect.InvocationTargetException;
     26 import java.lang.reflect.Method;
     27 import java.lang.reflect.Modifier;
     28 
     29 public class InvocationTargetExceptionTest extends junit.framework.TestCase {
     30 
     31     static class TestMethod {
     32         public TestMethod() {
     33         }
     34 
     35         public void voidMethod() throws IllegalArgumentException {
     36         }
     37 
     38         public void parmTest(int x, short y, String s, boolean bool, Object o,
     39                 long l, byte b, char c, double d, float f) {
     40         }
     41 
     42         public int intMethod() {
     43             return 1;
     44         }
     45 
     46         public static final void printTest(int x, short y, String s,
     47                 boolean bool, Object o, long l, byte b, char c, double d,
     48                 float f) {
     49         }
     50 
     51         public double doubleMethod() {
     52             return 1.0;
     53         }
     54 
     55         public short shortMethod() {
     56             return (short) 1;
     57         }
     58 
     59         public byte byteMethod() {
     60             return (byte) 1;
     61         }
     62 
     63         public float floatMethod() {
     64             return 1.0f;
     65         }
     66 
     67         public long longMethod() {
     68             return 1l;
     69         }
     70 
     71         public char charMethod() {
     72             return 'T';
     73         }
     74 
     75         public Object objectMethod() {
     76             return new Object();
     77         }
     78 
     79         private static void prstatic() {
     80         }
     81 
     82         public static void pustatic() {
     83         }
     84 
     85         public static synchronized void pustatsynch() {
     86         }
     87 
     88         public static int invokeStaticTest() {
     89             return 1;
     90         }
     91 
     92         public int invokeInstanceTest() {
     93             return 1;
     94         }
     95 
     96         private int privateInvokeTest() {
     97             return 1;
     98         }
     99 
    100         public int invokeExceptionTest() throws NullPointerException {
    101             throw new NullPointerException();
    102         }
    103 
    104         public static synchronized native void pustatsynchnat();
    105 
    106     }
    107 
    108     abstract class AbstractTestMethod {
    109         public abstract void puabs();
    110     }
    111 
    112     class SubInvocationTargetException extends InvocationTargetException {}
    113 
    114     /**
    115      * java.lang.reflect.InvocationTargetException#InvocationTargetException()
    116      */
    117     public void test_Constructor() throws Exception {
    118         Constructor<InvocationTargetException> ctor = InvocationTargetException.class
    119                 .getDeclaredConstructor();
    120         assertNotNull("Parameterless constructor does not exist.", ctor);
    121         assertTrue("Constructor is not protected", Modifier.isProtected(ctor
    122                 .getModifiers()));
    123         //create an instance of a subtype using this constructor
    124         SubInvocationTargetException subException = new SubInvocationTargetException();
    125     }
    126 
    127     /**
    128      * java.lang.reflect.InvocationTargetException#InvocationTargetException(java.lang.Throwable)
    129      */
    130     public void test_ConstructorLjava_lang_Throwable() {
    131         // Test for method
    132         // java.lang.reflect.InvocationTargetException(java.lang.Throwable)
    133         try {
    134             Method mth = TestMethod.class.getDeclaredMethod(
    135                     "invokeExceptionTest", new Class[0]);
    136             Object[] args = { Object.class };
    137             Object ret = mth.invoke(new TestMethod(), new Object[0]);
    138         } catch (InvocationTargetException e) {
    139             // Correct behaviour
    140             return;
    141         } catch (NoSuchMethodException e) {
    142         } catch (IllegalAccessException e) {
    143         }
    144         fail("Failed to throw exception");
    145     }
    146 
    147     /**
    148      * java.lang.reflect.InvocationTargetException#InvocationTargetException(java.lang.Throwable,
    149      *        java.lang.String)
    150      */
    151     public void test_ConstructorLjava_lang_ThrowableLjava_lang_String() {
    152         // Test for method
    153         // java.lang.reflect.InvocationTargetException(java.lang.Throwable,
    154         // java.lang.String)
    155         try {
    156             Method mth = TestMethod.class.getDeclaredMethod(
    157                     "invokeExceptionTest", new Class[0]);
    158             Object[] args = { Object.class };
    159             Object ret = mth.invoke(new TestMethod(), new Object[0]);
    160         } catch (InvocationTargetException e) {
    161             // Correct behaviour
    162             return;
    163         } catch (NoSuchMethodException e) {
    164             ;
    165         } catch (IllegalAccessException e) {
    166         }
    167         fail("Failed to throw exception");
    168     }
    169 
    170     /**
    171      * java.lang.reflect.InvocationTargetException#getTargetException()
    172      */
    173     public void test_getTargetException() {
    174         // Test for method java.lang.Throwable
    175         // java.lang.reflect.InvocationTargetException.getTargetException()
    176         try {
    177             Method mth = TestMethod.class.getDeclaredMethod(
    178                     "invokeExceptionTest", new Class[0]);
    179             Object[] args = { Object.class };
    180             Object ret = mth.invoke(new TestMethod(), new Object[0]);
    181         } catch (InvocationTargetException e) {
    182             // Correct behaviour
    183             assertTrue("Returned incorrect target exception", e
    184                     .getTargetException() instanceof NullPointerException);
    185             return;
    186         } catch (Exception e) {
    187             fail("Exception during constructor test : " + e.getMessage());
    188         }
    189         fail("Failed to throw exception");
    190     }
    191 
    192     /**
    193      * java.lang.reflect.InvocationTargetException#getCause()
    194      */
    195     public void test_getCause() {
    196         // java.lang.reflect.InvocationTargetException.getCause()
    197         try {
    198             Method mth = TestMethod.class.getDeclaredMethod(
    199                     "invokeExceptionTest", new Class[0]);
    200             Object[] args = {Object.class};
    201             Object ret = mth.invoke(new TestMethod(), new Object[0]);
    202         } catch (InvocationTargetException e) {
    203             // Correct behaviour
    204             assertTrue("Returned incorrect cause",
    205                     e.getCause() instanceof NullPointerException);
    206             return;
    207         } catch (Exception e) {
    208             fail("Exception during InvocationTargetException test : "
    209                     + e.getMessage());
    210         }
    211         fail("Failed to throw exception");
    212     }
    213 
    214     /**
    215      * java.lang.reflect.InvocationTargetException#printStackTrace()
    216      */
    217     public void test_printStackTrace() {
    218         // Test for method void
    219         // java.lang.reflect.InvocationTargetException.printStackTrace()
    220         try {
    221             ByteArrayOutputStream bao = new ByteArrayOutputStream();
    222             PrintStream ps = new PrintStream(bao);
    223             PrintStream oldErr = System.err;
    224             System.setErr(ps);
    225             InvocationTargetException ite = new InvocationTargetException(null);
    226             ite.printStackTrace();
    227             System.setErr(oldErr);
    228 
    229             String s = new String(bao.toByteArray());
    230 
    231             assertTrue("Incorrect Stack trace: " + s, s != null
    232                     && s.length() > 300);
    233         } catch (Exception e) {
    234             fail("printStackTrace() caused exception : " + e.getMessage());
    235         }
    236     }
    237 
    238     /**
    239      * java.lang.reflect.InvocationTargetException#printStackTrace(java.io.PrintStream)
    240      */
    241     public void test_printStackTraceLjava_io_PrintStream() {
    242         // Test for method void
    243         // java.lang.reflect.InvocationTargetException.printStackTrace(java.io.PrintStream)
    244         assertTrue("Tested via test_printStackTrace().", true);
    245         ByteArrayOutputStream bao = new ByteArrayOutputStream();
    246         PrintStream ps = new PrintStream(bao);
    247         InvocationTargetException ite = new InvocationTargetException(
    248                 new InvocationTargetException(null));
    249         ite.printStackTrace(ps);
    250         String s = bao.toString();
    251         assertTrue("printStackTrace failed." + s.length(), s != null
    252                 && s.length() > 400);
    253     }
    254 
    255     /**
    256      * java.lang.reflect.InvocationTargetException#printStackTrace(java.io.PrintWriter)
    257      */
    258     public void test_printStackTraceLjava_io_PrintWriter() {
    259         // Test for method void
    260         // java.lang.reflect.InvocationTargetException.printStackTrace(java.io.PrintWriter)
    261         try {
    262             PrintWriter pw;
    263             InvocationTargetException ite;
    264             String s;
    265             CharArrayWriter caw = new CharArrayWriter();
    266             pw = new PrintWriter(caw);
    267             ite = new InvocationTargetException(new InvocationTargetException(
    268                     null));
    269             ite.printStackTrace(pw);
    270 
    271             s = caw.toString();
    272             assertTrue("printStackTrace failed." + s.length(), s != null
    273                     && s.length() > 400);
    274             pw.close();
    275 
    276             ByteArrayOutputStream bao = new ByteArrayOutputStream();
    277             pw = new PrintWriter(bao);
    278             ite = new InvocationTargetException(new InvocationTargetException(
    279                     null));
    280             ite.printStackTrace(pw);
    281 
    282             pw.flush(); // Test will fail if this line removed.
    283             s = bao.toString();
    284             assertTrue("printStackTrace failed." + s.length(), s != null
    285                     && s.length() > 400);
    286 
    287         } catch (Exception e) {
    288             fail("Exception during test : " + e.getMessage());
    289         }
    290     }
    291 
    292     /**
    293      * Sets up the fixture, for example, open a network connection. This method
    294      * is called before a test is executed.
    295      */
    296     protected void setUp() {
    297     }
    298 
    299     /**
    300      * Tears down the fixture, for example, close a network connection. This
    301      * method is called after a test is executed.
    302      */
    303     protected void tearDown() {
    304     }
    305 }
    306