KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > installation > DeploymentTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.installation;
18
19 import java.io.File JavaDoc;
20
21 import javax.jbi.component.Bootstrap;
22 import javax.jbi.component.Component;
23 import javax.jbi.component.ComponentLifeCycle;
24 import javax.jbi.component.ServiceUnitManager;
25 import javax.jbi.management.DeploymentServiceMBean;
26 import javax.jbi.management.InstallerMBean;
27 import javax.jbi.management.LifeCycleMBean;
28 import javax.management.MBeanServerInvocationHandler JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.easymock.MockControl;
32
33 public class DeploymentTest extends AbstractManagementTest {
34
35     // Create mocks
36
protected ExtMockControl bootstrapMock;
37     protected Bootstrap bootstrap;
38     protected ExtMockControl componentMock;
39     protected Component component;
40     protected ExtMockControl lifecycleMock;
41     protected ComponentLifeCycle lifecycle;
42     protected ExtMockControl managerMock;
43     protected ServiceUnitManager manager;
44     
45     protected void setUp() throws Exception JavaDoc {
46         super.setUp();
47         // Create mocks
48
bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
49         bootstrap = (Bootstrap) bootstrapMock.getMock();
50         Bootstrap1.setDelegate(bootstrap);
51         componentMock = ExtMockControl.createControl(Component.class);
52         component = (Component) componentMock.getMock();
53         Component1.setDelegate(component);
54         lifecycleMock = ExtMockControl.createControl(ComponentLifeCycle.class);
55         lifecycle = (ComponentLifeCycle) lifecycleMock.getMock();
56         managerMock = ExtMockControl.createControl(ServiceUnitManager.class);
57         manager = (ServiceUnitManager) managerMock.getMock();
58     }
59     
60     protected void reset() {
61         bootstrapMock.reset();
62         componentMock.reset();
63         lifecycleMock.reset();
64         managerMock.reset();
65     }
66     
67     protected void replay() {
68         bootstrapMock.replay();
69         componentMock.replay();
70         lifecycleMock.replay();
71         managerMock.replay();
72     }
73     
74     protected void verify() {
75         bootstrapMock.verify();
76         componentMock.verify();
77         lifecycleMock.verify();
78         managerMock.verify();
79     }
80     
81     /**
82      * SA is deployed and started
83      * @throws Exception
84      */

85     public void testDeployAndStart() throws Exception JavaDoc {
86         // configure mocks
87
reset();
88         bootstrap.init(null);
89         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
90         bootstrap.onInstall();
91         bootstrap.getExtensionMBeanName();
92         bootstrapMock.setReturnValue(null);
93         bootstrap.cleanUp();
94         replay();
95         // test component installation
96
startContainer(true);
97         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
98         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
99         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
100         assertFalse(installer.isInstalled());
101         ObjectName JavaDoc lifecycleName = installer.install();
102         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
103         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
104         // check mocks
105
verify();
106         
107         // configure mocks
108
reset();
109         component.getLifeCycle();
110         componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE);
111         lifecycle.init(null);
112         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
113         lifecycle.start();
114         replay();
115         // test component installation
116
lifecycleMBean.start();
117         assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState());
118         // check mocks
119
verify();
120         
121         // configure mocks
122
reset();
123         component.getServiceUnitManager();
124         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
125         manager.deploy(null, null);
126         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
127         managerMock.setReturnValue(null);
128         replay();
129         // deploy sa
130
assertTrue(getDeploymentService().canDeployToComponent("component1"));
131         File JavaDoc installSaUrl = createServiceAssemblyArchive("sa", "su", "component1");
132         String JavaDoc result = getDeploymentService().deploy(installSaUrl.getAbsolutePath());
133         System.err.println(result);
134         String JavaDoc[] sas = getDeploymentService().getDeployedServiceAssemblies();
135         assertNotNull(sas);
136         assertEquals(1, sas.length);
137         assertEquals("sa", sas[0]);
138         sas = getDeploymentService().getDeployedServiceAssembliesForComponent("component1");
139         assertNotNull(sas);
140         assertEquals(1, sas.length);
141         assertEquals("sa", sas[0]);
142         assertEquals(DeploymentServiceMBean.SHUTDOWN, getDeploymentService().getState("sa"));
143         // check mocks
144
verify();
145         
146         // configure mocks
147
reset();
148         component.getServiceUnitManager();
149         componentMock.setReturnValue(manager, MockControl.ZERO_OR_MORE);
150         manager.init(null, null);
151         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
152         manager.start("su");
153         replay();
154         // start sa
155
getDeploymentService().start("sa");
156         assertEquals(DeploymentServiceMBean.STARTED, getDeploymentService().getState("sa"));
157         // check mocks
158
verify();
159         
160         // Clean shutdown
161
reset();
162         component.getLifeCycle();
163         componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE);
164         component.getServiceUnitManager();
165         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
166         lifecycle.stop();
167         lifecycle.shutDown();
168         //manager.stop("su");
169
manager.shutDown("su");
170         replay();
171         shutdownContainer();
172     }
173
174     /**
175      * SA is deployed and started
176      * @throws Exception
177      */

