136efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes/*
236efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * Copyright (C) 2009 The Android Open Source Project
3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
436efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * you may not use this file except in compliance with the License.
636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * You may obtain a copy of the License at
7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
836efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
1036efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * Unless required by applicable law or agreed to in writing, software
1136efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1236efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1336efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * See the License for the specific language governing permissions and
1436efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes * limitations under the License.
1536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes */
1636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes
174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.lang.reflect;
184557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilson
194557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.lang.reflect.Constructor;
2011229f6af89fc54e70675fcab098b1572d6e539eJesse Wilsonimport junit.framework.TestCase;
2136efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes
2211229f6af89fc54e70675fcab098b1572d6e539eJesse Wilsonpublic final class ConstructorTest extends TestCase {
2336efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    public void test_getExceptionTypes() throws Exception {
2436efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(new Class[0]);
2536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Class[] exceptions = constructor.getExceptionTypes();
2636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(1, exceptions.length);
2736efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(IndexOutOfBoundsException.class, exceptions[0]);
2836efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        // Check that corrupting our array doesn't affect other callers.
2936efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        exceptions[0] = NullPointerException.class;
3036efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        exceptions = constructor.getExceptionTypes();
3136efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(1, exceptions.length);
3236efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(IndexOutOfBoundsException.class, exceptions[0]);
3336efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    }
3436efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes
3536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    public void test_getParameterTypes() throws Exception {
3636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Class[] expectedParameters = new Class[] { Object.class };
3736efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
3836efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Class[] parameters = constructor.getParameterTypes();
3936efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(1, parameters.length);
4036efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(expectedParameters[0], parameters[0]);
4136efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        // Check that corrupting our array doesn't affect other callers.
4236efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        parameters[0] = String.class;
4336efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        parameters = constructor.getParameterTypes();
4436efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(1, parameters.length);
4536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(expectedParameters[0], parameters[0]);
4636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    }
4736efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes
4811229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    public void testGetConstructorWithNullArgumentsArray() throws Exception {
4911229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor((Class[]) null);
5011229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        assertEquals(0, constructor.getParameterTypes().length);
5111229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    }
5211229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson
5311229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    public void testGetConstructorWithNullArgument() throws Exception {
5411229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        try {
5511229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson            ConstructorTestHelper.class.getConstructor(new Class[] { null });
5611229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson            fail();
5711229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        } catch (NoSuchMethodException expected) {
5811229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        }
5911229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    }
6011229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson
6111229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    public void testGetConstructorReturnsDoesNotReturnPrivateConstructor() throws Exception {
6211229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        try {
6311229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson            ConstructorTestHelper.class.getConstructor(Object.class, Object.class);
6411229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson            fail();
6511229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        } catch (NoSuchMethodException expected) {
6611229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        }
6711229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    }
6811229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson
6911229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    public void testGetDeclaredConstructorReturnsPrivateConstructor() throws Exception {
7011229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        Constructor<?> constructor = ConstructorTestHelper.class.getDeclaredConstructor(
7111229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson                Object.class, Object.class);
7211229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        assertEquals(2, constructor.getParameterTypes().length);
7311229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    }
7411229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson
7536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    static class ConstructorTestHelper {
7636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        public ConstructorTestHelper() throws IndexOutOfBoundsException { }
7736efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        public ConstructorTestHelper(Object o) { }
7811229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        private ConstructorTestHelper(Object a, Object b) { }
7936efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    }
8036efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes}
81