KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > component > lifecycle > InstallerTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: InstallerTest.java 11:09:47 wjoseph $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.component.lifecycle;
23
24 import java.io.File JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.net.URI JavaDoc;
27
28 import javax.jbi.JBIException;
29 import javax.jbi.component.Bootstrap;
30 import javax.jbi.management.LifeCycleMBean;
31 import javax.management.MBeanNotificationInfo JavaDoc;
32 import javax.management.Notification JavaDoc;
33 import javax.management.NotificationBroadcasterSupport JavaDoc;
34 import javax.management.NotificationListener JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36
37 import junit.framework.TestCase;
38
39 import org.objectweb.petals.classloader.LoaderManager;
40 import org.objectweb.petals.classloader.PetalsClassLoader;
41 import org.objectweb.petals.jbi.component.context.ComponentContextImpl;
42 import org.objectweb.petals.jbi.component.event.StateChangeFailedEvent;
43 import org.objectweb.petals.jbi.component.event.StateChangedEvent;
44 import org.objectweb.petals.jbi.component.thread.BootstrapThread;
45 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService;
46 import org.objectweb.petals.jbi.management.systemstate.SystemState;
47 import org.objectweb.petals.jbi.mock.MockInstaller;
48 import org.objectweb.petals.jbi.mock.MockSystemState;
49 import org.objectweb.petals.tools.jbicommon.descriptor.ComponentDescription;
50 import org.objectweb.petals.util.LoggingUtil;
51 import org.objectweb.util.monolog.api.Logger;
52
53 import static org.easymock.EasyMock.expect;
54 import static org.easymock.EasyMock.isA;
55 import static org.easymock.classextension.EasyMock.createMock;
56 import static org.easymock.classextension.EasyMock.replay;
57 import static org.easymock.classextension.EasyMock.reset;
58 import static org.easymock.classextension.EasyMock.verify;
59
60 /**
61  * Test of the Installer
62  *
63  * @author wjoseph - eBMWebsourcing
64  */

65 public class InstallerTest extends TestCase {
66
67     private File JavaDoc testDataDir;
68     
69     public void testAddNotificationListener() {
70         /*
71          * Test Case 1
72          */

73         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
74         NotificationBroadcasterSupport JavaDoc notifSupport = createMock(NotificationBroadcasterSupport JavaDoc.class);
75         /*
76          * Initialize Mocks
77          */

78         notifSupport.addNotificationListener(null, null, null);
79         replay(notifSupport);
80         installer.notifSupport = notifSupport;
81         /*
82          * Run Test Case 1
83          */

84         installer.addNotificationListener(null, null, null);
85         verify(notifSupport);
86     }
87     
88     public void testGetComponentName() {
89         /*
90          * Test Case 1
91          */

92         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
93         ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class);
94        expect(componentCtx.getComponentName()).andReturn(null);
95        replay(componentCtx);
96        installer.componentContext = componentCtx;
97        /*
98         * Run Test Case 1
99         */

100        installer.getComponentName();
101        verify(componentCtx);
102     }
103     
104     public void testGetCurrentState() {
105         /*
106          * Test Case 1
107          */

108         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
109         /*
110          * Run Test Case 1
111          */

112         
113         assertEquals("Wrong invokation result",LifeCycleMBean.UNKNOWN,installer.getCurrentState());
114         
115     }
116     
117     public void testGetInstallerConfigurationMBean() {
118         /*
119          * Test Case 1
120          * The component is not in the INSTALLED state, an exception is raised
121          *
122          * Create Mocks
123          */

124         Method JavaDoc method1 = null;
125         try {
126             method1 = Installer.class.getDeclaredMethod("loadBootstrap", new Class JavaDoc[]{PetalsClassLoader.class});
127         } catch (Exception JavaDoc e ) {
128             e.printStackTrace();
129             fail("Could not retrieve mocked methods");
130         }
131         Installer installer = createMock(Installer.class,new Method JavaDoc[]{method1});
132         
133         BootstrapThread installerThread = createMock(BootstrapThread.class);
134         ObjectName JavaDoc oName = createMock(ObjectName JavaDoc.class);
135         installer.componentJmxName = oName;
136         
137         expect(installerThread.getExtensionMBeanName()).andReturn(oName);
138         replay(installerThread);
139         installer.bootstrapThread = installerThread;
140         
141         LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerService.class);
142         installer.jmxAdmin = lifeCycleManager;
143         ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class);
144         installer.componentContext = componentCtx;
145         
146         /*
147          * Run Test Case 1, an exception must be raised
148          */