178     public void testDeployAndRestart() throws Exception JavaDoc {
179         // configure mocks
180
reset();
181         bootstrap.init(null);
182         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
183         bootstrap.onInstall();
184         bootstrap.getExtensionMBeanName();
185         bootstrapMock.setReturnValue(null);
186         bootstrap.cleanUp();
187         replay();
188         // test component installation
189
startContainer(true);
190         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
191         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
192         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
193         assertFalse(installer.isInstalled());
194         ObjectName JavaDoc lifecycleName = installer.install();
195         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
196         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
197         // check mocks
198
verify();
199         
200         // configure mocks
201
reset();
202         component.getLifeCycle();
203         componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE);
204         lifecycle.init(null);
205         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
206         lifecycle.start();
207         replay();
208         // test component installation
209
lifecycleMBean.start();
210         assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState());
211         // check mocks
212
verify();
213         
214         // configure mocks
215
reset();
216         component.getServiceUnitManager();
217         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
218         manager.deploy(null, null);
219         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
220         managerMock.setReturnValue(null);
221         replay();
222         // deploy sa
223
assertTrue(getDeploymentService().canDeployToComponent("component1"));
224         File JavaDoc installSaUrl = createServiceAssemblyArchive("sa", "su", "component1");
225         getDeploymentService().deploy(installSaUrl.getAbsolutePath());
226         String JavaDoc[] sas = getDeploymentService().getDeployedServiceAssemblies();
227         assertNotNull(sas);
228         assertEquals(1, sas.length);
229         assertEquals("sa", sas[0]);
230         sas = getDeploymentService().getDeployedServiceAssembliesForComponent("component1");
231         assertNotNull(sas);
232         assertEquals(1, sas.length);
233         assertEquals("sa", sas[0]);
234         assertEquals(DeploymentServiceMBean.SHUTDOWN, getDeploymentService().getState("sa"));
235         // check mocks
236
verify();
237         
238         // configure mocks
239
reset();
240         lifecycle.stop();
241         lifecycle.shutDown();
242         replay();
243         // shutdown container
244
shutdownContainer();
245         // check mocks
246
verify();
247         
248         // configure mocks
249
reset();
250         // XXX Should the bootstrap re-init?
251
bootstrap.init(null);
252         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
253         bootstrap.getExtensionMBeanName();
254         bootstrapMock.setReturnValue(null);
255         component.getLifeCycle();
256         componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE);
257         lifecycle.init(null);
258         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
259         lifecycle.start();
260         component.getServiceUnitManager();
261         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
262         manager.init(null, null);
263         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
264         manager.shutDown("su");
265         replay();
266         // start container
267
startContainer(false);
268         // check mocks
269
verify();
270         
271         // Clean shutdown
272
reset();
273         component.getLifeCycle();
274         componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE);
275         component.getServiceUnitManager();
276         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
277         lifecycle.stop();
278         lifecycle.shutDown();
279         //manager.stop("su");
280
manager.shutDown("su");
281         replay();
282         shutdownContainer();
283     }
284
285     /**
286      * SA is deployed and started
287      * @throws Exception
288      */

