KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > autoload > AutoLoaderImplTest


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: AutoLoaderImplTest.java 3:54:28 PM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.autoload;
23
24 import static org.easymock.EasyMock.expect;
25 import static org.easymock.classextension.EasyMock.createMock;
26 import static org.easymock.classextension.EasyMock.replay;
27 import static org.easymock.classextension.EasyMock.verify;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.net.URI JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import javax.management.InstanceNotFoundException JavaDoc;
37 import javax.management.MBeanException JavaDoc;
38 import javax.management.MalformedObjectNameException JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.ReflectionException JavaDoc;
41
42 import junit.framework.TestCase;
43
44 import org.apache.commons.io.FileUtils;
45 import org.easymock.classextension.EasyMock;
46 import org.objectweb.petals.PetalsException;
47 import org.objectweb.petals.jbi.management.service.InstallationServiceImpl;
48 import org.objectweb.petals.jbi.management.service.LifeCycleManagerImpl;
49 import org.objectweb.petals.jbi.management.service.PackageHandler;
50 import org.objectweb.petals.jbi.management.systemstate.ComponentState;
51 import org.objectweb.petals.jbi.management.systemstate.ServiceAssemblyState;
52 import org.objectweb.petals.jbi.management.systemstate.SharedLibraryState;
53 import org.objectweb.petals.jbi.management.systemstate.SystemState;
54 import org.objectweb.petals.tools.jbicommon.descriptor.ComponentDescription;
55 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
56 import org.objectweb.petals.util.LoggingUtil;
57 import org.objectweb.util.monolog.Monolog;
58 /**
59  * Test of the AutoLoaderImpl
60  *
61  * @author ddesjardins - eBMWebsourcing
62  * @author wjoseph - eBMWebsourcing
63  */