149         try {
150             installer.getInstallerConfigurationMBean();
151             fail("No exception was raised");
152         } catch (Exception JavaDoc e) {
153             // Do nothing
154
}
155         
156         /*
157          * Test Case 2
158          * The component is in the INSTALLED state, and the component life cycle is in the SHUTDOWN state
159          */

160         reset(installer);
161         reset(installerThread);
162         reset(lifeCycleManager);
163         reset(componentCtx);
164         /*
165          * Create Test parameters
166          */

167         Bootstrap bootstrap = createMock(Bootstrap.class);
168         ObjectName JavaDoc objectName = createMock(ObjectName JavaDoc.class);
169         PetalsClassLoader classloader = createMock(PetalsClassLoader.class);
170         installer.state = Installer.INSTALLED;
171         installer.bootstrapThread = installerThread;
172         String JavaDoc componentName = "componentName";
173         ComponentLifeCycle lifeCycle = createMock(ComponentLifeCycle.class);
174         installer.componentJmxName = objectName;
175         /*
176          * Initialize Mocks
177          */

178         try {
179             expect(bootstrap.getExtensionMBeanName()).andReturn(objectName);
180             expect(installer.loadBootstrap(classloader)).andReturn(bootstrap);
181             expect(installerThread.getExtensionMBeanName()).andReturn(objectName);
182             expect(lifeCycleManager.getComponentByName(componentName)).andReturn(lifeCycle);
183             expect(lifeCycle.getCurrentState()).andReturn(LifeCycleMBean.SHUTDOWN);
184             expect(componentCtx.getComponentName()).andReturn(componentName);
185         } catch (Exception JavaDoc e) {
186             e.printStackTrace();
187             fail("Could not initialize Mocks");
188         }
189         replay(installer);
190         replay(installerThread);
191         replay(bootstrap);
192         replay(lifeCycle);
193         replay(lifeCycleManager);
194         replay(componentCtx);
195         installer.jmxAdmin = lifeCycleManager;
196         installer.componentContext = componentCtx;
197         installer.jbiBootstrap = bootstrap;
198         installer.bootstrapThread = installerThread;
199         ObjectName JavaDoc result = null;
200         
201         /*
202          * Run Test Case 2
203          */

204         try {
205             result = installer.getInstallerConfigurationMBean();
206         } catch (Exception JavaDoc e) {
207             e.printStackTrace();
208             fail("Error during invokation");
209         }
210         verify(installerThread);
211         verify(lifeCycle);
212         verify(lifeCycleManager);
213         verify(componentCtx);
214         assertEquals("Wrong invokation result",result, objectName);
215     }
216     
217     public void testGetInstallRoot() {
218         /*
219          * Test Case 1
220          */

221         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
222         /*
223          * Create Test parameters
224          */

225         String JavaDoc installationRoot = "installation/root";
226         ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class);
227         expect(componentCtx.getInstallRoot()).andReturn(installationRoot);
228         replay(componentCtx);
229         installer.componentContext = componentCtx;
230         /*
231          * Run Test Case 1
232          */

