changeset 1823:5f6dea7aad8f

Add tab completion for dbUrl option Reviewed-by: omajid, aazores Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-September/016483.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-July/014734.html PR2646
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 24 Sep 2015 18:23:53 +0200
parents 770c2f56188a
children 96c0923a42e3
files launcher/src/main/java/com/redhat/thermostat/launcher/internal/DbUrlCompleter.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/TabCompletion.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/DbUrlCompleterTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/ShellCommandTest.java
diffstat 5 files changed, 169 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/DbUrlCompleter.java	Thu Sep 24 18:23:53 2015 +0200
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2012-2015 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 java.util.List;
+
+import com.redhat.thermostat.common.config.ClientPreferences;
+import jline.console.completer.Completer;
+import jline.console.completer.StringsCompleter;
+
+public class DbUrlCompleter implements Completer {
+
+    private final ClientPreferences prefs;
+
+    public DbUrlCompleter(ClientPreferences preferences) {
+        this.prefs = preferences;
+    }
+
+    @Override
+    public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
+        String dbUrl = "";
+        if (prefs.getConnectionUrl() != null) {
+            dbUrl = prefs.getConnectionUrl();
+        }
+        StringsCompleter stringsCompleter = new StringsCompleter(dbUrl);
+        return stringsCompleter.complete(buffer, cursor, candidates);
+    }
+}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java	Tue Jul 07 15:43:43 2015 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java	Thu Sep 24 18:23:53 2015 +0200
@@ -42,7 +42,6 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import com.redhat.thermostat.storage.core.Storage;
 import jline.Terminal;
 import jline.TerminalFactory;
 import jline.console.ConsoleReader;
@@ -59,6 +58,7 @@
 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.config.ClientPreferences;
 import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.launcher.Launcher;
@@ -82,6 +82,7 @@
     private final ShellPrompt shellPrompt;
     private CommandInfoSource commandInfoSource;
     private final StorageState storageState = new StorageState();
+    private final ClientPreferences prefs;
 
     static class HistoryProvider {
 
@@ -103,14 +104,15 @@
     }
 
     public ShellCommand(BundleContext context, CommonPaths paths, ConfigurationInfoSource config) {
-        this(context, new Version(), new HistoryProvider(paths), config);
+        this(context, new Version(), new HistoryProvider(paths), config, new ClientPreferences(paths));
     }
 
