# HG changeset patch # User Omair Majid # Date 1363383818 14400 # Node ID 29e79f1daae2182fa4208756eebdb1368b6c2b1e # Parent b269be10beb4c923a17ab1e1d3c79114520a0539 Remove powermock from launcher tests Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-March/006038.html diff -r b269be10beb4 -r 29e79f1daae2 launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java --- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java Fri Mar 15 14:01:22 2013 -0400 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java Fri Mar 15 17:43:38 2013 -0400 @@ -89,21 +89,22 @@ private int usageCount = 0; private final Semaphore argsBarrier = new Semaphore(0); - private BundleContext context; - private BundleManager registry; + private final BundleContext context; + private final BundleManager registry; private final DbServiceFactory dbServiceFactory; + private final Version coreVersion; public LauncherImpl(BundleContext context, CommandContextFactory cmdCtxFactory, BundleManager registry) { - this(context, cmdCtxFactory, registry, new LoggingInitializer(), new DbServiceFactory()); + this(context, cmdCtxFactory, registry, new LoggingInitializer(), new DbServiceFactory(), new Version()); } LauncherImpl(BundleContext context, CommandContextFactory cmdCtxFactory, BundleManager registry, - LoggingInitializer loggingInitializer, DbServiceFactory dbServiceFactory) { + LoggingInitializer loggingInitializer, DbServiceFactory dbServiceFactory, Version version) { this.context = context; this.cmdCtxFactory = cmdCtxFactory; this.registry = registry; this.dbServiceFactory = dbServiceFactory; - + this.coreVersion = version; loggingInitializer.initialize(); } @@ -124,7 +125,6 @@ } else if (isVersionQuery(inShell)) { // We want to print the version of core // thermostat, so we use the no-arg constructor of Version - Version coreVersion = new Version(); cmdCtxFactory.getConsole().getOutput().println(coreVersion.getVersionInfo()); } else { runCommandFromArguments(listeners, inShell); diff -r b269be10beb4 -r 29e79f1daae2 launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java --- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java Fri Mar 15 14:01:22 2013 -0400 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java Fri Mar 15 17:43:38 2013 -0400 @@ -48,7 +48,6 @@ import java.io.IOException; import java.security.Permission; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -58,27 +57,18 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.common.ApplicationInfo; import com.redhat.thermostat.common.ApplicationService; import com.redhat.thermostat.common.Constants; import com.redhat.thermostat.common.Version; @@ -91,8 +81,6 @@ import com.redhat.thermostat.common.cli.CommandInfoNotFoundException; import com.redhat.thermostat.common.cli.CommandInfoSource; import com.redhat.thermostat.common.config.ClientPreferences; -import com.redhat.thermostat.common.locale.LocaleResources; -import com.redhat.thermostat.common.locale.Translate; import com.redhat.thermostat.common.tools.ApplicationState; import com.redhat.thermostat.launcher.BundleManager; import com.redhat.thermostat.launcher.TestCommand; @@ -102,15 +90,12 @@ import com.redhat.thermostat.launcher.internal.LauncherImpl.LoggingInitializer; import com.redhat.thermostat.storage.core.DbService; import com.redhat.thermostat.storage.core.DbServiceFactory; -import com.redhat.thermostat.storage.core.Storage; import com.redhat.thermostat.test.TestCommandContextFactory; import com.redhat.thermostat.test.TestTimerFactory; import com.redhat.thermostat.testutils.StubBundleContext; import com.redhat.thermostat.utils.keyring.Keyring; import com.redhat.thermostat.utils.keyring.KeyringProvider; -@RunWith(PowerMockRunner.class) -@PrepareForTest({FrameworkUtil.class, HelpCommand.class}) public class LauncherImplTest { private static String defaultKeyringProvider; @@ -160,13 +145,13 @@ private Bundle sysBundle; private TestTimerFactory timerFactory; private BundleManager registry; + private Version version; private LoggingInitializer loggingInitializer; private DbServiceFactory dbServiceFactory; private CommandInfoSource infos; private ActionNotifier notifier; private LauncherImpl launcher; - private Storage storage; @Before public void setUp() throws CommandInfoNotFoundException, BundleException, IOException { @@ -254,7 +239,7 @@ helpCommand.setCommandInfoSource(infos); registry = mock(BundleManager.class); - doAnswer(new Answer() { + doAnswer(new Answer() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { // simulate the real BundleManager which tries to find a CommandInfo @@ -264,17 +249,6 @@ } }).when(registry).addBundlesFor(anyString()); - PowerMockito.mockStatic(FrameworkUtil.class); - - storage = mock(Storage.class); - ServiceReference storageRef = mock(ServiceReference.class); - Bundle launcherBundle = mock(Bundle.class); - BundleContext launcherBundleCtx = mock(BundleContext.class); - when(launcherBundleCtx.getServiceReference(Storage.class)).thenReturn(storageRef); - when(launcherBundleCtx.getService(storageRef)).thenReturn(storage); - when(launcherBundle.getBundleContext()).thenReturn(launcherBundleCtx); - when(FrameworkUtil.getBundle(LauncherImpl.class)).thenReturn(launcherBundle); - timerFactory = new TestTimerFactory(); ExecutorService exec = mock(ExecutorService.class); ApplicationService appSvc = mock(ApplicationService.class); @@ -284,8 +258,9 @@ loggingInitializer = mock(LoggingInitializer.class); dbServiceFactory = mock(DbServiceFactory.class); + version = mock(Version.class); - launcher = new LauncherImpl(bundleContext, ctxFactory, registry, loggingInitializer, dbServiceFactory); + launcher = new LauncherImpl(bundleContext, ctxFactory, registry, loggingInitializer, dbServiceFactory, version); Keyring keyring = mock(Keyring.class); launcher.setPreferences(new ClientPreferences(keyring)); @@ -298,13 +273,6 @@ ctxFactory = new TestCommandContextFactory(bundleContext); } - - @After - public void tearDown() { - ctxFactory = null; - bundleContext = null; - } - @Test public void testMain() { runAndVerifyCommand(new String[] {name1, "--arg1", "Hello", "--arg2", "World"}, "Hello, World", false); @@ -515,29 +483,12 @@ @Test public void verifyVersionInfoQuery() { - int major = 0; - int minor = 3; - int micro = 0; - - ApplicationInfo appInfo = new ApplicationInfo(); - Translate t = LocaleResources.createLocalizer(); - String format = MessageFormat.format( - t.localize(LocaleResources.APPLICATION_VERSION_INFO), - appInfo.getName()) - + " " + Version.VERSION_NUMBER_FORMAT; - - String expectedVersionInfo = String.format(format, - major, minor, micro) + "\n"; - - String qualifier = "201207241700"; + String versionString = "foo bar baz"; - org.osgi.framework.Version ver = org.osgi.framework.Version - .parseVersion(String.format(Version.VERSION_NUMBER_FORMAT, - major, minor, micro) + "." + qualifier); - when(sysBundle.getVersion()).thenReturn(ver); - - PowerMockito.mockStatic(FrameworkUtil.class); - when(FrameworkUtil.getBundle(Version.class)).thenReturn(sysBundle); + String expectedVersionInfo = versionString + "\n"; + + when(version.getVersionInfo()).thenReturn(versionString); + launcher.setArgs(new String[] {Version.VERSION_OPTION}); wrappedRun(launcher, false);