Mercurial > hg > release > thermostat-1.6
changeset 1752:ec2678a35b0f
Refactor SetupCommand so as to use DependencyServices.
Reviewed-by: amukunda
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-August/015420.html
PR2581
line wrap: on
line diff
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/SetupCommand.java Wed Sep 02 14:37:13 2015 +0200 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/SetupCommand.java Thu Aug 20 11:58:36 2015 +0200 @@ -36,46 +36,42 @@ package com.redhat.thermostat.setup.command; +import java.awt.EventQueue; +import java.lang.reflect.InvocationTargetException; + import com.redhat.thermostat.common.cli.AbstractCommand; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; +import com.redhat.thermostat.common.cli.Console; +import com.redhat.thermostat.common.cli.DependencyServices; import com.redhat.thermostat.internal.utils.laf.ThemeManager; +import com.redhat.thermostat.launcher.Launcher; import com.redhat.thermostat.setup.command.internal.SetupWindow; import com.redhat.thermostat.setup.command.internal.ThermostatSetup; import com.redhat.thermostat.setup.command.internal.ThermostatSetupImpl; import com.redhat.thermostat.shared.config.CommonPaths; import com.redhat.thermostat.shared.locale.LocalizedString; -import org.osgi.framework.BundleContext; - -import java.awt.EventQueue; -import java.io.PrintStream; -import java.lang.reflect.InvocationTargetException; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; public class SetupCommand extends AbstractCommand { + + private final DependencyServices dependentServices = new DependencyServices(); private SetupWindow mainWindow; - private CommonPaths paths; - private BundleContext context; - private CountDownLatch pathsAvailable; + private Launcher launcher; private ThermostatSetup thermostatSetup; - private PrintStream out; - - public SetupCommand(BundleContext context) { - this.context = context; - pathsAvailable = new CountDownLatch(1); - } + private Console console; @Override public void run(CommandContext ctx) throws CommandException { - out = ctx.getConsole().getOutput(); + this.console = ctx.getConsole(); try { setLookAndFeel(); - pathsAvailable.await(1000l, TimeUnit.MILLISECONDS); + this.paths = dependentServices.getService(CommonPaths.class); requireNonNull(paths, new LocalizedString("CommonPaths dependency not available")); + this.launcher = dependentServices.getService(Launcher.class); + requireNonNull(launcher, new LocalizedString("Launcher dependency not available")); createMainWindowAndRun(); } catch (InterruptedException | InvocationTargetException e) { @@ -84,8 +80,16 @@ } public void setPaths(CommonPaths paths) { - this.paths = paths; - pathsAvailable.countDown(); + dependentServices.addService(CommonPaths.class, paths); + } + + public void setLauncher(Launcher launcher) { + dependentServices.addService(Launcher.class, launcher); + } + + public void setServicesUnavailable() { + dependentServices.removeService(Launcher.class); + dependentServices.removeService(CommonPaths.class); } public boolean isStorageRequired() { @@ -94,8 +98,8 @@ //package-private for testing void createMainWindowAndRun() { - thermostatSetup = new ThermostatSetupImpl(context, paths, out); - mainWindow = new SetupWindow(out, thermostatSetup); + thermostatSetup = new ThermostatSetupImpl(launcher, paths, console); + mainWindow = new SetupWindow(thermostatSetup); mainWindow.run(); }
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/Activator.java Wed Sep 02 14:37:13 2015 +0200 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/Activator.java Thu Aug 20 11:58:36 2015 +0200 @@ -41,9 +41,12 @@ import com.redhat.thermostat.common.MultipleServiceTracker; import com.redhat.thermostat.common.cli.CommandRegistry; import com.redhat.thermostat.common.cli.CommandRegistryImpl; +import com.redhat.thermostat.launcher.Launcher; import com.redhat.thermostat.shared.config.CommonPaths; + import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; + import com.redhat.thermostat.setup.command.SetupCommand; public class Activator implements BundleActivator { @@ -55,21 +58,25 @@ @Override public void start(final BundleContext context) throws Exception { reg = new CommandRegistryImpl(context); - setupCommand = new SetupCommand(context); + setupCommand = new SetupCommand(); reg.registerCommand("setup", setupCommand); Class<?>[] deps = new Class<?>[] { - CommonPaths.class + CommonPaths.class, + Launcher.class }; tracker = new MultipleServiceTracker(context, deps, new MultipleServiceTracker.Action() { @Override public void dependenciesAvailable(Map<String, Object> services) { CommonPaths paths = (CommonPaths) services.get(CommonPaths.class.getName()); + Launcher launcher = (Launcher) services.get(Launcher.class.getName()); setupCommand.setPaths(paths); + setupCommand.setLauncher(launcher); } @Override public void dependenciesUnavailable() { + setupCommand.setServicesUnavailable(); reg.unregisterCommands(); } }); @@ -80,5 +87,6 @@ public void stop(BundleContext context) throws Exception { reg.unregisterCommands(); tracker.close(); + setupCommand = null; } } \ No newline at end of file
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupWindow.java Wed Sep 02 14:37:13 2015 +0200 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupWindow.java Thu Aug 20 11:58:36 2015 +0200 @@ -36,14 +36,6 @@ package com.redhat.thermostat.setup.command.internal; -import com.redhat.thermostat.setup.command.locale.LocaleResources; -import com.redhat.thermostat.shared.locale.Translate; - -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.SwingUtilities; -import javax.swing.SwingWorker; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridBagConstraints; @@ -53,9 +45,17 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; -import java.io.PrintStream; +import java.util.Arrays; import java.util.concurrent.CountDownLatch; -import java.util.Arrays; + +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; + +import com.redhat.thermostat.setup.command.locale.LocaleResources; +import com.redhat.thermostat.shared.locale.Translate; public class SetupWindow { private CountDownLatch shutdown; @@ -79,10 +79,7 @@ private static final String DEFAULT_STORAGE_PASSWORD = "mongodevpassword"; private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer(); - private PrintStream out; - - public SetupWindow(PrintStream out, ThermostatSetup thermostatSetup) { - this.out = out; + public SetupWindow(ThermostatSetup thermostatSetup) { this.thermostatSetup = thermostatSetup; shutdown = new CountDownLatch(1); }
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImpl.java Wed Sep 02 14:37:13 2015 +0200 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImpl.java Thu Aug 20 11:58:36 2015 +0200 @@ -36,22 +36,10 @@ package com.redhat.thermostat.setup.command.internal; -import com.redhat.thermostat.common.ActionEvent; -import com.redhat.thermostat.common.ActionListener; -import com.redhat.thermostat.common.cli.AbstractStateNotifyingCommand; -import com.redhat.thermostat.common.tools.ApplicationState; -import com.redhat.thermostat.setup.command.locale.LocaleResources; -import com.redhat.thermostat.shared.config.CommonPaths; -import com.redhat.thermostat.shared.locale.Translate; -import com.redhat.thermostat.launcher.Launcher; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; - import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.io.PrintStream; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; @@ -61,13 +49,23 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.EnumSet; import java.util.List; -import java.util.Arrays; import java.util.Properties; import java.util.Set; +import com.redhat.thermostat.common.ActionEvent; +import com.redhat.thermostat.common.ActionListener; +import com.redhat.thermostat.common.cli.AbstractStateNotifyingCommand; +import com.redhat.thermostat.common.cli.Console; +import com.redhat.thermostat.common.tools.ApplicationState; +import com.redhat.thermostat.launcher.Launcher; +import com.redhat.thermostat.setup.command.locale.LocaleResources; +import com.redhat.thermostat.shared.config.CommonPaths; +import com.redhat.thermostat.shared.locale.Translate; + public class ThermostatSetupImpl implements ThermostatSetup { private static final String WEB_AUTH_FILE = "web.auth"; @@ -92,31 +90,22 @@ private String setupUnlockContentRegular; private String userDoneFile; private String createUserScript; - private PrintStream out; private CredentialFinder finder; private File setupCompleteFile; private File agentAuthFile; private Launcher launcher; private Properties roleProps; - public ThermostatSetupImpl(BundleContext context, CommonPaths paths, PrintStream out) { - this.out = out; - finder = new CredentialFinder(paths); - - ServiceReference launcherRef = context.getServiceReference(Launcher.class); - launcher = (Launcher) context.getService(launcherRef); - - listeners = new ArrayList<>(); - listeners.add(new StorageListener(out)); - setThermostatVars(paths); + public ThermostatSetupImpl(Launcher launcher, CommonPaths paths, Console console) { + this(launcher, paths, console, new CredentialFinder(paths)); } //package-private constructor for testing - ThermostatSetupImpl(Launcher launcher, CommonPaths paths, PrintStream out, CredentialFinder finder) { + ThermostatSetupImpl(Launcher launcher, CommonPaths paths, Console console, CredentialFinder finder) { this.launcher = launcher; this.finder = finder; listeners = new ArrayList<>(); - listeners.add(new StorageListener(out)); + listeners.add(new StorageListener(console)); setThermostatVars(paths); } @@ -385,10 +374,10 @@ private static class StorageListener implements ActionListener<ApplicationState> { - private final PrintStream out; + private final Console console; - private StorageListener(PrintStream out) { - this.out = out; + private StorageListener(Console console) { + this.console = console; } @Override @@ -405,7 +394,7 @@ storageFailed = false; break; case FAIL: - out.println(translator.localize(LocaleResources.STORAGE_FAILED).getContents()); + console.getOutput().println(translator.localize(LocaleResources.STORAGE_FAILED).getContents()); storageFailed = true; break; }
--- a/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/SetupCommandTest.java Wed Sep 02 14:37:13 2015 +0200 +++ b/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/SetupCommandTest.java Thu Aug 20 11:58:36 2015 +0200 @@ -47,20 +47,19 @@ import java.util.ArrayList; import java.util.List; -import com.redhat.thermostat.common.cli.CommandException; -import com.redhat.thermostat.shared.config.CommonPaths; import org.junit.Before; +import org.junit.Test; import com.redhat.thermostat.common.cli.Arguments; import com.redhat.thermostat.common.cli.CommandContext; +import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.cli.Console; -import org.junit.Test; -import org.osgi.framework.BundleContext; +import com.redhat.thermostat.launcher.Launcher; +import com.redhat.thermostat.shared.config.CommonPaths; public class SetupCommandTest { private SetupCommand cmd; - private BundleContext context; private CommandContext ctxt; private Arguments mockArgs; private Console console; @@ -68,11 +67,11 @@ private ByteArrayOutputStream outputBaos, errorBaos; private PrintStream output, error; private CommonPaths paths; + private Launcher launcher; @Before public void setUp() { paths = mock(CommonPaths.class); - context = mock(BundleContext.class); ctxt = mock(CommandContext.class); mockArgs = mock(Arguments.class); console = mock(Console.class); @@ -82,6 +81,7 @@ errorBaos = new ByteArrayOutputStream(); error = new PrintStream(errorBaos); + launcher = mock(Launcher.class); when(ctxt.getArguments()).thenReturn(mockArgs); when(ctxt.getConsole()).thenReturn(console); @@ -93,7 +93,7 @@ @Test public void testLookAndFeelIsSet() throws CommandException { final boolean[] isSet = {false}; - cmd = new SetupCommand(context) { + cmd = new SetupCommand() { @Override void setLookAndFeel() throws InvocationTargetException, InterruptedException { isSet[0] = true; @@ -105,7 +105,7 @@ } }; - cmd.setPaths(paths); + setServices(); cmd.run(ctxt); assertTrue(isSet[0]); @@ -114,7 +114,7 @@ @Test public void testCreateMainWindowIsCalled() throws CommandException { final boolean[] isSet = {false}; - cmd = new SetupCommand(context) { + cmd = new SetupCommand() { @Override void setLookAndFeel() throws InvocationTargetException, InterruptedException { //do nothing @@ -126,15 +126,40 @@ } }; - cmd.setPaths(paths); + setServices(); cmd.run(ctxt); assertTrue(isSet[0]); } + @Test public void testPathsNotSetFailure() { - cmd = new SetupCommand(context) { + cmd = createSetupCommand(); + + try { + cmd.run(ctxt); + fail(); + } catch (CommandException e) { + assertTrue(e.getMessage().contains("CommonPaths dependency not available")); + } + } + + @Test + public void testLauncherNotSetFailure() { + cmd = createSetupCommand(); + + cmd.setPaths(mock(CommonPaths.class)); + try { + cmd.run(ctxt); + fail(); + } catch (CommandException e) { + assertTrue(e.getMessage().contains("Launcher dependency not available")); + } + } + + private SetupCommand createSetupCommand() { + return new SetupCommand() { @Override void setLookAndFeel() throws InvocationTargetException, InterruptedException { //do nothing @@ -145,13 +170,11 @@ //do nothing } }; - - try { - cmd.run(ctxt); - fail(); - } catch (CommandException e) { - assertTrue(e.getMessage().contains("CommonPaths dependency not available")); - } + } + + private void setServices() { + cmd.setPaths(paths); + cmd.setLauncher(launcher); } }
--- a/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/ActivatorTest.java Wed Sep 02 14:37:13 2015 +0200 +++ b/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/ActivatorTest.java Thu Aug 20 11:58:36 2015 +0200 @@ -40,7 +40,9 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.mock; +import com.redhat.thermostat.launcher.Launcher; import com.redhat.thermostat.shared.config.CommonPaths; + import org.junit.Test; import com.redhat.thermostat.testutils.StubBundleContext; @@ -53,7 +55,9 @@ StubBundleContext ctx = new StubBundleContext(); CommonPaths paths = mock(CommonPaths.class); + Launcher launcher = mock(Launcher.class); ctx.registerService(CommonPaths.class, paths, null); + ctx.registerService(Launcher.class, launcher, null); Activator activator = new Activator(); @@ -63,7 +67,7 @@ activator.stop(ctx); - assertEquals(1, ctx.getAllServices().size()); + assertEquals(2, ctx.getAllServices().size()); } }
--- a/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImplTest.java Wed Sep 02 14:37:13 2015 +0200 +++ b/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImplTest.java Thu Aug 20 11:58:36 2015 +0200 @@ -40,6 +40,7 @@ import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.ActionNotifier; import com.redhat.thermostat.common.cli.AbstractStateNotifyingCommand; +import com.redhat.thermostat.common.cli.Console; import com.redhat.thermostat.common.tools.ApplicationState; import com.redhat.thermostat.launcher.Launcher; import com.redhat.thermostat.shared.config.CommonPaths; @@ -102,7 +103,8 @@ private ThermostatSetupImpl tSetup; private CommonPaths paths; private Launcher mockLauncher; - private OutputStream out; + private ByteArrayOutputStream out; + private Console console; private static ActionEvent<ApplicationState> mockActionEvent; private static Collection<ActionListener<ApplicationState>> listeners; private CredentialFinder mockCredentialFinder; @@ -140,6 +142,8 @@ makeTempFilesAndDirectories(); out = new ByteArrayOutputStream(); + console = mock(Console.class); + when(console.getOutput()).thenReturn(new PrintStream(out)); paths = mock(CommonPaths.class); when(paths.getSystemThermostatHome()).thenReturn(thermostatSysHome.toFile()); @@ -161,7 +165,7 @@ when(mockCredentialFinder.getConfiguration(USERS_PROPERTIES)).thenReturn(userPropertiesFile.toFile()); when(mockCredentialFinder.getConfiguration(ROLES_PROPERTIES)).thenReturn(rolesPropertiesFile.toFile()); - tSetup = new ThermostatSetupImpl(mockLauncher, paths, new PrintStream(out), mockCredentialFinder) { + tSetup = new ThermostatSetupImpl(mockLauncher, paths, console, mockCredentialFinder) { @Override int runMongo() { //instead of running mongo through ProcessBuilder @@ -328,7 +332,7 @@ @Test public void testCreateMongodbUserFail() { - tSetup = new ThermostatSetupImpl(mockLauncher, paths, new PrintStream(out), mockCredentialFinder) { + tSetup = new ThermostatSetupImpl(mockLauncher, paths, console, mockCredentialFinder) { @Override int runMongo() { //return non-zero val to test failure