Mercurial > hg > release > thermostat-0.13
view launcher/src/test/java/com/redhat/thermostat/launcher/internal/BundleManagerImplTest.java @ 1184:d448210350bf
Make thermostat work with a read-only $THERMOSTAT_HOME
Revewied-by: jerboaa, neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007592.html
PR1333
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Fri, 26 Jul 2013 12:29:07 -0400 |
parents | cb2842943f60 |
children |
line wrap: on
line source
/* * Copyright 2012, 2013 Red Hat, Inc. * * This file is part of Thermostat. * * Thermostat is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2, or (at your * option) any later version. * * Thermostat is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Thermostat; see the file COPYING. If not see * <http://www.gnu.org/licenses/>. * * Linking this code with other modules is making a combined work * based on this code. Thus, the terms and conditions of the GNU * General Public License cover the whole combination. * * As a special exception, the copyright holders of this code give * you permission to link this code with independent modules to * produce an executable, regardless of the license terms of these * independent modules, and to copy and distribute the resulting * executable under terms of your choice, provided that you also * meet, for each linked independent module, the terms and conditions * of the license of that module. An independent module is a module * which is not derived from or based on this code. If you modify * this code, you may extend this exception to your version of the * library, but you are not obligated to do so. If you do not wish * to do so, delete this exception statement from your version. */ package com.redhat.thermostat.launcher.internal; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.whenNew; import java.io.IOException; import java.lang.reflect.Method; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.launch.Framework; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import com.redhat.thermostat.shared.config.Configuration; @RunWith(PowerMockRunner.class) @PrepareForTest({BundleManagerImpl.class, FrameworkUtil.class}) public class BundleManagerImplTest { private static final String jar1Name = "/one.jar"; private static final String jar2Name = "/two.jar"; private static final String jar3Name = "/three.jar"; private Bundle b1, b2, b3; private List<String> bundleLocs; private Framework theFramework; private BundleContext theContext; private BundleLoader loader; private Configuration conf; private Path testRoot; @Before public void setUp() throws Exception { testRoot = Files.createTempDirectory("thermostat"); Path pluginRootDir = testRoot.resolve("plugins"); Files.createDirectory(pluginRootDir); Path jarRootDir = testRoot.resolve("libs"); Files.createDirectories(jarRootDir); conf = mock(Configuration.class); when(conf.getSystemLibRoot()).thenReturn(jarRootDir.toFile()); when(conf.getSystemPluginRoot()).thenReturn(pluginRootDir.toFile()); theContext = mock(BundleContext.class); theFramework = mock(Framework.class); when(theFramework.getBundleContext()).thenReturn(theContext); when(theContext.getBundle(0)).thenReturn(theFramework); bundleLocs = Arrays.asList(jar1Name, jar2Name, jar3Name); b1 = mock(Bundle.class); when(b1.getLocation()).thenReturn(jar1Name); when(b1.getState()).thenReturn(Bundle.ACTIVE); b2 = mock(Bundle.class); when(b2.getLocation()).thenReturn(jar2Name); when(b2.getState()).thenReturn(Bundle.ACTIVE); b3 = mock(Bundle.class); when(b3.getLocation()).thenReturn(jar3Name); when(b3.getState()).thenReturn(Bundle.ACTIVE); List<Bundle> installed = Arrays.asList(b1, b2, b3); loader = mock(BundleLoader.class); when(loader.installAndStartBundles(any(Framework.class), eq(bundleLocs))). thenReturn(installed); whenNew(BundleLoader.class).withParameterTypes(Boolean.TYPE). withArguments(any()).thenReturn(loader); } @After public void tearDown() throws IOException { Files.walkFileTree(testRoot, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { throw exc; } } }); } @Test public void testInstallAndStartBundles() throws Exception { Bundle theBundle = b2; when(theContext.getBundles()).thenReturn(new Bundle[] {}); when(theBundle.getBundleContext()).thenReturn(theContext); mockStatic(FrameworkUtil.class); when(FrameworkUtil.getBundle(any(Class.class))).thenReturn(theBundle); BundleManagerImpl registry = new BundleManagerImpl(conf); registry.loadBundlesByPath(bundleLocs); verify(loader).installAndStartBundles(any(Framework.class), eq(bundleLocs)); } @Test public void verifyAlreadyLoadedBundlesNotReloaded() throws Exception { Bundle theBundle = b2; when(theContext.getBundles()).thenReturn(new Bundle[] {b1, b2}); when(theContext.getBundle(jar1Name)).thenReturn(b1); when(theContext.getBundle(jar2Name)).thenReturn(b2); when(theBundle.getBundleContext()).thenReturn(theContext); mockStatic(FrameworkUtil.class); when(FrameworkUtil.getBundle(any(Class.class))).thenReturn(theBundle); BundleManagerImpl registry = new BundleManagerImpl(conf); registry.loadBundlesByPath(bundleLocs); verify(loader).installAndStartBundles(any(Framework.class), eq(Arrays.asList(jar3Name))); } @Test public void verifySetOSGiVerbosityByReflection() throws Exception { // All this fluff is just so constructor doesn't NPE. Bundle theBundle = b2; BundleContext theContext = mock(BundleContext.class); when(theContext.getBundles()).thenReturn(new Bundle[]{}); Framework theFramework = mock(Framework.class); when(theFramework.getBundleContext()).thenReturn(theContext); when(theContext.getBundle(0)).thenReturn(theFramework); when(theBundle.getBundleContext()).thenReturn(theContext); mockStatic(FrameworkUtil.class); when(FrameworkUtil.getBundle(any(Class.class))).thenReturn(theBundle); Object registry = new BundleManagerImpl(conf); Class<?> clazz = registry.getClass(); Method m = clazz.getMethod("setPrintOSGiInfo", Boolean.TYPE); m.invoke(registry, true); // If this fails, then API has changed in ways that break FrameworkProvider. } }