1 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 ; 30 import java.io.IOException ; 31 import java.lang.reflect.Method ; 32 import java.net.URI ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 import javax.management.InstanceNotFoundException ; 37 import javax.management.MBeanException ; 38 import javax.management.MalformedObjectNameException ; 39 import javax.management.ObjectName ; 40 import javax.management.ReflectionException ; 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 64 public class AutoLoaderImplTest extends TestCase { 65 66 private AutoLoaderImpl autoLoaderImpl; 67 68 private String 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 (baseDir + "target") 75 .getAbsolutePath()); 76 new File (baseDir + "target" + File.separator + "lib").mkdir(); 77 new File (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 { 83 autoLoaderImpl.logger = Monolog.initialize().getLogger("test"); 84 autoLoaderImpl.start(); 85 assertEquals(AutoLoaderImpl.getInstallDirectory(), new File (baseDir 86 + "target" + File.separator + "install")); 87 assertEquals(AutoLoaderImpl.getInstalledDirectory(), new File (baseDir 88 + "target" + File.separator + "installed")); 89 assertEquals(AutoLoaderImpl.getWorkDirectory(), new File (baseDir 90 + "target" + File.separator + "work")); 91 assertEquals(AutoLoaderImpl.getUninstalledDirectory(), new File (baseDir 92 + "target" + File.separator + "uninstalled")); 93 94 autoLoaderImpl.log = new LoggingUtil(autoLoaderImpl.logger); 96 assertNotNull(autoLoaderImpl.installDirectoryScanTimer); 97 assertNotNull(autoLoaderImpl.installedDirectoryScanTimer); 98 } 99 100 public void testGetInstallDirectory () { 101 104 File install = new File (""); 105 AutoLoaderImpl.installDirectory = install; 106 109 assertEquals("Wrong invokation result",AutoLoaderImpl.getInstallDirectory(),install); 110 } 111 112 public void testGetInstalledDirectory () { 113 116 File installed = new File (""); 117 AutoLoaderImpl.installedDirectory = installed; 118 121 assertEquals("Wrong invokation result",AutoLoaderImpl.getInstallDirectory(),installed); 122 } 123 124 public void testGetUninstalDirectory () { 125 128 File uninstalled = new File (""); 129 AutoLoaderImpl.uninstalledDirectory = uninstalled; 130 133 assertEquals("Wrong invokation result",AutoLoaderImpl.getUninstalledDirectory(),uninstalled); 134 } 135 136 public void testGetWorkDirectory () { 137 140 File work = new File (""); 141 AutoLoaderImpl.workDirectory = work; 142 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 , PetalsException, 153 MalformedObjectNameException , NullPointerException , 154 InstanceNotFoundException , MBeanException , ReflectionException { 155 FileUtils.copyFileToDirectory(new File (baseDir + "src" + File.separator 156 + "test-data" + File.separator + "autoload" + File.separator 157 + "toInstall.zip"), new File (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 (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 (baseDir + "target" 191 + File.separator + "work" + File.separator + "toInstall.zip") 192 .toURI().toString())).andReturn( 193 new ObjectName ("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 [] {new File (baseDir + "target" 201 + File.separator + "toInstall.zip")}); 202 203 assertTrue(server.isInvokeStart()); 204 } 205 206 public void testUninstall() { 207 210 213 Method method1 = null; 214 Method method2 = null; 215 Method method3 = null; 216 try { 217 method1 = AutoLoaderImpl.class.getDeclaredMethod("performCompUninstall", new Class []{String .class}); 218 method2 = AutoLoaderImpl.class.getDeclaredMethod("performSAUninstall", new Class []{String .class}); 219 method3 = AutoLoaderImpl.class.getDeclaredMethod("performSLUninstall", new Class []{String .class}); 220 } catch (Exception e) { 221 e.printStackTrace(); 222 fail("Could not retrieve mocked methods"); 223 } 224 autoLoaderImpl = createMock(AutoLoaderImpl.class,new Method []{method1, method2, method3}); 225 228 List <ComponentState> compStates = new ArrayList <ComponentState>(); 229 ComponentState compState = createMock(ComponentState.class); 230 compStates.add(compState); 231 List <SharedLibraryState> slStates = new ArrayList <SharedLibraryState>(); 232 SharedLibraryState slState = createMock(SharedLibraryState.class); 233 slStates.add(slState); 234 List <ServiceAssemblyState> saStates = new ArrayList <ServiceAssemblyState>(); 235 ServiceAssemblyState saState = createMock(ServiceAssemblyState.class); 236 saStates.add(saState); 237 String compName = "component Name"; 238 String slName = " shared library name"; 239 String saName = "service assembly name"; 240 String url = "myUrl"; 241 File file = new File (""); 242 URI uri = file.toURI(); 243 file = new File (uri); 244 PackageHandler packageHandler = createMock(PackageHandler.class); 245 List <File > fileToUninstall = new ArrayList <File >(); 246 fileToUninstall.add(file); 247 LoggingUtil log = createMock(LoggingUtil.class); 248 SystemState systemState = createMock(SystemState.class); 249 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 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 |