KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jbi.component.Bootstrap;
20 import javax.jbi.component.Component;
21 import javax.jbi.component.ComponentLifeCycle;
22 import javax.jbi.management.InstallerMBean;
23 import javax.jbi.management.LifeCycleMBean;
24 import javax.management.MBeanServerInvocationHandler JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 import org.easymock.MockControl;
28
29
30 /**
31  *
32  * JbiTaskTest
33  * @version $Revision: 426415 $
34  */

35 public class InstallationTest extends AbstractManagementTest {
36     
37     /**
38      * Installer should not be persistent across restart
39      * @throws Exception
40      */

41     public void testLoadNewInstallerAndRestart() throws Exception JavaDoc {
42         ExtMockControl bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
43         Bootstrap bootstrap = (Bootstrap) bootstrapMock.getMock();
44         Bootstrap1.setDelegate(bootstrap);
45         
46         // configure bootstrap
47
bootstrap.init(null);
48         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
49         bootstrap.getExtensionMBeanName();
50         bootstrapMock.setReturnValue(null);
51         bootstrapMock.replay();
52         // test component installation
53
startContainer(true);
54         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
55         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
56         assertNotNull(Bootstrap1.getInstallContext());
57         assertTrue(Bootstrap1.getInstallContext().isInstall());
58         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
59         assertFalse(installer.isInstalled());
60         shutdownContainer();
61         // check mocks
62
bootstrapMock.verify();
63         
64         // configure bootstrap
65
bootstrapMock.reset();
66         bootstrapMock.replay();
67         // test container start
68
startContainer(false);
69         // check mocks
70
bootstrapMock.verify();
71     }
72
73     /**
74      * Installer should not be persistent across restart
75      * @throws Exception
76      */

77     public void testLoadNewInstallerAndLoadNewInstaller() throws Exception JavaDoc {
78         ExtMockControl bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
79         Bootstrap bootstrap = (Bootstrap) bootstrapMock.getMock();
80         Bootstrap1.setDelegate(bootstrap);
81         
82         // configure bootstrap
83
bootstrap.init(null);
84         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
85         bootstrap.getExtensionMBeanName();
86         bootstrapMock.setReturnValue(null);
87         bootstrapMock.replay();
88         // test component installation
89
startContainer(true);
90         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
91         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
92         assertNotNull(Bootstrap1.getInstallContext());
93         assertTrue(Bootstrap1.getInstallContext().isInstall());
94         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
95         assertFalse(installer.isInstalled());
96         // check mocks
97
bootstrapMock.verify();
98         
99         // configure bootstrap
100
bootstrapMock.reset();
101         bootstrapMock.replay();
102         // test load new installer
103
try {
104             getInstallationService().loadNewInstaller(installJarUrl);
105             fail("Expected an exception");
106         } catch (Exception JavaDoc e) {
107             // ok, this should fail
108
}
109         // check mocks
110
bootstrapMock.verify();
111     }
112
113     /**
114      * Installer is created, component installed and server restarted
115      * @throws Exception
116      */

117     public void testInstallAndRestart() throws Exception JavaDoc {
118         // Create mocks
119
ExtMockControl bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
120         Bootstrap bootstrap = (Bootstrap) bootstrapMock.getMock();
121         Bootstrap1.setDelegate(bootstrap);
122         ExtMockControl componentMock = ExtMockControl.createControl(Component.class);
123         Component component = (Component) componentMock.getMock();
124         Component1.setDelegate(component);
125         
126         // configure bootstrap
127
bootstrapMock.reset();
128         bootstrap.init(null);
129         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
130         bootstrap.onInstall();
131         bootstrap.getExtensionMBeanName();
132         bootstrapMock.setReturnValue(null);
133         bootstrap.cleanUp();
134         bootstrapMock.replay();
135         // configure component
136
componentMock.reset();
137         componentMock.replay();
138         // test component installation
139
startContainer(true);
140         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
141         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
142         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
143         assertFalse(installer.isInstalled());
144         ObjectName JavaDoc lifecycleName = installer.install();
145         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
146         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
147         // check mocks
148
bootstrapMock.verify();
149         componentMock.verify();
150         
151         // configure bootstrap
152
bootstrapMock.reset();
153         bootstrapMock.replay();
154         // configure component
155
componentMock.reset();
156         componentMock.replay();
157         // unload installer
158
container.getInstallationService().unloadInstaller("component1", false);
159         // check mocks
160
bootstrapMock.verify();
161         componentMock.verify();
162
163         // configure bootstrap
164
bootstrapMock.reset();
165         bootstrapMock.replay();
166         // configure component
167
componentMock.reset();
168         componentMock.replay();
169         // shutdown container
170
shutdownContainer();
171         // check mocks
172
bootstrapMock.verify();
173         componentMock.verify();
174         
175         // configure bootstrap
176
bootstrapMock.reset();
177         bootstrap.init(null);
178         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
179         bootstrap.getExtensionMBeanName();
180         bootstrapMock.setReturnValue(null);
181         bootstrapMock.replay();
182         // configure component
183
componentMock.reset();
184         componentMock.replay();
185         // start container
186
startContainer(false);
187         lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
188         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
189         // check mocks
190
bootstrapMock.verify();
191         componentMock.verify();
192     }
193
194     /**
195      * Installer is created, component installed, started and server restarted
196      * @throws Exception
197      */

198     public void testInstallStartAndRestart() throws Exception JavaDoc {
199         // Create mocks
200
ExtMockControl bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
201         Bootstrap bootstrap = (Bootstrap) bootstrapMock.getMock();
202         Bootstrap1.setDelegate(bootstrap);
203         ExtMockControl componentMock = ExtMockControl.createControl(Component.class);
204         Component component = (Component) componentMock.getMock();
205         Component1.setDelegate(component);
206         ExtMockControl lifecycleMock = ExtMockControl.createControl(ComponentLifeCycle.class);
207         ComponentLifeCycle lifecycle = (ComponentLifeCycle) lifecycleMock.getMock();
208         
209         // configure bootstrap
210
bootstrapMock.reset();
211         bootstrap.init(null);
212         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
213         bootstrap.onInstall();
214         bootstrap.getExtensionMBeanName();
215         bootstrapMock.setReturnValue(null);
216         bootstrap.cleanUp();
217         bootstrapMock.replay();
218         // configure component
219
componentMock.reset();
220         componentMock.replay();
221         // configure lifecycle
222
lifecycleMock.reset();
223         lifecycleMock.replay();
224         // test component installation
225
startContainer(true);
226         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
227         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
228         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
229         assertFalse(installer.isInstalled());
230         ObjectName JavaDoc lifecycleName = installer.install();
231         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
232         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
233         // check mocks
234
bootstrapMock.verify();
235         componentMock.verify();
236         lifecycleMock.verify();
237         
238         // configure bootstrap
239
bootstrapMock.reset();
240         bootstrapMock.replay();
241         // configure component
242
componentMock.reset();
243         component.getLifeCycle();
244         componentMock.setReturnValue(lifecycle);
245         componentMock.replay();
246         // configure lifecycle
247
lifecycleMock.reset();
248         lifecycle.init(null);
249         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
250         lifecycle.start();
251         lifecycleMock.replay();
252         // test component installation
253
lifecycleMBean.start();
254         assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState());
255         // check mocks
256
bootstrapMock.verify();
257         componentMock.verify();
258         lifecycleMock.verify();
259         
260         // configure bootstrap
261
bootstrapMock.reset();
262         bootstrapMock.replay();
263         // configure component
264
componentMock.reset();
265         componentMock.replay();
266         // configure lifecycle
267
lifecycleMock.reset();
268         lifecycle.stop();
269         lifecycle.shutDown();
270         lifecycleMock.replay();
271         // shutdown container
272
shutdownContainer();
273         // check mocks
274
bootstrapMock.verify();
275         componentMock.verify();
276         lifecycleMock.verify();
277         
278         // configure bootstrap
279
bootstrapMock.reset();
280         bootstrap.init(null);
281         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
282         bootstrap.getExtensionMBeanName();
283         bootstrapMock.setReturnValue(null);
284         bootstrapMock.replay();
285         // configure component
286
componentMock.reset();
287         component.getLifeCycle();
288         componentMock.setDefaultReturnValue(lifecycle);
289         componentMock.replay();
290         // configure lifecycle
291
lifecycleMock.reset();
292         lifecycle.getExtensionMBeanName();
293         lifecycleMock.setDefaultReturnValue(null);
294         lifecycle.init(null);
295         lifecycleMock.setMatcher(MockControl.ALWAYS_MATCHER);
296         lifecycle.start();
297         lifecycleMock.replay();
298         // start container
299
startContainer(false);
300         lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
301         assertEquals(LifeCycleMBean.STARTED, lifecycleMBean.getCurrentState());
302         // check mocks
303
bootstrapMock.verify();
304         componentMock.verify();
305         lifecycleMock.verify();
306     }
307
308     /**
309      * Installer is created, component installed.
310      * Then we unload the installer and reload it.
311      * @throws Exception
312      */

