1d7d8a54c77e02d9cdb247635827862e7a233ff7
[utils] / system / general / src / test / java / org / wamblee / system / container / ContainerTest.java
1 /*
2  * Copyright 2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.system.container;
17
18 import junit.framework.TestCase;
19
20 import org.wamblee.general.Pair;
21
22 import org.wamblee.system.core.Component;
23 import org.wamblee.system.core.DefaultProvidedInterface;
24 import org.wamblee.system.core.DefaultRequiredInterface;
25 import org.wamblee.system.core.DefaultScope;
26 import org.wamblee.system.core.Environment;
27 import org.wamblee.system.core.ProvidedInterface;
28 import org.wamblee.system.core.RequiredInterface;
29 import org.wamblee.system.core.Scope;
30 import org.wamblee.system.core.StringComponent;
31 import org.wamblee.system.core.SystemAssemblyException;
32
33 import org.wamblee.test.AssertionUtils;
34 import org.wamblee.test.EventTracker;
35
36 import java.io.Serializable;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41
42 /**
43  * DOCUMENT ME!
44  *
45  * @author $author$
46  * @version $Revision$
47   */
48 public class ContainerTest extends TestCase {
49     /**
50      * DOCUMENT ME!
51      */
52     private EventTracker<String> tracker;
53
54     /**
55      * DOCUMENT ME!
56      *
57      * @throws Exception DOCUMENT ME!
58      */
59     @Override
60     protected void setUp() throws Exception {
61         super.setUp();
62         tracker = new EventTracker<String>();
63     }
64
65     /**
66      * DOCUMENT ME!
67      *
68      * @param aProvided DOCUMENT ME!
69      * @param aProvider DOCUMENT ME!
70      *
71      * @return DOCUMENT ME!
72      */
73     private List<Pair<ProvidedInterface, Component>> createProvidedInput(
74         ProvidedInterface[] aProvided, Component aProvider) {
75         List<Pair<ProvidedInterface, Component>> result = new ArrayList<Pair<ProvidedInterface, Component>>();
76
77         for (ProvidedInterface provided : aProvided) {
78             result.add(new Pair<ProvidedInterface, Component>(provided,
79                     aProvider));
80         }
81
82         return result;
83     }
84
85     /**
86      * DOCUMENT ME!
87      */
88     public void testEnvironmentApplication() {
89         Environment environment = new Environment(tracker);
90         Application application = new Application(tracker);
91         Container   container   = new Container("root",
92                 new Component[] { environment, application },
93                 new ProvidedInterface[0], new RequiredInterface[0]);
94         Scope       scope       = container.start();
95         assertTrue(container.isSealed());
96         AssertionUtils.assertEquals(new String[] {
97                 "start.environment", "start.application"
98             }, tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
99         assertEquals(0, scope.getProvidedInterfaces().size());
100
101         assertEquals(environment.getString(), application.getString());
102         assertEquals(environment.getInteger(), application.getInteger());
103     }
104
105     /**
106      * DOCUMENT ME!
107      */
108     public void testEnvironmentApplicationSimpleConstructor() {
109         Environment environment = new Environment(tracker);
110         Application application = new Application(tracker);
111         Container   container   = new Container("root").addComponent(environment)
112             .addComponent(application);
113
114         Scope       scope       = container.start();
115         AssertionUtils.assertEquals(new String[] {
116                 "start.environment", "start.application"
117             }, tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
118         assertEquals(0, scope.getProvidedInterfaces().size());
119
120         assertEquals(environment.getString(), application.getString());
121         assertEquals(environment.getInteger(), application.getInteger());
122     }
123
124     /**
125      * DOCUMENT ME!
126      */
127     public void testApplicationEnvironment() {
128         try {
129             Component<?> environment = new Environment();
130             Component<?> application = new Application();
131             Container    container   = new Container("root",
132                     new Component[] { application, environment },
133                     new ProvidedInterface[0], new RequiredInterface[0]);
134             container.start();
135         } catch (SystemAssemblyException e) {
136             // e.printStackTrace();
137             return;
138         }
139
140         fail();
141     }
142
143     /**
144      * DOCUMENT ME!
145      */
146     public void testComposite() {
147         Component<?> environment = new Environment(tracker);
148         Component<?> application = new Application(tracker);
149         assertEquals(0, tracker.getEventCount());
150
151         Container               system   = new Container("all",
152                 new Component[] { environment, application },
153                 new ProvidedInterface[0], new RequiredInterface[0]);
154         Scope                   runtime  = system.start();
155         List<RequiredInterface> required = system.getRequiredInterfaces();
156         assertEquals(0, required.size());
157
158         List<ProvidedInterface> provided = system.getProvidedInterfaces();
159         assertEquals(0, provided.size());
160
161         AssertionUtils.assertEquals(new String[] {
162                 "start.environment", "start.application"
163             }, tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
164         tracker.clear();
165
166         system.stop(runtime);
167         AssertionUtils.assertEquals(new String[] {
168                 "stop.application", "stop.environment"
169             }, tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
170     }
171
172     /**
173      * DOCUMENT ME!
174      */
175     public void testCompositeWithWrongProvidedInfo() {
176         try {
177             Component<?> environment = new Environment();
178             Component<?> application = new Application();
179             Container    system      = new Container("all",
180                     new Component[] { environment, application },
181                     new ProvidedInterface[] {
182                         new DefaultProvidedInterface("float", Float.class)
183                     }, new DefaultRequiredInterface[0]);
184             system.validate();
185         } catch (SystemAssemblyException e) {
186             return;
187         }
188
189         fail();
190     }
191
192     /**
193      * DOCUMENT ME!
194      */
195     public void testCompositeRequiredInterfaceNotProvided() {
196         try {
197             Component<?> environment = new Environment();
198             Component<?> application = new Application();
199             Container    system      = new Container("all",
200                     new Component[] { environment, application },
201                     new ProvidedInterface[0],
202                     new RequiredInterface[] {
203                         new DefaultRequiredInterface("string", String.class)
204                     });
205             system.start();
206         } catch (SystemAssemblyException e) {
207             return;
208         }
209
210         fail();
211     }
212
213     /**
214      * DOCUMENT ME!
215      */
216     public void testCompositeWithSuperfluousRequiredInfo() {
217         Component<?> environment = new Environment();
218         Component<?> application = new Application();
219         Container    system      = new Container("all",
220                 new Component[] { environment, application },
221                 new ProvidedInterface[0],
222                 new RequiredInterface[] {
223                     new DefaultRequiredInterface("float", Float.class)
224                 });
225         system.getRequiredInterfaces().get(0)
226         .setProvider(new DefaultProvidedInterface("hallo", Float.class));
227         system.start();
228
229         List<RequiredInterface> required = system.getRequiredInterfaces();
230         assertEquals(1, required.size());
231
232         List<ProvidedInterface> provided = system.getProvidedInterfaces();
233         assertEquals(0, provided.size());
234     }
235
236     /**
237      * DOCUMENT ME!
238      */
239     public void testCompositeWithExternalDependencesNotProvided() {
240         try {
241             Component<?> application = new Application();
242
243             Container    system      = new Container("all",
244                     new Component[] { application }, new ProvidedInterface[0],
245                     application.getRequiredInterfaces()
246                     .toArray(new RequiredInterface[0]));
247             system.start();
248         } catch (SystemAssemblyException e) {
249             return;
250         }
251
252         fail();
253     }
254
255     /**
256      * DOCUMENT ME!
257      */
258     public void testDuplicateComponent() {
259         try {
260             Component<?> comp1  = new Application();
261             Component<?> comp2  = new Application();
262             Container    system = new Container("top");
263             system.addComponent(comp1).addComponent(comp2);
264         } catch (SystemAssemblyException e) {
265             return;
266         }
267
268         fail();
269     }
270
271     /**
272      * DOCUMENT ME!
273      */
274     public void testInconsistentHierarchy() {
275         try {
276             Component<?> comp    = new Application();
277             Container    system  = new Container("top").addComponent(comp);
278             Container    system2 = new Container("top2").addComponent(comp);
279         } catch (SystemAssemblyException e) {
280             return;
281         }
282
283         fail();
284     }
285
286     /**
287      * DOCUMENT ME!
288      */
289     public void testCompositeWithExternalDependencesProvided() {
290         Component<?> environment = new Environment();
291         Component<?> application = new Application();
292         Container    system      = new Container("all",
293                 new Component[] { application }, new ProvidedInterface[0],
294                 application.getRequiredInterfaces()
295                 .toArray(new RequiredInterface[0]));
296         environment.start(new DefaultScope(new ProvidedInterface[0]));
297         system.getRequiredInterfaces().get(0)
298         .setProvider(environment.getProvidedInterfaces().get(0));
299         system.getRequiredInterfaces().get(1)
300         .setProvider(environment.getProvidedInterfaces().get(1));
301
302         system.start();
303
304         List<RequiredInterface> required = system.getRequiredInterfaces();
305         assertEquals(2, required.size());
306
307         List<ProvidedInterface> provided = system.getProvidedInterfaces();
308         assertEquals(0, provided.size());
309     }
310
311     /**
312      * DOCUMENT ME!
313      */
314     public void testAmbiguousInterfaces() {
315         try {
316             Component<?> environment1 = new Environment();
317             Component<?> environment2 = new Environment();
318             Component<?> application  = new Application();
319             Container    container    = new Container("root",
320                     new Component[] { environment1, environment2, application },
321                     new ProvidedInterface[0], new RequiredInterface[0]);
322             container.start();
323         } catch (SystemAssemblyException e) {
324             return;
325         }
326
327         fail();
328     }
329
330     /**
331      * DOCUMENT ME!
332      */
333     public void testIncompleteRequirements() {
334         try {
335             Component<?> application = new Application();
336             Container    system      = new Container("all",
337                     new Component[] { application }, new ProvidedInterface[0],
338                     new RequiredInterface[0]);
339             system.start();
340         } catch (SystemAssemblyException e) {
341             return;
342         }
343
344         fail();
345     }
346
347     /**
348      * DOCUMENT ME!
349      *
350      * @throws Exception DOCUMENT ME!
351      * @throws RuntimeException DOCUMENT ME!
352      */
353     public void testEnvironmentApplicationRollbackOnException()
354         throws Exception {
355         Environment environment = new Environment(tracker);
356         Application application = new Application() {
357                 @Override
358                 public Object doStart(Scope aScope) {
359                     throw new RuntimeException();
360                 }
361             };
362
363         try {
364             Container container = new Container("root",
365                     new Component[] { environment, application },
366                     new ProvidedInterface[0], new RequiredInterface[0]);
367
368             container.start();
369         } catch (RuntimeException e) {
370             AssertionUtils.assertEquals(new String[] {
371                     "start.environment", "stop.environment"
372                 },
373                 tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
374
375             return;
376         }
377
378         fail();
379     }
380
381     /**
382      * DOCUMENT ME!
383      *
384      * @throws Exception DOCUMENT ME!
385      * @throws RuntimeException DOCUMENT ME!
386      */
387     public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
388         throws Exception {
389         Environment environment = new Environment(tracker);
390
391         // Application 1 will throw an exception while stopping.
392         Application application1 = new Application("app1") {
393                 @Override
394                 public void doStop(Object aRuntime) {
395                     throw new RuntimeException();
396                 }
397             };
398
399         // application 2 will throw an exception while starting
400         Application application2 = new Application("app2") {
401                 public Object doStart(Scope aScope) {
402                     throw new RuntimeException();
403                 }
404             };
405
406         try {
407             Container container = new Container("root",
408                     new Component[] { environment, application1, application2 },
409                     new ProvidedInterface[0], new RequiredInterface[0]);
410
411             container.start();
412         } catch (RuntimeException e) {
413             AssertionUtils.assertEquals(new String[] {
414                     "start.environment", "stop.environment"
415                 },
416                 tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
417
418             return;
419         }
420
421         fail();
422     }
423
424     /**
425      * DOCUMENT ME!
426      */
427     public void testOptionalRequiredInterfaceProvidedOptionalInternal() {
428         Application application = new Application(true);
429         Container   container   = new Container("top",
430                 new Component[] { application }, new ProvidedInterface[0],
431                 Application.required(true));
432         Environment env         = new Environment();
433         container.getRequiredInterfaces().get(0)
434         .setProvider(env.getProvidedInterfaces().get(0));
435         container.getRequiredInterfaces().get(1)
436         .setProvider(env.getProvidedInterfaces().get(1));
437
438         Scope external = new DefaultScope(env.getProvidedInterfaces());
439         env.start(external);
440
441         container.start(external);
442         assertSame(env.getProvidedInterfaces().get(0),
443             container.getRequiredInterfaces().get(0).getProvider());
444         assertSame(env.getProvidedInterfaces().get(1),
445             container.getRequiredInterfaces().get(1).getProvider());
446         assertSame(env.getProvidedInterfaces().get(0),
447             application.getRequiredInterfaces().get(0).getProvider());
448         assertSame(env.getProvidedInterfaces().get(1),
449             application.getRequiredInterfaces().get(1).getProvider());
450     }
451
452     /**
453      * DOCUMENT ME!
454      */
455     public void testOptionalRequiredInterfaceNotProvidedOptionalInternal() {
456         Application application = new Application(true);
457         Container   container   = new Container("top",
458                 new Component[] { application }, new ProvidedInterface[0],
459                 Application.required(true));
460         Environment env         = new Environment();
461         container.getRequiredInterfaces().get(0)
462         .setProvider(env.getProvidedInterfaces().get(0));
463
464         Scope external = new DefaultScope(new ProvidedInterface[0]);
465         external.publishInterface(env.getProvidedInterfaces().get(0),
466             env.getString());
467         container.start(external);
468         assertSame(env.getProvidedInterfaces().get(0),
469             container.getRequiredInterfaces().get(0).getProvider());
470         assertNull(container.getRequiredInterfaces().get(1).getProvider());
471         assertSame(env.getProvidedInterfaces().get(0),
472             application.getRequiredInterfaces().get(0).getProvider());
473         assertNull(application.getRequiredInterfaces().get(1).getProvider());
474     }
475
476     /**
477      * DOCUMENT ME!
478      */
479     public void testOptionalRequiredInterfaceProvidedMandatoryInternal() {
480         Application application = new Application();
481         Container   container   = new Container("top",
482                 new Component[] { application }, new ProvidedInterface[0],
483                 Application.required(true));
484         Environment env         = new Environment();
485         container.getRequiredInterfaces().get(0)
486         .setProvider(env.getProvidedInterfaces().get(0));
487         container.getRequiredInterfaces().get(1)
488         .setProvider(env.getProvidedInterfaces().get(1));
489
490         try {
491             container.start();
492         } catch (SystemAssemblyException e) {
493             return;
494         }
495
496         fail();
497     }
498
499     /**
500      * DOCUMENT ME!
501      */
502     public void testSealed() {
503         final Container container = new Container("xx");
504         assertFalse(container.isSealed());
505         container.start();
506         assertTrue(container.isSealed());
507
508         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
509                 @Override
510                 public void run() throws Exception {
511                     container.addComponent(new Application());
512                 }
513             }, SystemAssemblyException.class);
514
515         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
516                 @Override
517                 public void run() throws Exception {
518                     container.connectRequiredProvided("x", "y", "a", "b");
519                 }
520             }, SystemAssemblyException.class);
521         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
522                 @Override
523                 public void run() throws Exception {
524                     container.connectExternalRequired("x", "y", "a");
525                 }
526             }, SystemAssemblyException.class);
527         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
528                 @Override
529                 public void run() throws Exception {
530                     container.connectExternalProvided("x", "y", "z");
531                 }
532             }, SystemAssemblyException.class);
533         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
534                 @Override
535                 public void run() throws Exception {
536                     container.addProvidedInterface(new DefaultProvidedInterface(
537                             "xx", String.class));
538                 }
539             }, SystemAssemblyException.class);
540
541         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
542                 @Override
543                 public void run() throws Exception {
544                     container.addRequiredInterface(new DefaultRequiredInterface(
545                             "xx", String.class));
546                 }
547             }, SystemAssemblyException.class);
548     }
549
550     /**
551      * DOCUMENT ME!
552      */
553     public void testRestriction() {
554         Environment env1      = new Environment("env1");
555         Environment env2      = new Environment("env2");
556         Application app       = new Application("app");
557         Container   container = new Container("top").addComponent(env1)
558             .addComponent(env2).addComponent(app);
559         container.connectRequiredProvided("app", null, "env1", null);
560         container.start();
561         assertEquals(env1.getString(), app.getString());
562         assertEquals(env1.getInteger(), app.getInteger());
563         assertFalse(env2.getString().equals(app.getString()));
564         assertFalse(env2.getInteger().equals(app.getInteger()));
565     }
566
567     /**
568      * DOCUMENT ME!
569      */
570     public void testRestrictionWithFromAndToInterfaceName() {
571         Environment env1      = new Environment("env1");
572         Environment env2      = new Environment("env2");
573         Application app       = new Application("app");
574         Container   container = new Container("top").addComponent(env1)
575             .addComponent(env2).addComponent(app);
576         container.connectRequiredProvided("app",
577             app.getRequiredInterfaces().get(0).getName(), "env1",
578             env1.getProvidedInterfaces().get(0).getName());
579         container.connectRequiredProvided("app",
580             app.getRequiredInterfaces().get(1).getName(), "env2",
581             env2.getProvidedInterfaces().get(1).getName());
582         container.start();
583         assertEquals(env1.getString(), app.getString());
584         assertEquals(env2.getInteger(), app.getInteger());
585         assertFalse(env2.getString().equals(app.getString()));
586         assertFalse(env1.getInteger().equals(app.getInteger()));
587     }
588
589     /**
590      * DOCUMENT ME!
591      */
592     public void testRestrictionWrongComponentNames() {
593         Environment     env1      = new Environment("env1");
594         Environment     env2      = new Environment("env2");
595         Application     app       = new Application("app");
596         final Container container = new Container("top").addComponent(env1)
597             .addComponent(env2).addComponent(app);
598         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
599                 @Override
600                 public void run() throws Exception {
601                     container.connectRequiredProvided("app2", null, "env1", null);
602                 }
603             }, SystemAssemblyException.class);
604         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
605                 @Override
606                 public void run() throws Exception {
607                     container.connectRequiredProvided("app", null, "env3", null);
608                 }
609             }, SystemAssemblyException.class);
610     }
611
612     /**
613      * DOCUMENT ME!
614      */
615     public void testRestrictionWrongInterfaceNames() {
616         final Environment env1      = new Environment("env1");
617         Environment       env2      = new Environment("env2");
618         final Application app       = new Application("app");
619         final Container   container = new Container("top").addComponent(env1)
620             .addComponent(env2).addComponent(app);
621         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
622                 @Override
623                 public void run() throws Exception {
624                     container.connectRequiredProvided("app",
625                         app.getRequiredInterfaces().get(0).getName() + "xxx",
626                         "env1", null);
627                 }
628             }, SystemAssemblyException.class);
629         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
630                 @Override
631                 public void run() throws Exception {
632                     container.connectRequiredProvided("app", null, "env1",
633                         env1.getProvidedInterfaces().get(0).getName() + "yyy");
634                 }
635             }, SystemAssemblyException.class);
636     }
637
638     /**
639      * DOCUMENT ME!
640      */
641     public void testProvidedInDifferentScopes() {
642         // Scoping problem occurred. Externally and internally provided
643         // components clashed
644         // because unique id generation in the scope was wrong.
645         StringComponent str       = new StringComponent("string");
646         Application     app       = new Application("app");
647         Container       container = new Container("top").addComponent(str)
648             .addComponent(app);
649         container.addRequiredInterface(new DefaultRequiredInterface("integer",
650                 Integer.class));
651
652         ProvidedInterface provided = new DefaultProvidedInterface("hallo",
653                 Integer.class);
654         container.getRequiredInterfaces().get(0).setProvider(provided);
655
656         Scope external = new DefaultScope(new ProvidedInterface[0]);
657         external.publishInterface(provided, 100);
658
659         Scope scope = container.start(external);
660     }
661
662     /**
663      * DOCUMENT ME!
664      */
665     public void testProvidedInterfaces() {
666         Environment env          = new Environment(tracker);
667         Container   envcontainer = new Container("0").addComponent(env)
668             .addProvidedInterface(new DefaultProvidedInterface("string",
669                     String.class))
670             .addProvidedInterface(new DefaultProvidedInterface("integer",
671                     Integer.class));
672         Scope       scope        = envcontainer.start();
673
674         AssertionUtils.assertEquals(new String[] { "start.environment" },
675             tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
676
677         envcontainer.stop(scope);
678     }
679
680     /**
681      * DOCUMENT ME!
682      */
683     public void testCoupleTwoContainers() {
684         Environment env          = new Environment(tracker);
685         Container   envcontainer = new Container("0").addComponent(env)
686             .addProvidedInterface(new DefaultProvidedInterface("string",
687                     String.class))
688             .addProvidedInterface(new DefaultProvidedInterface("integer",
689                     Integer.class));
690
691         Application app          = new Application(tracker);
692         Container   appcontainer = new Container("1").addComponent(app)
693             .addRequiredInterface(new DefaultRequiredInterface("string",
694                     String.class))
695             .addRequiredInterface(new DefaultRequiredInterface("integer",
696                     Integer.class));
697
698         Container   top          = new Container("top");
699         top.addComponent(envcontainer).addComponent(appcontainer);
700
701         top.start();
702         AssertionUtils.assertEquals(new String[] {
703                 "start.environment", "start.application"
704             }, tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
705     }
706
707     /**
708      * DOCUMENT ME!
709      */
710     public void testNonUniqueRequiredInterface() {
711         final Container container = new Container("top");
712         container.addRequiredInterface(new DefaultRequiredInterface("i",
713                 Integer.class));
714         container.addRequiredInterface(new DefaultRequiredInterface("x",
715                 String.class));
716         container.addRequiredInterface(new DefaultRequiredInterface("y",
717                 String.class));
718
719         Application app = new Application("1");
720         container.addComponent(app);
721
722         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
723                 @Override
724                 public void run() throws Exception {
725                     container.start();
726                 }
727             }, SystemAssemblyException.class);
728
729         container.connectExternalRequired("1",
730             app.getRequiredInterfaces().get(0).getName(), "y");
731
732         ProvidedInterface i             = new DefaultProvidedInterface("i",
733                 Integer.class);
734         ProvidedInterface x             = new DefaultProvidedInterface("x",
735                 String.class);
736         ProvidedInterface y             = new DefaultProvidedInterface("y",
737                 String.class);
738
739         Scope             externalScope = new DefaultScope(new ProvidedInterface[0]);
740
741         externalScope.publishInterface(i, 100);
742         externalScope.publishInterface(x, "x-value");
743         externalScope.publishInterface(y, "y-value");
744
745         container.getRequiredInterfaces().get(0).setProvider(i);
746         container.getRequiredInterfaces().get(1).setProvider(x);
747         container.getRequiredInterfaces().get(2).setProvider(y);
748
749         Scope runtime = container.start(externalScope);
750
751         assertEquals("y-value", app.getString());
752     }
753
754     /**
755      * DOCUMENT ME!
756      */
757     public void testNonUniqueRequiredInterfaceWrongNames() {
758         final Container container = new Container("top");
759         container.addRequiredInterface(new DefaultRequiredInterface("i",
760                 Integer.class));
761         container.addRequiredInterface(new DefaultRequiredInterface("x",
762                 String.class));
763         container.addRequiredInterface(new DefaultRequiredInterface("y",
764                 String.class));
765
766         final Application app = new Application("1");
767         container.addComponent(app);
768
769         // wrong component name.
770         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
771                 @Override
772                 public void run() throws Exception {
773                     container.connectExternalRequired("2", "x", "y");
774                 }
775             }, SystemAssemblyException.class);
776
777         // Wrong interface name of component.
778         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
779                 @Override
780                 public void run() throws Exception {
781                     container.connectExternalRequired("1",
782                         app.getRequiredInterfaces().get(0).getName() + "xxx",
783                         "y");
784                 }
785             }, SystemAssemblyException.class);
786
787         // Wrong external interface name of container
788         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
789                 @Override
790                 public void run() throws Exception {
791                     container.connectExternalRequired("1",
792                         app.getRequiredInterfaces().get(0).getName(), "z");
793                 }
794             }, SystemAssemblyException.class);
795     }
796
797     /**
798      * DOCUMENT ME!
799      */
800     public void testNonUniqueProvidedInterface() {
801         final Container container = new Container("top").addProvidedInterface(new DefaultProvidedInterface(
802                     "external", String.class));
803         Environment     env1      = new Environment("env1");
804         Environment     env2      = new Environment("env2");
805
806         container.addComponent(env1);
807         container.addComponent(env2);
808
809         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
810                 @Override
811                 public void run() throws Exception {
812                     container.start();
813                 }
814             }, SystemAssemblyException.class);
815
816         // now choose env2
817         container.connectExternalProvided(container.getProvidedInterfaces()
818             .get(0).getName(), env2.getName(),
819             env2.getProvidedInterfaces().get(0).getName());
820
821         Scope scope = container.start();
822
823         // check the value of the provided interface of the container
824         String value = scope.getInterfaceImplementation(container.getProvidedInterfaces()
825                 .get(0), String.class);
826         assertNotNull(value);
827         assertEquals(value, env2.getString());
828         assertFalse(value.equals(env1.getString()));
829     }
830
831     /**
832      * DOCUMENT ME!
833      */
834     public void testNonUniqueProvidedInterfaceWrongNames() {
835         final Container   container = new Container("top").addProvidedInterface(new DefaultProvidedInterface(
836                     "external", String.class));
837         final Environment env1      = new Environment("env1");
838         final Environment env2      = new Environment("env2");
839
840         container.addComponent(env1);
841         container.addComponent(env2);
842
843         // Wrong external provided interface name
844         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
845                 @Override
846                 public void run() throws Exception {
847                     container.connectExternalProvided(container.getProvidedInterfaces()
848                         .get(0).getName() + "xx", "env1",
849                         env1.getProvidedInterfaces().get(0).getName());
850                 }
851             }, SystemAssemblyException.class);
852
853         // Wrong provided interface name.
854         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
855                 @Override
856                 public void run() throws Exception {
857                     container.connectExternalProvided(container.getProvidedInterfaces()
858                         .get(0).getName(), "env1",
859                         env1.getProvidedInterfaces().get(0).getName() + "xx");
860                 }
861             }, SystemAssemblyException.class);
862
863         // Wrong provided component
864         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
865                 @Override
866                 public void run() throws Exception {
867                     container.connectExternalProvided(container.getProvidedInterfaces()
868                         .get(0).getName(), "env3",
869                         env1.getProvidedInterfaces().get(0).getName());
870                 }
871             }, SystemAssemblyException.class);
872     }
873
874     private static class MyMultiple implements Serializable, Runnable {
875         @Override
876         public void run() {
877             // Empty
878         }
879     }
880 }