ConstructorTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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 tests.api.java.lang.reflect; 19 20import dalvik.annotation.TestInfo; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTarget; 23import dalvik.annotation.TestTargetClass; 24 25import java.lang.reflect.Constructor; 26import java.lang.reflect.Modifier; 27 28@TestTargetClass(Constructor.class) 29public class ConstructorTest extends junit.framework.TestCase { 30 31 static class ConstructorTestHelper extends Object { 32 int cval; 33 34 public ConstructorTestHelper() throws IndexOutOfBoundsException { 35 cval = 99; 36 } 37 38 public ConstructorTestHelper(Object x) { 39 } 40 41 private ConstructorTestHelper(int a) { 42 } 43 44 protected ConstructorTestHelper(long a) { 45 } 46 47 public int check() { 48 return cval; 49 } 50 } 51 52 /** 53 * @tests java.lang.reflect.Constructor#equals(java.lang.Object) 54 */ 55 @TestInfo( 56 level = TestLevel.COMPLETE, 57 purpose = "", 58 targets = { 59 @TestTarget( 60 methodName = "equals", 61 methodArgs = {java.lang.Object.class} 62 ) 63 }) 64 public void test_equalsLjava_lang_Object() { 65 // Test for method boolean 66 // java.lang.reflect.Constructor.equals(java.lang.Object) 67 Class[] types = null; 68 Constructor ctor1 = null, ctor2 = null; 69 try { 70 ctor1 = new ConstructorTestHelper().getClass().getConstructor( 71 new Class[0]); 72 73 Class[] parms = null; 74 parms = new Class[1]; 75 parms[0] = new Object().getClass(); 76 ctor2 = new ConstructorTestHelper().getClass() 77 .getConstructor(parms); 78 } catch (Exception e) { 79 fail("Exception during equals test : " + e.getMessage()); 80 } 81 assertTrue("Different Contructors returned equal", !ctor1.equals(ctor2)); 82 } 83 84 /** 85 * @tests java.lang.reflect.Constructor#getDeclaringClass() 86 */ 87 @TestInfo( 88 level = TestLevel.COMPLETE, 89 purpose = "", 90 targets = { 91 @TestTarget( 92 methodName = "getDeclaringClass", 93 methodArgs = {} 94 ) 95 }) 96 public void test_getDeclaringClass() { 97 // Test for method java.lang.Class 98 // java.lang.reflect.Constructor.getDeclaringClass() 99 boolean val = false; 100 try { 101 Class pclass = new ConstructorTestHelper().getClass(); 102 Constructor ctor = pclass.getConstructor(new Class[0]); 103 val = ctor.getDeclaringClass().equals(pclass); 104 } catch (Exception e) { 105 fail("Exception during test : " + e.getMessage()); 106 } 107 assertTrue("Returned incorrect declaring class", val); 108 } 109 110 /** 111 * @tests java.lang.reflect.Constructor#getExceptionTypes() 112 */ 113 @TestInfo( 114 level = TestLevel.COMPLETE, 115 purpose = "", 116 targets = { 117 @TestTarget( 118 methodName = "getExceptionTypes", 119 methodArgs = {} 120 ) 121 }) 122 public void test_getExceptionTypes() { 123 // Test for method java.lang.Class [] 124 // java.lang.reflect.Constructor.getExceptionTypes() 125 Class[] exceptions = null; 126 Class ex = null; 127 try { 128 Constructor ctor = new ConstructorTestHelper().getClass() 129 .getConstructor(new Class[0]); 130 exceptions = ctor.getExceptionTypes(); 131 ex = new IndexOutOfBoundsException().getClass(); 132 } catch (Exception e) { 133 fail("Exception during test : " + e.getMessage()); 134 } 135 assertEquals("Returned exception list of incorrect length", 136 1, exceptions.length); 137 assertTrue("Returned incorrect exception", exceptions[0].equals(ex)); 138 } 139 140 /** 141 * @tests java.lang.reflect.Constructor#getModifiers() 142 */ 143 @TestInfo( 144 level = TestLevel.COMPLETE, 145 purpose = "", 146 targets = { 147 @TestTarget( 148 methodName = "getModifiers", 149 methodArgs = {} 150 ) 151 }) 152 public void test_getModifiers() { 153 // Test for method int java.lang.reflect.Constructor.getModifiers() 154 int mod = 0; 155 try { 156 Constructor ctor = new ConstructorTestHelper().getClass() 157 .getConstructor(new Class[0]); 158 mod = ctor.getModifiers(); 159 assertTrue("Returned incorrect modifers for public ctor", 160 ((mod & Modifier.PUBLIC) == Modifier.PUBLIC) 161 && ((mod & Modifier.PRIVATE) == 0)); 162 } catch (NoSuchMethodException e) { 163 fail("Exception during test : " + e.getMessage()); 164 } 165 try { 166 Class[] cl = { int.class }; 167 Constructor ctor = new ConstructorTestHelper().getClass() 168 .getDeclaredConstructor(cl); 169 mod = ctor.getModifiers(); 170 assertTrue("Returned incorrect modifers for private ctor", 171 ((mod & Modifier.PRIVATE) == Modifier.PRIVATE) 172 && ((mod & Modifier.PUBLIC) == 0)); 173 } catch (NoSuchMethodException e) { 174 fail("Exception during test : " + e.getMessage()); 175 } 176 try { 177 Class[] cl = { long.class }; 178 Constructor ctor = new ConstructorTestHelper().getClass() 179 .getDeclaredConstructor(cl); 180 mod = ctor.getModifiers(); 181 assertTrue("Returned incorrect modifers for private ctor", 182 ((mod & Modifier.PROTECTED) == Modifier.PROTECTED) 183 && ((mod & Modifier.PUBLIC) == 0)); 184 } catch (NoSuchMethodException e) { 185 fail("NoSuchMethodException during test : " + e.getMessage()); 186 } 187 } 188 189 /** 190 * @tests java.lang.reflect.Constructor#getName() 191 */ 192 @TestInfo( 193 level = TestLevel.COMPLETE, 194 purpose = "", 195 targets = { 196 @TestTarget( 197 methodName = "getName", 198 methodArgs = {} 199 ) 200 }) 201 public void test_getName() { 202 // Test for method java.lang.String 203 // java.lang.reflect.Constructor.getName() 204 try { 205 Constructor ctor = new ConstructorTestHelper().getClass() 206 .getConstructor(new Class[0]); 207 assertTrue( 208 "Returned incorrect name: " + ctor.getName(), 209 ctor 210 .getName() 211 .equals( 212 "tests.api.java.lang.reflect.ConstructorTest$ConstructorTestHelper")); 213 } catch (Exception e) { 214 fail("Exception obtaining contructor : " + e.getMessage()); 215 } 216 } 217 218 /** 219 * @tests java.lang.reflect.Constructor#getParameterTypes() 220 */ 221 @TestInfo( 222 level = TestLevel.COMPLETE, 223 purpose = "", 224 targets = { 225 @TestTarget( 226 methodName = "getParameterTypes", 227 methodArgs = {} 228 ) 229 }) 230 public void test_getParameterTypes() { 231 // Test for method java.lang.Class [] 232 // java.lang.reflect.Constructor.getParameterTypes() 233 Class[] types = null; 234 try { 235 Constructor ctor = new ConstructorTestHelper().getClass() 236 .getConstructor(new Class[0]); 237 types = ctor.getParameterTypes(); 238 } catch (Exception e) { 239 fail("Exception during getParameterTypes test:" 240 + e.toString()); 241 } 242 assertEquals("Incorrect parameter returned", 0, types.length); 243 244 Class[] parms = null; 245 try { 246 parms = new Class[1]; 247 parms[0] = new Object().getClass(); 248 Constructor ctor = new ConstructorTestHelper().getClass() 249 .getConstructor(parms); 250 types = ctor.getParameterTypes(); 251 } catch (Exception e) { 252 fail("Exception during getParameterTypes test:" 253 + e.toString()); 254 } 255 assertTrue("Incorrect parameter returned", types[0].equals(parms[0])); 256 } 257 258 /** 259 * @tests java.lang.reflect.Constructor#newInstance(java.lang.Object[]) 260 */ 261 @TestInfo( 262 level = TestLevel.COMPLETE, 263 purpose = "", 264 targets = { 265 @TestTarget( 266 methodName = "newInstance", 267 methodArgs = {java.lang.Object[].class} 268 ) 269 }) 270 public void test_newInstance$Ljava_lang_Object() { 271 // Test for method java.lang.Object 272 // java.lang.reflect.Constructor.newInstance(java.lang.Object []) 273 274 ConstructorTestHelper test = null; 275 try { 276 Constructor ctor = new ConstructorTestHelper().getClass() 277 .getConstructor(new Class[0]); 278 test = (ConstructorTestHelper) ctor.newInstance((Object[])null); 279 } catch (Exception e) { 280 fail("Failed to create instance : " + e.getMessage()); 281 } 282 assertEquals("improper instance created", 99, test.check()); 283 } 284 285 /** 286 * @tests java.lang.reflect.Constructor#toString() 287 */ 288 @TestInfo( 289 level = TestLevel.COMPLETE, 290 purpose = "", 291 targets = { 292 @TestTarget( 293 methodName = "toString", 294 methodArgs = {} 295 ) 296 }) 297 public void test_toString() { 298 // Test for method java.lang.String 299 // java.lang.reflect.Constructor.toString() 300 Class[] parms = null; 301 Constructor ctor = null; 302 try { 303 parms = new Class[1]; 304 parms[0] = new Object().getClass(); 305 ctor = new ConstructorTestHelper().getClass().getConstructor(parms); 306 } catch (Exception e) { 307 fail("Exception during getParameterTypes test:" 308 + e.toString()); 309 } 310 assertTrue( 311 "Returned incorrect string representation: " + ctor.toString(), 312 ctor 313 .toString() 314 .equals( 315 "public tests.api.java.lang.reflect.ConstructorTest$ConstructorTestHelper(java.lang.Object)")); 316 } 317 318 /** 319 * Sets up the fixture, for example, open a network connection. This method 320 * is called before a test is executed. 321 */ 322 protected void setUp() { 323 } 324 325 /** 326 * Tears down the fixture, for example, close a network connection. This 327 * method is called after a test is executed. 328 */ 329 protected void tearDown() { 330 } 331} 332