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