1 18 package org.objectweb.kilim.repository; 19 20 import java.util.Iterator ; 21 import java.util.LinkedHashMap ; 22 23 import javax.xml.parsers.ParserConfigurationException ; 24 import junit.framework.TestCase; 25 26 import org.objectweb.kilim.InternalException; 27 import org.objectweb.kilim.KilimException; 28 import org.objectweb.kilim.KilimConfiguration; 29 30 import org.objectweb.kilim.description.TemplateDescription; 31 import org.objectweb.kilim.model.Component; 32 import org.objectweb.kilim.model.ComponentFactory; 33 import org.objectweb.kilim.model.ComponentInterface; 34 import org.objectweb.kilim.model.RtExternalValue; 35 36 import org.objectweb.kilim.model.instanciation.DefaultInstanciationStrategy; 37 import org.objectweb.kilim.model.instanciation.InstanciationStrategy; 38 39 import org.objectweb.kilim.model.mapping.DefaultMappingContext; 40 import org.objectweb.kilim.model.mapping.DefaultMappingStrategy; 41 import org.objectweb.kilim.model.mapping.JavaRuntimeMapper; 42 import org.objectweb.kilim.model.mapping.Mapper; 43 import org.objectweb.kilim.model.mapping.MappingContext; 44 import org.objectweb.kilim.model.mapping.MappingStrategy; 45 import org.objectweb.kilim.model.mapping.NullMapper; 46 import org.objectweb.kilim.model.mapping.TextAreaMapper; 47 import org.objectweb.kilim.model.mapping.TreeModelMapper; 48 49 import org.objectweb.kilim.model.services.Annotations; 50 import org.objectweb.kilim.model.services.ExternalValueReferences; 51 52 import org.objectweb.kilim.tools.KilimComponentViewer; 53 import org.objectweb.kilim.tools.KilimTemplateViewer; 54 import org.objectweb.kilim.tools.KilimTraceTreeModel; 55 import org.objectweb.kilim.tools.KilimTraceTreeViewer; 56 57 import org.xml.sax.SAXException ; 58 59 67 public class MonoFeatureTests extends TestCase { 68 69 private ResourceRepository rep; 70 private ResourceRepository rep1; 71 72 78 public MonoFeatureTests(String arg0) throws SAXException , ParserConfigurationException { 79 super(arg0); 80 TemplateDescriptionParser parser = new TemplateDescriptionParser(true); 81 rep = new ResourceRepository(parser); 82 rep.setResourceLoader(new ClassLoaderResourceLoader(this.getClass().getClassLoader())); 83 rep1 = new ResourceRepository1(parser); 84 rep1.setResourceLoader(new ClassLoaderResourceLoader(this.getClass().getClassLoader())); 85 } 86 87 93 public void testPropertiesArrays() throws ResourceNotFoundException, SAXException , ParserConfigurationException { 94 try { 95 TemplateDescription ts0 = rep1.getTemplateDescription("writers/ArrayDummyExample"); 96 Component compo = ComponentFactory.newComponent(ts0); 98 ComponentInterface interf = compo.getInterface("array"); 99 interf.getValue(); 100 interf = compo.getInterface("array2"); 101 interf.getValue(); 102 } catch (Exception ex) { 103 ex.printStackTrace(); 104 throw new InternalException(ex); 105 } 106 } 107 108 114 public void testPropertiesOverloading() throws ResourceNotFoundException, SAXException , ParserConfigurationException { 115 try { 116 TemplateDescription ts0 = rep1.getTemplateDescription("test/two"); 117 Component compo = ComponentFactory.newComponent(ts0); 118 119 ComponentInterface interf = compo.getInterface("string port"); 120 Object res = interf.getValue(); 121 assertEquals(res.toString() , "Ceci est un test réussi"); 122 123 interf = compo.getInterface("integer port"); 124 res = interf.getValue(); 125 126 assertEquals(((Integer ) res).intValue() , 42); 127 128 interf = compo.getInterface("long port"); 129 res = interf.getValue(); 130 131 assertEquals(((Long ) res).longValue() , 42); 132 } catch (Exception ex) { 133 ex.printStackTrace(); 134 throw new InternalException(ex); 135 } 136 } 137 138 144 public void testTriggersOrder() throws ResourceNotFoundException, SAXException , ParserConfigurationException { 145 try { 146 TemplateDescription ts0 = rep1.getTemplateDescription("test/B states"); 147 Component compo = ComponentFactory.newComponent(ts0); 148 ComponentInterface interf = compo.getInterface("initial"); 149 StringBuffer rslt = (StringBuffer ) interf.getValue(); 150 assertEquals("123", rslt.toString()); 151 } catch (Exception ex) { 152 ex.printStackTrace(); 153 throw new InternalException(ex); 154 } 155 } 156 157 163 public void testPropertiesInterfaceAndProvidersBindings() throws ResourceNotFoundException, SAXException , ParserConfigurationException { 164 try { 165 TemplateDescription ts0 = rep1.getTemplateDescription("test/Plugging Plug into Container"); 166 Component compo = ComponentFactory.newComponent(ts0); 167 ComponentInterface interf = compo.getInterface("result"); 168 StringBuffer rslt = (StringBuffer ) interf.getValue(); 169 assertEquals(rslt.toString() , "42-is-the answer"); 170 } catch (Exception ex) { 171 ex.printStackTrace(); 172 throw new InternalException(ex); 173 } 174 } 175 176 182 public void testSuperAndSuperSuper() throws ResourceNotFoundException, SAXException , ParserConfigurationException { 183 try { 184 TemplateDescription ts0 = rep1.getTemplateDescription("test/PLentyOfInstances A"); 185 TemplateDescription ts1 = rep1.getTemplateDescription("test/PLenty of Instances B"); 186 Component compo = ComponentFactory.newComponent(ts0); 187 ComponentInterface interf = compo.getInterface("a of level 1"); 188 String rslt = (String ) interf.getValue(); 189 assertEquals(rslt , "Valeur de a modifiée"); 190 191 compo = ComponentFactory.newComponent(ts1); 192 interf = compo.getInterface("a of level 2"); 193 rslt = (String ) interf.getValue(); 194 assertEquals(rslt , "Valeur de a modifiée"); 195 } catch (Exception ex) { 196 ex.printStackTrace(); 197 throw new InternalException(ex); 198 } 199 } 200 201 208 public void testExternalReference() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 209 try { 210 TemplateDescription ts0 = rep1.getTemplateDescription("test/Container for Externals"); 211 ExternalValueReferences xRefs = new ExternalValueReferences(); 213 xRefs.addExternalValueReference("/extern/b", new String ("-is-")); 214 xRefs.addExternalValueReference("/extern/c", new String ("" + 42)); 215 xRefs.addExternalValueReference("/extern/a", new String ("the answer")); 216 KilimConfiguration.setExternalReferences(xRefs); 220 Component compo = ComponentFactory.newComponent(ts0); 221 ComponentInterface interf = compo.getInterface("result"); 222 StringBuffer rslt_bff = (StringBuffer ) interf.getValue(); 223 224 String rslt = rslt_bff.toString(); 225 assertEquals(rslt, "the answer-is-42"); 226 } catch (Exception ex) { 227 ex.printStackTrace(); 228 throw new InternalException(ex); 229 } 230 } 231 232 238 public void testMultipleBindingsDetection() throws ResourceNotFoundException, SAXException , ParserConfigurationException { 239 boolean exception_thrown = false; 240 try { 241 TemplateDescription ts0 = rep1.getTemplateDescription("test/Multiple Bindings Subtemplate"); 242 Component compo = ComponentFactory.newComponent(ts0); 243 } catch (KilimException e) { 244 e.printStackTrace(); 245 exception_thrown = true; 246 } finally { 247 assertEquals (exception_thrown , false); 248 } 249 } 250 251 258 public void testMultipleTypesOfOverloading() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 259 TemplateDescription ts0 = rep1.getTemplateDescription("test/Overloading Things"); 260 Component compo = ComponentFactory.newComponent(ts0); 261 assertEquals(compo.getInterface("a").getValue() , "a modified"); 262 assertEquals(compo.getInterface("b").getValue() , "b modified"); 263 } 264 265 272 public void testOrderOnCompositionOfMultiplePort() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 273 try { 274 TemplateDescription ts0 = rep1.getTemplateDescription("test/Plugging Plug into Container with NAry Slot"); 275 Component compo = ComponentFactory.newComponent(ts0); 276 ComponentInterface interf = compo.getInterface("result"); 277 StringBuffer rslt = (StringBuffer ) interf.getValue(); 278 assertEquals(rslt.toString() , "4242-is--is-the answerthe answer"); 279 } catch (Exception ex) { 282 ex.printStackTrace(); 283 throw new InternalException(ex); 284 } 285 } 286 287 294 public void testDynamicPlugOnNAryPort() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 295 try { 296 TemplateDescription ts0 = rep1.getTemplateDescription("test/Container with NAry Slot"); 297 Component compo = ComponentFactory.newComponent(ts0); 298 TemplateDescription ts1 = rep1.getTemplateDescription("test/Plug"); 299 Component compo1 = ComponentFactory.newComponent(ts1); 300 Component compo2 = ComponentFactory.newComponent(ts1); 301 compo.plug("slot", compo1); 302 compo.plug("slot", compo2); 303 ComponentInterface interf = compo.getInterface("result"); 304 StringBuffer rslt = (StringBuffer ) interf.getValue(); 305 assertEquals(rslt.toString() , "4242-is--is-the answerthe answer"); 306 } catch (Exception ex) { 309 ex.printStackTrace(); 310 throw new InternalException(ex); 311 } 312 } 313 314 321 public void testDynamicPlug() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 322 try { 323 TemplateDescription ts0 = rep1.getTemplateDescription("test/Container"); 324 Component compo = ComponentFactory.newComponent(ts0); 325 TemplateDescription ts1 = rep1.getTemplateDescription("test/Plug"); 326 Component compo1 = ComponentFactory.newComponent(ts1); 327 compo.plug("slot", compo1); 328 ComponentInterface interf = compo.getInterface("result"); 329 StringBuffer rslt = (StringBuffer ) interf.getValue(); 330 assertEquals(rslt.toString() , "42-is-the answer"); 331 } catch (Exception ex) { 332 ex.printStackTrace(); 333 throw new InternalException(ex); 334 } 335 } 336 337 344 public void testSimpleFork1() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 345 try { 346 TemplateDescription ts0 = rep1.getTemplateDescription("test/Container"); 347 Component compo = ComponentFactory.newComponent(ts0); 348 TemplateDescription ts1 = rep1.getTemplateDescription("test/Plug"); 349 Component compo1 = ComponentFactory.newComponent(ts1); 350 Component compo2 = compo1.fork(); 351 compo.plug("slot", compo2); 352 ComponentInterface interf = compo.getInterface("result"); 353 StringBuffer rslt = (StringBuffer ) interf.getValue(); 354 assertEquals(rslt.toString() , "42-is-the answer"); 355 } catch (Exception ex) { 356 ex.printStackTrace(); 357 throw new InternalException(ex); 358 } 359 } 360 361 368 public void testSimpleFork1A() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 369 try { 370 TemplateDescription ts0 = rep1.getTemplateDescription("test/Container with Plug"); 371 Component compo = ComponentFactory.newComponent(ts0); 372 ComponentInterface interf = compo.getInterface("result"); 373 StringBuffer rslt = (StringBuffer ) interf.getValue(); 374 assertEquals(rslt.toString() , "42-is-the answer"); 375 Component compo1 = compo.getSubComponent("Plug"); 376 Component compo2 = compo1.fork(); 377 interf.update(); 378 rslt = (StringBuffer ) interf.getValue(); 379 assertEquals(rslt.toString() , "42-is-the answer42-is-the answer"); 380 } catch (Exception ex) { 381 ex.printStackTrace(); 382 throw new InternalException(ex); 383 } 384 } 385 386 393 public void testSimpleFork2() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 394 try { 395 TemplateDescription ts0 = rep1.getTemplateDescription("test/Container with NAry Slot"); 396 Component compo = ComponentFactory.newComponent(ts0); 397 TemplateDescription ts1 = rep1.getTemplateDescription("test/Plug"); 398 Component compo1 = ComponentFactory.newComponent(ts1); 399 Component compo2 = compo1.fork(); 400 Component compo3 = compo2.fork(); 401 compo.plug("slot", compo1); 402 compo.plug("slot", compo2); 403 compo.plug("slot", compo3); 404 ComponentInterface interf = compo.getInterface("result"); 405 StringBuffer rslt = (StringBuffer ) interf.getValue(); 406 assertEquals(rslt.toString() , "424242-is--is--is-the answerthe answerthe answer"); 407 } catch (Exception ex) { 410 ex.printStackTrace(); 411 throw new InternalException(ex); 412 } 413 } 414 415 422 public void testForkOfPlugged1() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 423 try { 424 TemplateDescription ts0 = rep1.getTemplateDescription("test/Container with NAry Slot"); 425 Component compo = ComponentFactory.newComponent(ts0); 426 TemplateDescription ts1 = rep1.getTemplateDescription("test/Plug"); 427 Component compo1 = ComponentFactory.newComponent(ts1); 428 compo.plug("slot", compo1); 429 Component compo2 = compo1.fork(); 430 compo.plug("slot", compo2); 431 Component compo3 = compo2.fork(); 432 compo.plug("slot", compo3); 433 ComponentInterface interf = compo.getInterface("result"); 434 StringBuffer rslt = (StringBuffer ) interf.getValue(); 435 assertEquals(rslt.toString() , "424242-is--is--is-the answerthe answerthe answer"); 436 } catch (Exception ex) { 439 ex.printStackTrace(); 440 throw new InternalException(ex); 441 } 442 } 443 444 451 public void testForkOfPlugged2() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 452 try { 453 TemplateDescription ts0 = rep1.getTemplateDescription("test/Plugging Plug into Container with NAry Slot"); 454 Component compo = ComponentFactory.newComponent(ts0); 455 Component compo1 = compo.getSubComponent("Container"); 456 Component compo2 = compo.getSubComponent("Plug"); 457 Component compo3 = compo2.fork(); 458 ComponentInterface interf = compo.getInterface("result"); 459 StringBuffer rslt = (StringBuffer ) interf.getValue(); 460 assertEquals(rslt.toString() , "424242-is--is--is-the answerthe answerthe answer"); 461 } catch (Exception ex) { 464 ex.printStackTrace(); 465 throw new InternalException(ex); 466 } 467 } 468 469 476 public void testForkOfPlugged3() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 477 try { 478 TemplateDescription ts0 = rep1.getTemplateDescription("test/Plugging Plug into Container with NAry Slot"); 479 Component compo = ComponentFactory.newComponent(ts0); 480 Component compo1 = compo.getSubComponent("Container"); 481 Component compo2 = compo.getSubComponent("Plug"); 482 Component compo3 = compo2.fork(); 483 Component compo4 = compo3.fork(); 484 ComponentInterface interf = compo.getInterface("result"); 485 StringBuffer rslt = (StringBuffer ) interf.getValue(); 486 assertEquals(rslt.toString() , "42424242-is--is--is--is-the answerthe answerthe answerthe answer"); 487 } catch (Exception ex) { 490 ex.printStackTrace(); 491 throw new InternalException(ex); 492 } 493 } 494 495 502 public void testForkOfPlugged4() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 503 try { 504 TemplateDescription ts0 = rep1.getTemplateDescription("test/Plugging Plug into Container with NAry Slot"); 505 Component compo = ComponentFactory.newComponent(ts0); 506 ComponentInterface interf = compo.getInterface("result"); 507 StringBuffer rslt = (StringBuffer ) interf.getValue(); 508 Component compo1 = compo.getSubComponent("Container"); 509 Component compo2 = compo.getSubComponent("Plug"); 510 Component compo3 = compo2.fork(); 511 Component compo4 = compo3.fork(); 512 assertEquals(rslt.toString() , "4242-is--is-the answerthe answer42-is-the answer42-is-the answer"); 513 } catch (Exception ex) { 516 ex.printStackTrace(); 517 throw new InternalException(ex); 518 } 519 } 520 521 528 public void testForkOfPlugged5() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 529 try { 530 TemplateDescription ts0 = rep1.getTemplateDescription("test/Plugging Plug into Container with NAry Slot"); 531 Component compo = ComponentFactory.newComponent(ts0); 532 ComponentInterface interf = compo.getInterface("result"); 533 Component compo1 = compo.getSubComponent("Container"); 534 Component compo2 = compo.getSubComponent("Plug"); 535 Component compo3 = compo2.fork(); 536 StringBuffer rslt = (StringBuffer ) interf.getValue(); 537 Component compo4 = compo3.fork(); 538 assertEquals(rslt.toString() , "424242-is--is--is-the answerthe answerthe answer42-is-the answer"); 539 } catch (Exception ex) { 542 ex.printStackTrace(); 543 throw new InternalException(ex); 544 } 545 } 546 547 553 public void testCycleAndTriggers() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 554 TemplateDescription ts0 = rep1.getTemplateDescription("test/Cycle via Trigger"); 555 Component compo = ComponentFactory.newComponent(ts0); 556 assertEquals(compo.getInterface("Un").getValue() , "Toto est un héros"); 557 } 558 559 566 public void testTriggersOnTHIS() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 567 TemplateDescription ts0 = rep1.getTemplateDescription("test/Triggers on THIS"); 568 Component compo = ComponentFactory.newComponent(ts0); 569 System.out.print("L'affichage correct doit être 12 : "); 570 assertEquals(compo.getInterface("bidon").getValue(), null); 571 } 572 573 579 public void testContainmentAndStart() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 580 TemplateDescription ts0 = rep1.getTemplateDescription("test/Containment Test"); 581 Component compo = ComponentFactory.newComponent(ts0); 582 assertEquals("ContentContent" , compo.getInterface("text").getValue().toString()); 583 } 584 585 591 public void testBindingValue1() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 592 TemplateDescription ts0 = rep1.getTemplateDescription("test/BindingValue Test"); 593 Component compo1 = ComponentFactory.newComponent(ts0); 594 Component compo2 = compo1.fork(); 595 ComponentInterface interf1a = compo1.getInterface("toBeBound"); 596 interf1a.bindValue("Toto est un héros"); 597 ComponentInterface interf1b = compo1.getInterface("result"); 598 assertEquals(interf1b.getValue() , "Toto est un héros"); 599 ComponentInterface interf2a = compo2.getInterface("toBeBound"); 600 interf2a.bindValue("Titi est un héros"); 601 ComponentInterface interf2b = compo2.getInterface("result"); 602 assertEquals(interf2b.getValue() , "Titi est un héros"); 603 Component compo3 = compo2.fork(); 604 ComponentInterface interf3a = compo3.getInterface("toBeBound"); 605 interf3a.bindValue("Tutu est un héros"); 606 ComponentInterface interf3b = compo3.getInterface("result"); 607 assertEquals(interf3b.getValue() , "Tutu est un héros"); 608 } 609 610 617 public void testBindingProvider1() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 618 TemplateDescription ts0 = rep1.getTemplateDescription("test/BindingValue Test"); 619 Mapper mapper = new JavaRuntimeMapper(); 620 MappingContext mContext = new DefaultMappingContext(); 621 622 Component compo1 = ComponentFactory.newComponent(ts0); 623 Component compo2 = compo1.fork(); 624 ComponentInterface interf1a = compo1.getInterface("toBeBound"); 625 interf1a.bindProvider(new RtExternalValue("toto", "Toto est un héros"), false); 626 ComponentInterface interf1b = compo1.getInterface("result"); 627 assertEquals(interf1b.getValue() , "Toto est un héros"); 628 ComponentInterface interf2a = compo2.getInterface("toBeBound"); 629 interf2a.bindProvider(new RtExternalValue("toto", "Titi est un héros"), false); 630 ComponentInterface interf2b = compo2.getInterface("result"); 631 assertEquals(interf2b.getValue() , "Titi est un héros"); 632 Component compo3 = compo2.fork(); 633 ComponentInterface interf3a = compo3.getInterface("toBeBound"); 634 interf3a.bindProvider(new RtExternalValue("tutu", "Tutu est un héros"), false); 635 ComponentInterface interf3b = compo3.getInterface("result"); 636 assertEquals(interf3b.getValue() , "Tutu est un héros"); 637 } 638 639 646 public void testNullMapper() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 647 TemplateDescription ts0 = rep.getTemplateDescription("test/Plugging Plug into Container with NAry Slot"); 648 try { 650 MappingStrategy strgy = new DefaultMappingStrategy(new NullMapper(null)); 652 KilimConfiguration.setMappingStrategy(strgy); 653 Component compo = ComponentFactory.newComponent(ts0); 654 Iterator iter = compo.getInterfaces(); 655 while (iter.hasNext()) { 656 ComponentInterface interf = ((ComponentInterface) iter.next()); 657 System.out.println("interface : " + interf.getQualifiedName()); 658 interf.getValue(); 659 } 660 } catch (Exception ex) { 661 ex.printStackTrace(); 662 throw new InternalException(ex); 663 } 664 } 665 666 673 public void testOtherMapper1() throws ResourceNotFoundException, SAXException , ParserConfigurationException , KilimException { 674 try { 675 TemplateDescription ts0 = rep1.getTemplateDescription("test/Plugging Plug into Container with NAry Slot"); 676 TextAreaMapper textMapper = new TextAreaMapper(new JavaRuntimeMapper()); 677 TreeModelMapper treeMapper = new TreeModelMapper(textMapper); 678 KilimTraceTreeModel treeModel = treeMapper.getTraceTree(); 679 MappingStrategy strgy = new DefaultMappingStrategy(treeMapper); 680 KilimConfiguration.setMappingStrategy(strgy); 681 Component compo = ComponentFactory.newComponent(ts0); 682 Component compo1 = compo.getSubComponent("Container"); 683 Component compo2 = compo.getSubComponent("Plug"); 684 Component compo3 = compo2.fork(); 685 Component compo4 = compo3.fork(); 686 ComponentInterface interf = compo.getInterface("result"); 687 StringBuffer rslt = (StringBuffer ) interf.getValue(); 688 assertEquals(rslt.toString() , "42424242-is--is--is--is-the answerthe answerthe answerthe answer"); 690 } catch (Exception ex) { 693 ex.printStackTrace(); 694 throw new InternalException(ex); 695 } 696 } 697 698 704 705 719 } 720 | Popular Tags |