1 19 package org.netbeans.jmi.javamodel; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.jmi.reflect.RefAssociation; 27 import javax.jmi.reflect.RefAssociationLink; 28 29 import junit.textui.TestRunner; 30 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.junit.NbTestSuite; 33 import org.netbeans.modules.javacore.jmiimpl.javamodel.ConstructorImpl; 34 import org.netbeans.modules.javacore.jmiimpl.javamodel.IsOfTypeImpl; 35 import org.netbeans.modules.javacore.jmiimpl.javamodel.JavaClassImpl; 36 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl; 37 import org.netbeans.modules.javacore.jmiimpl.javamodel.SemiPersistentElement; 38 import org.netbeans.jmi.javamodel.codegen.Utility; 39 40 41 46 public class IsOfTypeTest extends NbTestCase { 47 48 49 public IsOfTypeTest(String name) { 50 super(name); 51 52 } 53 54 public static NbTestSuite suite() { 55 NbTestSuite suite = new NbTestSuite(); 56 suite.addTest(new IsOfTypeTest("testAllLinks")); 57 suite.addTest(new IsOfTypeTest("testNullArguments")); 58 suite.addTest(new IsOfTypeTest("testImproperArguments")); 59 suite.addTest(new IsOfTypeTest("testAddRemoveAssociation")); 60 suite.addTest(new IsOfTypeTest("testAddRemoveTypedElement")); 61 suite.addTest(new IsOfTypeTest("testReferenceSet")); 62 return suite; 63 } 64 65 66 public static void main(java.lang.String [] args) { 67 TestRunner.run(suite()); 68 } 69 70 JavaModelPackage pkg; 71 IsOfTypeImpl isOfTypeImpl; 72 JavaClass class1, class2, interface2; 73 Field field1, field2, field3; 74 CallableFeature cfeature1, cfeature2; 75 76 protected void setUp() { 77 class1 = Utility.findClass("org.netbeans.test.classes.Class1"); 78 pkg = (JavaModelPackage) class1.refImmediatePackage(); 79 isOfTypeImpl = (IsOfTypeImpl) pkg.getIsOfType(); 80 try { Thread.sleep(2000); } catch (Exception ex) {} 81 82 assertNotNull("Class1", class1); 83 assertFalse("Class1 is instance of UnresolvedClass", class1 instanceof UnresolvedClass); 84 class2 = Utility.findClass("org.netbeans.test.classes.Class2"); 85 assertNotNull("Class2", class2); 86 assertFalse("Class2 is instance of UnresolvedClass", class2 instanceof UnresolvedClass); 87 interface2 = Utility.findClass("org.netbeans.test.interfaces.Interface2"); 88 assertNotNull("Interface2", interface2); 89 assertFalse("Interface2 is instance of UnresolvedClass", interface2 instanceof UnresolvedClass); 90 91 field1 = class1.getField("field1_1", false); 92 assertNotNull("class1.field1", field1); 93 field2 = class1.getField("field1_2", false); 94 assertNotNull("class1.field2", field1); 95 field3 = class1.getField("field1_3", false); 96 assertNotNull("class1.field1_3", field3); 97 98 List list = new ArrayList (); 99 Type intType = pkg.getType().resolve("int"); list.add(intType); 100 Type stringType = pkg.getType().resolve("String"); list.add(stringType); 101 cfeature1 = class2.getMethod("method2_1", list, false); 102 assertNotNull("class2.method2_1", cfeature1); 103 list = new ArrayList (); list.add(intType); 104 Type interface2Type = pkg.getType().resolve("org.netbeans.test.interfaces.Interface2"); list.add(interface2Type); 105 cfeature2 = class2.getMethod("method2_2", list, false); 106 assertNotNull("class2.method2_2", cfeature2); 107 108 117 checkNumber("field1 type", true, field1.getType()); 118 checkNumber("field2 type", true, field2.getType()); 119 checkNumber("field3 type", true, field3.getType()); 120 checkNumber("method2_1 type", true, cfeature1.getType()); 121 checkNumber("method2_2 type", true, cfeature2.getType()); 122 } 123 124 private static String getDesc(Object o) { 125 String name = null; 126 127 if (o instanceof Constructor) { 128 JavaClass cls = (JavaClass) (((ConstructorImpl)o).getDeclaringClass()); 129 if (cls != null) 130 name = cls.getName(); 131 132 } else if (o instanceof NamedElement) { 133 name = ((NamedElement)o).getName(); 134 } 135 136 if (name != null) 137 return name + " / " + o.getClass(); 138 else 139 return o.toString(); 140 } 141 142 protected void printAllLinks(RefAssociation refAssociation) { 143 Collection types = refAssociation.refAllLinks(); 144 int numOfLinks = 0; 145 for (Iterator it = types.iterator(); it.hasNext(); ) { 146 numOfLinks++; 147 RefAssociationLink link = (RefAssociationLink) it.next(); 148 Type type = (Type) link.refFirstEnd(); 149 TypedElement typedElement = (TypedElement) link.refSecondEnd(); 150 System.out.println(" #"+numOfLinks+": "+getDesc(typedElement)+" is of type "+type.getName()); 151 } 152 } 153 154 protected void checkNumber(String msg, boolean expectedValue, Type actualValue) { 155 assertEquals("Total number of "+msg, (expectedValue)?1:0, (actualValue!=null && !actualValue.getName().equals("java.lang.Object"))?1:0); 156 } 157 158 protected void checkNumber(String msg, int expectedValue, int actualValue) { 159 assertEquals("Total number of "+msg, expectedValue, actualValue); 160 } 161 162 protected void checkNumber(String msg, int expectedValue, Collection coll) { 163 if (expectedValue == 0) { 164 assertTrue("Total number of "+msg, coll.isEmpty()); 165 } 166 else { 167 checkNumber(msg, expectedValue, coll.size()); 168 } 169 } 170 171 public void testAllLinks() { 172 Utility.beginTrans(false); 173 try { 174 Collection types = isOfTypeImpl.refAllLinks(); 175 int numOfLinks = 0; 176 int numOfInterfaceTypes = 0; 177 178 String packageOfInterfaces = interface2.getResource().getPackageName(); 179 180 for (Iterator it = types.iterator(); it.hasNext(); ) { 181 numOfLinks++; 182 RefAssociationLink link = (RefAssociationLink) it.next(); 183 Type type = (Type) link.refFirstEnd(); 184 TypedElement typedElement = (TypedElement) link.refSecondEnd(); 185 if (typedElement.getType().getName().startsWith(packageOfInterfaces)) 186 ++numOfInterfaceTypes; 187 System.out.println(" #"+numOfLinks+": "+getDesc(typedElement)+" is of type "+type.getName()); 188 } 189 190 System.out.println(" numOfInterfaceTypes "+numOfInterfaceTypes); 191 checkNumber("elements with Class1 type", 9, numOfInterfaceTypes); 192 } 193 finally { 194 Utility.endTrans(); 195 } 196 } 197 198 public void testNullArguments() { 199 String msg = "Association's operation with null argument should throw exception"; 200 boolean fail = true; 201 Utility.beginTrans(true); 202 try { 203 try { 204 isOfTypeImpl.exists(class1, null); 205 fail(msg+" (exists)"); 206 } 207 catch (NullPointerException ex) { 208 } 209 try { 210 isOfTypeImpl.exists(null, field1); 211 fail(msg+" (exists)"); 212 } 213 catch (NullPointerException ex) { 214 } 215 try { 216 isOfTypeImpl.add(class1, null); 217 fail(msg+" (add)"); 218 } 219 catch (NullPointerException ex) { 220 } 221 try { 222 isOfTypeImpl.add(null, field1); 223 fail(msg+" (add)"); 224 } 225 catch (NullPointerException ex) { 226 } 227 try { 228 isOfTypeImpl.remove(class1, null); 229 fail(msg+" (remove)"); 230 } 231 catch (NullPointerException ex) { 232 } 233 try { 234 isOfTypeImpl.remove(null, field1); 235 fail(msg+" (remove)"); 236 } 237 catch (NullPointerException ex) { 238 } 239 try { 240 isOfTypeImpl.getTypedElements(class1).add(null); 241 fail(msg+" (getTypedElements.add)"); 242 } 243 catch (NullPointerException ex) { 244 } 245 try { 246 isOfTypeImpl.getTypedElements(class2).iterator().remove(); 247 fail(msg+" (getTypedElements.iterator.remove)"); 248 } 249 catch (IllegalStateException ex) { 250 } 251 catch (NullPointerException ex) { 252 } 254 255 fail = false; 256 } 257 finally { 258 Utility.endTrans(fail); 259 } 260 } 261 262 public void testImproperArguments() { 263 String msg = "Association's operation with improper argument should throw exception"; 264 265 Object obj = new Object (); 266 boolean fail = true; 267 Utility.beginTrans(true); 268 try { 269 try { 270 isOfTypeImpl.getTypedElements(class2).add(obj); 271 fail(msg+" (getTypedElements.add)"); 272 } 273 catch (javax.jmi.reflect.TypeMismatchException ex) { 274 } 275 276 fail = false; 277 } 278 finally { 279 Utility.endTrans(fail); 280 } 281 282 fail = true; 283 Utility.beginTrans(true); 284 try { 285 try { 286 isOfTypeImpl.add(class1, field3); 287 fail(msg+" (Adding duplicate association)"); 288 } 289 catch (javax.jmi.reflect.WrongSizeException ex) { 290 } 291 292 fail = false; 293 } 294 finally { 295 Utility.endTrans(fail); 296 } 297 298 fail = true; 299 Utility.beginTrans(true); 300 try { 301 boolean result = isOfTypeImpl.add(interface2, field3); 302 assertFalse("Adding existing association", result); 303 304 fail = false; 305 } 306 finally { 307 Utility.endTrans(fail); 308 } 309 310 fail = true; 311 Utility.beginTrans(true); 312 try { 313 boolean result = isOfTypeImpl.remove(class1, field3); 314 assertFalse("Removing nonexisting association", result); 315 316 fail = false; 317 } 318 finally { 319 Utility.endTrans(fail); 320 } 321 } 322 323 public void testAddRemoveAssociation() { 324 Type oldType; 325 326 boolean fail = true; 327 Utility.beginTrans(true); 328 try { 329 oldType = field1.getType(); 330 isOfTypeImpl.remove(oldType, field1); 331 isOfTypeImpl.add(class1, field1); 332 333 checkNumber("field1 type", true, field1.getType()); 334 checkNumber("elements with Class1 type", 1, isOfTypeImpl.getTypedElements(class1)); 335 336 fail = false; 337 } 338 finally { 339 Utility.endTrans(fail); 340 } 341 342 fail = true; 343 Utility.beginTrans(true); 344 try { 345 field1.setType(oldType); 346 isOfTypeImpl.remove(class1, field1); 347 isOfTypeImpl.add(oldType, field1); 348 349 checkNumber("field1 type", true, field1.getType()); 350 checkNumber("elements with Class1 type", 0, isOfTypeImpl.getTypedElements(class1)); 351 352 fail = false; 353 } 354 finally { 355 Utility.endTrans(fail); 356 } 357 } 358 359 public void testAddRemoveTypedElement() { 360 Type oldType; 361 362 boolean fail = true; 363 Utility.beginTrans(true); 364 try { 365 oldType = field1.getType(); 366 isOfTypeImpl.remove(oldType, field1); 367 Collection typedElements = isOfTypeImpl.getTypedElements(class1); 368 typedElements.add(field1); 369 370 checkNumber("field1 type", true, field1.getType()); 371 checkNumber("elements with Class1 type", 1, isOfTypeImpl.getTypedElements(class1)); 372 373 fail = false; 374 } 375 finally { 376 Utility.endTrans(fail); 377 } 378 379 fail = true; 380 Utility.beginTrans(true); 381 try { 382 Collection typedElements = isOfTypeImpl.getTypedElements(class1); 383 typedElements.remove(field1); 384 isOfTypeImpl.add(oldType, field1); 385 386 checkNumber("field1 type", true, field1.getType()); 387 checkNumber("elements with Class1 type", 0, isOfTypeImpl.getTypedElements(class1)); 388 389 fail = false; 390 } 391 finally { 392 Utility.endTrans(fail); 393 } 394 395 fail = true; 396 Utility.beginTrans(true); 397 try { 398 isOfTypeImpl.remove(oldType, field1); 399 Collection typedElements = isOfTypeImpl.getTypedElements(class1); 400 List list = new ArrayList (); 401 list.add(field1); 402 typedElements.addAll(list); 403 404 checkNumber("field1 type", true, field1.getType()); 405 checkNumber("elements with Class1 type", 1, isOfTypeImpl.getTypedElements(class1)); 406 407 fail = false; 408 } 409 finally { 410 Utility.endTrans(fail); 411 } 412 413 fail = true; 414 Utility.beginTrans(true); 415 try { 416 Collection typedElements = isOfTypeImpl.getTypedElements(class1); 417 List list = new ArrayList (); 418 list.add(field1); 419 typedElements.removeAll(list); 420 isOfTypeImpl.add(oldType, field1); 421 422 checkNumber("field1 type", true, field1.getType()); 423 checkNumber("elements with Class1 type", 0, isOfTypeImpl.getTypedElements(class1)); 424 425 fail = false; 426 } 427 finally { 428 Utility.endTrans(fail); 429 } 430 431 fail = true; 432 Utility.beginTrans(true); 433 try { 434 isOfTypeImpl.remove(oldType, field1); 435 Collection typedElements = isOfTypeImpl.getTypedElements(class1); 436 List list = new ArrayList (); 437 list.add(field1); 438 typedElements.addAll(list); 439 440 checkNumber("field1 type", true, field1.getType()); 441 checkNumber("elements with Class1 type", 1, isOfTypeImpl.getTypedElements(class1)); 442 443 fail = false; 444 } 445 finally { 446 Utility.endTrans(fail); 447 } 448 449 fail = true; 450 Utility.beginTrans(true); 451 try { 452 Collection typedElements = isOfTypeImpl.getTypedElements(class1); 453 Iterator it = typedElements.iterator(); 454 while (it.hasNext()) { 455 TypedElement typedElement = (TypedElement) it.next(); 456 if (typedElement.equals(field1)) { 457 it.remove(); 458 break; 459 } 460 } 461 isOfTypeImpl.add(oldType, field1); 462 463 checkNumber("field1 type", true, field1.getType()); 464 checkNumber("elements with Class1 type", 0, isOfTypeImpl.getTypedElements(class1)); 465 466 fail = false; 467 } 468 finally { 469 Utility.endTrans(fail); 470 } 471 } 472 473 public void testReferenceSet() { 474 Type oldType; 475 476 boolean fail = true; 477 Utility.beginTrans(true); 478 try { 479 oldType = field1.getType(); 480 field1.setType(class1); 481 482 checkNumber("field1 type", true, field1.getType()); 483 checkNumber("elements with Class1 type", 1, isOfTypeImpl.getTypedElements(class1)); 484 485 fail = false; 486 } 487 finally { 488 Utility.endTrans(fail); 489 } 490 491 fail = true; 492 Utility.beginTrans(true); 493 try { 494 field1.setType(oldType); 495 496 checkNumber("field1 type", true, field1.getType()); 497 checkNumber("elements with Class1 type", 0, isOfTypeImpl.getTypedElements(class1)); 498 499 fail = false; 500 } 501 finally { 502 Utility.endTrans(fail); 503 } 504 } 505 } 506 | Popular Tags |