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