233         String JavaDoc result = installer.getInstallRoot();
234         assertNotNull("Result of getInstallRoot must not be null",result);
235         assertFalse("Result of getInstallRoot must not be empty",result.length() == 0);
236         verify(componentCtx);
237     }
238     
239     public void testGetMBeanName() {
240         /*
241          * Test Case 1
242          */

243         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
244         /*
245          * Create Test parameters
246          */

247         installer.mBeanName = createMock(ObjectName JavaDoc.class);
248         /*
249          * Run Test Case 1
250          */

251         assertEquals("Wroong invokation result",installer.getMBeanName(),installer.mBeanName);
252     }
253     
254     public void testGetNotificationInfo() {
255         /*
256          * Test Case 1
257          */

258         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
259         /*
260          * Create Test parameters
261          */

262         MBeanNotificationInfo JavaDoc[] result= null;
263         /*
264          * Run Test Case 1
265          */

266         result = installer.getNotificationInfo();
267         assertTrue("Wrong invokation result",result.length == 1);
268     }
269     
270     public void testInstall() {
271         /*
272          * Test Case 1
273          * The installer is in the wrong state an exception is raised
274          */

275         Method JavaDoc method1 = null;
276         Method JavaDoc method2 = null;
277         Method JavaDoc method3 = null;
278         try {
279             method1 = Installer.class.getDeclaredMethod("stateChangeFailed", new Class JavaDoc[]{StateChangeFailedEvent.class});
280             method2 = Installer.class.getDeclaredMethod("setState", new Class JavaDoc[]{String JavaDoc.class});
281             method3 = Installer.class.getDeclaredMethod("doInstall", new Class JavaDoc[]{});
282         } catch (Exception JavaDoc e) {
283             e.printStackTrace();
284             fail("Could not retrieve mocked methods");
285         }
286         Method JavaDoc[] mockedMethods = new Method JavaDoc[]{method1, method2, method3};
287         Installer installer = createMock(Installer.class,mockedMethods);
288         LoggingUtil log = createMock(LoggingUtil.class);
289         installer.log = log;
290         /*
291          * Create Test parameters
292          */

293         /*
294          * Initialize Mocks
295          */

296         installer.stateChangeFailed(isA(StateChangeFailedEvent.class));
297         replay(installer);
298         installer.activitySynchronizer = new Object JavaDoc();
299         /*
300          * Run Test Case 1
301          */

302         try {
303             installer.install();
304             fail("No exception was raised");
305         } catch (Exception JavaDoc e) {
306                 verify(installer);
307             }
308         
309         /*
310          * Test Case 2
311          */

312         reset(installer);
313         /*
314          * Create Test parameters
315          */

316         installer.state = Installer.UNINSTALLED;
317         ObjectName JavaDoc objectName = createMock(ObjectName JavaDoc.class);
318         installer.componentJmxName = objectName;
319         ObjectName JavaDoc result = null;
320         /*
321          * Initialize Mocks
322          */

323         try {
324             installer.setState(Installer.INSTALLING);
325             installer.doInstall();
326             installer.setState(Installer.INSTALLED);
327         } catch (Exception JavaDoc e) {
328             e.printStackTrace();
329             fail("Could not initialize Mocks");
330         }
331         replay(installer);
332         /*
333          * Run Test Case 2
334          */

335         try {
336             result = installer.install();
337         } catch (Exception JavaDoc e) {
338             e.printStackTrace();
339             fail("Error during invokation");
340         }
341         verify(installer);
342         assertEquals("Wrong invokation result",result,objectName);
343         
344         /*
345          * Test Case 3
346          */

347         //reset(installer);
348
/*
349          * Create Test parameters
350          */

351         installer = createMock(MockInstaller.class,new Method JavaDoc[]{method1, method2});
352         installer.state = Installer.UNINSTALLED;
353         installer.componentJmxName = objectName;
354         installer.log = log;
355         installer.activitySynchronizer = new Object JavaDoc();
356         result = null;
357         try {
358            // installer.doInstall();
359
installer.setState(Installer.INSTALLING);
360             installer.setState(Installer.UNKNOWN);
361             installer.stateChangeFailed(isA(StateChangeFailedEvent.class));
362         } catch (Exception JavaDoc e) {
363             e.printStackTrace();
364             fail("Could not initialize Mocks");
365         }
366         replay(installer);
367         /*
368          * Run Test Case 3
369          */

370         try {
371             result = installer.install();
372             fail("No exception was raised");
373         } catch (Exception JavaDoc e) {
374             verify(installer);
375         }
376     }
377     
378     public void testStateChanged() {
379         /*
380          * Test Case 1
381          */

382         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
383         /*
384          * Create Test parameters
385          */

386         LoggingUtil log = createMock(LoggingUtil.class);
387         installer.log = log;
388         installer.mBeanName = createMock(ObjectName JavaDoc.class);
389         StateChangedEvent stateChange = new StateChangedEvent("source","oldState","newState");
390         NotificationBroadcasterSupport JavaDoc notificationBroacaster = createMock(NotificationBroadcasterSupport JavaDoc.class);
391         /*
392          * Initialize Mock
393          */

394         notificationBroacaster.sendNotification(isA(Notification JavaDoc.class));
395         replay(notificationBroacaster);
396         installer.notifSupport = notificationBroacaster;
397         /*
398          * Run Test Case 1
399          */

400         installer.stateChanged(stateChange);
401         verify(notificationBroacaster);
402         
403     }
404     
405     public void testStateChangeFailed() {
406         /*
407          * Test Case 1
408          */

409         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
410         /*
411          * Create Test parameters
412          */

413         LoggingUtil log = createMock(LoggingUtil.class);
414         installer.log = log;
415         installer.mBeanName = createMock(ObjectName JavaDoc.class);
416         StateChangeFailedEvent stateChangeFailed = new StateChangeFailedEvent("source",new JBIException("testStateChangeFailed testing exception"));
417         NotificationBroadcasterSupport JavaDoc notificationBroacaster = createMock(NotificationBroadcasterSupport JavaDoc.class);
418         /*
419          * Initialize Mock
420          */

421         notificationBroacaster.sendNotification(isA(Notification JavaDoc.class));
422         replay(notificationBroacaster);
423         installer.notifSupport = notificationBroacaster;
424         /*
425          * Run Test Case 1
426          */

427         installer.stateChangeFailed(stateChangeFailed);
428         verify(notificationBroacaster);
429     }
430     
431     public void testInstaller() {
432         /*
433          * Test Case 1
434          */

435         /*
436          * Create test parameters
437          */

438         Installer result = null;
439         URI JavaDoc installationURI = new File JavaDoc (testDataDir, "fakearchive" + File.separator + "archive").toURI();
440         LoaderManager loaderManager = createMock(LoaderManager.class);
441         ComponentContextImpl componentCtx = createMock(ComponentContextImpl.class);
442         ComponentDescription componentDescription = createMock(ComponentDescription.class);
443         LifeCycleManagerService lifeCycleManager = createMock(LifeCycleManagerService.class);
444         SystemState systemState = createMock(SystemState.class);
445         Logger logger = null;
446         ObjectName JavaDoc objectName = createMock(ObjectName JavaDoc.class);
447         /*
448          * Run Test Case 1
449          */

450         try {
451         result = new Installer(installationURI, loaderManager,
452                 componentCtx, componentDescription, lifeCycleManager,
453                 systemState, objectName, logger);
454         } catch (Exception JavaDoc e) {
455             e.printStackTrace();
456             fail("Error during invokation");
457         }
458         assertNotNull("",result);
459     }
460     
461     public void testIsInstalled() {
462         /*
463          * Test Case 1
464          */

465         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
466         /*
467          * Create Test parameters
468          */

469         installer.state = Installer.INSTALLED;
470         /*
471          * Run Test Case 1
472          */

473         assertTrue("Wrong invokation result",installer.isInstalled());
474     }
475     
476     public void testRemoveNotificationListener() {
477         /*
478          * Test Case 1
479          */

480         Installer installer = createMock(Installer.class,new Method JavaDoc[]{});
481         /*
482          * Create Test parameters
483          */

484         installer.notifSupport = createMock(NotificationBroadcasterSupport JavaDoc.class);
485         NotificationListener JavaDoc notificationListener = createMock(NotificationListener JavaDoc.class);
486         /*
487          * Run Test Case 1
488          */

489         try {
490             installer.removeNotificationListener(notificationListener);
491         } catch (Exception JavaDoc e) {
492             e.printStackTrace();
493             fail("Error during invokation");
494         }
495     }
496     
497     public void testSetState() {
498         /*
499          * Test Case 1
500          * recoverySrv.updateComponentInstallationState throw an exception
501          */

502         Method JavaDoc method1 = null;
503         Method JavaDoc method2 = null;
504         try {
505             method1 = Installer.class.getDeclaredMethod("stateChanged", new Class JavaDoc[]{StateChangedEvent.class});
506             method2 = Installer.class.getDeclaredMethod("getComponentName", new Class JavaDoc[]{});
507         } catch (Exception JavaDoc e) {
508             e.printStackTrace();
509             fail("Could not retrieve mocked methods");
510         }
511         Method JavaDoc[] mockedMethods = new Method JavaDoc[]{method1,method2};
512         Installer installer = createMock(Installer.class,mockedMethods);
513         /*
514          * Create Test parameters
515          */

516         LoggingUtil log = createMock(LoggingUtil.class);
517         installer.log = log;
518         installer.recoverySrv = createMock(MockSystemState.class, new Method JavaDoc[]{});
519         String JavaDoc componentName = "componentName";
520         /*
521          * Initialize Mocks
522          */

523         installer.stateChanged(isA(StateChangedEvent.class));
524         expect(installer.getComponentName()).andReturn(componentName);
525         /*
526          * Run Test Case 1
527          */

528         try {
529             installer.setState("state");
530             fail("No exception was raised");
531         } catch (Exception JavaDoc e) {
532             // Do nothing
533
}
534     }
535     
536     public void testUninstall() {
537         /*
538          * Test Case 1
539          * The installer state is not Installer.INSTALLED
540          */

541         Method JavaDoc method1 = null;
542         Method JavaDoc method2 = null;
543         Method JavaDoc method3 = null;
544         try {
545             method1 = Installer.class.getDeclaredMethod("stateChangeFailed", new Class JavaDoc[]{StateChangeFailedEvent.class});
546             method2 = Installer.class.getDeclaredMethod("getComponentLifeCycle", new Class JavaDoc[]{});
547             method3 = Installer.class.getDeclaredMethod("doUninstall", new Class JavaDoc[]{});
548         } catch (Exception JavaDoc e) {
549             e.printStackTrace();
550             fail("Could not retrieve mocked methods");
551         }
552         Method JavaDoc[] mockedMethods = new Method JavaDoc[]{method1, method2, method3};
553         Installer installer = createMock(Installer.class,mockedMethods);
554         installer.state = Installer.UNINSTALLED;
555         LoggingUtil log = createMock(LoggingUtil.class);
556         installer.log = log;
557         /*
558          * Create Test parameters
559          */

560         /*
561          * Initialize Mocks
562          */

563         installer.stateChangeFailed(isA(StateChangeFailedEvent.class));
564         replay(installer);
565         /*
566          * Run Test Case 1
567          */

568         try {
569         installer.uninstall();
570         fail("No exception was raised");
571         } catch (Exception JavaDoc e) {
572             verify(installer);
573         }
574     }
575     
576     @Override JavaDoc
577     protected void setUp() throws Exception JavaDoc {
578         testDataDir = new File JavaDoc("src" + File.separator + "test-data");
579         if (!testDataDir.exists()) {
580             testDataDir = new File JavaDoc("container" + File.separator + "src"
581                     + File.separator + "test-data");
582             if (!testDataDir.exists()) {
583                 testDataDir = new File JavaDoc("platform" + File.separator
584                         + "container" + File.separator + "src" + File.separator
585                         + "test-data");
586             }
587         }
588     }
589
590    
591 }
592
Popular Tags