313     public void testInstallAndReloadInstaller() throws Exception JavaDoc {
314         // Create mocks
315
ExtMockControl bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
316         Bootstrap bootstrap = (Bootstrap) bootstrapMock.getMock();
317         Bootstrap1.setDelegate(bootstrap);
318         ExtMockControl componentMock = ExtMockControl.createControl(Component.class);
319         Component component = (Component) componentMock.getMock();
320         Component1.setDelegate(component);
321         ExtMockControl lifecycleMock = ExtMockControl.createControl(ComponentLifeCycle.class);
322         ComponentLifeCycle lifecycle = (ComponentLifeCycle) lifecycleMock.getMock();
323         
324         // configure bootstrap
325
bootstrapMock.reset();
326         bootstrap.init(null);
327         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
328         bootstrap.onInstall();
329         bootstrap.getExtensionMBeanName();
330         bootstrapMock.setReturnValue(null);
331         bootstrap.cleanUp();
332         bootstrapMock.replay();
333         // configure component
334
componentMock.reset();
335         component.getLifeCycle();
336         componentMock.setDefaultReturnValue(lifecycle);
337         componentMock.replay();
338         // configure lifecycle
339
lifecycleMock.reset();
340         lifecycleMock.replay();
341         // test component installation
342
startContainer(true);
343         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
344         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
345         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
346         assertFalse(installer.isInstalled());
347         ObjectName JavaDoc lifecycleName = installer.install();
348         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
349         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
350         // check mocks
351
bootstrapMock.verify();
352         componentMock.verify();
353         
354         // configure bootstrap
355
bootstrapMock.reset();
356         bootstrapMock.replay();
357         // configure component
358
componentMock.reset();
359         componentMock.replay();
360         // unload installer
361
container.getInstallationService().unloadInstaller("component1", false);
362         // check mocks
363
bootstrapMock.verify();
364         componentMock.verify();
365
366         // configure bootstrap
367
bootstrapMock.reset();
368         bootstrapMock.replay();
369         // configure component
370
componentMock.reset();
371         componentMock.replay();
372         // shutdown container
373
installerName = container.getInstallationService().loadInstaller("component1");
374         assertNotNull(installerName);
375         // check mocks
376
bootstrapMock.verify();
377         componentMock.verify();
378     }
379
380     /**
381      * Installer is created, component installed, uninstalled and reinstalled
382      * @throws Exception
383      */

