1 package test.crispy.server;
2
3 import net.sf.crispy.InvocationException;
4 import net.sf.crispy.server.MultiServiceContainerImpl;
5 import net.sf.crispy.server.ServiceCreator;
6 import net.sf.crispy.server.ServiceCreatorDefaultImpl;
7 import test.crispy.example.service.Calculator;
8 import test.crispy.example.service.CalculatorImpl;
9 import test.crispy.example.service.Echo;
10 import test.crispy.example.service.EchoImpl;
11 import junit.framework.TestCase;
12
13 public class MultiServiceContainerTest extends TestCase {
14
15 private String key ="123Crispy_Key321";
16 private String key_2 ="123Crispy_Key321_2";
17
18 public void testServiceSize() throws Exception {
19 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
20 assertEquals(0, lvContainer.getKeySize());
21 }
22
23 public void testRegisterService() throws Exception {
24 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
25 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
26 assertEquals(1, lvContainer.getRegisterServiceSize());
27 }
28
29 public void testRegisterService2() throws Exception {
30 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
31 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
32 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
33 lvContainer.getService(key, Echo.class.getName());
34 assertEquals(1, lvContainer.getKeySize());
35 }
36
37 public void testRemoveService() throws Exception {
38 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
39 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
40 lvContainer.removeRegisterServiceByServiceInterfaceName(Echo.class.getName());
41 assertEquals(0, lvContainer.getRegisterServiceSize());
42 }
43
44 public void testRemoveService2() throws Exception {
45 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
46 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
47 lvContainer.registerService(Calculator.class.getName(), CalculatorImpl.class.getName());
48 lvContainer.removeRegisterServiceByServiceInterfaceName(Echo.class.getName());
49 assertEquals(1, lvContainer.getRegisterServiceSize());
50
51 lvContainer.removeRegisterServiceByServiceInterfaceName(Calculator.class.getName());
52 assertEquals(0, lvContainer.getRegisterServiceSize());
53 }
54
55 public void testRemoveService3() throws Exception {
56 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
57 assertEquals(0, lvContainer.getKeySize());
58
59 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
60 assertEquals(1, lvContainer.getRegisterServiceSize());
61
62 Object lvEchoServiceImpl = lvContainer.getService(key, Echo.class.getName());
63 assertNotNull(lvEchoServiceImpl);
64 assertTrue(lvEchoServiceImpl instanceof EchoImpl);
65
66 lvContainer.removeService(key, Echo.class.getName());
67 assertEquals(0, lvContainer.getKeySize());
68 }
69
70 public void testRemoveService4() throws Exception {
71 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
72 assertEquals(0, lvContainer.getKeySize());
73
74 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
75 lvContainer.removeService(key, Calculator.class.getName());
76
77 assertEquals(0, lvContainer.getKeySize());
78 }
79
80 public void testRemoveService5() throws Exception {
81 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
82 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
83 lvContainer.removeRegisterServiceByServiceInterfaceName(Echo.class.getName());
84 assertEquals(0, lvContainer.getRegisterServiceSize());
85 }
86
87 public void testRemoveService6() throws Exception {
88 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
89 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
90 lvContainer.registerService(Calculator.class.getName(), CalculatorImpl.class.getName());
91 Object lvEchoServiceImpl = lvContainer.getService(key, Echo.class.getName());
92 Object lvCalculatorServiceImpl = lvContainer.getService(key, Calculator.class.getName());
93 assertNotNull(lvEchoServiceImpl);
94 assertNotNull(lvCalculatorServiceImpl);
95 assertEquals(1, lvContainer.getKeySize());
96 assertEquals(2, lvContainer.getServiceSize(key));
97
98 lvEchoServiceImpl = lvContainer.getService(key_2, Echo.class.getName());
99 assertEquals(2, lvContainer.getKeySize());
100 assertEquals(1, lvContainer.getServiceSize(key_2));
101
102 lvContainer.removeService(key, Echo.class.getName());
103 assertEquals(2, lvContainer.getKeySize());
104 }
105
106
107 public void testRemoveAllService() throws Exception {
108 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
109 assertEquals(0, lvContainer.getKeySize());
110
111 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
112 lvContainer.registerService(Calculator.class.getName(), CalculatorImpl.class.getName());
113
114 lvContainer.getService(key, Echo.class.getName());
115 lvContainer.getService(key, Calculator.class.getName());
116
117 assertEquals(1, lvContainer.getKeySize());
118
119 lvContainer.removeAllServices(key);
120 assertEquals(0, lvContainer.getKeySize());
121 }
122
123
124 public void testGetService() throws Exception {
125 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
126 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
127 assertNotNull(lvContainer.getService(key, Echo.class.getName()));
128
129 try {
130 lvContainer.getService(key, Calculator.class.getName());
131 fail("Calculator is not register, must thrown a Exception.");
132 } catch (InvocationException e) {
133 assertTrue(true);
134 }
135 }
136
137 public void testGetServiceNotFound() throws Exception {
138 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
139 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
140 lvContainer.getService(key, Echo.class.getName());
141 assertEquals(1, lvContainer.getKeySize());
142 assertNotNull(lvContainer.getService(key + "_notValidKey", Echo.class.getName()));
143 }
144
145 public void testGetServiceSizeByKey() throws Exception {
146 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
147 assertEquals(0, lvContainer.getServiceSize("aa"));
148
149 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
150 lvContainer.getService(key, Echo.class.getName());
151
152 assertEquals(1, lvContainer.getServiceSize(key));
153
154 lvContainer.getService(key_2, Echo.class.getName());
155 assertEquals(1, lvContainer.getServiceSize(key_2));
156
157 lvContainer.registerService(Calculator.class.getName(), CalculatorImpl.class.getName());
158 lvContainer.getService(key, Calculator.class.getName());
159
160 assertEquals(2, lvContainer.getServiceSize(key));
161 assertEquals(1, lvContainer.getServiceSize(key_2));
162 }
163
164 public void testServiceCreator() throws Exception {
165 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
166 ServiceCreator lvServiceCreator = new ServiceCreatorDefaultImpl();
167 lvContainer.setServiceCreator(lvServiceCreator);
168 assertNotNull(lvServiceCreator);
169 assertEquals(lvServiceCreator, lvContainer.getServiceCreator());
170 assertSame(lvServiceCreator, lvContainer.getServiceCreator());
171 }
172
173 public void testGetServiceWithDifferentObjects() throws Exception {
174 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
175 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
176
177 Object lvEcho1 = lvContainer.getService(key, Echo.class.getName());
178 Object lvEcho2 = lvContainer.getService(key_2, Echo.class.getName());
179
180 assertNotNull(lvEcho1);
181 assertNotNull(lvEcho2);
182 assertFalse(lvEcho1.equals(lvEcho2));
183 }
184
185 public void testGetServiceWithDifferentObjects2() throws Exception {
186 MultiServiceContainerImpl lvContainer = new MultiServiceContainerImpl();
187 lvContainer.registerService(Echo.class.getName(), EchoImpl.class.getName());
188
189 Object lvEcho1 = lvContainer.getService(key, Echo.class.getName());
190 Object lvEcho2 = lvContainer.getService(key, Echo.class.getName());
191
192 assertNotNull(lvEcho1);
193 assertNotNull(lvEcho2);
194 assertEquals(lvEcho1, lvEcho2);
195 assertSame(lvEcho1, lvEcho2);
196 }
197
198 }