1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.lang.reflect; 18 19import java.lang.reflect.Constructor; 20import junit.framework.TestCase; 21 22public final class ConstructorTest extends TestCase { 23 public void test_getExceptionTypes() throws Exception { 24 Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(new Class[0]); 25 Class[] exceptions = constructor.getExceptionTypes(); 26 assertEquals(1, exceptions.length); 27 assertEquals(IndexOutOfBoundsException.class, exceptions[0]); 28 // Check that corrupting our array doesn't affect other callers. 29 exceptions[0] = NullPointerException.class; 30 exceptions = constructor.getExceptionTypes(); 31 assertEquals(1, exceptions.length); 32 assertEquals(IndexOutOfBoundsException.class, exceptions[0]); 33 } 34 35 public void test_getParameterTypes() throws Exception { 36 Class[] expectedParameters = new Class[] { Object.class }; 37 Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(expectedParameters); 38 Class[] parameters = constructor.getParameterTypes(); 39 assertEquals(1, parameters.length); 40 assertEquals(expectedParameters[0], parameters[0]); 41 // Check that corrupting our array doesn't affect other callers. 42 parameters[0] = String.class; 43 parameters = constructor.getParameterTypes(); 44 assertEquals(1, parameters.length); 45 assertEquals(expectedParameters[0], parameters[0]); 46 } 47 48 public void testGetConstructorWithNullArgumentsArray() throws Exception { 49 Constructor<?> constructor = ConstructorTestHelper.class.getConstructor((Class[]) null); 50 assertEquals(0, constructor.getParameterTypes().length); 51 } 52 53 public void testGetConstructorWithNullArgument() throws Exception { 54 try { 55 ConstructorTestHelper.class.getConstructor(new Class[] { null }); 56 fail(); 57 } catch (NoSuchMethodException expected) { 58 } 59 } 60 61 public void testGetConstructorReturnsDoesNotReturnPrivateConstructor() throws Exception { 62 try { 63 ConstructorTestHelper.class.getConstructor(Object.class, Object.class); 64 fail(); 65 } catch (NoSuchMethodException expected) { 66 } 67 } 68 69 public void testGetDeclaredConstructorReturnsPrivateConstructor() throws Exception { 70 Constructor<?> constructor = ConstructorTestHelper.class.getDeclaredConstructor( 71 Object.class, Object.class); 72 assertEquals(2, constructor.getParameterTypes().length); 73 } 74 75 static class ConstructorTestHelper { 76 public ConstructorTestHelper() throws IndexOutOfBoundsException { } 77 public ConstructorTestHelper(Object o) { } 78 private ConstructorTestHelper(Object a, Object b) { } 79 } 80} 81