1 17 18 package org.sape.carbon.core.config.test; 19 20 import junit.extensions.ActiveTestSuite; 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 25 import org.sape.carbon.core.config.Config; 26 import org.sape.carbon.core.config.Configuration; 27 import org.sape.carbon.core.config.ConfigurationException; 28 import org.sape.carbon.core.config.ConfigurationNotFoundException; 29 import org.sape.carbon.core.config.format.ConfigurationFormatException; 30 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService; 31 import org.sape.carbon.core.config.node.ConfigurationDocument; 32 import org.sape.carbon.core.config.node.Folder; 33 import org.sape.carbon.core.config.node.NestedConfigurationDocument; 34 import org.sape.carbon.core.config.node.Node; 35 import org.sape.carbon.core.config.node.NodeCreationException; 36 import org.sape.carbon.core.config.node.NodeException; 37 import org.sape.carbon.core.config.node.NodeNotFoundException; 38 import org.sape.carbon.core.config.node.NodeRemovedException; 39 import org.sape.carbon.core.config.node.link.GenericLinkNodeConfiguration; 40 import org.sape.carbon.core.config.node.link.GenericLinkNodeFactory; 41 import org.sape.carbon.core.exception.ExceptionUtility; 42 import org.sape.carbon.core.exception.InvalidParameterException; 43 44 51 public class InternalNodeTest extends TestCase { 52 protected Folder root; 53 54 public static final String FOLDER_NAME = "foo"; 55 public static final String DOCUMENT_NAME = "bar"; 56 private static final String NESTED_DOCUMENT_NAME = "splat"; 57 private static final String TEST_VALUE = "-|-|-|-"; 58 private static String TEST_CONFIG_NAME = "/core/config/test/DeadlockTest"; 59 private static String TEST_LINK_NAME = "DeadlockTestLink"; 60 private static String TEST_LINK_PARENT_NAME = "/core/config/test"; 61 62 private static Throwable threadFailureCause = null; 63 64 public InternalNodeTest(String name) { 65 super(name); 66 } 67 68 public void testAddSubFolder() { 69 try { 70 this.root.addSubFolder(FOLDER_NAME); 71 } catch (Exception e) { 72 TestCase.fail( 73 "Caught exception: " 74 + ExceptionUtility.printStackTracesToString(e)); 75 } 76 } 77 78 public void testAddConfigurationDocument() { 79 try { 80 Folder subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 81 82 TestConfiguration testConfig = (TestConfiguration) 83 Config.getInstance().createConfiguration(TestConfiguration.class); 84 85 subFolder.addConfigurationDocument(DOCUMENT_NAME, testConfig); 86 } catch (NodeException ne) { 87 TestCase.fail( 88 "Caught unexpected NodeException: " 89 + ExceptionUtility.printStackTracesToString(ne)); 90 } 91 } 92 93 public void testWriteConfiguration() { 94 try { 95 Folder subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 96 ConfigurationDocument doc = 97 (ConfigurationDocument) subFolder.fetchChild(DOCUMENT_NAME); 98 99 DefaultConfigurationFormatService formatService = 100 new DefaultConfigurationFormatService(); 101 102 Configuration config = 103 formatService.newConfiguration(TestConfiguration.class); 104 105 ((TestConfiguration) config).setTestValue(TEST_VALUE); 106 107 doc.writeConfiguration(config); 108 } catch (NodeException ne) { 109 TestCase.fail( 110 "Caught unexpected NodeException: " 111 + ExceptionUtility.printStackTracesToString(ne)); 112 } catch (ConfigurationFormatException cfe) { 113 TestCase.fail( 114 "Caught unexpected " 115 + "ConfigurationFormatException: " 116 + ExceptionUtility.printStackTracesToString(cfe)); 117 } 118 } 119 120 public void testReadConfiguration() { 121 try { 122 Folder subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 123 ConfigurationDocument doc = 124 (ConfigurationDocument) subFolder.fetchChild(DOCUMENT_NAME); 125 126 TestConfiguration config = 127 (TestConfiguration) doc.readConfiguration(); 128 129 if (!TEST_VALUE.equals(config.getTestValue())) { 130 TestCase.fail( 131 "Configuration value not as expected. " 132 + "Actual: [" 133 + config.getTestValue() 134 + "], " 135 + "Expected: [" 136 + TEST_VALUE 137 + "]"); 138 } 139 } catch (NodeException ne) { 140 TestCase.fail( 141 "Caught unexpected NodeException: " 142 + ExceptionUtility.printStackTracesToString(ne)); 143 } catch (ConfigurationFormatException cfe) { 144 TestCase.fail( 145 "Caught unexpected " 146 + "ConfigurationFormatException: " 147 + ExceptionUtility.printStackTracesToString(cfe)); 148 } 149 } 150 151 public void testNodeAttributes() { 152 try { 153 Folder subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 154 ConfigurationDocument doc = 155 (ConfigurationDocument) subFolder.fetchChild(DOCUMENT_NAME); 156 157 final String ABSOLUTE_NAME = getTestNodeAttributesAbsoluteName(); 158 159 if (!ABSOLUTE_NAME.equals(doc.getAbsoluteName())) { 160 TestCase.fail( 161 "Absolute name not as expected. " 162 + "Actual: [" 163 + doc.getAbsoluteName() 164 + "], " 165 + "Expected: [" 166 + ABSOLUTE_NAME 167 + "]"); 168 } 169 if (!DOCUMENT_NAME.equals(doc.getName())) { 170 TestCase.fail( 171 "Name not as expected. " 172 + "Actual: [" 173 + doc.getName() 174 + "], " 175 + "Expected: [" 176 + DOCUMENT_NAME 177 + "]"); 178 } 179 if (doc.getParent() != subFolder) { 180 TestCase.fail("Reference to parent not as expected."); 181 } 182 183 } catch (NodeException ne) { 184 TestCase.fail( 185 "Caught unexpected NodeException: " 186 + ExceptionUtility.printStackTracesToString(ne)); 187 } 188 } 189 190 protected String getTestNodeAttributesAbsoluteName() { 191 return "/" + FOLDER_NAME + Node.DELIMITER + DOCUMENT_NAME; 192 } 193 194 public void testFetchChildren() { 195 Folder subFolder = null; 196 try { 197 subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 198 subFolder.addSubFolder(FOLDER_NAME); 199 } catch (NodeException ne) { 200 TestCase.fail("Caught unexpected NodeException: " + ne); 201 } 202 203 Node[] children = subFolder.fetchChildren(); 204 205 if (children.length != 2) { 206 TestCase.fail( 207 "fetchChildren did not return expected number " 208 + "of children. Expected [2], found [" 209 + children.length 210 + "]"); 211 } 212 213 if (!((children[0].getName().equals(FOLDER_NAME) 214 && children[1].getName().equals(DOCUMENT_NAME)) 215 || (children[0].getName().equals(DOCUMENT_NAME) 216 && children[1].getName().equals(FOLDER_NAME)))) { 217 218 TestCase.fail("fetchChildren did not return expected children"); 219 } 220 } 221 222 public void testAddDuplicateConfigurationDocument() { 223 try { 224 Folder subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 225 TestConfiguration testConfig = (TestConfiguration) 226 Config.getInstance().createConfiguration(TestConfiguration.class); 227 subFolder.addConfigurationDocument(DOCUMENT_NAME, testConfig); 228 TestCase.fail("Did not catch expected NodeCreationException"); 229 } catch (NodeNotFoundException nnfe) { 230 TestCase.fail( 231 "Caught unexpected NodeNotFoundException: " 232 + ExceptionUtility.printStackTracesToString(nnfe)); 233 } catch (NodeCreationException nce) { 234 } 236 } 237 238 public void testAddDuplicateSubFolder() { 239 try { 240 this.root.addSubFolder(FOLDER_NAME); 241 TestCase.fail("Did not catch expected NodeCreationException"); 242 } catch (NodeCreationException nce) { 243 } 245 } 246 247 public void testNodeRemoval() { 248 try { 249 Folder subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 250 int removedNodes = subFolder.remove(); 251 252 assertTrue( 253 "Unexpected number of nodes removed, expected [4], actual [" + 254 removedNodes + "]", 255 removedNodes == 4); 256 257 try { 258 TestConfiguration testConfig = (TestConfiguration) 259 Config.getInstance().createConfiguration(TestConfiguration.class); 260 subFolder.addConfigurationDocument(DOCUMENT_NAME, testConfig); 261 TestCase.fail("Did not encounter NodeRemovedException"); 262 } catch (NodeRemovedException nre) { 263 } 265 266 try { 267 subFolder.fetchChild(DOCUMENT_NAME); 268 TestCase.fail("Did not encounter NodeRemovedException"); 269 } catch (NodeRemovedException nre) { 270 } 272 } catch (NodeException ne) { 273 TestCase.fail( 274 "Caught unexpected NodeException: " 275 + ExceptionUtility.printStackTracesToString(ne)); 276 } 277 } 278 279 public void testFetchRemovedNode() { 280 try { 281 Folder subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 282 283 TestCase.fail("Did not catch expected NodeNotFoundException"); 284 } catch (NodeNotFoundException e) { 285 } 287 } 288 289 public void testNullParameters() { 290 try { 291 try { 292 this.root.addConfigurationDocument(null, null); 293 TestCase.fail( 294 "Did not catch expected NullPointerException " 295 + "in addConfigurationDocument"); 296 } catch (InvalidParameterException ipe) { 297 } 299 try { 300 this.root.addSubFolder(null); 301 TestCase.fail( 302 "Did not catch expected NullPointerException " 303 + "in addSubFolder"); 304 } catch (InvalidParameterException ipe) { 305 } 307 try { 308 this.root.fetchChild(null); 309 TestCase.fail( 310 "Did not catch expected InvalidParameterException " 311 + "in fetchChild"); 312 } catch (InvalidParameterException ipe) { 313 } 315 } catch (NodeException ne) { 316 TestCase.fail( 317 "caught NodeException when NullPointerException " 318 + "expected: " 319 + ExceptionUtility.printStackTracesToString(ne)); 320 } 321 } 322 323 public void testLinkFolder() { 324 try { 325 Folder f1 = (Folder) this.root.fetchChild("core"); 326 Folder f2 = (Folder) f1.fetchChild("test"); 327 Folder f3 = (Folder) f2.fetchChild("testFolderLink"); 329 ConfigurationDocument doc = 330 (ConfigurationDocument) f3.fetchChild("TestComponent"); 331 } catch (NodeNotFoundException nnfe) { 332 TestCase.fail( 333 "Caught node not found exception: " 334 + ExceptionUtility.printStackTracesToString(nnfe)); 335 } 336 } 337 338 public void testLinkDoc() { 339 try { 340 Folder f1 = (Folder) this.root.fetchChild("core"); 341 Folder f2 = (Folder) f1.fetchChild("test"); 342 ConfigurationDocument doc = 344 (ConfigurationDocument) f2.fetchChild("testConfigDocLink"); 345 } catch (NodeNotFoundException nnfe) { 346 TestCase.fail( 347 "Caught node not found exception: " 348 + ExceptionUtility.printStackTracesToString(nnfe)); 349 } 350 } 351 352 public void testAddNestedConfiguration() throws ConfigurationException{ 353 Folder subFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 354 ConfigurationDocument doc = 355 (ConfigurationDocument) subFolder.fetchChild(DOCUMENT_NAME); 356 357 TestConfiguration config = 358 (TestConfiguration) Config.getInstance().createConfiguration( 359 TestConfiguration.class); 360 361 config.setTestValue(TEST_VALUE); 362 ConfigurationDocument nested = 363 doc.addNestedConfigurationDocument(NESTED_DOCUMENT_NAME, config); 364 365 if (!(nested instanceof NestedConfigurationDocument)) { 366 fail("Nested configuration doc was of an unexpected type, expected: " 367 + NestedConfigurationDocument.class.getName() 368 + ", actual: " 369 + nested.getClass().getName()); 370 } 371 372 subFolder.addConfigurationDocument(DOCUMENT_NAME + DOCUMENT_NAME, config); 373 config = (TestConfiguration) ((ConfigurationDocument) subFolder.fetchChild(DOCUMENT_NAME)).readConfiguration(); 374 375 ConfigurationDocument doubleNested = 377 nested.addNestedConfigurationDocument(NESTED_DOCUMENT_NAME, config); 378 379 if (doubleNested == null || 380 doubleNested instanceof NestedConfigurationDocument) { 381 382 fail("Nested configuration doc was null or of type " 383 + NestedConfigurationDocument.class.getName() 384 + " and should not have been"); 385 } 386 387 } 388 389 public void testNestedReferenceWrite() { 390 final String DOCUMENT_NAME_2 = DOCUMENT_NAME + "2"; 391 392 try { 393 Folder parentFolder = (Folder) this.root.fetchChild(FOLDER_NAME); 394 395 TestConfiguration refConfig = 396 (TestConfiguration) Config.getInstance().createConfiguration( 397 TestConfiguration.class); 398 refConfig.setTestValue("foobar"); 399 400 ConfigurationDocument refDoc = 401 parentFolder.addConfigurationDocument(DOCUMENT_NAME_2, refConfig); 402 403 refConfig = 404 (TestConfiguration) ((ConfigurationDocument) parentFolder 405 .fetchChild(DOCUMENT_NAME_2)) 406 .readConfiguration(); 407 408 refConfig.setTestValue("barfoo"); 409 ConfigurationDocument parentDoc = 410 (ConfigurationDocument) parentFolder.fetchChild(DOCUMENT_NAME); 411 TestConfiguration parentConfig = 412 (TestConfiguration) parentDoc.readConfiguration(); 413 parentConfig.setSubConfig(refConfig); 414 parentDoc.writeConfiguration(parentConfig); 415 416 refConfig = (TestConfiguration) refDoc.readConfiguration(); 417 418 assertTrue( 419 "Referenced child configuration was not written " 420 + "expected test value [barfoo], actual test value [" 421 + refConfig.getTestValue() 422 + "]", 423 refConfig.getTestValue().equals("barfoo")); 424 425 } catch (NodeException ne) { 426 TestCase.fail( 427 "Caught unexpected NodeException: " 428 + ExceptionUtility.printStackTracesToString(ne)); 429 } catch (ConfigurationFormatException cfe) { 430 TestCase.fail( 431 "Caught unexpected " 432 + "ConfigurationFormatException: " 433 + ExceptionUtility.printStackTracesToString(cfe)); 434 } 435 } 436 437 public void testReadNestedConfiguration() { 438 try { 439 ConfigurationDocument nestedDoc = 440 (ConfigurationDocument) this 441 .root 442 .fetchChild(FOLDER_NAME) 443 .fetchChild(DOCUMENT_NAME) 444 .fetchChild(NESTED_DOCUMENT_NAME); 445 446 TestConfiguration config = 447 (TestConfiguration) nestedDoc.readConfiguration(); 448 assertTrue( 449 "Unexpected config value, expected [" 450 + TEST_VALUE 451 + "], actual [" 452 + config.getTestValue() 453 + "]", 454 config.getTestValue().equals(TEST_VALUE)); 455 456 } catch (NodeException ne) { 457 TestCase.fail( 458 "Caught unexpected NodeException: " 459 + ExceptionUtility.printStackTracesToString(ne)); 460 } catch (ConfigurationFormatException cfe) { 461 TestCase.fail( 462 "Caught unexpected " 463 + "ConfigurationFormatException: " 464 + ExceptionUtility.printStackTracesToString(cfe)); 465 } 466 } 467 468 public void testRemoveNestedConfiguration() { 469 try { 470 ConfigurationDocument nestedDoc = 471 (ConfigurationDocument) this 472 .root 473 .fetchChild(FOLDER_NAME) 474 .fetchChild(DOCUMENT_NAME) 475 .fetchChild(NESTED_DOCUMENT_NAME); 476 477 nestedDoc.remove(); 478 } catch (NodeException ne) { 479 TestCase.fail( 480 "Caught unexpected NodeException: " 481 + ExceptionUtility.printStackTracesToString(ne)); 482 } 483 484 try { 485 ConfigurationDocument nestedDoc = 486 (ConfigurationDocument) this 487 .root 488 .fetchChild(FOLDER_NAME) 489 .fetchChild(DOCUMENT_NAME) 490 .fetchChild(NESTED_DOCUMENT_NAME); 491 492 fail("Nested node was not removed"); 493 } catch (NodeNotFoundException nfge) { 494 } 496 } 497 498 private static synchronized void setThreadFailureCause(Throwable t) { 499 if (threadFailureCause == null) { 500 threadFailureCause = t; 501 } 502 } 503 504 private static synchronized Throwable getThreadFailureCause() { 505 return threadFailureCause; 506 } 507 508 public void testDeadlock() throws Throwable { 509 Thread linkCreator = new Thread (new LinkCreator(), "LinkCreator"); 510 Thread nodeTraverser = new Thread (new NodeTraverser(), "NodeTraverser"); 511 512 linkCreator.start(); 513 nodeTraverser.start(); 514 515 nodeTraverser.join(20000); 516 517 if (getThreadFailureCause() != null) { 518 throw getThreadFailureCause(); 519 } 520 521 if (linkCreator.isAlive() || nodeTraverser.isAlive()) { 522 fail("Threads did not complete in alotted time, deadlock suspected"); 523 } 524 525 } 526 527 static class LinkCreator implements Runnable { 528 public void run() { 529 try { 530 Folder linkParent; 531 GenericLinkNodeConfiguration linkConfig; 532 533 linkParent = 534 (Folder) Config.getInstance().fetchNode(TEST_LINK_PARENT_NAME); 535 536 linkConfig = (GenericLinkNodeConfiguration) 537 Config.getInstance().createConfiguration(GenericLinkNodeConfiguration.class); 538 539 linkConfig.setLinkNodeFactoryClass(GenericLinkNodeFactory.class); 540 linkConfig.setTargetNodeName(TEST_CONFIG_NAME); 541 542 if (linkParent.containsChild(TEST_LINK_NAME)) { 543 linkParent.fetchChild(TEST_LINK_NAME).remove(); 544 } 545 546 for (int count = 1; count<=20; count++) { 547 linkParent.addLink(TEST_LINK_NAME, linkConfig); 548 linkParent.fetchChild(TEST_LINK_NAME).remove(); 549 } 550 } catch (Throwable t) { 551 setThreadFailureCause(t); 552 } 553 } 554 } 555 556 static class NodeTraverser implements Runnable { 557 public void run() { 558 for (int count = 1; count<=1000; count++) { 559 try { 560 TestConfiguration config = (TestConfiguration) 561 Config.getInstance().fetchConfiguration( 562 TEST_LINK_PARENT_NAME + Node.DELIMITER + TEST_LINK_NAME); 563 564 config.getTestValue(); 565 Thread.yield(); 566 } catch (ConfigurationNotFoundException cnfe) { 567 } catch (Throwable t) { 569 setThreadFailureCause(t); 570 } 571 } 572 573 } 574 } 575 579 public static Test suite() { 580 TestSuite masterSuite = new TestSuite(); 581 Test singleThreadedTests = getSingleThreadedTests(); 583 if (singleThreadedTests != null) { 584 masterSuite.addTest(singleThreadedTests); 585 } 586 Test multiThreadedTests = getMultiThreadedTests(); 588 if (multiThreadedTests != null) { 589 masterSuite.addTest(multiThreadedTests); 590 } 591 return masterSuite; 592 } 593 594 600 private static Test getSingleThreadedTests() { 601 TestSuite suite = new TestSuite(); 602 603 suite.addTest(new InternalNodeTest("testAddSubFolder")); 604 suite.addTest(new InternalNodeTest("testAddConfigurationDocument")); 605 suite.addTest(new InternalNodeTest("testWriteConfiguration")); 606 suite.addTest(new InternalNodeTest("testReadConfiguration")); 607 suite.addTest(new InternalNodeTest("testNodeAttributes")); 608 suite.addTest(new InternalNodeTest("testFetchChildren")); 609 suite.addTest( 610 new InternalNodeTest("testAddDuplicateConfigurationDocument")); 611 suite.addTest(new InternalNodeTest("testAddDuplicateSubFolder")); 612 suite.addTest(new InternalNodeTest("testAddNestedConfiguration")); 613 suite.addTest(new InternalNodeTest("testReadNestedConfiguration")); 614 suite.addTest(new InternalNodeTest("testRemoveNestedConfiguration")); 615 suite.addTest(new InternalNodeTest("testNodeRemoval")); 617 suite.addTest(new InternalNodeTest("testFetchRemovedNode")); 618 suite.addTest(new InternalNodeTest("testNullParameters")); 619 suite.addTest(new InternalNodeTest("testLinkFolder")); 620 suite.addTest(new InternalNodeTest("testLinkDoc")); 621 suite.addTest(new InternalNodeTest("testDeadlock")); 622 623 return suite; 624 } 625 626 631 private static Test getMultiThreadedTests() { 632 TestSuite suite = new ActiveTestSuite(); 633 634 640 641 return suite; 642 } 643 644 652 private static void addTest(TestSuite suite, String testName, int number) { 653 for (int count = 0; count < number; count++) { 654 suite.addTest(new InternalNodeTest(testName)); 655 } 656 } 657 660 protected void setUp() throws Exception { 661 this.root = 662 (Folder) Config.getInstance().fetchNode(Config.ROOT_NODE_NAME); 663 } 664 665 } 666 | Popular Tags |