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