289     public void testDeployStartAndRestart() throws Exception JavaDoc {
290         // configure mocks
291
reset();
292         bootstrap.init(null);
293         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
294         bootstrap.onInstall();
295         bootstrap.getExtensionMBeanName();
296         bootstrapMock.setReturnValue(null);
297         bootstrap.cleanUp();
298         replay();
299         // test component installation
300
startContainer(true);
301         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
302         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
303         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
304         assertFalse(installer.isInstalled());
305         ObjectName JavaDoc lifecycleName = installer.install();
306         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
307         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
308         // check mocks
309
verify();
310         
311         // configure mocks
312
reset();
313         component.getLifeCycle();
314         componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE);
315         lifecycle.init(null);
316         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
317         lifecycle.start();
318         replay();
319         // test component installation
320
lifecycleMBean.start();
321         assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState());
322         // check mocks
323
verify();
324         
325         // configure mocks
326
reset();
327         component.getServiceUnitManager();
328         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
329         manager.deploy(null, null);
330         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
331         managerMock.setReturnValue(null);
332         replay();
333         // deploy sa
334
assertTrue(getDeploymentService().canDeployToComponent("component1"));
335         File JavaDoc installSaUrl = createServiceAssemblyArchive("sa", "su", "component1");
336         getDeploymentService().deploy(installSaUrl.getAbsolutePath());
337         String JavaDoc[] sas = getDeploymentService().getDeployedServiceAssemblies();
338         assertNotNull(sas);
339         assertEquals(1, sas.length);
340         assertEquals("sa", sas[0]);
341         sas = getDeploymentService().getDeployedServiceAssembliesForComponent("component1");
342         assertNotNull(sas);
343         assertEquals(1, sas.length);
344         assertEquals("sa", sas[0]);
345         assertEquals(DeploymentServiceMBean.SHUTDOWN, getDeploymentService().getState("sa"));
346         // check mocks
347
verify();
348         
349         // configure mocks
350
reset();
351         manager.init(null, null);
352         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
353         manager.start("su");
354         replay();
355         // start sa
356
getDeploymentService().start("sa");
357         assertEquals(DeploymentServiceMBean.STARTED, getDeploymentService().getState("sa"));
358         // check mocks
359
verify();
360         
361         // configure mocks
362
reset();
363         lifecycle.stop();
364         lifecycle.shutDown();
365         manager.stop("su");
366         manager.shutDown("su");
367         replay();
368         // shutdown container
369
shutdownContainer();
370         // check mocks
371
verify();
372         
373         // configure mocks
374
reset();
375         // XXX Should the bootstrap re-init?
376
bootstrap.init(null);
377         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
378         bootstrap.getExtensionMBeanName();
379         bootstrapMock.setReturnValue(null);
380         component.getLifeCycle();
381         componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE);
382         lifecycle.init(null);
383         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
384         lifecycle.start();
385         component.getServiceUnitManager();
386         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
387         manager.init(null, null);
388         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
389         manager.start("su");
390         managerMock.setMatcher(MockControl.ALWAYS_MATCHER);
391         replay();
392         // start container
393
startContainer(false);
394         // check mocks
395
verify();
396         
397         // Clean shutdown
398
reset();
399         component.getLifeCycle();
400         componentMock.setReturnValue(lifecycle, MockControl.ONE_OR_MORE);
401         component.getServiceUnitManager();
402         componentMock.setReturnValue(manager, MockControl.ONE_OR_MORE);
403         lifecycle.stop();
404         lifecycle.shutDown();
405         //manager.stop("su");
406
manager.shutDown("su");
407         replay();
408         shutdownContainer();
409     }
410
411 }
412
Popular Tags