384     public void testInstallAndReinstall() throws Exception JavaDoc {
385         // Create mocks
386
ExtMockControl bootstrapMock = ExtMockControl.createControl(Bootstrap.class);
387         Bootstrap bootstrap = (Bootstrap) bootstrapMock.getMock();
388         Bootstrap1.setDelegate(bootstrap);
389         ExtMockControl componentMock = ExtMockControl.createControl(Component.class);
390         Component component = (Component) componentMock.getMock();
391         Component1.setDelegate(component);
392         
393         // configure bootstrap
394
bootstrapMock.reset();
395         bootstrap.init(null);
396         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
397         bootstrap.onInstall();
398         bootstrap.getExtensionMBeanName();
399         bootstrapMock.setReturnValue(null);
400         bootstrap.cleanUp();
401         bootstrapMock.replay();
402         // configure component
403
componentMock.reset();
404         componentMock.replay();
405         // test component installation
406
startContainer(true);
407         String JavaDoc installJarUrl = createInstallerArchive("component1").getAbsolutePath();
408         ObjectName JavaDoc installerName = getInstallationService().loadNewInstaller(installJarUrl);
409         InstallerMBean installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
410         assertFalse(installer.isInstalled());
411         ObjectName JavaDoc lifecycleName = installer.install();
412         LifeCycleMBean lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
413         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
414         // check mocks
415
bootstrapMock.verify();
416         componentMock.verify();
417         
418         // configure bootstrap
419
bootstrapMock.reset();
420         bootstrap.onUninstall();
421         bootstrap.cleanUp();
422         bootstrapMock.replay();
423         // configure component
424
componentMock.reset();
425         componentMock.replay();
426         // unload installer
427
container.getInstallationService().unloadInstaller("component1", true);
428         // check mocks
429
bootstrapMock.verify();
430         componentMock.verify();
431
432         // configure bootstrap
433
bootstrapMock.reset();
434         bootstrap.init(null);
435         bootstrapMock.setMatcher(MockControl.ALWAYS_MATCHER);
436         bootstrap.getExtensionMBeanName();
437         bootstrapMock.setReturnValue(null);
438         bootstrap.onInstall();
439         bootstrap.cleanUp();
440         bootstrapMock.replay();
441         // configure component
442
componentMock.reset();
443         componentMock.replay();
444         // test component installation
445
startContainer(true);
446         installJarUrl = createInstallerArchive("component1").getAbsolutePath();
447         installerName = getInstallationService().loadNewInstaller(installJarUrl);
448         installer = (InstallerMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), installerName, InstallerMBean.class, false);
449         assertFalse(installer.isInstalled());
450         lifecycleName = installer.install();
451         lifecycleMBean = (LifeCycleMBean) MBeanServerInvocationHandler.newProxyInstance(container.getMBeanServer(), lifecycleName, LifeCycleMBean.class, false);
452         assertEquals(LifeCycleMBean.SHUTDOWN, lifecycleMBean.getCurrentState());
453         // check mocks
454
bootstrapMock.verify();
455         componentMock.verify();
456         
457         // configure bootstrap
458
bootstrapMock.reset();
459         bootstrapMock.replay();
460         // configure component
461
componentMock.reset();
462         componentMock.replay();
463         // shutdown container
464
shutdownContainer();
465         // check mocks
466
bootstrapMock.verify();
467         componentMock.verify();
468     }
469
470 }
471
Popular Tags