64 public class AutoLoaderImplTest extends TestCase {
65
66     private AutoLoaderImpl autoLoaderImpl;
67
68     private String JavaDoc baseDir;
69
70     public void setUp() {
71         baseDir = this.getClass().getResource(".").toString();
72         baseDir = baseDir.substring(0, baseDir.indexOf("target"));
73         baseDir = baseDir.substring(baseDir.indexOf(":") + 1);
74         System.setProperty("petals.home", new File JavaDoc(baseDir + "target")
75             .getAbsolutePath());
76         new File JavaDoc(baseDir + "target" + File.separator + "lib").mkdir();
77         new File JavaDoc(baseDir + "target" + File.separator + "lib" + File.separator
78             + "petals-1.0-SNAPSHOT.jar").mkdir();
79         autoLoaderImpl = new AutoLoaderImpl();
80     }
81
82     public void testStart() throws IOException JavaDoc {
83         autoLoaderImpl.logger = Monolog.initialize().getLogger("test");
84         autoLoaderImpl.start();
85         assertEquals(AutoLoaderImpl.getInstallDirectory(), new File JavaDoc(baseDir
86             + "target" + File.separator + "install"));
87         assertEquals(AutoLoaderImpl.getInstalledDirectory(), new File JavaDoc(baseDir
88             + "target" + File.separator + "installed"));
89         assertEquals(AutoLoaderImpl.getWorkDirectory(), new File JavaDoc(baseDir
90             + "target" + File.separator + "work"));
91         assertEquals(AutoLoaderImpl.getUninstalledDirectory(), new File JavaDoc(baseDir
92             + "target" + File.separator + "uninstalled"));
93
94         //old testStart method
95
autoLoaderImpl.log = new LoggingUtil(autoLoaderImpl.logger);
96         assertNotNull(autoLoaderImpl.installDirectoryScanTimer);
97         assertNotNull(autoLoaderImpl.installedDirectoryScanTimer);
98     }
99
100     public void testGetInstallDirectory () {
101         /*
102          * Test Case 1
103          */

104         File JavaDoc install = new File JavaDoc("");
105         AutoLoaderImpl.installDirectory = install;
106         /*
107          * Run Test Case 1
108          */

109         assertEquals("Wrong invokation result",AutoLoaderImpl.getInstallDirectory(),install);
110     }
111  
112     public void testGetInstalledDirectory () {
113         /*
114          * Test Case 1
115          */

116         File JavaDoc installed = new File JavaDoc("");
117         AutoLoaderImpl.installedDirectory = installed;
118         /*
119          * Run Test Case 1
120          */

121         assertEquals("Wrong invokation result",AutoLoaderImpl.getInstallDirectory(),installed);
122     }
123     
124     public void testGetUninstalDirectory () {
125         /*
126          * Test Case 1
127          */

128         File JavaDoc uninstalled = new File JavaDoc("");
129         AutoLoaderImpl.uninstalledDirectory = uninstalled;
130         /*
131          * Run Test Case 1
132          */

133         assertEquals("Wrong invokation result",AutoLoaderImpl.getUninstalledDirectory(),uninstalled);
134     }
135     
136     public void testGetWorkDirectory () {
137         /*
138          * Test Case 1
139          */

140         File JavaDoc work = new File JavaDoc("");
141         AutoLoaderImpl.workDirectory = work;
142         /*
143          * Run Test Case 1
144          */

145         assertEquals("Wrong invokation result",AutoLoaderImpl.getWorkDirectory(),work);
146     }
147     
148     public void tearDown() {
149         System.clearProperty("petals.home");
150     }
151
152     public void testInstallComponent() throws IOException JavaDoc, PetalsException,
153         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc,
154         InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
155         FileUtils.copyFileToDirectory(new File JavaDoc(baseDir + "src" + File.separator
156             + "test-data" + File.separator + "autoload" + File.separator
157             + "toInstall.zip"), new File JavaDoc(baseDir + "target"));
158         testStart();
159
160         JBIDescriptor descriptor = EasyMock.createMock(JBIDescriptor.class);
161         ComponentDescription componentDescription = EasyMock
162             .createMock(ComponentDescription.class);
163         EasyMock.replay(componentDescription);
164
165         EasyMock.expect(descriptor.getComponent()).andReturn(
166             componentDescription).anyTimes();
167         EasyMock.replay(descriptor);
168
169         PackageHandler packageHandler = EasyMock
170             .createMock(PackageHandler.class);
171         EasyMock.expect(
172             packageHandler.loadDescriptor(new File JavaDoc(baseDir + "target"
173                 + File.separator + "work" + File.separator + "toInstall.zip")
174                 .toURI())).andReturn(descriptor).anyTimes();
175         EasyMock.replay(packageHandler);
176
177         LifeCycleManagerImpl lifeCycleManagerImpl = EasyMock
178             .createMock(LifeCycleManagerImpl.class);
179
180         MockMbeanServer server = new MockMbeanServer();
181
182         EasyMock.expect(lifeCycleManagerImpl.getMBeanServer())
183             .andReturn(server).anyTimes();
184
185         EasyMock.replay(lifeCycleManagerImpl);
186
187         InstallationServiceImpl installationService = EasyMock
188             .createMock(InstallationServiceImpl.class);
189         EasyMock.expect(
190             installationService.loadNewInstaller(new File JavaDoc(baseDir + "target"
191                 + File.separator + "work" + File.separator + "toInstall.zip")
192                 .toURI().toString())).andReturn(
193             new ObjectName JavaDoc("test@test:name=installer")).anyTimes();
194         EasyMock.replay(installationService);
195
196         autoLoaderImpl.installationService = installationService;
197         autoLoaderImpl.managerService = lifeCycleManagerImpl;
198         autoLoaderImpl.packageHandler = packageHandler;
199         autoLoaderImpl.log = new LoggingUtil(autoLoaderImpl.logger);
200         autoLoaderImpl.install(new File JavaDoc[] {new File JavaDoc(baseDir + "target"
201             + File.separator + "toInstall.zip")});
202
203         assertTrue(server.isInvokeStart());
204     }
205     
206     public void testUninstall() {
207         /*
208          * Test Case 1
209          */

210         /*
211          * Create Mocks
212          */

213         Method JavaDoc method1 = null;
214         Method JavaDoc method2 = null;
215         Method JavaDoc method3 = null;
216         try {
217             method1 = AutoLoaderImpl.class.getDeclaredMethod("performCompUninstall", new Class JavaDoc[]{String JavaDoc.class});
218             method2 = AutoLoaderImpl.class.getDeclaredMethod("performSAUninstall", new Class JavaDoc[]{String JavaDoc.class});
219             method3 = AutoLoaderImpl.class.getDeclaredMethod("performSLUninstall", new Class JavaDoc[]{String JavaDoc.class});
220         } catch (Exception JavaDoc e) {
221             e.printStackTrace();
222             fail("Could not retrieve mocked methods");
223         }
224         autoLoaderImpl = createMock(AutoLoaderImpl.class,new Method JavaDoc[]{method1, method2, method3});
225         /*
226          * Create Test parameters
227          */

228         List JavaDoc<ComponentState> compStates = new ArrayList JavaDoc<ComponentState>();
229         ComponentState compState = createMock(ComponentState.class);
230         compStates.add(compState);
231         List JavaDoc<SharedLibraryState> slStates = new ArrayList JavaDoc<SharedLibraryState>();
232         SharedLibraryState slState = createMock(SharedLibraryState.class);
233         slStates.add(slState);
234         List JavaDoc<ServiceAssemblyState> saStates = new ArrayList JavaDoc<ServiceAssemblyState>();
235         ServiceAssemblyState saState = createMock(ServiceAssemblyState.class);
236         saStates.add(saState);
237         String JavaDoc compName = "component Name";
238         String JavaDoc slName = " shared library name";
239         String JavaDoc saName = "service assembly name";
240         String JavaDoc url = "myUrl";
241         File JavaDoc file = new File JavaDoc("");
242         URI JavaDoc uri = file.toURI();
243         file = new File JavaDoc(uri);
244         PackageHandler packageHandler = createMock(PackageHandler.class);
245         List JavaDoc<File JavaDoc> fileToUninstall = new ArrayList JavaDoc<File JavaDoc>();
246         fileToUninstall.add(file);
247         LoggingUtil log = createMock(LoggingUtil.class);
248         SystemState systemState = createMock(SystemState.class);
249         /*
250          * Initialize Mocks
251          */

252         expect(compState.getName()).andReturn(compName);
253         expect(compState.getArchiveURL()).andReturn(url);
254         expect(slState.getName()).andReturn(slName);
255         expect(slState.getArchiveURL()).andReturn(url);
256         expect(saState.getName()).andReturn(saName);
257         expect(saState.getArchiveURL()).andReturn(url);
258         expect(packageHandler.processAndGetPackageURI(url, false)).andReturn(uri).anyTimes();
259         autoLoaderImpl.performCompUninstall(compName);
260         autoLoaderImpl.performSAUninstall(saName);
261         autoLoaderImpl.performSLUninstall(slName);
262         expect(systemState.retrieveAllComponentStates()).andReturn(compStates);
263         expect(systemState.retrieveAllSharedLibraryStates()).andReturn(slStates);
264         expect(systemState.retrieveAllServiceAssemblyStates()).andReturn(saStates);
265         replay(compState);
266         replay(slState);
267         replay(saState);
268         replay(packageHandler);
269         replay(autoLoaderImpl);
270         replay(systemState);
271         autoLoaderImpl.packageHandler = packageHandler;
272         autoLoaderImpl.log = log;
273         autoLoaderImpl.recoverySrv = systemState;
274         /*
275          * Run Test Case 1
276          */

277         autoLoaderImpl.uninstall(fileToUninstall);
278         verify(compState);
279         verify(slState);
280         verify(saState);
281         verify(packageHandler);
282         verify(autoLoaderImpl);
283         verify(systemState);
284     }
285 }
286
Popular Tags