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.lang.reflect.Constructor; 21import java.lang.reflect.InvocationTargetException; 22import java.lang.reflect.Modifier; 23import java.util.Vector; 24 25public class ConstructorTest extends junit.framework.TestCase { 26 27 static class ConstructorTestHelper extends Object { 28 int cval; 29 30 public ConstructorTestHelper() throws IndexOutOfBoundsException { 31 cval = 99; 32 } 33 34 public ConstructorTestHelper(Object x) { 35 } 36 37 private ConstructorTestHelper(int a) { 38 } 39 40 protected ConstructorTestHelper(long a) { 41 } 42 43 public int check() { 44 return cval; 45 } 46 } 47 48 /** 49 * @tests java.lang.reflect.Constructor#equals(java.lang.Object) 50 */ 51 public void test_equalsLjava_lang_Object() throws Exception { 52 Class[] types = null; 53 Constructor ctor1 = null, ctor2 = null; 54 ctor1 = new ConstructorTestHelper().getClass().getConstructor( 55 new Class[0]); 56 57 Class[] parms = null; 58 parms = new Class[1]; 59 parms[0] = new Object().getClass(); 60 ctor2 = new ConstructorTestHelper().getClass().getConstructor(parms); 61 62 assertTrue("Different Constructors returned equal", !ctor1 63 .equals(ctor2)); 64 } 65 66 /** 67 * @tests java.lang.reflect.Constructor#getDeclaringClass() 68 */ 69 public void test_getDeclaringClass() throws Exception { 70 // Test for method java.lang.Class 71 // java.lang.reflect.Constructor.getDeclaringClass() 72 boolean val = false; 73 Class pclass = new ConstructorTestHelper().getClass(); 74 Constructor ctor = pclass.getConstructor(new Class[0]); 75 val = ctor.getDeclaringClass().equals(pclass); 76 77 assertTrue("Returned incorrect declaring class", val); 78 } 79 80 /** 81 * @tests java.lang.reflect.Constructor#getExceptionTypes() 82 */ 83 public void test_getExceptionTypes() throws Exception { 84 Class[] exceptions = null; 85 Class ex = null; 86 Constructor ctor = new ConstructorTestHelper().getClass() 87 .getConstructor(new Class[0]); 88 exceptions = ctor.getExceptionTypes(); 89 ex = new IndexOutOfBoundsException().getClass(); 90 91 assertEquals("Returned exception list of incorrect length", 1, 92 exceptions.length); 93 assertTrue("Returned incorrect exception", exceptions[0].equals(ex)); 94 } 95 96 /** 97 * @tests java.lang.reflect.Constructor#getModifiers() 98 */ 99 public void test_getModifiers() { 100 int mod = 0; 101 try { 102 Constructor ctor = new ConstructorTestHelper().getClass() 103 .getConstructor(new Class[0]); 104 mod = ctor.getModifiers(); 105 assertTrue("Returned incorrect modifers for public ctor", 106 ((mod & Modifier.PUBLIC) == Modifier.PUBLIC) 107 && ((mod & Modifier.PRIVATE) == 0)); 108 } catch (NoSuchMethodException e) { 109 fail("Exception during test : " + e.getMessage()); 110 } 111 try { 112 Class[] cl = { int.class }; 113 Constructor ctor = new ConstructorTestHelper().getClass() 114 .getDeclaredConstructor(cl); 115 mod = ctor.getModifiers(); 116 assertTrue("Returned incorrect modifers for private ctor", 117 ((mod & Modifier.PRIVATE) == Modifier.PRIVATE) 118 && ((mod & Modifier.PUBLIC) == 0)); 119 } catch (NoSuchMethodException e) { 120 fail("Exception during test : " + e.getMessage()); 121 } 122 try { 123 Class[] cl = { long.class }; 124 Constructor ctor = new ConstructorTestHelper().getClass() 125 .getDeclaredConstructor(cl); 126 mod = ctor.getModifiers(); 127 assertTrue("Returned incorrect modifers for private ctor", 128 ((mod & Modifier.PROTECTED) == Modifier.PROTECTED) 129 && ((mod & Modifier.PUBLIC) == 0)); 130 } catch (NoSuchMethodException e) { 131 fail("NoSuchMethodException during test : " + e.getMessage()); 132 } 133 } 134 135 /** 136 * @tests java.lang.reflect.Constructor#getName() 137 */ 138 public void test_getName() throws Exception { 139 Constructor ctor = new ConstructorTestHelper().getClass() 140 .getConstructor(new Class[0]); 141 assertTrue( 142 "Returned incorrect name: " + ctor.getName(), 143 ctor 144 .getName() 145 .equals( 146 "org.apache.harmony.luni.tests.java.lang.reflect.ConstructorTest$ConstructorTestHelper")); 147 } 148 149 /** 150 * @tests java.lang.reflect.Constructor#getParameterTypes() 151 */ 152 public void test_getParameterTypes() throws Exception { 153 Class[] types = null; 154 Constructor ctor = new ConstructorTestHelper().getClass() 155 .getConstructor(new Class[0]); 156 types = ctor.getParameterTypes(); 157 158 assertEquals("Incorrect parameter returned", 0, types.length); 159 160 Class[] parms = null; 161 parms = new Class[1]; 162 parms[0] = new Object().getClass(); 163 ctor = new ConstructorTestHelper().getClass().getConstructor(parms); 164 types = ctor.getParameterTypes(); 165 166 assertTrue("Incorrect parameter returned", types[0].equals(parms[0])); 167 } 168 169 /** 170 * @tests java.lang.reflect.Constructor#newInstance(java.lang.Object[]) 171 */ 172 public void test_newInstance$Ljava_lang_Object() throws Exception { 173 ConstructorTestHelper test = null; 174 Constructor ctor = new ConstructorTestHelper().getClass() 175 .getConstructor(new Class[0]); 176 test = (ConstructorTestHelper) ctor.newInstance((Object[]) null); 177 178 assertEquals("improper instance created", 99, test.check()); 179 } 180 181 /** 182 * @tests java.lang.reflect.Constructor#newInstance(java.lang.Object[]) 183 */ 184 public void test_newInstance_IAE() throws Exception { 185 Constructor constructor = Vector.class 186 .getConstructor(new Class[] { Integer.TYPE }); 187 188 try { 189 constructor.newInstance(new Object[] { null }); 190 fail("should throw IllegalArgumentException"); 191 } catch (IllegalArgumentException e) { 192 // Expected 193 } 194 } 195 196 public void test_newInstance_InvocationTargetException() throws Exception { 197 Constructor constructor = MockObject.class.getConstructor(Class.class); 198 199 try { 200 constructor.newInstance(InvocationTargetException.class); 201 fail("should throw InvocationTargetException"); 202 } catch (InvocationTargetException e) { 203 // Expected 204 } 205 206 try { 207 constructor.newInstance(IllegalAccessException.class); 208 fail("should throw InvocationTargetException"); 209 } catch (InvocationTargetException e) { 210 // Expected 211 } 212 213 try { 214 constructor.newInstance(IllegalArgumentException.class); 215 fail("should throw InvocationTargetException"); 216 } catch (InvocationTargetException e) { 217 // Expected 218 } 219 220 try { 221 constructor.newInstance(InvocationTargetException.class); 222 fail("should throw InvocationTargetException"); 223 } catch (InvocationTargetException e) { 224 // Expected 225 } 226 227 try { 228 constructor.newInstance(Throwable.class); 229 fail("should throw InvocationTargetException"); 230 } catch (InvocationTargetException e) { 231 // Expected 232 } 233 } 234 235 static class MockObject { 236 237 public MockObject(Class<?> clazz) throws Exception { 238 if (clazz == InstantiationException.class) { 239 throw new InstantiationException(); 240 } else if (clazz == IllegalAccessException.class) { 241 throw new IllegalAccessException(); 242 } else if (clazz == IllegalArgumentException.class) { 243 throw new IllegalArgumentException(); 244 } else if (clazz == InvocationTargetException.class) { 245 throw new InvocationTargetException(new Throwable()); 246 } else { 247 throw new Exception(); 248 } 249 } 250 251 } 252 253 /** 254 * @tests java.lang.reflect.Constructor#toString() 255 */ 256 public void test_toString() throws Exception { 257 Class[] parms = null; 258 Constructor ctor = null; 259 parms = new Class[1]; 260 parms[0] = new Object().getClass(); 261 ctor = new ConstructorTestHelper().getClass().getConstructor(parms); 262 263 assertTrue( 264 "Returned incorrect string representation: " + ctor.toString(), 265 ctor 266 .toString() 267 .equals( 268 "public org.apache.harmony.luni.tests.java.lang.reflect.ConstructorTest$ConstructorTestHelper(java.lang.Object)")); 269 } 270} 271