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;
207d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fullerimport java.lang.reflect.Parameter;
217d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller
2211229f6af89fc54e70675fcab098b1572d6e539eJesse Wilsonimport junit.framework.TestCase;
2336efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes
2411229f6af89fc54e70675fcab098b1572d6e539eJesse Wilsonpublic final class ConstructorTest extends TestCase {
2536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    public void test_getExceptionTypes() throws Exception {
2636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(new Class[0]);
2736efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Class[] exceptions = constructor.getExceptionTypes();
2836efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(1, exceptions.length);
2936efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(IndexOutOfBoundsException.class, exceptions[0]);
3036efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        // Check that corrupting our array doesn't affect other callers.
3136efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        exceptions[0] = NullPointerException.class;
3236efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        exceptions = constructor.getExceptionTypes();
3336efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(1, exceptions.length);
3436efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(IndexOutOfBoundsException.class, exceptions[0]);
3536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    }
3636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes
3736efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    public void test_getParameterTypes() throws Exception {
387d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        Class[] expectedParameters = new Class[0];
3936efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
407d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        assertEquals(0, constructor.getParameterTypes().length);
417d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller
427d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        expectedParameters = new Class[] { Object.class };
437d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
4436efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        Class[] parameters = constructor.getParameterTypes();
4536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(1, parameters.length);
4636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(expectedParameters[0], parameters[0]);
4736efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        // Check that corrupting our array doesn't affect other callers.
4836efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        parameters[0] = String.class;
4936efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        parameters = constructor.getParameterTypes();
5036efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(1, parameters.length);
5136efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        assertEquals(expectedParameters[0], parameters[0]);
5236efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    }
5336efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes
5420938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller    public void test_getParameterCount() throws Exception {
557d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        Class[] expectedParameters = new Class[0];
5620938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
577d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        assertEquals(0, constructor.getParameterCount());
587d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller
597d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        expectedParameters = new Class[] { Object.class };
607d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
6120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller        int count = constructor.getParameterCount();
6220938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller        assertEquals(1, count);
6320938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller    }
6420938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller
657d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller    public void test_getParameters() throws Exception {
667d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        Class[] expectedParameters = new Class[0];
677d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
687d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        assertEquals(0, constructor.getParameters().length);
697d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller
707d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        expectedParameters = new Class[] { Object.class };
717d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        constructor = ConstructorTestHelper.class.getConstructor(expectedParameters);
727d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller
737d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        // Test the information available via other Constructor methods. See ParameterTest and
747d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        // annotations.ParameterTest for more in-depth Parameter testing.
757d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        Parameter[] parameters = constructor.getParameters();
767d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        assertEquals(1, parameters.length);
777d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        assertEquals(Object.class, parameters[0].getType());
787d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller
797d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        // Check that corrupting our array doesn't affect other callers.
807d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        parameters[0] = null;
817d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        parameters = constructor.getParameters();
827d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        assertEquals(1, parameters.length);
837d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller        assertEquals(Object.class, parameters[0].getType());
847d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller    }
857d38257be058ccc23f31a39ecf6d73294acb3c1eNeil Fuller
8611229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    public void testGetConstructorWithNullArgumentsArray() throws Exception {
8711229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        Constructor<?> constructor = ConstructorTestHelper.class.getConstructor((Class[]) null);
8811229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        assertEquals(0, constructor.getParameterTypes().length);
8911229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    }
9011229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson
9111229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    public void testGetConstructorWithNullArgument() throws Exception {
9211229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        try {
9311229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson            ConstructorTestHelper.class.getConstructor(new Class[] { null });
9411229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson            fail();
9511229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        } catch (NoSuchMethodException expected) {
9611229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        }
9711229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    }
9811229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson
9911229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    public void testGetConstructorReturnsDoesNotReturnPrivateConstructor() throws Exception {
10011229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        try {
10111229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson            ConstructorTestHelper.class.getConstructor(Object.class, Object.class);
10211229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson            fail();
10311229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        } catch (NoSuchMethodException expected) {
10411229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        }
10511229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    }
10611229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson
10711229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    public void testGetDeclaredConstructorReturnsPrivateConstructor() throws Exception {
10811229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        Constructor<?> constructor = ConstructorTestHelper.class.getDeclaredConstructor(
10911229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson                Object.class, Object.class);
11011229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        assertEquals(2, constructor.getParameterTypes().length);
11111229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson    }
11211229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson
1137c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson    public void testEqualConstructorEqualsAndHashCode() throws Exception {
1147c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        Constructor<?> c1 = ConstructorTestHelper.class.getConstructor();
1157c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        Constructor<?> c2 = ConstructorTestHelper.class.getConstructor();
1167c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        assertEquals(c1, c2);
1177c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        assertEquals(c1.hashCode(), c2.hashCode());
1187c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson    }
1197c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson
1207c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson    public void testHashCodeSpec() throws Exception {
1217c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        Constructor<?> c1 = ConstructorTestHelper.class.getConstructor();
1227c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        assertEquals(ConstructorTestHelper.class.getName().hashCode(), c1.hashCode());
1237c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson    }
1247c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson
1257c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson    public void testDifferentConstructorEqualsAndHashCode() throws Exception {
1267c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        Constructor<?> c1 = ConstructorTestHelper.class.getConstructor();
1277c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        Constructor<?> c2 = ConstructorTestHelper.class.getConstructor(Object.class);
1287c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson        assertFalse(c1.equals(c2));
1297c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson    }
1307c462dbaf5b0ef4a7a9c5b45e8769902edd5714bJesse Wilson
131337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    public void testToString() throws Exception {
132337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToString(
133337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$ConstructorTestHelper() throws java.lang.IndexOutOfBoundsException",
134337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                ConstructorTestHelper.class);
135337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToString(
136337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$ConstructorTestHelper(java.lang.Object)",
137337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                ConstructorTestHelper.class, Object.class);
138337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToString(
139337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "private libcore.java.lang.reflect.ConstructorTest$ConstructorTestHelper(java.lang.Object,java.lang.Object)",
140337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                ConstructorTestHelper.class, Object.class, Object.class);
141337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToString(
142337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$GenericConstructorTestHelper() throws java.lang.Exception",
143337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                GenericConstructorTestHelper.class);
144337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToString(
145337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$GenericConstructorTestHelper(java.lang.String)",
146337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                GenericConstructorTestHelper.class, String.class);
147337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToString(
148337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$GenericConstructorTestHelper(java.lang.String,java.lang.Integer)",
149337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                GenericConstructorTestHelper.class, String.class, Integer.class);
150337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    }
151337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller
152337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    private static void checkToString(String expected, Class<?> clazz, Class... constructorArgTypes)
153337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller            throws Exception {
154337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        Constructor c = clazz.getDeclaredConstructor(constructorArgTypes);
155337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        assertEquals(expected, c.toString());
156337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    }
157337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller
158337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    public void testToGenericString() throws Exception {
159337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToGenericString(
160337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$ConstructorTestHelper() throws java.lang.IndexOutOfBoundsException",
161337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                ConstructorTestHelper.class);
162337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToGenericString(
163337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$ConstructorTestHelper(java.lang.Object)",
164337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                ConstructorTestHelper.class, Object.class);
165337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToGenericString(
166337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "private libcore.java.lang.reflect.ConstructorTest$ConstructorTestHelper(java.lang.Object,java.lang.Object)",
167337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                ConstructorTestHelper.class, Object.class, Object.class);
168337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToGenericString(
169337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$GenericConstructorTestHelper() throws E",
170337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                GenericConstructorTestHelper.class);
171337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToGenericString(
172337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public libcore.java.lang.reflect.ConstructorTest$GenericConstructorTestHelper(A)",
173337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                GenericConstructorTestHelper.class, String.class);
174337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        checkToGenericString(
175337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                "public <B> libcore.java.lang.reflect.ConstructorTest$GenericConstructorTestHelper(A,B)",
176337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller                GenericConstructorTestHelper.class, String.class, Integer.class);
177337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    }
178337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller
179337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    private static void checkToGenericString(String expected, Class<?> clazz,
180337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller            Class... constructorArgTypes) throws Exception {
181337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        Constructor c = clazz.getDeclaredConstructor(constructorArgTypes);
182337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        assertEquals(expected, c.toGenericString());
183337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    }
184337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller
18536efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    static class ConstructorTestHelper {
18636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        public ConstructorTestHelper() throws IndexOutOfBoundsException { }
18736efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes        public ConstructorTestHelper(Object o) { }
18811229f6af89fc54e70675fcab098b1572d6e539eJesse Wilson        private ConstructorTestHelper(Object a, Object b) { }
18936efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes    }
190337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller
191337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    static class GenericConstructorTestHelper<A extends String, E extends Exception> {
192337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        public GenericConstructorTestHelper() throws E { }
193337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        public GenericConstructorTestHelper(A a) { }
194337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller        public <B extends Integer> GenericConstructorTestHelper(A a, B b) { }
195337414da3371c513e9b99b226bc21a93d1533c92Neil Fuller    }
19636efe4ab6422c34c98e196eee7022fecf52c7534Elliott Hughes}
197