-    ShellCommand(BundleContext context, Version version, HistoryProvider provider, ConfigurationInfoSource config) {
+    ShellCommand(BundleContext context, Version version, HistoryProvider provider, ConfigurationInfoSource config, ClientPreferences prefs) {
         this.historyProvider = provider;
         this.bundleContext = context;
         this.version = version;
 
+        this.prefs = prefs;
         this.shellPrompt = new ShellPrompt();
 
         try {
@@ -157,7 +159,7 @@
     private void shellMainLoop(CommandContext ctx, History history, Terminal term) throws IOException, CommandException {
         ConsoleReader reader = new ConsoleReader(ctx.getConsole().getInput(), ctx.getConsole().getOutput(), term);
         if (reader.getCompleters().isEmpty() && commandInfoSource != null) {
-            TabCompletion.setupTabCompletion(reader, commandInfoSource, bundleContext, storageState);
+            TabCompletion.setupTabCompletion(reader, commandInfoSource, bundleContext, storageState, prefs);
         }
         if (history != null) {
             reader.setHistory(history);
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/TabCompletion.java	Tue Jul 07 15:43:43 2015 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/TabCompletion.java	Thu Sep 24 18:23:53 2015 +0200
@@ -40,6 +40,7 @@
 import java.util.Collection;
 import java.util.List;
 
+import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 import jline.console.ConsoleReader;
 import jline.console.completer.Completer;
@@ -55,7 +56,7 @@
     private static final String LONG_OPTION_PREFIX = "--";
     private static final String SHORT_OPTION_PREFIX = "-";
 
-    public static void setupTabCompletion(ConsoleReader reader, CommandInfoSource commandInfoSource, BundleContext context, StorageState storageState) {
+    public static void setupTabCompletion(ConsoleReader reader, CommandInfoSource commandInfoSource, BundleContext context, StorageState storageState, ClientPreferences prefs) {
         List<String> logLevels = new ArrayList<>();
 
         for (LoggingUtils.LogLevel level : LoggingUtils.LogLevel.values()) {
@@ -78,6 +79,8 @@
                         setupCompletion(command, option, new IdCompleter(new VmIdsFinder(context), storageState));
                     } else if (option.getLongOpt().equals("agentId")) {
                         setupCompletion(command, option, new IdCompleter(new AgentIdsFinder(context), storageState));
+                    } else if (option.getLongOpt().equals("dbUrl")) {
+                        setupCompletion(command, option, new DbUrlCompleter(prefs));
                     } else {
                         setupDefaultCompletion(command, option);
                     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/DbUrlCompleterTest.java	Thu Sep 24 18:23:53 2015 +0200
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2012-2015 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 junit.framework.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import com.redhat.thermostat.common.config.ClientPreferences;
+import org.junit.Test;
+
+public class DbUrlCompleterTest {
+
+    @Test
+    public void testDbUrlCompleter() {
+        ClientPreferences prefs = mock(ClientPreferences.class);
+        DbUrlCompleter dbUrlCompleter = new DbUrlCompleter(prefs);
+
+        String partialUrl = "https://ip.addr";
+        String fullUrl = partialUrl + "ess.example.com:25813/thermostat/storage";
+        when(prefs.getConnectionUrl()).thenReturn(fullUrl);
+
+        List<CharSequence> candidates = new LinkedList<>();
+        dbUrlCompleter.complete(partialUrl, partialUrl.length(), candidates);
+
+        assertEquals(1, candidates.size());
+        assertEquals(fullUrl, candidates.get(0).toString().trim());
+    }
+}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ShellCommandTest.java	Tue Jul 07 15:43:43 2015 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ShellCommandTest.java	Thu Sep 24 18:23:53 2015 +0200
@@ -50,6 +50,7 @@
 import java.util.Collection;
 import java.util.EnumSet;
 
+import com.redhat.thermostat.common.config.ClientPreferences;
 import jline.TerminalFactory;
 import jline.TerminalFactory.Flavor;
 import jline.TerminalFactory.Type;
@@ -91,6 +92,7 @@
     private ConfigurationInfoSource config;
     private CommandInfoSource infos;
     private File dir;
+    private ClientPreferences prefs;
 
     @Before
     public void setUp() {
@@ -100,8 +102,10 @@
         when(version.getVersionInfo()).thenReturn(VERSION);
         config = mock(ConfigurationInfoSource.class);
 
+        prefs = mock(ClientPreferences.class);
+        when(prefs.getConnectionUrl()).thenReturn("http://127.0.0.1:mockStorage");
         infos = mock(CommandInfoSource.class);
-        cmd = new ShellCommand(bundleContext, version, historyProvider, config);
+        cmd = new ShellCommand(bundleContext, version, historyProvider, config, prefs);
         setupCommandInfoSource();
 
         dir = new File(System.getProperty("java.io.tmpdir") + File.separator + "shellcommand");
@@ -406,6 +410,27 @@
     }
 
     @Test
+    public void testDbUrlOptionCompletes() throws CommandException {
+        ServiceReference ref = mock(ServiceReference.class);
+        when(bundleContext.getServiceReference(Launcher.class.getName())).thenReturn(ref);
+        Launcher launcher = mock(Launcher.class);
+        when(bundleContext.getService(ref)).thenReturn(launcher);
+
+        TestCommandContextFactory ctxFactory = new TestCommandContextFactory(bundleContext);
+        ctxFactory.setInput("validate --dbUrl \t\nexit\n");
+        Arguments args = new SimpleArguments();
+        CommandContext ctx = ctxFactory.createContext(args);
+        cmd.run(ctx);
+
+        String usefulOutput = getOutputWithoutIntro(ctxFactory);
+        String tabOutput = getTabOutput(usefulOutput);
+        assertTrue(tabOutput.length() == 0);
+        assertEquals(PROMPT + "validate --dbUrl http://127.0.0.1:mockStorage ", usefulOutput.split("\n")[0]);
+        assertEquals(PROMPT + "exit", usefulOutput.split("\n")[1]);
+        assertEquals("", ctxFactory.getError());
+    }
+
+    @Test
     public void testFullOptionDoesNotTabComplete() throws CommandException {
         ServiceReference ref = mock(ServiceReference.class);
         when(bundleContext.getServiceReference(Launcher.class.getName())).thenReturn(ref);
@@ -577,7 +602,7 @@
         createTempFile(filename + "12345678");
 
         TestCommandContextFactory ctxFactory = new TestCommandContextFactory(bundleContext);
-        ctxFactory.setInput("validate --dbUrl -d " + dir.getAbsolutePath() + File.separator + "testFil\t\nexit\n");
+        ctxFactory.setInput("validate --fake-option -f " + dir.getAbsolutePath() + File.separator + "testFil\t\nexit\n");
         Arguments args = new SimpleArguments();
         CommandContext ctx = ctxFactory.createContext(args);
         cmd.run(ctx);
@@ -809,9 +834,13 @@
         Option option9 = mock(Option.class);
         when(option9.getLongOpt()).thenReturn("agent");
         when(option9.getOpt()).thenReturn("a");
+        Option option10 = mock(Option.class);
+        when(option10.getLongOpt()).thenReturn("fake-option");
+        when(option10.getOpt()).thenReturn("f");
         optionsList3.add(option7);
         optionsList3.add(option8);
         optionsList3.add(option9);
+        optionsList3.add(option10);
 
         Options options3 = mock(Options.class);
         when(info3.getOptions()).thenReturn(options3);