60d1b587007387dc710ef1e56cdfe0b32b8f04b7
[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();
17                 SubSystem application = new Application();
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
28         public void testApplicationEnvironment() {
29                 try {
30                         SubSystem environment = new Environment();
31                         SubSystem application = new Application();
32                         SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
33                                         application, environment }, new ServiceDescriptor[0]);
34                         assembler.start(_registry, new Service[0]);
35                 } catch (SystemAssemblyException e) {
36                         // e.printStackTrace();
37                         return;
38                 }
39                 fail();
40         }
41
42         public void testComposite() {
43                 SubSystem environment = new Environment();
44                 SubSystem application = new Application();
45                 CompositeSystem system = new CompositeSystem("all", new SubSystem[] {
46                                 environment, application }, new ServiceDescriptor[0],
47                                 new ServiceDescriptor[0]);
48                 system.start("root", _registry, new Service[0]);
49                 ServiceDescriptor[] required = system.getRequiredServices();
50                 assertEquals(0, required.length);
51                 ServiceDescriptor[] provided = system.getProvidedServices();
52                 assertEquals(0, provided.length);
53         }
54
55         public void testCompositeWithWrongProvidedInfo() {
56                 try {
57                         SubSystem environment = new Environment();
58                         SubSystem application = new Application();
59                         CompositeSystem system = new CompositeSystem("all",
60                                         new SubSystem[] { environment, application },
61                                         new ServiceDescriptor[] { new DefaultServiceDescriptor(
62                                                         String.class) }, new ServiceDescriptor[0]);
63                 } catch (SystemAssemblyException e) {
64                         return;
65                 }
66                 fail();
67         }
68
69         public void testCompositeWithSuperfluousRequiredInfo() {
70                 SubSystem environment = new Environment();
71                 SubSystem application = new Application();
72                 CompositeSystem system = new CompositeSystem("all", new SubSystem[] {
73                                 environment, application }, new ServiceDescriptor[0],
74                                 new ServiceDescriptor[] { new DefaultServiceDescriptor(
75                                                 String.class) });
76                 system.start("root", _registry, new Service[0]);
77                 ServiceDescriptor[] required = system.getRequiredServices();
78                 assertEquals(1, required.length);
79                 ServiceDescriptor[] provided = system.getProvidedServices();
80                 assertEquals(0, provided.length);
81         }
82
83         public void testCompositeWithExternalDependencesNotProvided() {
84                 try {
85                         SubSystem environment = new Environment();
86                         SubSystem application = new Application();
87                         CompositeSystem system = new CompositeSystem("all",
88                                         new SubSystem[] { application }, new ServiceDescriptor[0],
89                                         application.getRequiredServices());
90                         system.start("root", _registry, new Service[0]);
91                 } catch (SystemAssemblyException e) {
92                         return;
93                 }
94                 fail();
95
96         }
97
98         public void testCompositeWithExternalDependencesProvided() {
99
100                 SubSystem environment = new Environment();
101                 SubSystem application = new Application();
102                 CompositeSystem system = new CompositeSystem("all",
103                                 new SubSystem[] { application }, new ServiceDescriptor[0],
104                                 application.getRequiredServices());
105                 Service[] envServices = environment.start("env", _registry,new Service[0]);
106                 system.start("root", _registry, envServices);
107                 ServiceDescriptor[] required = system.getRequiredServices();
108                 assertEquals(2, required.length);
109                 ServiceDescriptor[] provided = system.getProvidedServices();
110                 assertEquals(0, provided.length);
111
112         }
113
114 }