added spring subsystem implementation.
[utils] / system / general / src / test / java / org / wamblee / system / SystemAssemblerTest.java
1 package org.wamblee.system;
2
3 import junit.framework.TestCase;
4
5 public class SystemAssemblerTest extends TestCase {
6         
7         private ServiceRegistry _registry; 
8         
9         @Override
10         protected void setUp() throws Exception {
11                 super.setUp();
12                 _registry = new DefaultServiceRegistry(); 
13         }
14
15         public void testEnvironmentApplication() {
16                 SubSystem environment = new Environment(_registry);
17                 SubSystem application = new Application(_registry);
18                 SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
19                                 environment, application }, new ServiceDescriptor[0]);
20                 assembler.start(_registry, new Service[0]);
21                 Service[] envServices = environment.getRunningServices();
22                 assertEquals(2, envServices.length);
23                 Service[] appServices = environment.getRunningServices();
24                 assertEquals(2, appServices.length);
25                 assertEquals(2, _registry.listAllServices().length);
26                 
27                 environment.stop();
28                 assertEquals(0, _registry.listAllServices().length);
29                 
30                 application.stop(); 
31                 assertEquals(0, _registry.listAllServices().length);
32         }
33
34         public void testApplicationEnvironment() {
35                 try {
36                         SubSystem environment = new Environment(_registry);
37                         SubSystem application = new Application(_registry);
38                         SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
39                                         application, environment }, new ServiceDescriptor[0]);
40                         assembler.start(_registry, new Service[0]);
41                 } catch (SystemAssemblyException e) {
42                         // e.printStackTrace();
43                         return;
44                 }
45                 fail();
46         }
47
48         public void testComposite() {
49                 SubSystem environment = new Environment(_registry);
50                 SubSystem application = new Application(_registry);
51                 CompositeSystem system = new CompositeSystem("all", _registry, new SubSystem[] {
52                                 environment, application }, new ServiceDescriptor[0],
53                                 new ServiceDescriptor[0]);
54                 system.start("root", new Service[0]);
55                 ServiceDescriptor[] required = system.getRequiredServices();
56                 assertEquals(0, required.length);
57                 ServiceDescriptor[] provided = system.getProvidedServices();
58                 assertEquals(0, provided.length);
59         }
60
61         public void testCompositeWithWrongProvidedInfo() {
62                 try {
63                         SubSystem environment = new Environment(_registry);
64                         SubSystem application = new Application(_registry);
65                         CompositeSystem system = new CompositeSystem("all", _registry,
66                                         new SubSystem[] { environment, application },
67                                         new ServiceDescriptor[] { new DefaultServiceDescriptor(
68                                                         String.class) }, new ServiceDescriptor[0]);
69                 } catch (SystemAssemblyException e) {
70                         return;
71                 }
72                 fail();
73         }
74
75         public void testCompositeWithSuperfluousRequiredInfo() {
76                 SubSystem environment = new Environment(_registry);
77                 SubSystem application = new Application(_registry);
78                 CompositeSystem system = new CompositeSystem("all", _registry,new SubSystem[] {
79                                 environment, application }, new ServiceDescriptor[0],
80                                 new ServiceDescriptor[] { new DefaultServiceDescriptor(
81                                                 String.class) });
82                 system.start("root", new Service[0]);
83                 ServiceDescriptor[] required = system.getRequiredServices();
84                 assertEquals(1, required.length);
85                 ServiceDescriptor[] provided = system.getProvidedServices();
86                 assertEquals(0, provided.length);
87         }
88
89         public void testCompositeWithExternalDependencesNotProvided() {
90                 try {
91                         SubSystem environment = new Environment(_registry);
92                         SubSystem application = new Application(_registry);
93                         CompositeSystem system = new CompositeSystem("all", _registry,
94                                         new SubSystem[] { application }, new ServiceDescriptor[0],
95                                         application.getRequiredServices());
96                         system.start("root", new Service[0]);
97                 } catch (SystemAssemblyException e) {
98                         return;
99                 }
100                 fail();
101
102         }
103
104         public void testCompositeWithExternalDependencesProvided() {
105
106                 SubSystem environment = new Environment(_registry);
107                 SubSystem application = new Application(_registry);
108                 CompositeSystem system = new CompositeSystem("all", _registry,
109                                 new SubSystem[] { application }, new ServiceDescriptor[0],
110                                 application.getRequiredServices());
111                 Service[] envServices = environment.start("env", new Service[0]);
112                 system.start("root", envServices);
113                 ServiceDescriptor[] required = system.getRequiredServices();
114                 assertEquals(2, required.length);
115                 ServiceDescriptor[] provided = system.getProvidedServices();
116                 assertEquals(0, provided.length);
117
118         }
119
120 }