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