Mercurial > hg > thermostat-ng > agent
changeset 2658:4f1f13f48d0e
Remove completers
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023215.html
reviewed-by: ebaron, jerboaa
author | Mario Torre <neugens.limasoftware@gmail.com> |
---|---|
date | Wed, 24 May 2017 12:13:27 +0200 |
parents | 209c6fbeab9c |
children | fa39903816d6 |
files | common/core/src/main/java/com/redhat/thermostat/common/cli/AbstractCompleterCommand.java common/core/src/main/java/com/redhat/thermostat/common/cli/AbstractCompleterService.java common/core/src/main/java/com/redhat/thermostat/common/cli/AbstractCompletionFinder.java common/core/src/main/java/com/redhat/thermostat/common/cli/CompleterService.java common/core/src/main/java/com/redhat/thermostat/common/cli/CompletionFinder.java common/core/src/main/java/com/redhat/thermostat/common/cli/CompletionFinderTabCompleter.java common/core/src/main/java/com/redhat/thermostat/common/cli/CompletionInfo.java common/core/src/main/java/com/redhat/thermostat/common/cli/DirectoryContentsCompletionFinder.java common/core/src/main/java/com/redhat/thermostat/common/cli/FileNameTabCompleter.java common/core/src/main/java/com/redhat/thermostat/common/cli/StringsTabCompleter.java common/core/src/main/java/com/redhat/thermostat/common/cli/TabCompleter.java common/core/src/main/java/com/redhat/thermostat/common/internal/BaseCompletionFinder.java common/core/src/main/java/com/redhat/thermostat/common/internal/PredefinedCompletionFinder.java common/core/src/main/java/com/redhat/thermostat/common/internal/StringsListCompletionFinder.java common/core/src/test/java/com/redhat/thermostat/common/cli/AbstractCompleterCommandTest.java common/core/src/test/java/com/redhat/thermostat/common/cli/AbstractCompleterServiceTest.java common/core/src/test/java/com/redhat/thermostat/common/cli/AbstractCompletionFinderTest.java common/core/src/test/java/com/redhat/thermostat/common/cli/CompletionFinderTabCompleterTest.java common/core/src/test/java/com/redhat/thermostat/common/cli/CompletionInfoTest.java common/core/src/test/java/com/redhat/thermostat/common/cli/DirectoryContentsCompletionFinderTest.java common/core/src/test/java/com/redhat/thermostat/common/cli/StringsTabCompleterTest.java common/core/src/test/java/com/redhat/thermostat/common/internal/BaseCompletionFinderTest.java common/core/src/test/java/com/redhat/thermostat/common/internal/PredefinedCompletionFinderTest.java common/core/src/test/java/com/redhat/thermostat/common/internal/StringsListCompletionFinderTest.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdCompleterService.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdsFinder.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdsFinderImpl.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/CompleterServiceRegistry.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/DbUrlCompleterService.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/DbUrlFinderImpl.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/FileNameCompleterService.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommandCompleterService.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/JLineCompleterWrapper.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleter.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/LogLevelCompleterService.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/PingCommandCompleterService.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/TabCompletion.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/TreeCompleter.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdCompleterService.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinder.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinderImpl.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/AgentIdCompleterServiceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/AgentIdsFinderImplTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/DbUrlCompleterServiceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/DbUrlFinderImplTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/FileNameCompleterServiceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandCompleterServiceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleterTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/LogLevelCompleterServiceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/PingCommandCompleterServiceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/TabCompletionTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/TreeCompleterTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdCompleterServiceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdsFinderImplTest.java |
diffstat | 56 files changed, 2 insertions(+), 6339 deletions(-) [+] |
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/AbstractCompleterCommand.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import com.redhat.thermostat.shared.locale.Translate; -import org.apache.felix.scr.annotations.Activate; -import org.osgi.service.component.ComponentContext; - -import java.util.Collections; -import java.util.Map; -import java.util.Objects; -import java.util.Set; - -/** - * An abstract class to help with implementing commands which also provide tab completions for arguments and sub- - * commands within Thermostat shell. This is most useful in cases where an argument and its completions are unique to a - * single command; if your plugin provides multiple commands which have shared options and shared arguments/completions, - * more flexibility in the implementation can be had by implementing a {@link CompleterService} directly and separately - * from the Command implementations. - * - * This class is meant for use with OSGi Declarative Services. Subclasses should be annotated with @Component, @Service, - * and @Property(name = Command.NAME, value = "command-name-here") at minimum. - */ -public abstract class AbstractCompleterCommand extends AbstractCommand implements CompleterService { - - private static final Translate<LocaleResources> t = LocaleResources.createLocalizer(); - - private String commandName = null; - - /** - * If implementation subclasses override this method, they <b>must</b> call through to this super implementation as - * well, or else tab completions will fail to be registered correctly. - */ - @Activate - protected void activate(ComponentContext context) { - commandName = validateCommandName((String) context.getProperties().get(Command.NAME)); - } - - /** - * The set of command names for which tab completions are provided. - * - * Implementations of this class only provide completions for a single command name value. - * This value is expected to be provided by an @Property annotation defining a {@link Command.NAME} property. - * @return a singleton set containing the command name - */ - @Override - public final Set<String> getCommands() { - return Collections.singleton(validateCommandName(commandName)); - } - - private String validateCommandName(String name) { - return Objects.requireNonNull(name, t.localize(LocaleResources.MISSING_COMMAND_NAME, getClass().getName()).getContents()); - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - return Collections.emptyMap(); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/AbstractCompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import java.util.Collections; -import java.util.Map; - -/** - * An abstract {@link CompleterService} implementation which adds facilities for easy - * dependency tracking. - * - * Default (empty) implementations are provided for {@link #getOptionCompleters()} and - * {@link #getSubcommandCompleters()} since not all implementations have reason to implement - * both of these. - * - * May be useful for CompleterServices which require access to a resource like a DAO to perform - * their duties. In this situation, consider also passing the DependencyServices instance - * through to a {@link CompletionFinder}, which can decide to provide no results if the - * dependencies are unavailable, or use the dependency resources to gather results. - */ -public abstract class AbstractCompleterService implements CompleterService { - - protected final DependencyServices dependencyServices; - - public AbstractCompleterService() { - this(new DependencyServices()); - } - - AbstractCompleterService(DependencyServices dependencyServices) { - this.dependencyServices = dependencyServices; - } - - protected <T> void setService(Class<T> klazz, T t) { - if (t != null) { - dependencyServices.addService(klazz, t); - } else { - dependencyServices.removeService(klazz); - } - } - - protected void unsetService(Class<?> klazz) { - setService(klazz, null); - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - return Collections.emptyMap(); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/AbstractCompletionFinder.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -/** - * A {@link CompletionFinder} with facilities for service dependency tracking. - * - * @see AbstractCompleterService - */ -public abstract class AbstractCompletionFinder implements CompletionFinder { - - protected final DependencyServices dependencyServices; - - /** - * Construct an instance. - * - * The dependency services are expected to be passed in from the "parent" CompleterService, - * and dependency addition/removal handled by the CompleterService as well, most likely - * via the Activator that registered the CompleterService. - * @param dependencyServices the dependency services - */ - protected AbstractCompletionFinder(DependencyServices dependencyServices) { - this.dependencyServices = dependencyServices; - } - - /** - * Get an instance of a service dependency, if any. - * @param klazz the Class parameter - * @param <T> the type of the class parameter - * @return the service instance, or null if none available. - * @see #allDependenciesAvailable() - */ - protected <T> T getService(Class<T> klazz) { - return dependencyServices.getService(klazz); - } - - /** - * Get the list of dependency classes required for this CompletionFinder to work correctly. - * - * If there are no required service dependencies, then you can implement {@link CompletionFinder}. - * @return an array representing the list of dependencies. - */ - protected abstract Class<?>[] getRequiredDependencies(); - - /** - * Helper method which returns true iff all dependencies listed by {@link #getRequiredDependencies()} - * are currently available. - * - * Implementations of {@link #findCompletions()} should first call this method and, most likely, - * return an empty list if it returns false. - * @return - */ - protected boolean allDependenciesAvailable() { - boolean available = true; - for (Class<?> cl : getRequiredDependencies()) { - if (!dependencyServices.hasService(cl)) { - available = false; - break; - } - } - return available; - } - -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/CompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import com.redhat.thermostat.annotations.ExtensionPoint; - -import java.util.Map; -import java.util.Set; - -/** - * An interface marking a service which provides tab completions for cli/shell commands. - * For example, a plugin providing a new "foo-command" which has an option "-f/--fooId", can provide a CompleterService - * implementation so that "-f TAB" can produce a list of possible fooIds. This implementation would return a singleton - * Set of "foo-command" for {@link CompleterService#getCommands()}, and a Map from a CliCommandOption with short-opt "-f"/ - * long-opt "--fooId" to the relevant {@link TabCompleter} instance for {@link CompleterService#getOptionCompleters()}. - * - * There are several built-in completions which Thermostat provides. If your command uses a vmId, then declaring a - * -v/--vmId option in your thermostat-plugin.xml will give you automagic vmId completions in Thermostat shell. - * Likewise, -a/--agentId, -d/--dbUrl, and -f/--filename completions are provided if you simply include these options - * in your XML. - * - * @see AbstractCompleterService - */ -@ExtensionPoint -public interface CompleterService { - - /** - * The set of command names for which this service provides completions. In the shell, these are top-level command - * names, such as "ping" or "list-vms". In the cli, there are preceded by "thermostat", eg. "thermostat list-vms". - * @return the set of command names - */ - Set<String> getCommands(); - - /** - * Provides the mapping of options to corresponding completers. - * @return the map - */ - Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters(); - - /** - * Provides the mapping of subcommand options to corresponding completers. - * The String key is the name of the subcommand; the Map value is a map of options to completers, the same as in - * {@link #getOptionCompleters()}. - * @return the map - */ - Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters(); - -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/CompletionFinder.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import java.util.List; - -/** - * Intended for use with {@link CompletionFinderTabCompleter}. Provides a list of {@link CompletionInfo}, - * which is displayed as tab-completion results in Thermostat shell. - * - * @see AbstractCompletionFinder - * @see CompletionFinderTabCompleter - * @see CompleterService - */ -public interface CompletionFinder { - List<CompletionInfo> findCompletions(); -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/CompletionFinderTabCompleter.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import jline.console.completer.StringsCompleter; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; - -/** - * Provides shell tab completions as provided by {@link CompletionFinder}s. - */ -public class CompletionFinderTabCompleter implements TabCompleter { - - private CompletionFinder finder; - - /** - * Construct a new instance. - * @param finder the CompletionFinder which provides completion candidates. Must not be null. - */ - public CompletionFinderTabCompleter(CompletionFinder finder) { - this.finder = Objects.requireNonNull(finder); - } - - /** - * {@inheritDoc} - */ - @Override - public int complete(String buffer, int cursor, List<CharSequence> candidates) { - List<CompletionInfo> completions = filterCompletionsWithBuffer(finder.findCompletions(), buffer); - - // If there is only one it will be completed on the prompt line. - // Then any additional information is unwanted there. - List<String> results; - if (completions.isEmpty()) { - results = Collections.emptyList(); - } else if (completions.size() == 1) { - results = getCompletions(completions); - } else { - results = getCompletionsWithUserVisibleText(completions); - } - - return new StringsCompleter(results).complete(buffer, cursor, candidates); - } - - private List<CompletionInfo> filterCompletionsWithBuffer(List<CompletionInfo> completions, String buffer) { - List<CompletionInfo> result = new ArrayList<>(); - for (CompletionInfo completion : completions) { - if (buffer == null || completion.getActualCompletion().startsWith(buffer)) { - result.add(completion); - } - } - return result; - } - - private List<String> getCompletions(List<CompletionInfo> completions) { - List<String> result = new ArrayList<>(); - for (CompletionInfo completion : completions) { - result.add(completion.getActualCompletion() + " "); - } - return result; - } - - private List<String> getCompletionsWithUserVisibleText(List<CompletionInfo> completions) { - List<String> result = new ArrayList<>(); - for (CompletionInfo completion : completions) { - result.add(completion.getCompletionWithUserVisibleText()); - } - return result; - } - -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/CompletionInfo.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,119 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import java.util.Objects; - -import static java.util.Objects.requireNonNull; - -/** - * A simple structure used for tab-completion candidates. - */ -public class CompletionInfo { - - private String actualCompletion; - private String userVisibleText; - - /** - * Cosntruct a CompletionInfo with a tab completion candidate as well as a descriptive label - * to distinguish the candidate. - * @param actualCompletion the text which may be tab completed - * @param userVisibleText a label displayed beside the completion text - */ - public CompletionInfo(String actualCompletion, String userVisibleText) { - this.actualCompletion = requireNonNull(actualCompletion); - this.userVisibleText = userVisibleText; - } - - /** - * Construct a CompletionInfo with a tab completion candidate, and no additional label to - * distinguish the candidate. - * @param actualCompletion the text which may be tab completed - */ - public CompletionInfo(String actualCompletion) { - this(actualCompletion, null); - } - - /** - * Provides the String completion when there is only one tab-completion match. - * @return the completed String - */ - public String getActualCompletion() { - return actualCompletion; - } - - /** - * Provides a human-friendly identifier text describing {@link #getActualCompletion()}. - * @return the identifier text - */ - public String getUserVisibleText() { - return userVisibleText; - } - - /** - * Provides the String displayed to the user when tab-completion returns more than one possible result. - * This will contain the completion candidate ({@link #getActualCompletion()}), as well as the descriptive - * label ({@link #getUserVisibleText()}), if available. - * @return the display String - */ - public String getCompletionWithUserVisibleText() { - if (userVisibleText == null) { - return actualCompletion; - } else { - return actualCompletion + " [" + userVisibleText + "]"; - } - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - CompletionInfo that = (CompletionInfo) o; - return Objects.equals(this.actualCompletion, that.actualCompletion) - && Objects.equals(this.userVisibleText, that.userVisibleText); - } - - @Override - public int hashCode() { - return Objects.hash(actualCompletion, userVisibleText); - } -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/DirectoryContentsCompletionFinder.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,180 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import java.io.File; -import java.io.FileFilter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; - -/** - * A CompletionFinder which provides completions for paths within a specific directory. - * - * This may be useful for commands which require a path to a config file, which is only - * expected to reside within an application-specific directory. - * - * This is NOT recursive - paths will only be completed if they are direct children of - * the specified directory. - */ -public class DirectoryContentsCompletionFinder implements CompletionFinder { - - public static final CompletionMode DEFAULT_COMPLETION_MODE = CompletionMode.NAME_ONLY; - public static final DefaultFileFilter DEFAULT_FILE_FILTER = new DefaultFileFilter(); - - private final File directory; - private CompletionMode mode = DEFAULT_COMPLETION_MODE; - private FileFilter filter = DEFAULT_FILE_FILTER; - - /** - * @param directory the directory to search. Must not be null. - */ - public DirectoryContentsCompletionFinder(File directory) { - this.directory = Objects.requireNonNull(directory); - } - - /** - * Set the configurable completion mode. - * The provided completions may be only the file name, or the relative path, - * or the absolute path, or the canonical path. This affects what text is - * offered as tab completions, but does not affect the search behaviour of - * this CompletionFinder. - * The default mode is NAME_ONLY. - * @param mode the completion mode. Must not be null. - */ - public void setCompletionMode(CompletionMode mode) { - this.mode = Objects.requireNonNull(mode); - } - - /** - * Set the file filter. - * This can be used to only suggest completion of files within the specified - * directory which also match some criteria. For example, a filter - * might be provided which accepts only PNG files, in which case no tab - * completions will be offered for JPEGs within the directory. Or for another - * example, a filter can even be provided which accepts only subdirectories, - * rather than files. - * The default filter accepts all files and ignores subdirectories. - * @param filter the file filter. Must not be null. - */ - public void setFileFilter(FileFilter filter) { - this.filter = Objects.requireNonNull(filter); - } - - @Override - public List<CompletionInfo> findCompletions() { - if (!directory.exists()) { - return Collections.emptyList(); - } - if (!directory.canRead()) { - return Collections.emptyList(); - } - if (directory.isFile()) { - try { - return Collections.singletonList(getInfo(directory)); - } catch (IOException e) { - // just ignore this file path - } - } - // path exists, is readable, and is not a file (-> is a directory) - List<CompletionInfo> results = new ArrayList<>(); - for (File file : directory.listFiles(filter)) { - try { - results.add(getInfo(file)); - } catch (IOException e) { - // just ignore this file path - } - } - return results; - } - - private CompletionInfo getInfo(File file) throws IOException { - return mode.map(file); - } - - public enum CompletionMode { - NAME_ONLY(new FileInfoMapper() { - @Override - public CompletionInfo map(File file) { - return new CompletionInfo(file.getName()); - } - }), - RELATIVE_PATH(new FileInfoMapper() { - @Override - public CompletionInfo map(File file) { - return new CompletionInfo(file.getPath()); - } - }), - ABSOLUTE_PATH(new FileInfoMapper() { - @Override - public CompletionInfo map(File file) { - return new CompletionInfo(file.getAbsolutePath()); - } - }), - CANONICAL_PATH(new FileInfoMapper() { - @Override - public CompletionInfo map(File file) throws IOException { - return new CompletionInfo(file.getCanonicalPath()); - } - }); - - CompletionMode(FileInfoMapper mapper) { - this.mapper = mapper; - } - - private FileInfoMapper mapper; - - CompletionInfo map(File file) throws IOException { - return mapper.map(file); - } - - } - - private interface FileInfoMapper { - CompletionInfo map(File file) throws IOException; - } - - private static class DefaultFileFilter implements FileFilter { - @Override - public boolean accept(File file) { - return file.isFile(); - } - } - -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/FileNameTabCompleter.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import com.redhat.thermostat.annotations.Service; - -/** - * A stub interface for marking a TabCompleter which specifically provides file name completions. - * This is intended to be exported as an OSGi service. If you are writing a {@link CompleterService} - * which provides file name completion, you may use this interface as a dependency and use the - * Thermostat-provided file name completions this way. - * - * However, if it is applicable, you may alternatively simply use the convention of naming your - * filename option argument -f/--filename (ex. foo-command -f /path/to/file, or - * foo-command --filename /path/to/file). If you define your file name completion argument to match - * this naming convention exactly in your thermostat-plugin.xml, then the filename completions will - * be taken care of automatically by Thermostat. - */ -@Service -public interface FileNameTabCompleter extends TabCompleter { -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/StringsTabCompleter.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import com.redhat.thermostat.common.internal.StringsListCompletionFinder; - -import java.util.Arrays; -import java.util.List; - -public class StringsTabCompleter implements TabCompleter { - - protected final CompletionFinderTabCompleter tabCompleter; - - /* Only invoke directly within this class or for testing */ - StringsTabCompleter(CompletionFinderTabCompleter tabCompleter) { - this.tabCompleter = tabCompleter; - } - - public StringsTabCompleter(String... strings) { - this(Arrays.asList(strings)); - } - - public StringsTabCompleter(Iterable<String> strings) { - this(new CompletionFinderTabCompleter(new StringsListCompletionFinder(strings))); - } - - @Override - public int complete(String buffer, int cursor, List<CharSequence> candidates) { - return tabCompleter.complete(buffer, cursor, candidates); - } -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/TabCompleter.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import java.util.List; - -/** - * Interface for tab completion providers. - * - * Tab completions for plugins are typically expected to use {@link CompletionFinderTabCompleter} - * rather than implementing this interface directly. - * - * If you need to supply tab completions for a filename, see {@link FileNameTabCompleter}. - */ -public interface TabCompleter { - /** - * Perform tab completion. - * - * @param buffer the current contents of the command line - * @param cursor the position of the editor cursor within the command line - * @param candidates out variable containing the possible completions - * @return -1 iff no completions are available from this TabCompleter - */ - int complete(String buffer, int cursor, List<CharSequence> candidates); -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/internal/BaseCompletionFinder.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* - * Copyright 2012-2017 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.common.internal; - -import com.redhat.thermostat.common.cli.CompletionFinder; -import com.redhat.thermostat.common.cli.CompletionInfo; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -public class BaseCompletionFinder implements CompletionFinder { - - protected final List<CompletionInfo> completions = new ArrayList<>(); - - public BaseCompletionFinder() {} - - public BaseCompletionFinder(Collection<CompletionInfo> completions) { - this.completions.addAll(completions); - } - - protected void addCompletion(CompletionInfo completionInfo) { - completions.add(completionInfo); - } - - @Override - public List<CompletionInfo> findCompletions() { - return new ArrayList<>(completions); - } -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/internal/PredefinedCompletionFinder.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -/* - * Copyright 2012-2017 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.common.internal; - -import com.redhat.thermostat.common.cli.CompletionInfo; - -public class PredefinedCompletionFinder<T> extends BaseCompletionFinder { - - protected final InfoAdapter<T> adapter; - - public PredefinedCompletionFinder(Iterable<T> ts, InfoAdapter<T> adapter) { - this.adapter = adapter; - for (T t : ts) { - addCompletion(t); - } - } - - protected void addCompletion(T t) { - super.addCompletion(adapter.adapt(t)); - } - - public interface InfoAdapter<T> { - CompletionInfo adapt(T t); - } - -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/internal/StringsListCompletionFinder.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -/* - * Copyright 2012-2017 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.common.internal; - -import com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.common.internal.PredefinedCompletionFinder; - -import java.util.Arrays; - -public class StringsListCompletionFinder extends PredefinedCompletionFinder<String> { - - public StringsListCompletionFinder(String... strings) { - this(Arrays.asList(strings)); - } - - public StringsListCompletionFinder(Iterable<String> strings) { - super(strings, new StringInfoAdapter()); - } - - protected static class StringInfoAdapter implements InfoAdapter<String> { - @Override - public CompletionInfo adapt(String s) { - return new CompletionInfo(s); - } - } -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/cli/AbstractCompleterCommandTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.osgi.service.component.ComponentContext; - -import java.util.Collections; -import java.util.Dictionary; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class AbstractCompleterCommandTest { - - public static final String COMMAND_NAME = "StubCompleterCommand"; - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - private AbstractCompleterCommand command; - private ComponentContext componentContext; - - @Before - public void setup() { - command = new StubCompleterCommand(); - componentContext = mock(ComponentContext.class); - Dictionary dict = mock(Dictionary.class); - when(dict.get(Command.NAME)).thenReturn(COMMAND_NAME); - when(componentContext.getProperties()).thenReturn(dict); - } - - @Test - public void testActivationSetsCommandName() { - command.activate(componentContext); - assertThat(command.getCommands(), is(equalTo(Collections.singleton(COMMAND_NAME)))); - } - - @Test - public void testThrowsExceptionIfGetCommandsCalledBeforeActivation() { - thrown.expect(NullPointerException.class); - thrown.expectMessage("The implementation class " + - "com.redhat.thermostat.common.cli.AbstractCompleterCommandTest$StubCompleterCommand does not define an" + - " OSGi property for COMMAND_NAME, which is required."); - command.getCommands(); - } - - @Test - public void testActivationFailsIfCommandNamePropertyNotSet() { - Dictionary dict = mock(Dictionary.class); - when(componentContext.getProperties()).thenReturn(dict); - thrown.expect(NullPointerException.class); - thrown.expectMessage("The implementation class " + - "com.redhat.thermostat.common.cli.AbstractCompleterCommandTest$StubCompleterCommand does not define an" + - " OSGi property for COMMAND_NAME, which is required."); - command.activate(componentContext); - } - - private static class StubCompleterCommand extends AbstractCompleterCommand { - @Override - public void run(CommandContext ctx) throws CommandException { - // no-op - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - return Collections.emptyMap(); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/cli/AbstractCompleterServiceTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,122 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import java.util.Collections; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -public class AbstractCompleterServiceTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - private DependencyServices dependencyServices; - private AbstractCompleterService completerService; - - @Before - public void setup() { - dependencyServices = mock(DependencyServices.class); - completerService = new StubCompleterService(dependencyServices); - } - - @Test - public void verifySetServiceAddsDependency() { - Dependency dep = new Dependency(); - completerService.setService(Dependency.class, dep); - verify(dependencyServices).addService(Dependency.class, dep); - } - - @Test - public void verifySetServiceNullRemovesDependency() { - completerService.setService(Dependency.class, null); - verify(dependencyServices).removeService(Dependency.class); - } - - @Test - public void verifySetServiceTwiceThrowsException() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("service is already being tracked"); - completerService = new StubCompleterService(new DependencyServices()); - Dependency dep = new Dependency(); - completerService.setService(Dependency.class, dep); - completerService.setService(Dependency.class, dep); - } - - @Test - public void testUnsetServiceRemovesDependency() { - Dependency dep = new Dependency(); - completerService.setService(Dependency.class, dep); - verify(dependencyServices).addService(Dependency.class, dep); - completerService.unsetService(Dependency.class); - verify(dependencyServices).removeService(Dependency.class); - } - - @Test - public void testDefaultGetOptionCompletersReturnsEmpty() { - assertThat(completerService.getOptionCompleters().isEmpty(), is(true)); - } - - @Test - public void testDefaultGetSubcommandCompletersReturnsEmpty() { - assertThat(completerService.getSubcommandCompleters().isEmpty(), is(true)); - } - - private static class StubCompleterService extends AbstractCompleterService { - StubCompleterService(DependencyServices dependencyServices) { - super(dependencyServices); - } - - @Override - public Set<String> getCommands() { - return Collections.singleton("command"); - } - } - - private static class Dependency { - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/cli/AbstractCompletionFinderTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import org.junit.Before; -import org.junit.Test; - -import java.util.List; -import java.util.Set; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -public class AbstractCompletionFinderTest { - - private DependencyServices dependencyServices; - private Class<?>[] deps; - private List<CompletionInfo> infos; - private AbstractCompletionFinder finder; - - @Before - @SuppressWarnings("unchecked") - public void setup() { - dependencyServices = mock(DependencyServices.class); - infos = mock(List.class); - deps = new Class[0]; - finder = new StubCompletionFinder(dependencyServices, deps, infos); - } - - @Test - public void testGetService() { - Class<?> klazz = List.class; // not a great example dependency, but works - finder.getService(klazz); - verify(dependencyServices).getService(klazz); - } - - @Test - public void testAllDependenciesAvailableReturnsTrueWhenNoDeps() { - assertTrue(finder.allDependenciesAvailable()); - } - - @Test - public void testAllDependenciesAvailableReturnsTrueWhenAllAvailable() { - deps = new Class[]{ List.class, Set.class }; - when(dependencyServices.hasService(List.class)).thenReturn(true); - when(dependencyServices.hasService(Set.class)).thenReturn(true); - finder = new StubCompletionFinder(dependencyServices, deps, infos); - assertTrue(finder.allDependenciesAvailable()); - verify(dependencyServices).hasService(List.class); - verify(dependencyServices).hasService(Set.class); - verifyNoMoreInteractions(dependencyServices); - } - - @Test - public void testAllDependenciesAvailableReturnsFalseWhenOneUnavailable() { - deps = new Class[]{ List.class, Set.class }; - when(dependencyServices.hasService(List.class)).thenReturn(true); - when(dependencyServices.hasService(Set.class)).thenReturn(false); - finder = new StubCompletionFinder(dependencyServices, deps, infos); - assertFalse(finder.allDependenciesAvailable()); - verify(dependencyServices).hasService(List.class); - verify(dependencyServices).hasService(Set.class); - verifyNoMoreInteractions(dependencyServices); - } - - private static class StubCompletionFinder extends AbstractCompletionFinder { - - Class<?>[] deps; - List<CompletionInfo> infos; - - StubCompletionFinder(DependencyServices dependencyServices, Class<?>[] deps, List<CompletionInfo> infos) { - super(dependencyServices); - this.deps = deps; - this.infos = infos; - } - - @Override - protected Class<?>[] getRequiredDependencies() { - return deps; - } - - @Override - public List<CompletionInfo> findCompletions() { - return infos; - } - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/cli/CompletionFinderTabCompleterTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,113 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class CompletionFinderTabCompleterTest { - - private CompletionFinder finder; - private TabCompleter completer; - - @Before - public void setup() { - finder = mock(CompletionFinder.class); - completer = new CompletionFinderTabCompleter(finder); - } - - @Test - public void testNoCandidates() { - List<CharSequence> candidates = new ArrayList<>(); - - when(finder.findCompletions()).thenReturn(Collections.<CompletionInfo>emptyList()); - - completer.complete(null, 0, candidates); - - verify(finder).findCompletions(); - - assertThat(candidates, is(equalTo(Collections.<CharSequence>emptyList()))); - } - - @Test - public void testOnlyOneCandidate() { - List<CharSequence> candidates = new ArrayList<>(); - - CompletionInfo completionInfo = new CompletionInfo("actual-completion", "user-text"); - when(finder.findCompletions()).thenReturn(Collections.singletonList(completionInfo)); - - completer.complete(null, 0, candidates); - - verify(finder).findCompletions(); - - assertThat(candidates, is(equalTo(Collections.singletonList((CharSequence) (completionInfo.getActualCompletion() + " "))))); - } - - @Test - public void testMultipleCandidates() { - List<CharSequence> candidates = new ArrayList<>(); - - CompletionInfo info1 = new CompletionInfo("actual-completion1", "user-text1"); - CompletionInfo info2 = new CompletionInfo("actual-completion2", "user-text2"); - when(finder.findCompletions()).thenReturn(Arrays.asList(info1, info2)); - - completer.complete(null, 0, candidates); - - List<String> stringCandidates = new ArrayList<>(); - for (CharSequence cs : candidates) { - stringCandidates.add(cs.toString()); - } - - verify(finder).findCompletions(); - - assertThat(stringCandidates, - is(equalTo(Arrays.asList(info1.getCompletionWithUserVisibleText(), info2.getCompletionWithUserVisibleText())))); - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/cli/CompletionInfoTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - -public class CompletionInfoTest { - - @Test(expected = NullPointerException.class) - public void testInstantiateWithNullActualCompletion() { - new CompletionInfo(null, "foo"); - } - - @Test - public void testInstantiateWithNullUserVisibleText() { - CompletionInfo info = new CompletionInfo("foo", null); - assertThat(info.getActualCompletion(), is("foo")); - assertThat(info.getUserVisibleText(), is(nullValue())); - assertThat(info.getCompletionWithUserVisibleText(), is("foo")); - } - - @Test - public void testCompletionWithUserVisibleText() { - CompletionInfo info = new CompletionInfo("foo", "bar"); - assertThat(info.getActualCompletion(), is("foo")); - assertThat(info.getUserVisibleText(), is("bar")); - assertThat(info.getCompletionWithUserVisibleText(), is("foo [bar]")); - } - - @Test - public void testEqualsWhenSame() { - CompletionInfo info1 = new CompletionInfo("foo", "bar"); - CompletionInfo info2 = new CompletionInfo("foo", "bar"); - assertThat(info1, is(equalTo(info2))); - } - - @Test - public void testEqualsWhenDifferent() { - CompletionInfo info1 = new CompletionInfo("foo", "bar"); - CompletionInfo info2 = new CompletionInfo("foo", "foo"); - assertThat(info1, is(not(equalTo(info2)))); - } - - @Test - public void testHashcodeWhenSame() { - CompletionInfo info1 = new CompletionInfo("foo", "bar"); - CompletionInfo info2 = new CompletionInfo("foo", "bar"); - assertThat(info1.hashCode(), is(info2.hashCode())); - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/cli/DirectoryContentsCompletionFinderTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,202 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; - -import java.io.File; -import java.io.FileFilter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.isA; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class DirectoryContentsCompletionFinderTest { - - private File f1; - private File f2; - private File d1; - private File dir; - private DirectoryContentsCompletionFinder finder; - - @Before - public void setup() throws IOException { - f1 = mock(File.class); - when(f1.isFile()).thenReturn(true); - when(f1.isDirectory()).thenReturn(false); - when(f1.getName()).thenReturn("f1"); - when(f1.getPath()).thenReturn("dir/f1"); - when(f1.getAbsolutePath()).thenReturn("/some/dir/f1"); - when(f1.getCanonicalPath()).thenReturn("/some/dir/f1"); - f2 = mock(File.class); - when(f2.isFile()).thenReturn(true); - when(f2.isDirectory()).thenReturn(false); - when(f2.getName()).thenReturn("f2"); - when(f2.getPath()).thenReturn("dir/f2"); - when(f2.getAbsolutePath()).thenReturn("/some/dir/f2"); - when(f2.getCanonicalPath()).thenReturn("/some/dir/f2"); - d1 = mock(File.class); - when(d1.isFile()).thenReturn(false); - when(d1.isDirectory()).thenReturn(true); - when(d1.getName()).thenReturn("d1/"); - when(d1.getPath()).thenReturn("dir/d1/"); - when(d1.getAbsolutePath()).thenReturn("/some/dir/d1/"); - when(d1.getCanonicalPath()).thenReturn("/some/dir/d1/"); - dir = mock(File.class); - when(dir.isFile()).thenReturn(false); - when(dir.isDirectory()).thenReturn(true); - when(dir.exists()).thenReturn(true); - when(dir.canRead()).thenReturn(true); - when(dir.getName()).thenReturn("dir/"); - when(dir.getPath()).thenReturn("some/dir/"); - when(dir.getAbsolutePath()).thenReturn("/some/dir/"); - when(dir.getCanonicalPath()).thenReturn("/some/dir/"); - when(dir.list()).thenReturn(new String[]{"f1", "f2", "d1/"}); - when(dir.listFiles()).thenReturn(new File[]{f1, f2, d1}); - when(dir.listFiles(isA(FileFilter.class))).thenAnswer(new Answer<File[]>() { - @Override - public File[] answer(InvocationOnMock invocationOnMock) throws Throwable { - List<File> result = new ArrayList<>(); - FileFilter filter = (FileFilter) invocationOnMock.getArguments()[0]; - for (File file : dir.listFiles()) { - if (filter.accept(file)) { - result.add(file); - } - } - return result.toArray(new File[result.size()]); - } - }); - finder = new DirectoryContentsCompletionFinder(dir); - } - - @Test(expected = NullPointerException.class) - public void testDoesNotAcceptNullDirectory() { - new DirectoryContentsCompletionFinder(null); - } - - @Test(expected = NullPointerException.class) - public void testDoesNotAcceptNullMode() { - finder.setCompletionMode(null); - } - - @Test(expected = NullPointerException.class) - public void testDoesNotAcceptNullFilter() { - finder.setFileFilter(null); - } - - @Test - public void testReturnsEmptyIfDirectoryDoesntExist() { - when(dir.exists()).thenReturn(false); - List<CompletionInfo> list = finder.findCompletions(); - assertThat(list, is(equalTo(Collections.<CompletionInfo>emptyList()))); - } - - @Test - public void testReturnsEmptyWhenDirectoryExistsButIsNotReadable() { - when(dir.exists()).thenReturn(true); - when(dir.canRead()).thenReturn(false); - List<CompletionInfo> list = finder.findCompletions(); - assertThat(list, is(equalTo(Collections.<CompletionInfo>emptyList()))); - } - - @Test - public void testReturnsSingleResultWhenDirIsReallyFile() { - when(dir.exists()).thenReturn(true); - when(dir.canRead()).thenReturn(true); - when(dir.isDirectory()).thenReturn(false); - when(dir.isFile()).thenReturn(true); - List<CompletionInfo> list = finder.findCompletions(); - assertThat(list.size(), is(1)); - CompletionInfo info = list.get(0); - assertThat(info, is(equalTo(new CompletionInfo(dir.getName())))); - } - - @Test - public void testFindsOnlyFilesByDefault() { - List<CompletionInfo> list = finder.findCompletions(); - assertThat(list, is(equalTo(Arrays.asList(new CompletionInfo(f1.getName()), new CompletionInfo(f2.getName()))))); - } - - @Test - public void testSetFileFilterWorks() { - FileFilter dirFilter = new FileFilter() { - @Override - public boolean accept(File file) { - return file.isDirectory(); - } - }; - finder.setFileFilter(dirFilter); - List<CompletionInfo> list = finder.findCompletions(); - assertThat(list, is(equalTo(Collections.singletonList(new CompletionInfo(d1.getName()))))); - } - - @Test - public void testRelativePathMode() { - finder.setCompletionMode(DirectoryContentsCompletionFinder.CompletionMode.RELATIVE_PATH); - List<CompletionInfo> list = finder.findCompletions(); - assertThat(list, - is(equalTo(Arrays.asList(new CompletionInfo(f1.getPath()), new CompletionInfo(f2.getPath()))))); - } - - @Test - public void testAbsolutePathMode() { - finder.setCompletionMode(DirectoryContentsCompletionFinder.CompletionMode.ABSOLUTE_PATH); - List<CompletionInfo> list = finder.findCompletions(); - assertThat(list, - is(equalTo(Arrays.asList(new CompletionInfo(f1.getAbsolutePath()), new CompletionInfo(f2.getAbsolutePath()))))); - } - - @Test - public void testCanonicalPathMode() throws IOException { - finder.setCompletionMode(DirectoryContentsCompletionFinder.CompletionMode.CANONICAL_PATH); - List<CompletionInfo> list = finder.findCompletions(); - assertThat(list, - is(equalTo(Arrays.asList(new CompletionInfo(f1.getCanonicalPath()), new CompletionInfo(f2.getCanonicalPath()))))); - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/cli/StringsTabCompleterTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -/* - * Copyright 2012-2017 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.common.cli; - -import org.junit.Test; - -import java.util.ArrayList; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -/* For more thorough testing of actual tab completion behaviour, see CompletionFinderTabCompleter - * and CompletionFinderTabCompleterTest. StringsTabCompleter is meant to delegate to CompletionFinderTabCompleter - * to do the actual completion work. - */ -public class StringsTabCompleterTest { - - @Test - public void testDelegatesToActualCompleter() { - CompletionFinderTabCompleter mockCompleter = mock(CompletionFinderTabCompleter.class); - StringsTabCompleter completer = new StringsTabCompleter(mockCompleter); - completer.complete(null, 0, new ArrayList<CharSequence>()); - verify(mockCompleter).complete(null, 0, new ArrayList<CharSequence>()); - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/internal/BaseCompletionFinderTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * Copyright 2012-2017 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.common.internal; - -import com.redhat.thermostat.common.cli.CompletionInfo; -import org.junit.Test; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public class BaseCompletionFinderTest { - - @Test - public void testEmptyByDefault() { - BaseCompletionFinder finder = new BaseCompletionFinder(); - assertThat(finder.findCompletions().size(), is(0)); - } - - @Test - public void testAddCompletion() { - BaseCompletionFinder finder = new BaseCompletionFinder(); - CompletionInfo info = new CompletionInfo("foo", "bar"); - finder.addCompletion(info); - assertThat(finder.findCompletions(), is(equalTo(Collections.singletonList(info)))); - } - - @Test - public void testAddsCompletionsFromConstructor() { - CompletionInfo info1 = new CompletionInfo("foo"); - CompletionInfo info2 = new CompletionInfo("bar"); - CompletionInfo info3 = new CompletionInfo("baz"); - List<CompletionInfo> list = Arrays.asList(info1, info2, info3); - BaseCompletionFinder finder = new BaseCompletionFinder(list); - assertThat(finder.findCompletions(), is(equalTo(list))); - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/internal/PredefinedCompletionFinderTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -/* - * Copyright 2012-2017 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.common.internal; - -import com.redhat.thermostat.common.cli.CompletionInfo; -import org.junit.Test; - -import java.util.Arrays; -import java.util.Collections; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public class PredefinedCompletionFinderTest { - - @Test - public void testAdaptingString() { - PredefinedCompletionFinder<String> finder = new PredefinedCompletionFinder<>(Collections.singletonList("foo"), - new PredefinedCompletionFinder.InfoAdapter<String>() { - @Override - public CompletionInfo adapt(String s) { - return new CompletionInfo(s); - } - }); - assertThat(finder.findCompletions(), is(equalTo(Collections.singletonList(new CompletionInfo("foo"))))); - } - - @Test - public void testAdaptingStrings() { - PredefinedCompletionFinder<String> finder = new PredefinedCompletionFinder<>(Arrays.asList("foo", "bar", "baz"), - new PredefinedCompletionFinder.InfoAdapter<String>() { - @Override - public CompletionInfo adapt(String s) { - return new CompletionInfo(s); - } - }); - assertThat(finder.findCompletions(), - is(equalTo(Arrays.asList(new CompletionInfo("foo"), new CompletionInfo("bar"), new CompletionInfo("baz"))))); - } - - @Test - public void testAddCompletion() { - PredefinedCompletionFinder<String> finder = new PredefinedCompletionFinder<>(Collections.singletonList("foo"), - new PredefinedCompletionFinder.InfoAdapter<String>() { - @Override - public CompletionInfo adapt(String s) { - return new CompletionInfo(s); - } - }); - finder.addCompletion("bar"); - assertThat(finder.findCompletions(), - is(equalTo(Arrays.asList(new CompletionInfo("foo"), new CompletionInfo("bar"))))); - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/internal/StringsListCompletionFinderTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -/* - * Copyright 2012-2017 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.common.internal; - -import com.redhat.thermostat.common.cli.CompletionInfo; -import org.junit.Test; - -import java.util.Arrays; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public class StringsListCompletionFinderTest { - - @Test - public void testVarArgsConstructor() { - StringsListCompletionFinder finder = new StringsListCompletionFinder("foo", "bar", "baz"); - assertThat(finder.findCompletions(), - is(equalTo(Arrays.asList(new CompletionInfo("foo"), new CompletionInfo("bar"), new CompletionInfo("baz"))))); - } - - @Test - public void testIterableConstructor() { - StringsListCompletionFinder finder = new StringsListCompletionFinder(Arrays.asList("foo", "bar", "baz")); - assertThat(finder.findCompletions(), - is(equalTo(Arrays.asList(new CompletionInfo("foo"), new CompletionInfo("bar"), new CompletionInfo("baz"))))); - } - - @Test - public void testAddCompletion() { - StringsListCompletionFinder finder = new StringsListCompletionFinder("foo"); - finder.addCompletion("bar"); - assertThat(finder.findCompletions(), - is(equalTo(Arrays.asList(new CompletionInfo("foo"), new CompletionInfo("bar"))))); - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java Tue May 23 14:10:33 2017 -0400 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java Wed May 24 12:13:27 2017 +0200 @@ -43,27 +43,22 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; -import com.redhat.thermostat.common.ActionEvent; -import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.ExitStatus; import com.redhat.thermostat.common.MultipleServiceTracker; import com.redhat.thermostat.common.MultipleServiceTracker.Action; import com.redhat.thermostat.common.MultipleServiceTracker.DependencyProvider; -import com.redhat.thermostat.common.NotImplementedException; -import com.redhat.thermostat.common.ThermostatExtensionRegistry; import com.redhat.thermostat.common.cli.CommandContextFactory; import com.redhat.thermostat.common.cli.CommandRegistry; import com.redhat.thermostat.common.cli.CommandRegistryImpl; -import com.redhat.thermostat.common.cli.CompleterService; import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; + import com.redhat.thermostat.launcher.BundleManager; import com.redhat.thermostat.launcher.Launcher; + import com.redhat.thermostat.shared.config.CommonPaths; public class Activator implements BundleActivator { - private CompleterServiceRegistry completerServiceRegistry; - @SuppressWarnings({ "rawtypes" }) class RegisterLauncherAction implements Action { @@ -134,7 +129,6 @@ private CommandRegistry registry; - private TabCompletion tabCompletion; private MultipleServiceTracker commandInfoSourceTracker; @SuppressWarnings({ "rawtypes" }) @@ -149,28 +143,7 @@ registerLauncherAction); launcherDepsTracker.open(); - tabCompletion = new TabCompletion(); - - completerServiceRegistry = new CompleterServiceRegistry(context); - completerServiceRegistry.addActionListener(new ActionListener<ThermostatExtensionRegistry.Action>() { - @Override - public void actionPerformed(ActionEvent<ThermostatExtensionRegistry.Action> actionEvent) { - CompleterService service = (CompleterService) actionEvent.getPayload(); - switch (actionEvent.getActionId()) { - case SERVICE_ADDED: - tabCompletion.addCompleterService(service); - break; - case SERVICE_REMOVED: - tabCompletion.removeCompleterService(service); - break; - default: - throw new NotImplementedException("Unknown action: " + actionEvent.getActionId()); - } - } - }); - final HelpCommand helpCommand = new HelpCommand(); - final HelpCommandCompleterService helpCommandCompleterService = new HelpCommandCompleterService(); final Class<?>[] helpCommandClasses = new Class<?>[] { CommandInfoSource.class, CommandGroupMetadataSource.class @@ -180,7 +153,6 @@ public void dependenciesAvailable(DependencyProvider services) { CommandInfoSource infoSource = services.get(CommandInfoSource.class); helpCommand.setCommandInfoSource(infoSource); - helpCommandCompleterService.bindCommandInfoSource(infoSource); CommandGroupMetadataSource commandGroupMetadataSource = services.get(CommandGroupMetadataSource.class); helpCommand.setCommandGroupMetadataSource(commandGroupMetadataSource); @@ -190,14 +162,11 @@ public void dependenciesUnavailable() { helpCommand.setCommandInfoSource(null); helpCommand.setCommandGroupMetadataSource(null); - helpCommandCompleterService.unbindCommandInfoSource(); } }); commandInfoSourceTracker.open(); registry.registerCommand("help", helpCommand); - - context.registerService(CompleterService.class.getName(), helpCommandCompleterService, null); } @Override @@ -208,9 +177,6 @@ if (commandInfoSourceTracker != null) { commandInfoSourceTracker.close(); } - if (completerServiceRegistry != null) { - completerServiceRegistry.stop(); - } registry.unregisterCommands(); } }
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdCompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompleterService; -import com.redhat.thermostat.common.cli.CompletionFinderTabCompleter; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -@Component(immediate = true) -@Service -public class AgentIdCompleterService implements CompleterService { - - public static final CliCommandOption AGENT_ID_OPTION = new CliCommandOption("a", "agentId", true, "Agent ID", false); - - @Reference - private AgentIdsFinder agentIdsFinder; - - @Override - public Set<String> getCommands() { - return TabCompletion.ALL_COMMANDS_COMPLETER; - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - if (agentIdsFinder == null) { - return Collections.emptyMap(); - } - return Collections.singletonMap(AGENT_ID_OPTION, new CompletionFinderTabCompleter(agentIdsFinder)); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } - - public void bindAgentIdsFinder(AgentIdsFinder agentIdsFinder) { - this.agentIdsFinder = agentIdsFinder; - } - - public void unbindAgentIdsFinder(AgentIdsFinder agentIdsFinder) { - this.agentIdsFinder = null; - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdsFinder.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CompletionFinder; - -interface AgentIdsFinder extends CompletionFinder { -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdsFinderImpl.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -@Component -@Service -public class AgentIdsFinderImpl implements AgentIdsFinder { - - @Reference - private AgentInfoDAO agentInfoDAO; - - @Override - public List<CompletionInfo> findCompletions() { - if (agentInfoDAO == null) { - return Collections.emptyList(); - } - return getAgentIdCompletions(agentInfoDAO.getAllAgentInformation()); - } - - private List<CompletionInfo> getAgentIdCompletions(Collection<AgentInformation> agentInfos) { - List<CompletionInfo> agentIds = new ArrayList<>(); - for (AgentInformation agentInfo : agentInfos) { - agentIds.add(new CompletionInfo(agentInfo.getAgentId())); - } - return agentIds; - } - - public void bindAgentInfoDAO(AgentInfoDAO agentInfoDAO) { - this.agentInfoDAO = agentInfoDAO; - } - - public void unbindAgentInfoDAO(AgentInfoDAO agentInfoDAO) { - this.agentInfoDAO = null; - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CompleterServiceRegistry.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.ThermostatExtensionRegistry; -import com.redhat.thermostat.common.cli.CompleterService; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.InvalidSyntaxException; - -public class CompleterServiceRegistry extends ThermostatExtensionRegistry<CompleterService> { - - private static final String FILTER = "(" + Constants.OBJECTCLASS + "=" + CompleterService.class.getName() + ")"; - - public CompleterServiceRegistry(BundleContext context) throws InvalidSyntaxException { - super(context, FILTER, CompleterService.class); - } - -} \ No newline at end of file
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/DbUrlCompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompleterService; -import com.redhat.thermostat.common.cli.CompletionFinder; -import com.redhat.thermostat.common.cli.CompletionFinderTabCompleter; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -@Component -@Service -public class DbUrlCompleterService implements CompleterService { - - public static final CliCommandOption DB_URL_OPTION = new CliCommandOption("d", "dbUrl", true, "Database URL", false); - - @Reference - private DbUrlFinder dbUrlFinder; - - @Override - public Set<String> getCommands() { - return TabCompletion.ALL_COMMANDS_COMPLETER; - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - if (dbUrlFinder == null) { - return Collections.emptyMap(); - } - TabCompleter completer = new CompletionFinderTabCompleter(dbUrlFinder); - return Collections.singletonMap(DB_URL_OPTION, completer); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } - - public void bindDbUrlFinder(DbUrlFinder dbUrlFinder) { - this.dbUrlFinder = dbUrlFinder; - } - - public void unbindDbUrlFinder(DbUrlFinder dbUrlFinder) { - this.dbUrlFinder = null; - } - - interface DbUrlFinder extends CompletionFinder {} - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/DbUrlFinderImpl.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.common.config.ClientPreferences; -import com.redhat.thermostat.shared.config.CommonPaths; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; - -import java.util.Collections; -import java.util.List; - -@Component -@Service -@Reference(name = "commonPaths", referenceInterface = CommonPaths.class) -public class DbUrlFinderImpl implements DbUrlCompleterService.DbUrlFinder { - - private ClientPreferences clientPreferences; - - @Override - public List<CompletionInfo> findCompletions() { - if (clientPreferences == null) { - return Collections.emptyList(); - } - - String dbUrl = clientPreferences.getConnectionUrl(); - if (dbUrl == null) { - dbUrl = ""; - } - - return Collections.singletonList(new CompletionInfo(dbUrl)); - } - - public void bindCommonPaths(CommonPaths commonPaths) { - clientPreferences = new ClientPreferences(commonPaths); - } - - public void unbindCommonPaths(CommonPaths commonPaths) { - clientPreferences = null; - } - - /* Testing only */ - void setClientPreferences(ClientPreferences clientPreferences) { - this.clientPreferences = clientPreferences; - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/FileNameCompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompleterService; -import com.redhat.thermostat.common.cli.FileNameTabCompleter; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.ReferenceCardinality; -import org.apache.felix.scr.annotations.ReferencePolicy; -import org.apache.felix.scr.annotations.Service; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -@Component(immediate = true) -@Service -public class FileNameCompleterService implements CompleterService { - - public static final CliCommandOption FILENAME_OPTION = new CliCommandOption("f", "filename", true, "path/to/local/file", false); - - @Reference(policy = ReferencePolicy.STATIC, cardinality = ReferenceCardinality.MANDATORY_UNARY) - private FileNameTabCompleter fileNameTabCompleter; - - @Override - public Set<String> getCommands() { - return TabCompletion.ALL_COMMANDS_COMPLETER; - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - if (fileNameTabCompleter == null) { - return Collections.emptyMap(); - } - return Collections.singletonMap(FILENAME_OPTION, fileNameTabCompleter); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } - - public void bindFileNameTabCompleter(FileNameTabCompleter fileNameTabCompleter) { - this.fileNameTabCompleter = fileNameTabCompleter; - } - - public void unbindFileNameTabCompleter(FileNameTabCompleter fileNameTabCompleter) { - this.fileNameTabCompleter = null; - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommandCompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.AbstractCompleterService; -import com.redhat.thermostat.common.cli.AbstractCompletionFinder; -import com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompletionFinderTabCompleter; -import com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.common.cli.DependencyServices; -import com.redhat.thermostat.common.cli.TabCompleter; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public class HelpCommandCompleterService extends AbstractCompleterService { - - @Override - public Set<String> getCommands() { - return Collections.singleton(HelpCommand.COMMAND_NAME); - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - return Collections.singletonMap(CliCommandOption.POSITIONAL_ARG_COMPLETION, - new CompletionFinderTabCompleter(new HelpCommandCompletionFinder(dependencyServices))); - } - - public void bindCommandInfoSource(CommandInfoSource commandInfoSource) { - setService(CommandInfoSource.class, commandInfoSource); - } - - public void unbindCommandInfoSource() { - unsetService(CommandInfoSource.class); - } - - static class HelpCommandCompletionFinder extends AbstractCompletionFinder { - - HelpCommandCompletionFinder(DependencyServices dependencyServices) { - super(dependencyServices); - } - - @Override - protected Class<?>[] getRequiredDependencies() { - return new Class<?>[]{CommandInfoSource.class}; - } - - @Override - public List<CompletionInfo> findCompletions() { - if (!allDependenciesAvailable()) { - return Collections.emptyList(); - } - List<CompletionInfo> result = new ArrayList<>(); - for (CommandInfo info : getService(CommandInfoSource.class).getCommandInfos()) { - result.add(new CompletionInfo(info.getName())); - } - return result; - } - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/JLineCompleterWrapper.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.TabCompleter; -import jline.console.completer.Completer; - -import java.util.List; - -public class JLineCompleterWrapper implements TabCompleter { - - private final Completer jlineCompleter; - - public JLineCompleterWrapper(Completer jlineCompleter) { - this.jlineCompleter = jlineCompleter; - } - - @Override - public int complete(String buffer, int cursor, List<CharSequence> candidates) { - return jlineCompleter.complete(buffer, cursor, candidates); - } -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleter.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.FileNameTabCompleter; -import jline.console.completer.FileNameCompleter; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Service; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -@Component -@Service(FileNameTabCompleter.class) -public class JLineFileNameCompleter extends JLineCompleterWrapper implements FileNameTabCompleter { - - public JLineFileNameCompleter() { - super(new ThermostatFileNameCompleter()); - } - - static class ThermostatFileNameCompleter extends FileNameCompleter { - @Override - protected int matchFiles(final String buffer, final String translated, final File[] files, final List<CharSequence> candidates) { - // overridden for test access only - return super.matchFiles(buffer, translated, files, candidates); - } - - @Override - protected CharSequence render(File file, CharSequence name) { - // when super.matchFiles calls this method, the 'name' parameter passed is equal to - // file.getName() + ' '. For directories, we want to include the File.separator character - // after the directory name, to indicate that this is a directory, and remove the whitespace - // to allow for more fluid completion of file paths. - return file.isDirectory() ? file.getName() + File.separator : name; - } - } -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LogLevelCompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompleterService; -import com.redhat.thermostat.common.cli.StringsTabCompleter; -import com.redhat.thermostat.common.cli.TabCompleter; -import com.redhat.thermostat.common.utils.LoggingUtils; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Service; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; - -@Component(immediate = true) -@Service -public class LogLevelCompleterService implements CompleterService { - - public static final CliCommandOption LOG_LEVEL_OPTION = new CliCommandOption("l", "logLevel", true, "log level", false); - - static final List<String> LOG_LEVELS = new ArrayList<>(); - - static { - for (LoggingUtils.LogLevel level : LoggingUtils.LogLevel.values()) { - LOG_LEVELS.add(level.getLevel().getName()); - } - } - - @Override - public Set<String> getCommands() { - return TabCompletion.ALL_COMMANDS_COMPLETER; - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - return Collections.singletonMap(LOG_LEVEL_OPTION, (TabCompleter) new StringsTabCompleter(LOG_LEVELS)); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PingCommandCompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompleterService; -import com.redhat.thermostat.common.cli.CompletionFinderTabCompleter; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -/* FIXME: the Ping command should be provided by a separate plugin and have a thermostat-plugin.xml of its own. -* http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=2876 -* http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=2877 -*/ -@Component(immediate = true) -@Service -public class PingCommandCompleterService implements CompleterService { - - @Reference - private AgentIdsFinder agentIdsFinder; - - @Override - public Set<String> getCommands() { - return Collections.singleton("ping"); - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - if (agentIdsFinder == null) { - return Collections.emptyMap(); - } - return Collections.singletonMap(CliCommandOption.POSITIONAL_ARG_COMPLETION, new CompletionFinderTabCompleter(agentIdsFinder)); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } - - public void bindAgentIdsFinder(AgentIdsFinder agentIdsFinder) { - this.agentIdsFinder = agentIdsFinder; - } - - public void unbindAgentIdsFinder(AgentIdsFinder agentIdsFinder) { - this.agentIdsFinder = null; - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/TabCompletion.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,347 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompleterService; -import com.redhat.thermostat.common.cli.TabCompleter; -import jline.console.ConsoleReader; -import jline.console.completer.Completer; -import org.apache.commons.cli.Option; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static com.redhat.thermostat.launcher.internal.TreeCompleter.createStringNode; - -public class TabCompletion { - - private static final String LONG_OPTION_PREFIX = "--"; - private static final String SHORT_OPTION_PREFIX = "-"; - - /** - * Not intended to be available to plugins, only built-ins such as vmId and agentId completion. - * This is why reference equality is used when checking for this constant, otherwise it is too - * easy for plugins to "hijack" this class and offer completions for commands from other plugins. - */ - static final Set<String> ALL_COMMANDS_COMPLETER = Collections.singleton("ALL_COMMANDS_COMPLETER"); - - private TreeCompleter treeCompleter; - private Map<String, TreeCompleter.Node> commandMap; - private Map<TreeCompleter.Node, Set<TreeCompleter.Node>> subcommandMap; - - public TabCompletion() { - this(new TreeCompleter(), new HashMap<String, TreeCompleter.Node>(), - new HashMap<TreeCompleter.Node, Set<TreeCompleter.Node>>()); - } - - /* - * Testing only - */ - TabCompletion(TreeCompleter treeCompleter, Map<String, TreeCompleter.Node> commandMap, - Map<TreeCompleter.Node, Set<TreeCompleter.Node>> subcommandMap) { - this.treeCompleter = treeCompleter; - this.commandMap = commandMap; - this.subcommandMap = subcommandMap; - - treeCompleter.setAlphabeticalCompletions(true); - } - - public void addCompleterService(CompleterService service) { - for (String commandName : getCommandsForService(service)) { - TreeCompleter.Node commandNode = getCommandByName(commandName); - addCompleterServiceImpl(commandNode, service); - } - } - - private Set<String> getCommandsForService(CompleterService service) { - if (ALL_COMMANDS_COMPLETER == service.getCommands()) { - return commandMap.keySet(); - } else { - return service.getCommands(); - } - } - - private TreeCompleter.Node getCommandByName(String commandName) { - if (!commandMap.containsKey(commandName)) { - TreeCompleter.Node command = createStringNode(commandName); - commandMap.put(commandName, command); - } - return commandMap.get(commandName); - } - - private void addCompleterServiceImpl(TreeCompleter.Node commandNode, CompleterService service) { - addTopLevelCommandOptionCompletions(commandNode, service); - addSubCommandCompletionsIfRequired(commandNode, service); - } - - private void addTopLevelCommandOptionCompletions(TreeCompleter.Node commandNode, CompleterService service) { - for (Map.Entry<CliCommandOption, ? extends TabCompleter> entry : service.getOptionCompleters().entrySet()) { - CliCommandOption cliCommandOption = entry.getKey(); - if (cliCommandOption == CliCommandOption.POSITIONAL_ARG_COMPLETION) { - TreeCompleter.Node node = new TreeCompleter.Node(commandNode.getTag() + " completer", entry.getValue()); - node.setRestartNode(commandNode); - commandNode.addBranch(node); - } - if (completerIsApplicable(cliCommandOption, commandNode)) { - TreeCompleter.Node completionNode = new TreeCompleter.Node(commandNode.getTag() + " completer", entry.getValue()); - completionNode.setRestartNode(commandNode); - addNodeByOption(commandNode, cliCommandOption, completionNode); - } - } - } - - private static boolean completerIsApplicable(CliCommandOption option, TreeCompleter.Node node) { - boolean matchesLongOpt = false; - boolean matchesShortOpt = false; - for (TreeCompleter.Node branch : node.getBranches()) { - if (branch.getTag().equals("--" + option.getLongOpt())) { - matchesLongOpt = true; - } else if (branch.getTag().equals("-" + option.getOpt())) { - matchesShortOpt = true; - } - } - return matchesLongOpt && matchesShortOpt; - } - - private boolean addNodeByOption(TreeCompleter.Node parent, CliCommandOption option, TreeCompleter.Node toAdd) { - TreeCompleter.Node shortOptChild = findChildNode(parent, "-" + option.getOpt()); - TreeCompleter.Node longOptChild = findChildNode(parent, "--" + option.getLongOpt()); - if (shortOptChild == null || longOptChild == null) { - return false; - } - shortOptChild.addBranch(toAdd); - longOptChild.addBranch(toAdd); - return true; - } - - private TreeCompleter.Node findChildNode(TreeCompleter.Node parent, String childTag) { - for (TreeCompleter.Node node : parent.getBranches()) { - if (node.getTag().equals(childTag)) { - return node; - } - } - return null; - } - - private void addSubCommandCompletionsIfRequired(TreeCompleter.Node commandNode, CompleterService service) { - for (TreeCompleter.Node subcommand : getSubcommands(commandNode)) { - addTopLevelCommandOptionCompletions(subcommand, service); - } - Map<String, Map<CliCommandOption, ? extends TabCompleter>> subcommandCompleters = service.getSubcommandCompleters(); - if (subcommandCompleters == null || subcommandCompleters.isEmpty()) { - return; - } - for (String subcommand : subcommandCompleters.keySet()) { - for (Map.Entry<CliCommandOption, ? extends TabCompleter> entry : subcommandCompleters.get(subcommand).entrySet()) { - CliCommandOption cliCommandOption = entry.getKey(); - TabCompleter completer = entry.getValue(); - TreeCompleter.Node subcommandNode = findChildNode(commandNode, subcommand); - if (subcommandNode == null) { - break; - } - TreeCompleter.Node completionNode = new TreeCompleter.Node(subcommand + " completer", completer); - completionNode.setRestartNode(commandNode); - addNodeByOption(subcommandNode, cliCommandOption, completionNode); - } - } - } - - private Set<TreeCompleter.Node> getSubcommands(TreeCompleter.Node commandNode) { - if (!subcommandMap.containsKey(commandNode)) { - return Collections.emptySet(); - } - return subcommandMap.get(commandNode); - } - - public void removeCompleterService(CompleterService service) { - for (String commandName : getCommandsForService(service)) { - TreeCompleter.Node command = commandMap.get(commandName); - if (command != null) { - removeCompleterServiceImpl(command, service); - } - } - } - - private void removeCompleterServiceImpl(TreeCompleter.Node commandNode, CompleterService service) { - removeTopLevelCommandOptionCompletions(commandNode, service); - removeSubCommandCompletionsIfRequired(commandNode, service); - } - - private void removeTopLevelCommandOptionCompletions(TreeCompleter.Node commandNode, CompleterService service) { - if (service.getOptionCompleters() == null) { - return; - } - for (CliCommandOption cliCommandOption : service.getOptionCompleters().keySet()) { - if (cliCommandOption == CliCommandOption.POSITIONAL_ARG_COMPLETION) { - commandNode.removeByTag(commandNode.getTag() + " completer"); - } else { - for (TreeCompleter.Node node : commandNode.getBranches()) { - if (node.getTag().equals("-" + cliCommandOption.getOpt()) - || node.getTag().equals("--" + cliCommandOption.getLongOpt())) { - node.clear(); - } - } - } - } - } - - private void removeSubCommandCompletionsIfRequired(TreeCompleter.Node commandNode, CompleterService service) { - for (TreeCompleter.Node subcommand : getSubcommands(commandNode)) { - removeTopLevelCommandOptionCompletions(subcommand, service); - } - Map<String, Map<CliCommandOption, ? extends TabCompleter>> subcommandCompleters = service.getSubcommandCompleters(); - if (subcommandCompleters == null || subcommandCompleters.isEmpty()) { - return; - } - for (String subcommand : subcommandCompleters.keySet()) { - TreeCompleter.Node subcommandNode = findChildNode(commandNode, subcommand); - if (subcommandNode == null) { - continue; - } - for (CliCommandOption cliCommandOption : subcommandCompleters.get(subcommand).keySet()) { - if (cliCommandOption == CliCommandOption.POSITIONAL_ARG_COMPLETION) { - subcommandNode.removeByTag(subcommandNode.getTag() + " completer"); - } else { - for (TreeCompleter.Node node : subcommandNode.getBranches()) { - if (node.getTag().equals("-" + cliCommandOption.getOpt()) - || node.getTag().equals("--" + cliCommandOption.getLongOpt())) { - node.clear(); - } - } - } - } - removeTopLevelCommandOptionCompletions(subcommandNode, service); - } - } - - public void setupTabCompletion(CommandInfoSource commandInfoSource) { - for (CommandInfo info : commandInfoSource.getCommandInfos()) { - String commandName = info.getName(); - TreeCompleter.Node command = getCommandByName(commandName); - - setupSubcommandCompletion(command, info); - - for (Option option : (Collection<Option>) info.getOptions().getOptions()) { - setupDefaultCompletion(command, option); - } - - addHelpOptionIfRequired(command); - - treeCompleter.addBranch(command); - } - } - - private void setupSubcommandCompletion(TreeCompleter.Node commandNode, CommandInfo commandInfo) { - List<PluginConfiguration.Subcommand> subcommands = commandInfo.getSubcommands(); - for (PluginConfiguration.Subcommand subcommand : subcommands) { - TreeCompleter.Node subcommandNode = createStringNode(subcommand.getName()); - subcommandNode.setRestartNode(commandNode); - for (Option option : (Collection<Option>) commandInfo.getOptions().getOptions()) { - setupDefaultCompletion(subcommandNode, option); - } - if (subcommand.getOptions() != null) { - for (Option option : (Collection<Option>) subcommand.getOptions().getOptions()) { - setupDefaultCompletion(subcommandNode, option); - } - } - commandNode.addBranch(subcommandNode); - registerSubcommand(commandNode, subcommandNode); - } - } - - private void setupDefaultCompletion(final TreeCompleter.Node command, final Option option) { - setupDefaultCompletion(command, option.getLongOpt(), LONG_OPTION_PREFIX); - setupDefaultCompletion(command, option.getOpt(), SHORT_OPTION_PREFIX); - } - - private void setupDefaultCompletion(final TreeCompleter.Node command, final String option, final String prefix) { - if (option != null) { - String optionShortName = prefix + option; - TreeCompleter.Node defaultNode = createStringNode(optionShortName); - defaultNode.setRestartNode(command); - command.addBranch(defaultNode); - } - } - - private void registerSubcommand(TreeCompleter.Node commandNode, TreeCompleter.Node subcommandNode) { - if (!subcommandMap.containsKey(commandNode)) { - subcommandMap.put(commandNode, new HashSet<TreeCompleter.Node>()); - } - subcommandMap.get(commandNode).add(subcommandNode); - } - - private void addHelpOptionIfRequired(TreeCompleter.Node command) { - if (HelpCommand.COMMAND_NAME.equals(command.getTag())) { - return; - } - TreeCompleter.Node helpNode = TreeCompleter.createStringNode("--help"); - helpNode.setRestartNode(command); - command.addBranch(helpNode); - } - - public void attachToReader(ConsoleReader reader) { - if (reader.getCompleters().isEmpty()) { - reader.addCompleter(new JLineCompleterAdapter(treeCompleter)); - } - } - - /* Testing only */ - Set<String> getKnownCommands() { - return new HashSet<>(commandMap.keySet()); - } - - private static class JLineCompleterAdapter implements Completer { - - private TabCompleter tabCompleter; - - public JLineCompleterAdapter(TabCompleter tabCompleter) { - this.tabCompleter = tabCompleter; - } - - @Override - public int complete(String s, int i, List<CharSequence> list) { - return tabCompleter.complete(s, i, list); - } - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/TreeCompleter.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,359 +0,0 @@ -/* - * Copyright 2012-2017 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 java.util.Objects.requireNonNull; -import static jline.console.completer.ArgumentCompleter.ArgumentDelimiter; -import static jline.console.completer.ArgumentCompleter.ArgumentList; -import static jline.console.completer.ArgumentCompleter.WhitespaceArgumentDelimiter; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -import com.redhat.thermostat.common.cli.StringsTabCompleter; -import com.redhat.thermostat.common.cli.TabCompleter; - -public class TreeCompleter implements TabCompleter { - - private final ArgumentDelimiter delimiter; - - private final List<Node> branches; - - private Node currentNode; - private ArgumentList list; - private boolean alphabeticalSortingEnabled = false; - - public static final int NOT_FOUND = -1; - private static final String EMPTY_SPACE = " "; - private static final Node START_NODE = null; - - /** - * This method adds a child branch to the start node - * @param child a node containing a completer - */ - public void addBranch(final Node child) { - branches.add(child); - } - - /** - * This method adds child branches to the start node - * @param branchList a list of nodes containing a completer each - */ - public void addBranches(final List<Node> branchList) { - for (final Node branch : branchList) { - addBranch(branch); - } - } - - /** - * @return the start node branches - */ - public List<Node> getBranches() { - return branches; - } - - public TreeCompleter() { - delimiter = new WhitespaceArgumentDelimiter(); - branches = new ArrayList<>(); - } - - /** - * Sets whether the completions returned by the completer will be alphabetical - * @param completeAlphabetically true will result in alphabetically sorted completions - */ - public void setAlphabeticalCompletions(boolean completeAlphabetically) { - alphabeticalSortingEnabled = completeAlphabetically; - } - - /** - * @return whether the completer will be alphabetically sorted - */ - public boolean isAlphabeticalSortingEnabled() { - return alphabeticalSortingEnabled; - } - - /** - * This method is called when attempting to tab complete - * @param buffer the input that will be tab completed - * @param cursorPosition the position of the cursorPosition within the buffer - * @param candidates the list of possible completions will get filled when found - * @return the new position of the cursorPosition, a return of NOT_FOUND means no completion - * was found or completion is finished, resulting in no change of the cursorPosition position - */ - @Override - public int complete(final String buffer, final int cursorPosition, final List<CharSequence> candidates) { - requireNonNull(candidates); - - if (cursorPosition > buffer.length()) { - return NOT_FOUND; - } - final String currentBuffer = buffer.substring(0, cursorPosition); - refreshCompleter(); - - list = delimiter.delimit(currentBuffer, cursorPosition); - if (list.getCursorArgumentIndex() < 0) { - return NOT_FOUND; - } - - int position = cursorPosition; - currentNode = traverseBranches(currentBuffer, Arrays.asList(list.getArguments())); - final List<TabCompleter> completers = getAllCompleters(currentNode); - - //Complete possible arguments off a space or the current word up to the cursor - if (currentBuffer.endsWith(EMPTY_SPACE)) { - completeList(candidates, completers); - } else { - if (currentNode != START_NODE && currentNode.getBranches().isEmpty()) { - currentNode = currentNode.getRestartNode(); - } - final List<TabCompleter> relevantCompleters = filterRelevantCompleters(completers, list.getCursorArgument()); - for (final TabCompleter completer : relevantCompleters) { - position = getInlinedCursorPosition(completer, candidates); - } - } - - if (alphabeticalSortingEnabled) { - inplaceSortAlphabetically(candidates); - } - - return position; - } - - private void inplaceSortAlphabetically(List<CharSequence> candidates) { - Collections.sort(candidates, new Comparator<CharSequence>() { - @Override - public int compare(final CharSequence t0, final CharSequence t1) { - return t0.toString().compareTo(t1.toString()); - } - }); - } - - private int getInlinedCursorPosition(final TabCompleter completer, final List<CharSequence> candidates) { - int cursor = completer.complete(list.getCursorArgument(), list.getArgumentPosition(), candidates); - return cursor + list.getBufferPosition() - list.getArgumentPosition(); - } - - private void refreshCompleter() { - currentNode = START_NODE; - } - - private Node traverseBranches(final String currentBuffer, final List<String> arguments) { - Node resultNode = START_NODE; - for (final Iterator<String> it = arguments.iterator(); it.hasNext();) { - final String arg = it.next(); - if (!it.hasNext() && !currentBuffer.endsWith(" ")) { - //inline completion detected - break; - } - - final int branchIndex = getBranchIndex(resultNode, arg); - if (branchIndex != NOT_FOUND) { - resultNode = findBranches(resultNode).get(branchIndex); - } - } - return resultNode; - } - - private void completeList(final List<CharSequence> candidates, final List<TabCompleter> completerList) { - for (final TabCompleter completer : completerList) { - completer.complete(null, 0, candidates); - } - } - - private List<TabCompleter> getAllCompleters(final Node currentNode) { - final List<TabCompleter> completersFromBranches = new ArrayList<>(); - for (final Node node : findBranches(currentNode)) { - completersFromBranches.add(node.getCompleter()); - } - return completersFromBranches; - } - - private List<Node> getChildNodesFromRestartNode(final Node node) { - final List<Node> childrenNodeList; - if (node.getRestartNode() == START_NODE) { - childrenNodeList = getBranches(); - } else { - childrenNodeList = node.getRestartNode().getBranches(); - } - currentNode = node.getRestartNode(); - return childrenNodeList; - } - - private List<TabCompleter> filterRelevantCompleters(final List<TabCompleter> completersFromBranches, final String cursorArgument) { - final List<TabCompleter> completers = new ArrayList<>(); - for (final TabCompleter branchCompleter : completersFromBranches) { - final List<CharSequence> candidates = new LinkedList<>(); - branchCompleter.complete(cursorArgument, 0, candidates); - if (!candidates.isEmpty()) { - completers.add(branchCompleter); - } - } - return completers; - } - - private List<CharSequence> findCompletions(final TabCompleter branchCompleter, String argument) { - final List<CharSequence> candidates = new LinkedList<>(); - branchCompleter.complete(argument, argument.length() - 1, candidates); - return candidates; - } - - private int getBranchIndex(final Node node, String argument) { - for (final Node branch : findBranches(node)) { - List<CharSequence> match = findCompletions(branch.getCompleter(), argument); - if (!match.isEmpty()) { - return findBranches(node).indexOf(branch); - } - } - return NOT_FOUND; - } - - private List<Node> findBranches(final Node node) { - if (node == START_NODE) { - return getBranches(); - } else { - if (node.getBranches().isEmpty()) { - return getChildNodesFromRestartNode(node); - } - List<CharSequence> candidates = getAllCandidates(node.getBranches()); - if (candidates.isEmpty()) { - return getChildNodesFromRestartNode(node); - } - return node.getBranches(); - } - } - - private List<CharSequence> getAllCandidates(final List<Node> nodes) { - List<CharSequence> candidates = new LinkedList<>(); - for (Node node : nodes) { - node.completer.complete("", 0, candidates); - } - return candidates; - } - - /** - * A class to be used with the TreeCompleter - * Each node contains a completer that will be used by the TreeCompleter - * to check completion. Each node contains branches that are possible - * paths for the completion to follow. Once completion has reached a node - * with no branches it will use the restartNode to continue to find - * any further completions. - */ - public static class Node { - private final String tag; - private final TabCompleter completer; - private final List<Node> branches; - private Node restartNode = START_NODE; - - public Node(final String tag, final TabCompleter completer) { - this.tag = requireNonNull(tag); - this.completer = requireNonNull(completer); - branches = new ArrayList<>(); - } - - public void addBranch(final Node branch) { - branches.add(branch); - } - - public String getTag() { - return tag; - } - - public TabCompleter getCompleter() { - return completer; - } - - public List<Node> getBranches() { - return branches; - } - - public void removeByTag(String tag) { - Iterator<Node> it = getBranches().iterator(); - while (it.hasNext()) { - Node node = it.next(); - if (node.getTag().equals(tag)) { - it.remove(); - } - } - } - - public void clear() { - branches.clear(); - } - - public void setRestartNode(final Node restartNode) { - this.restartNode = restartNode; - } - - public Node getRestartNode() { - return restartNode; - } - - @Override - public String toString() { - return "Node{" + - "tag='" + tag + '\'' + - '}'; - } - } - - /** - * A helper method to quickly create a node containing a strings completer - * @param strings the strings to be completed by the strings completer - * @return the node containing the string completer - */ - public static Node createStringNode(String tag, String... strings) { - return createStringNode(tag, Arrays.asList(strings)); - } - - public static Node createStringNode(String tag) { - return createStringNode(tag, tag); - } - - /** - * A helper method to quickly create a node containing a strings completer - * @param strings the strings to be completed by the strings completer - * @return the node containing the string completer - */ - public static Node createStringNode(String tag, List<String> strings) { - return new Node(tag, new StringsTabCompleter(strings)); - } -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdCompleterService.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompleterService; -import com.redhat.thermostat.common.cli.CompletionFinderTabCompleter; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -@Component(immediate = true) -@Service -public class VmIdCompleterService implements CompleterService { - - public static final CliCommandOption VM_ID_OPTION = new CliCommandOption("v", "vmId", true, "VM ID", false); - - @Reference - private VmIdsFinder vmIdsFinder; - - @Override - public Set<String> getCommands() { - return TabCompletion.ALL_COMMANDS_COMPLETER; - } - - @Override - public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - return Collections.singletonMap(VM_ID_OPTION, new CompletionFinderTabCompleter(vmIdsFinder)); - } - - @Override - public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { - return Collections.emptyMap(); - } - - void bindVmIdsFinder(VmIdsFinder vmIdsFinder) { - this.vmIdsFinder = vmIdsFinder; - } - - void unbindVmIdsFinder(VmIdsFinder vmIdsFinder) { - this.vmIdsFinder = null; - } - -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinder.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CompletionFinder; - -interface VmIdsFinder extends CompletionFinder { -}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinderImpl.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,105 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.VmId; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; -import com.redhat.thermostat.storage.model.VmInfo; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -@Component -@Service -public class VmIdsFinderImpl implements VmIdsFinder { - - @Reference - private VmInfoDAO vmInfoDao; - - @Reference - private AgentInfoDAO agentInfoDao; - - @Override - public List<CompletionInfo> findCompletions() { - if (vmInfoDao == null || agentInfoDao == null) { - return Collections.emptyList(); - } - - List<CompletionInfo> vmIds = new ArrayList<>(); - for (AgentId agentId : agentInfoDao.getAgentIds()) { - AgentInformation agentInfo = agentInfoDao.getAgentInformation(agentId); - if (agentInfo != null) { - Collection<VmId> vms = vmInfoDao.getVmIds(agentId); - for (VmId vm : vms) { - VmInfo info = vmInfoDao.getVmInfo(vm); - vmIds.add(new CompletionInfo(info.getVmId(), getUserVisibleText(info, agentInfo))); - } - } - } - return vmIds; - } - - private String getUserVisibleText(VmInfo info, AgentInformation agentInfo) { - return info.getMainClass() + "(" + info.isAlive(agentInfo).toString() + ")"; - } - - void bindVmInfoDao(VmInfoDAO vmInfoDAO) { - this.vmInfoDao = vmInfoDAO; - } - - void unbindVmInfoDao(VmInfoDAO vmInfoDAO) { - this.vmInfoDao = null; - } - - void bindAgentInfoDao(AgentInfoDAO agentInfoDAO) { - this.agentInfoDao = agentInfoDAO; - } - - void unbindAgentInfoDao(AgentInfoDAO agentInfoDAO) { - this.agentInfoDao = null; - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java Tue May 23 14:10:33 2017 -0400 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java Wed May 24 12:13:27 2017 +0200 @@ -45,7 +45,6 @@ import com.redhat.thermostat.launcher.BundleManager; import com.redhat.thermostat.launcher.Launcher; import com.redhat.thermostat.shared.config.CommonPaths; -import com.redhat.thermostat.shared.config.SSLConfiguration; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.testutils.StubBundleContext;
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/AgentIdCompleterServiceTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.junit.Before; -import org.junit.Test; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - -public class AgentIdCompleterServiceTest { - - private AgentIdCompleterService service; - private AgentIdsFinder finder; - - @Before - public void setup() { - service = new AgentIdCompleterService(); - finder = mock(AgentIdsFinder.class); - service.bindAgentIdsFinder(finder); - } - - @Test - public void testCompleterAppliesToAllCommands() { - Set<String> commands = service.getCommands(); - Set<String> expected = TabCompletion.ALL_COMMANDS_COMPLETER; - assertThat(commands, is(equalTo(expected))); - } - - @Test - public void testProvidesCompleterForAgentIdOptionOnly() { - Map<CliCommandOption, ? extends TabCompleter> map = service.getOptionCompleters(); - assertThat(map.keySet(), is(equalTo(Collections.singleton(AgentIdCompleterService.AGENT_ID_OPTION)))); - } - - @Test - public void testAgentIdCompleterIsProvided() { - TabCompleter completer = service.getOptionCompleters().get(AgentIdCompleterService.AGENT_ID_OPTION); - assertThat(completer, is(not(equalTo(null)))); - } - - @Test - @SuppressWarnings("unchecked") - public void testProvidesNoSubcommandCompletions() { - assertThat(service.getSubcommandCompleters().size(), is(0)); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/AgentIdsFinderImplTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class AgentIdsFinderImplTest { - - private AgentIdsFinderImpl finder; - - @Before - public void setup() { - finder = new AgentIdsFinderImpl(); - } - - @Test - public void testFindIds() { - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); - finder.bindAgentInfoDAO(agentInfoDAO); - - String id1 = "012345-56789"; - String id2 = "111111-22222"; - String id3 = "98765-543210"; - String id4 = "abcdef-01234564-848156"; - AgentInformation agentInfo1 = mock(AgentInformation.class); - agentInfo1.setAgentId(id1); - AgentInformation agentInfo2 = mock(AgentInformation.class); - agentInfo2.setAgentId(id2); - AgentInformation agentInfo3 = mock(AgentInformation.class); - agentInfo3.setAgentId(id3); - AgentInformation agentInfo4 = mock(AgentInformation.class); - agentInfo4.setAgentId(id4); - - Collection<AgentInformation> collection = new ArrayList<>(); - collection.add(agentInfo1); - collection.add(agentInfo2); - collection.add(agentInfo3); - collection.add(agentInfo4); - when(agentInfoDAO.getAllAgentInformation()).thenReturn((List<AgentInformation>) collection); - - List<CompletionInfo> result = finder.findCompletions(); - assertEquals(4, result.size()); - assertEquals(id1, result.get(0).getActualCompletion()); - assertEquals(id2, result.get(1).getActualCompletion()); - assertEquals(id3, result.get(2).getActualCompletion()); - assertEquals(id4, result.get(3).getActualCompletion()); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/DbUrlCompleterServiceTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.junit.Before; -import org.junit.Test; - -import java.util.Collections; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - -public class DbUrlCompleterServiceTest { - - private DbUrlCompleterService service; - private DbUrlCompleterService.DbUrlFinder finder; - - @Before - public void setup() { - service = new DbUrlCompleterService(); - finder = mock(DbUrlCompleterService.DbUrlFinder.class); - service.bindDbUrlFinder(finder); - } - - @Test - public void testGetCommandsReturnsAllCommands() { - assertThat(service.getCommands(), is(equalTo(TabCompletion.ALL_COMMANDS_COMPLETER))); - } - - @Test - @SuppressWarnings("unchecked") - public void testGetSubcommandCompletionsIsEmpty() { - assertThat(service.getSubcommandCompleters().size(), is(0)); - } - - @Test - public void testGetOptionCompleters() { - Map<CliCommandOption, ? extends TabCompleter> completerMap = service.getOptionCompleters(); - assertThat(completerMap.size(), is(1)); - assertThat(completerMap.keySet(), is(equalTo(Collections.singleton(DbUrlCompleterService.DB_URL_OPTION)))); - assertNotNull(completerMap.get(DbUrlCompleterService.DB_URL_OPTION)); - } - - @Test - @SuppressWarnings("unchecked") - public void testReturnsNoOptionCompletersIfDbUrlFinderNotBound() { - service.unbindDbUrlFinder(finder); - assertThat(service.getOptionCompleters().size(), is(0)); - } - - @Test - public void testDbUrlOption() { - assertThat(DbUrlCompleterService.DB_URL_OPTION.getOpt(), is(equalTo("d"))); - assertThat(DbUrlCompleterService.DB_URL_OPTION.getLongOpt(), is(equalTo("dbUrl"))); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/DbUrlFinderImplTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.common.config.ClientPreferences; -import org.junit.Before; -import org.junit.Test; - -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class DbUrlFinderImplTest { - - private DbUrlFinderImpl finder; - - @Before - public void setup() { - finder = new DbUrlFinderImpl(); - } - - @Test - public void testDbUrlCompleter() { - ClientPreferences prefs = mock(ClientPreferences.class); - finder.setClientPreferences(prefs); - - String partialUrl = "https://ip.addr"; - String fullUrl = partialUrl + "ess.example.com:25813/thermostat/storage"; - when(prefs.getConnectionUrl()).thenReturn(fullUrl); - - List<CompletionInfo> completions = finder.findCompletions(); - - assertEquals(1, completions.size()); - assertEquals(fullUrl, completions.get(0).getCompletionWithUserVisibleText().trim()); - } - - @Test - public void testReturnsNoResultsIfClientPreferencesAreNull() { - finder.unbindCommonPaths(null); - finder.setClientPreferences(null); - List<CompletionInfo> completions = finder.findCompletions(); - assertThat(completions, is(equalTo(Collections.<CompletionInfo>emptyList()))); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/FileNameCompleterServiceTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,100 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.FileNameTabCompleter; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.junit.Before; -import org.junit.Test; - -import java.util.Collections; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - -public class FileNameCompleterServiceTest { - - private FileNameCompleterService completerService; - - @Before - public void setup() { - completerService = new FileNameCompleterService(); - completerService.bindFileNameTabCompleter(mock(FileNameTabCompleter.class)); - } - - @Test - public void testProvidesAllCommandsCompletion() { - assertThat(completerService.getCommands(), is(equalTo(TabCompletion.ALL_COMMANDS_COMPLETER))); - } - - @Test - public void testProvidesOnlyOneCompletion() { - Map<CliCommandOption, ? extends TabCompleter> map = completerService.getOptionCompleters(); - assertThat(map.size(), is(1)); - } - - @Test - public void testProvidesCompletionForFileNameArguments() { - Map<CliCommandOption, ? extends TabCompleter> map = completerService.getOptionCompleters(); - assertThat(map.keySet(), is(equalTo(Collections.singleton(FileNameCompleterService.FILENAME_OPTION)))); - } - - @Test - public void testFileNameCompleterIsNotNull() { - Map<CliCommandOption, ? extends TabCompleter> map = completerService.getOptionCompleters(); - assertThat(map.get(FileNameCompleterService.FILENAME_OPTION), is(not(equalTo(null)))); - } - - @Test - public void testFileNameOptionArgumentName() { - assertThat(FileNameCompleterService.FILENAME_OPTION.getLongOpt(), is("filename")); - assertThat(FileNameCompleterService.FILENAME_OPTION.getOpt(), is("f")); - } - - @Test - @SuppressWarnings("unchecked") - public void testProvidesNoCompleterIfFileNameTabCompleterUnavailable() { - completerService.unbindFileNameTabCompleter(mock(FileNameTabCompleter.class)); - assertThat(completerService.getOptionCompleters().size(), is(0)); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandCompleterServiceTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,142 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.common.cli.DependencyServices; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.runners.Enclosed; -import org.junit.runner.RunWith; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@RunWith(Enclosed.class) -public class HelpCommandCompleterServiceTest { - - public static class CompleterServiceTest { - - private CommandInfoSource commandInfoSource; - private HelpCommandCompleterService service; - - @Before - public void setup() { - this.commandInfoSource = mock(CommandInfoSource.class); - - this.service = new HelpCommandCompleterService(); - this.service.bindCommandInfoSource(commandInfoSource); - } - - @Test - public void testOnlyProvidesCompletionForHelpCommand() { - assertThat(service.getCommands(), is(equalTo(Collections.singleton(HelpCommand.COMMAND_NAME)))); - } - - @Test - public void testOnlyProvidesPositionalArgumentCompletions() { - Map<CliCommandOption, ? extends TabCompleter> completerMap = service.getOptionCompleters(); - assertThat(completerMap.keySet(), is(equalTo(Collections.singleton(CliCommandOption.POSITIONAL_ARG_COMPLETION)))); - assertThat(completerMap.get(CliCommandOption.POSITIONAL_ARG_COMPLETION), is(not(equalTo(null)))); - } - - } - - public static class HelpCommandCompletionFinderTest { - - private CommandInfoSource commandInfoSource; - private DependencyServices dependencyServices; - private HelpCommandCompleterService.HelpCommandCompletionFinder finder; - - @Before - public void setup() { - List<CommandInfo> commandInfos = new ArrayList<>(); - CommandInfo info1 = mock(CommandInfo.class); - when(info1.getName()).thenReturn("foo-command"); - commandInfos.add(info1); - CommandInfo info2 = mock(CommandInfo.class); - when(info2.getName()).thenReturn("bar-command"); - commandInfos.add(info2); - - this.commandInfoSource = mock(CommandInfoSource.class); - when(commandInfoSource.getCommandInfos()).thenReturn(commandInfos); - - this.dependencyServices = mock(DependencyServices.class); - when(dependencyServices.hasService(CommandInfoSource.class)).thenReturn(true); - when(dependencyServices.getService(CommandInfoSource.class)).thenReturn(commandInfoSource); - - this.finder = new HelpCommandCompleterService.HelpCommandCompletionFinder(dependencyServices); - } - - @Test - public void testGetRequiredDependencies() { - assertThat(finder.getRequiredDependencies(), is(equalTo(new Class<?>[]{CommandInfoSource.class}))); - } - - @Test - public void testReturnsEmptyWhenCommandInfoSourceUnavailable() { - when(dependencyServices.hasService(CommandInfoSource.class)).thenReturn(false); - when(dependencyServices.getService(CommandInfoSource.class)).thenReturn(null); - - assertThat(finder.findCompletions(), is(equalTo(Collections.<CompletionInfo>emptyList()))); - } - - @Test - public void testReturnsSimpleCompletionInfosWithCommandNames() { - List<CompletionInfo> completionInfos = finder.findCompletions(); - - List<CompletionInfo> expected = - Arrays.asList(new CompletionInfo("foo-command"), new CompletionInfo("bar-command")); - - assertThat(completionInfos, is(equalTo(expected))); - } - - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleterTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -/* - * Copyright 2012-2017 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 org.junit.Before; -import org.junit.Test; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class JLineFileNameCompleterTest { - - private JLineFileNameCompleter.ThermostatFileNameCompleter completer; - - @Before - public void setup() { - completer = new JLineFileNameCompleter.ThermostatFileNameCompleter(); - } - - @Test - public void testAppendsFileSeparatorToDirectoriesAndNotFiles() { - String cwd = File.separator + "foo" + File.separator; - - File file = mock(File.class); - when(file.getName()).thenReturn("file"); - when(file.getAbsolutePath()).thenReturn(cwd + "file"); - when(file.isFile()).thenReturn(true); - when(file.isDirectory()).thenReturn(false); - - File dir = mock(File.class); - when(dir.getName()).thenReturn("dir"); - when(dir.getAbsolutePath()).thenReturn(cwd + "dir"); - when(dir.isFile()).thenReturn(false); - when(dir.isDirectory()).thenReturn(true); - - List<CharSequence> candidates = new ArrayList<>(); - completer.matchFiles("", cwd, new File[]{file, dir}, candidates); - - assertThat(candidates.size(), is(2)); - assertTrue(candidates.contains("file ")); - assertTrue(candidates.contains("dir" + File.separator)); - } - - @Test - public void testAppendsSpaceIfOnlyOneMatch() { - String cwd = File.separator + "foo" + File.separator; - - File file = mock(File.class); - when(file.getName()).thenReturn("file"); - when(file.getAbsolutePath()).thenReturn(cwd + "file"); - when(file.isFile()).thenReturn(true); - when(file.isDirectory()).thenReturn(false); - - List<CharSequence> candidates = new ArrayList<>(); - completer.matchFiles("", cwd, new File[]{file}, candidates); - - assertThat(candidates.size(), is(1)); - assertTrue(candidates.contains("file ")); - } - - @Test - public void testNoResultsIfNoMatches() { - String cwd = File.separator + "foo" + File.separator; - - List<CharSequence> candidates = new ArrayList<>(); - completer.matchFiles("", cwd, new File[]{}, candidates); - - assertThat(candidates.size(), is(0)); - } - - @Test - public void testReturnsZeroIfFilesNull() { - assertThat(completer.matchFiles("", "", null, new ArrayList<CharSequence>()), is(-1)); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LogLevelCompleterServiceTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,113 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.TabCompleter; -import com.redhat.thermostat.common.utils.LoggingUtils; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; - -public class LogLevelCompleterServiceTest { - - private LogLevelCompleterService service; - - @Before - public void setup() { - service = new LogLevelCompleterService(); - } - - @Test - public void testLogLevelsAreComplete() { - LoggingUtils.LogLevel[] levels = LoggingUtils.LogLevel.values(); - List<String> expected = new ArrayList<>(); - for (LoggingUtils.LogLevel level : levels) { - expected.add(level.getLevel().getName()); - } - Collections.sort(expected, String.CASE_INSENSITIVE_ORDER); - - List<String> values = new ArrayList<>(LogLevelCompleterService.LOG_LEVELS); - Collections.sort(values, String.CASE_INSENSITIVE_ORDER); - - assertThat(values, is(equalTo(expected))); - - // check for no duplicates, order doesn't really matter - Set<String> expectedSet = new HashSet<>(expected); - Set<String> valuesSet = new HashSet<>(values); - - assertThat(valuesSet, is(equalTo(expectedSet))); - assertThat(valuesSet.size(), is(values.size())); - } - - @Test - public void testCompleterAppliesToAllCommands() { - Set<String> commands = service.getCommands(); - Set<String> expected = TabCompletion.ALL_COMMANDS_COMPLETER; - assertThat(commands, is(equalTo(expected))); - } - - @Test - public void testProvidesCompleterForLogLevelOptionOnly() { - Map<CliCommandOption, ? extends TabCompleter> map = service.getOptionCompleters(); - assertThat(map.keySet(), is(equalTo(Collections.singleton(LogLevelCompleterService.LOG_LEVEL_OPTION)))); - } - - @Test - public void testLogLevelCompleterIsProvided() { - TabCompleter completer = service.getOptionCompleters().get(LogLevelCompleterService.LOG_LEVEL_OPTION); - assertThat(completer, is(not(equalTo(null)))); - } - - @Test - @SuppressWarnings("unchecked") - public void testProvidesNoSubcommandCompletions() { - assertThat(service.getSubcommandCompleters().size(), is(0)); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PingCommandCompleterServiceTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.TabCompleter; -import com.redhat.thermostat.storage.core.AgentId; -import org.junit.Before; -import org.junit.Test; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - -public class PingCommandCompleterServiceTest { - - private PingCommandCompleterService service; - private AgentIdsFinder finder; - - @Before - public void setup() { - service = new PingCommandCompleterService(); - finder = mock(AgentIdsFinder.class); - service.bindAgentIdsFinder(finder); - } - - @Test - public void testCompleterAppliesToPingCommandOnly() { - Set<String> commands = service.getCommands(); - Set<String> expected = Collections.singleton("ping"); - assertThat(commands, is(equalTo(expected))); - } - - @Test - public void testProvidesCompleterForPingMainArgOptionOnly() { - Map<CliCommandOption, ? extends TabCompleter> map = service.getOptionCompleters(); - assertThat(map.keySet(), is(equalTo(Collections.singleton(CliCommandOption.POSITIONAL_ARG_COMPLETION)))); - } - - @Test - public void testPingMainArgCompleterIsProvided() { - TabCompleter completer = service.getOptionCompleters().get(CliCommandOption.POSITIONAL_ARG_COMPLETION); - assertThat(completer, is(not(equalTo(null)))); - } - - @Test - @SuppressWarnings("unchecked") - public void testProvidesNoSubcommandCompletions() { - assertThat(service.getSubcommandCompleters().size(), is(0)); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/TabCompletionTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,342 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.CompleterService; -import com.redhat.thermostat.common.cli.TabCompleter; -import com.redhat.thermostat.launcher.BundleInformation; -import jline.console.ConsoleReader; -import jline.console.completer.Completer; -import org.apache.commons.cli.Options; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.isA; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -public class TabCompletionTest { - - private TabCompletion tabCompletion; - private TreeCompleter treeCompleter; - private Map<String, TreeCompleter.Node> commandMap; - private Map<TreeCompleter.Node, Set<TreeCompleter.Node>> subcommandMap; - - @Before - @SuppressWarnings("unchecked") - public void setup() { - treeCompleter = mock(TreeCompleter.class); - commandMap = new HashMap<>(); - subcommandMap = new HashMap<>(); - tabCompletion = new TabCompletion(treeCompleter, commandMap, subcommandMap); - } - - @Test - public void verifyAddCompleterServiceDoesNothingWhenCommandMapEmpty() { - CompleterService service = mock(CompleterService.class); - tabCompletion.addCompleterService(service); - verify(treeCompleter).setAlphabeticalCompletions(true); - verifyNoMoreInteractions(treeCompleter); - assertThat(commandMap.isEmpty(), is(true)); - } - - @Test - public void verifyRemoveCompleterServiceDoesNothingWhenCommandMapEmpty() { - CompleterService service = mock(CompleterService.class); - tabCompletion.removeCompleterService(service); - verify(treeCompleter).setAlphabeticalCompletions(true); - verifyNoMoreInteractions(treeCompleter); - assertThat(commandMap.isEmpty(), is(true)); - } - - @Test - @SuppressWarnings("unchecked") - public void testAddCompleterServiceWorks() { - doSetupTabCompletion(); - CompleterService service = mock(CompleterService.class); - when(service.getCommands()).thenReturn(Collections.singleton("mock-command")); - Map completerMap = new HashMap(); - TabCompleter completer = mock(TabCompleter.class); - completerMap.put(new CliCommandOption("l", "long", true, "long option", false), completer); - completerMap.put(new CliCommandOption("s", "short", true, "short option", false), completer); - when(service.getOptionCompleters()).thenReturn(completerMap); - Map subcommandMap = new HashMap(); - String subcommand = "sub"; - Map subcommandOptionMap = new HashMap(); - CliCommandOption subcommandOption = new CliCommandOption("f", "foo", true, "foo option", false); - subcommandOptionMap.put(subcommandOption, completer); - subcommandMap.put(subcommand, subcommandOptionMap); - when(service.getSubcommandCompleters()).thenReturn(subcommandMap); - - tabCompletion.addCompleterService(service); - - TreeCompleter.Node node = commandMap.get("mock-command"); - assertThat(node, is(not(equalTo(null)))); - for (TreeCompleter.Node branch : node.getBranches()) { - if (branch.getTag().equals("--long")) { - assertThat(branch.getBranches().isEmpty(), is(false)); - } else if (branch.getTag().equals("-l")) { - assertThat(branch.getBranches().isEmpty(), is(false)); - } else if (branch.getTag().equals("-s")) { - assertThat(branch.getBranches().isEmpty(), is(false)); - } else if (branch.getTag().equals("--short")) { - assertThat(branch.getBranches().isEmpty(), is(false)); - } else if (branch.getTag().equals("--help")) { - // pass - } else if (branch.getTag().equals("sub")) { - List<TreeCompleter.Node> subBranches = branch.getBranches(); - Set<String> expected = new HashSet<>(Arrays.asList("--long", "-l", "--short", "-s")); - Set<String> actual = new HashSet<>(); - for (TreeCompleter.Node n : subBranches) { - actual.add(n.getTag()); - } - assertThat(actual, is(equalTo(expected))); - } else { - fail("should not reach here. branch tag: " + branch.getTag()); - } - } - } - - @Test - @SuppressWarnings("unchecked") - public void testRemoveCompleterServiceWorks() { - doSetupTabCompletion(); - CompleterService service = mock(CompleterService.class); - when(service.getCommands()).thenReturn(Collections.singleton("mock-command")); - Map completerMap = new HashMap(); - TabCompleter completer = mock(TabCompleter.class); - completerMap.put(new CliCommandOption("l", "long", true, "long option", false), completer); - completerMap.put(new CliCommandOption("s", "short", true, "short option", false), completer); - when(service.getOptionCompleters()).thenReturn(completerMap); - - tabCompletion.addCompleterService(service); - tabCompletion.removeCompleterService(service); - - TreeCompleter.Node node = commandMap.get("mock-command"); - assertThat(node, is(not(equalTo(null)))); - for (TreeCompleter.Node branch : node.getBranches()) { - if (branch.getTag().equals("--long")) { - assertThat(branch.getBranches().isEmpty(), is(true)); - } else if (branch.getTag().equals("-l")) { - assertThat(branch.getBranches().isEmpty(), is(true)); - } else if (branch.getTag().equals("--short")) { - assertThat(branch.getBranches().isEmpty(), is(true)); - } else if (branch.getTag().equals("-s")) { - assertThat(branch.getBranches().isEmpty(), is(true)); - } else if (branch.getTag().equals("--help")) { - // pass - } else if (branch.getTag().equals("sub")) { - for (TreeCompleter.Node subcommandCompletionNode : branch.getBranches()) { - assertThat(subcommandCompletionNode.getBranches().isEmpty(), is(true)); - } - } else { - fail("should not reach here"); - } - } - } - - @Test - public void testAddCompleterWithConflictingShortOpt() { - ConsoleReader reader = mock(ConsoleReader.class); - - Options mockOptions = new Options(); - mockOptions.addOption("a", "agentId", true, "Agent ID"); - - Options fakeOptions = new Options(); - fakeOptions.addOption("a", "all", false, "all data"); - - CommandInfo mockCommand = mock(CommandInfo.class); - when(mockCommand.getName()).thenReturn("mock-command"); - when(mockCommand.getBundles()).thenReturn(Collections.<BundleInformation>emptyList()); - when(mockCommand.getDescription()).thenReturn("description"); - when(mockCommand.getOptions()).thenReturn(mockOptions); - - CommandInfo fakeCommand = mock(CommandInfo.class); - when(fakeCommand.getName()).thenReturn("fake-command"); - when(fakeCommand.getBundles()).thenReturn(Collections.<BundleInformation>emptyList()); - when(fakeCommand.getDescription()).thenReturn("description"); - when(fakeCommand.getOptions()).thenReturn(fakeOptions); - - CommandInfoSource infoSource = mock(CommandInfoSource.class); - when(infoSource.getCommandInfos()).thenReturn(Arrays.asList(mockCommand, fakeCommand)); - when(infoSource.getCommandInfo(mockCommand.getName())).thenReturn(mockCommand); - when(infoSource.getCommandInfo(fakeCommand.getName())).thenReturn(fakeCommand); - - CompleterService service = mock(CompleterService.class); - when(service.getCommands()).thenReturn(TabCompletion.ALL_COMMANDS_COMPLETER); - Map completerMap = new HashMap(); - TabCompleter completer = mock(TabCompleter.class); - completerMap.put(new CliCommandOption("a", "agentId", true, "Agent ID", false), completer); - when(service.getOptionCompleters()).thenReturn(completerMap); - - tabCompletion.setupTabCompletion(infoSource); - tabCompletion.attachToReader(reader); - tabCompletion.addCompleterService(service); - - TreeCompleter.Node mockNode = commandMap.get("mock-command"); - assertThat(mockNode, is(not(equalTo(null)))); - for (TreeCompleter.Node branch : mockNode.getBranches()) { - if (branch.getTag().equals("--agentId")) { - assertThat(branch.getBranches(), is(not(equalTo(Collections.<TreeCompleter.Node>emptyList())))); - } else if (branch.getTag().equals("-a")) { - assertThat(branch.getBranches(), is(not(equalTo(Collections.<TreeCompleter.Node>emptyList())))); - } - } - - TreeCompleter.Node fakeNode = commandMap.get("fake-command"); - assertThat(fakeNode, is(not(equalTo(null)))); - for (TreeCompleter.Node branch : fakeNode.getBranches()) { - if (branch.getTag().equals("--all")) { - assertThat(branch.getBranches(), is(equalTo(Collections.<TreeCompleter.Node>emptyList()))); - } else if (branch.getTag().equals("-a")) { - assertThat(branch.getBranches(), is(equalTo(Collections.<TreeCompleter.Node>emptyList()))); - } - } - } - - @Test - public void testAttachToReader() { - ConsoleReader reader = mock(ConsoleReader.class); - when(reader.getCompleters()).thenReturn(Collections.<Completer>emptyList()); - tabCompletion.attachToReader(reader); - verify(reader).getCompleters(); - verify(reader).addCompleter(isA(Completer.class)); - } - - @Test - public void testSetupCorrectlyPopulatesKnownCommands() { - doSetupTabCompletion(); - assertThat((HashSet<String>) tabCompletion.getKnownCommands(), - is(equalTo(new HashSet<>(Arrays.asList("mock-command", "fake-command"))))); - } - - private void doSetupTabCompletion() { - Options options = new Options(); - options.addOption("s", "short", true, "short option"); - options.addOption("l", "long", true, "long option"); - - PluginConfiguration.Subcommand subcommand = mock(PluginConfiguration.Subcommand.class); - when(subcommand.getName()).thenReturn("sub"); - when(subcommand.getDescription()).thenReturn("description"); - when(subcommand.getOptions()).thenReturn(options); - - CommandInfo mockCommand = mock(CommandInfo.class); - when(mockCommand.getName()).thenReturn("mock-command"); - when(mockCommand.getBundles()).thenReturn(Collections.<BundleInformation>emptyList()); - when(mockCommand.getDescription()).thenReturn("description"); - when(mockCommand.getSubcommands()).thenReturn(Collections.singletonList(subcommand)); - when(mockCommand.getOptions()).thenReturn(options); - - CommandInfo fakeCommand = mock(CommandInfo.class); - when(fakeCommand.getName()).thenReturn("fake-command"); - when(fakeCommand.getBundles()).thenReturn(Collections.<BundleInformation>emptyList()); - when(fakeCommand.getDescription()).thenReturn("description"); - when(fakeCommand.getOptions()).thenReturn(options); - - CommandInfoSource infoSource = mock(CommandInfoSource.class); - when(infoSource.getCommandInfos()).thenReturn(Arrays.asList(mockCommand, fakeCommand)); - when(infoSource.getCommandInfo(mockCommand.getName())).thenReturn(mockCommand); - when(infoSource.getCommandInfo(fakeCommand.getName())).thenReturn(fakeCommand); - - tabCompletion.setupTabCompletion(infoSource); - - assertThat(commandMap.containsKey("mock-command"), is(true)); - assertThat(commandMap.containsKey("fake-command"), is(true)); - assertThat(commandMap.size(), is(2)); - verify(treeCompleter, times(2)).addBranch(isA(TreeCompleter.Node.class)); - } - - @Test - public void testSubcommandsReceiveParentOptionCompletions() { - Options parentOptions = new Options(); - parentOptions.addOption("p", "parent", true, "parent option"); - - Options subcommandOptions = new Options(); - subcommandOptions.addOption("s", "subcommand", true, "subcommand option"); - - PluginConfiguration.Subcommand subcommand = mock(PluginConfiguration.Subcommand.class); - when(subcommand.getName()).thenReturn("sub"); - when(subcommand.getDescription()).thenReturn("sub desc"); - when(subcommand.getOptions()).thenReturn(subcommandOptions); - - CommandInfo parent = mock(CommandInfo.class); - when(parent.getName()).thenReturn("parent"); - when(parent.getBundles()).thenReturn(Collections.<BundleInformation>emptyList()); - when(parent.getDescription()).thenReturn("parent desc"); - when(parent.getSubcommands()).thenReturn(Collections.singletonList(subcommand)); - when(parent.getOptions()).thenReturn(parentOptions); - - CommandInfoSource infoSource = mock(CommandInfoSource.class); - when(infoSource.getCommandInfos()).thenReturn(Collections.singletonList(parent)); - when(infoSource.getCommandInfo(parent.getName())).thenReturn(parent); - - tabCompletion.setupTabCompletion(infoSource); - - assertThat(commandMap.keySet(), is(equalTo(Collections.singleton(parent.getName())))); - assertThat(subcommandMap.size(), is(1)); - TreeCompleter.Node parentNode = new ArrayList<>(subcommandMap.keySet()).get(0); - assertThat(parentNode.getTag(), is("parent")); - TreeCompleter.Node subcommandNode = new ArrayList<>(new ArrayList<>(subcommandMap.values()).get(0)).get(0); - assertThat(subcommandNode.getTag(), is("sub")); - - List<TreeCompleter.Node> subcommandChildren = subcommandNode.getBranches(); - HashSet<String> childTags = new HashSet<>(); - for (TreeCompleter.Node node : subcommandChildren) { - childTags.add(node.getTag()); - } - assertThat(childTags, is(equalTo(new HashSet<>(Arrays.asList("-s", "--subcommand", "-p", "--parent"))))); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/TreeCompleterTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,594 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.launcher.internal.TreeCompleter.createStringNode; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -import com.redhat.thermostat.common.cli.CommandException; -import com.redhat.thermostat.testutils.TestUtils; -import org.junit.Before; -import org.junit.Test; - -public class TreeCompleterTest { - - private File testDir; - private TreeCompleter tree; - - @Before - public void setUp() throws IOException { - tree = new TreeCompleter(); - - testDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "treecompleter"); - testDir.deleteOnExit(); - testDir.mkdirs(); - setupTreeCompleter(); - } - - @Test - public void testInlineCompletion() throws IOException, CommandException { - List<String> candidates = completeBuffer("other"); - - assertTrue(candidates.contains("otherCommand")); - } - - @Test - public void testBaseIndexCompletion() throws IOException, CommandException { - List<String> candidates = completeBuffer(""); - - assertTrue(candidates.contains("otherCommand")); - assertTrue(candidates.contains("command1")); - assertTrue(candidates.contains("command2")); - assertTrue(candidates.contains("anotherCommand")); - } - - @Test - public void testVeryLongCompletion() throws IOException, CommandException { - String[] longChainOfWords = { "otherCommand", "create", "a", "long", "chain", "of", "tab", "completing", "words"}; - List<String> chain = new ArrayList<String>(); - Collections.addAll(chain, longChainOfWords); - String firstPart = ""; - for (String word : chain) { - firstPart += word + " "; - List<String> tabOutput = completeBuffer(firstPart + " "); - - if (chain.indexOf(word) == 0) { - assertTrue(tabOutput.contains("list")); - assertTrue(tabOutput.contains("create")); - assertTrue(tabOutput.contains("start")); - } else if (chain.indexOf(word) == chain.size() - 1) { - assertTrue(tabOutput.contains("otherCommand")); - assertTrue(tabOutput.contains("anotherCommand")); - assertTrue(tabOutput.contains("command1")); - assertTrue(tabOutput.contains("command2")); - } else { - assertTrue(tabOutput.contains(chain.get(chain.indexOf(word) + 1))); - } - - } - } - - @Test - public void testVeryLongCompletionRestartedTwice() throws IOException, CommandException { - String[] longChainOfWords = { "otherCommand", "create", "a", "long", "chain", "of", "tab", "completing", "words"}; - List<String> chain = new ArrayList<String>(); - Collections.addAll(chain, longChainOfWords); - Collections.addAll(chain, longChainOfWords); - Collections.addAll(chain, longChainOfWords); - String firstPart = ""; - for (String word : chain) { - firstPart += word + " "; - List<String> tabOutput = completeBuffer(firstPart + " "); - - if (chain.indexOf(word) == 0) { - assertTrue(tabOutput.contains("list")); - assertTrue(tabOutput.contains("create")); - assertTrue(tabOutput.contains("start")); - } else if (chain.indexOf(word) == chain.size() - 1) { - assertTrue(tabOutput.contains("otherCommand")); - assertTrue(tabOutput.contains("anotherCommand")); - assertTrue(tabOutput.contains("command1")); - assertTrue(tabOutput.contains("command2")); - } else { - assertTrue(tabOutput.contains(chain.get(chain.indexOf(word) + 1))); - } - - } - } - - @Test - public void testRestartIndexOtherThanZero() throws CommandException { - String[] input = { "command1", "list", "nothing"}; - List<String> chain = new ArrayList<String>(); - Collections.addAll(chain, input); - String firstPart = ""; - for (String word : chain) { - firstPart += word + " "; - List<String> tabOutput = completeBuffer(firstPart + " "); - - if (chain.indexOf(word) == 0) { - assertTrue(tabOutput.contains("list")); - assertTrue(tabOutput.contains("create")); - assertTrue(tabOutput.contains("delete")); - } else if (chain.indexOf(word) == chain.size() - 1) { - assertTrue(tabOutput.contains("list")); - assertTrue(tabOutput.contains("create")); - assertTrue(tabOutput.contains("delete")); - } else { - assertTrue(tabOutput.contains(chain.get(chain.indexOf(word) + 1))); - } - - } - } - - @Test - public void testCompletionOfSameTextInDifferentBranchesReturnsDifferentResults() throws CommandException { - List<String> firstOutput = completeBuffer("otherCommand list "); - List<String> secondOutput = completeBuffer("command1 list "); - - assertTrue(firstOutput.contains("parts")); - assertTrue(firstOutput.contains("assemblies")); - assertTrue(firstOutput.contains("degreesOfFreedom")); - assertTrue(firstOutput.contains("bolts")); - assertTrue(firstOutput.contains("tools")); - - assertFalse(firstOutput.contains("everything")); - assertFalse(firstOutput.contains("nothing")); - assertFalse(firstOutput.contains("firstHalf")); - assertFalse(firstOutput.contains("secondHalf")); - - assertTrue(secondOutput.contains("everything")); - assertTrue(secondOutput.contains("nothing")); - assertTrue(secondOutput.contains("firstHalf")); - assertTrue(secondOutput.contains("secondHalf")); - - assertFalse(secondOutput.contains("parts")); - assertFalse(secondOutput.contains("assemblies")); - assertFalse(secondOutput.contains("degreesOfFreedom")); - assertFalse(secondOutput.contains("bolts")); - assertFalse(secondOutput.contains("tools")); - } - - @Test - public void testRestartIndexOne() throws CommandException { - List<String> output = completeBuffer("command2 stop yes "); - - assertTrue(output.contains("find")); - assertTrue(output.contains("climb")); - assertTrue(output.contains("stop")); - } - - @Test - public void testRestartIndexTwo() throws CommandException { - List<String> output = completeBuffer("command1 list firstHalf "); - - assertTrue(output.contains("everything")); - assertTrue(output.contains("nothing")); - assertTrue(output.contains("firstHalf")); - assertTrue(output.contains("secondHalf")); - } - - @Test - public void testParametersDoNotInterfereWithInlineCompletion() throws CommandException { - List<String> inlineOutput = completeBuffer("command1 blah list blue red green notValid second"); - - assertTrue(inlineOutput.contains("secondHalf")); - } - - @Test - public void testParametersDoNotInterfereWithCompletion() throws CommandException { - List<String> output = completeBuffer("otherCommand blue green list red purple "); - - assertTrue(output.contains("parts")); - assertTrue(output.contains("assemblies")); - assertTrue(output.contains("degreesOfFreedom")); - assertTrue(output.contains("bolts")); - assertTrue(output.contains("tools")); - } - - @Test - public void testFileCompletion() throws CommandException, IOException { - String filename1 = "testFilesNow"; - String filename2 = "document"; - String filename3 = "musicFile"; - String filename4 = "slideshow"; - - createTempFile(filename1); - createTempFile(filename2); - createTempFile(filename3); - createTempFile(filename4); - - // on Windows, the backslashes look like escape characters, so we need to escape them - // (there is no effect on Linux assuming the path doesn't contain backslashes) - final String escapedDir = TestUtils.escapeBackslashes(testDir.getAbsolutePath() + File.separator); - - List<String> output = completeBuffer("command2 find " + escapedDir); - - assertTrue(output.contains(filename1)); - assertTrue(output.contains(filename2)); - assertTrue(output.contains(filename3)); - assertTrue(output.contains(filename4)); - } - - @Test - public void testFileCompletionInline() throws CommandException, IOException { - String filename1 = "testFilesNow"; - String filename2 = "document"; - String filename3 = "musicFile"; - String filename4 = "slideshow"; - - createTempFile(filename1); - createTempFile(filename2); - createTempFile(filename3); - createTempFile(filename4); - - // on Windows, the backslashes look like escape characters, so we need to escape them - // (there is no effect on Linux) - final String escapedDir = TestUtils.escapeBackslashes(testDir.getAbsolutePath() + File.separator); - - List<String> output = completeBuffer("command2 find " + escapedDir); - - assertTrue(output.contains(filename2)); - } - - @Test - public void testCursorInMiddleOfWord() { - List<String> output = completeBuffer("command1 ", 3); - - assertTrue(output.contains("command1")); - assertTrue(output.contains("command2")); - assertFalse(output.contains("otherCommand")); - assertFalse(output.contains("anotherCommand")); - } - - @Test - public void testCursorBeforeWord() { - List<String> output = completeBuffer("command1 ", 0); - - assertTrue(output.contains("command1")); - assertTrue(output.contains("command2")); - assertTrue(output.contains("otherCommand")); - assertTrue(output.contains("anotherCommand")); - } - - @Test - public void testCursorBeforeSpace() { - List<String> output = completeBuffer("command1 ", 8); - - assertTrue(output.contains("command1")); - assertFalse(output.contains("command2")); - assertFalse(output.contains("otherCommand")); - assertFalse(output.contains("anotherCommand")); - } - - @Test - public void testCursorAtSpace() { - List<String> output = completeBuffer("command1 ", 9); - - assertTrue(output.contains("list")); - assertTrue(output.contains("create")); - assertTrue(output.contains("delete")); - } - - @Test - public void testCursorAtSpaceBeforeAnotherWord() { - List<String> output = completeBuffer("command1 list", 9); - - assertTrue(output.contains("list")); - assertTrue(output.contains("create")); - assertTrue(output.contains("delete")); - } - - @Test - public void testCursorInMiddleOfSecondWord() { - List<String> output = completeBuffer("command1 list", 11); - - assertTrue(output.contains("list")); - assertFalse(output.contains("create")); - assertFalse(output.contains("delete")); - } - - @Test - public void testCursorLongerThanBufferLength() { - List<CharSequence> candidates = new LinkedList<>(); - - int cursor = tree.complete("command1 ", 503, candidates); - List<String> convertedCandidates = convertToStringList(candidates); - assertEquals(TreeCompleter.NOT_FOUND, cursor); - assertTrue(convertedCandidates.isEmpty()); - } - - @Test - public void testAlphabeticalOrder() { - tree.setAlphabeticalCompletions(true); - - List<String> output = completeBuffer(""); - - assertEquals(4, output.size()); - assertTrue(output.get(0).equals("anotherCommand")); - assertTrue(output.get(1).equals("command1")); - assertTrue(output.get(2).equals("command2")); - assertTrue(output.get(3).equals("otherCommand")); - } - - @Test - public void testAlphabeticalOrderForOptions() { - tree.setAlphabeticalCompletions(true); - - List<String> output = completeBuffer("otherCommand list "); - - assertEquals(5 ,output.size()); - assertTrue(output.get(0).equals("assemblies")); - assertTrue(output.get(1).equals("bolts")); - assertTrue(output.get(2).equals("degreesOfFreedom")); - assertTrue(output.get(3).equals("parts")); - assertTrue(output.get(4).equals("tools")); - } - - @Test - public void testAlphabeticalOrderNotSetToTrue() { - tree.setAlphabeticalCompletions(false); - - List<String> output = completeBuffer(""); - - assertEquals(4 ,output.size()); - assertFalse(output.get(0).equals("anotherCommand")); - assertFalse(output.get(1).equals("command1")); - assertFalse(output.get(2).equals("command2")); - assertFalse(output.get(3).equals("otherCommand")); - } - - @Test - public void testAlphabeticalOrderForInlineOptions() { - tree.setAlphabeticalCompletions(true); - - List<String> output = completeBuffer("anotherCommand grab dri"); - - assertEquals(12 ,output.size()); - assertTrue(output.get(0).equals("drink-apple-juice")); - assertTrue(output.get(1).equals("drink-apple-soda")); - assertTrue(output.get(2).equals("drink-banana-juice")); - assertTrue(output.get(3).equals("drink-coke")); - assertTrue(output.get(4).equals("drink-fanta")); - assertTrue(output.get(5).equals("drink-grape-soda")); - assertTrue(output.get(6).equals("drink-orange-juice")); - assertTrue(output.get(7).equals("drink-pepsi")); - assertTrue(output.get(8).equals("drink-pepsi-diet")); - assertTrue(output.get(9).equals("drink-pepsi-lime")); - assertTrue(output.get(10).equals("drink-pepsi-vanilla")); - assertTrue(output.get(11).equals("drink-pepsi-zero")); - } - - @Test - public void testAlphabeticalOrderForFurtherInlinedOptions() { - tree.setAlphabeticalCompletions(true); - - List<String> output = completeBuffer("anotherCommand grab drink-pep"); - - assertEquals(5 ,output.size()); - assertTrue(output.get(0).equals("drink-pepsi")); - assertTrue(output.get(1).equals("drink-pepsi-diet")); - assertTrue(output.get(2).equals("drink-pepsi-lime")); - assertTrue(output.get(3).equals("drink-pepsi-vanilla")); - assertTrue(output.get(4).equals("drink-pepsi-zero")); - } - - private List<String> completeBuffer(String buffer) { - return completeBuffer(buffer, buffer.length()); - } - - private List<String> completeBuffer(String buffer, int cursor) { - List<CharSequence> candidates = new LinkedList<>(); - - tree.complete(buffer, cursor, candidates); - List<String> convertedCandidates = convertToStringList(candidates); - return convertedCandidates; - } - - private List<String> convertToStringList(List<CharSequence> list) { - List<String> stringsList = new ArrayList<>(); - for (CharSequence chars : list) { - stringsList.add(chars.toString().trim()); - } - return stringsList; - } - - private void createTempFile(String name) throws IOException { - File file = new File(testDir, name); - file.deleteOnExit(); - file.createNewFile(); - } - - public void setupTreeCompleter() throws IOException { - - List<TreeCompleter.Node> commands = new ArrayList<>(); - TreeCompleter.Node command1 = createStringNode("command1"); - { - TreeCompleter.Node create = createStringNode("create"); - TreeCompleter.Node delete = createStringNode("delete"); - TreeCompleter.Node list = createStringNode("list"); - { - TreeCompleter.Node everything = createStringNode("everything"); - TreeCompleter.Node nothing = createStringNode("nothing"); - nothing.setRestartNode(command1); - TreeCompleter.Node firstHalf = createStringNode("firstHalf"); - firstHalf.setRestartNode(list); - TreeCompleter.Node secondHalf = createStringNode("secondHalf"); - secondHalf.setRestartNode(list); // maybe secondHalf - list.addBranch(everything); - list.addBranch(nothing); - list.addBranch(firstHalf); - list.addBranch(secondHalf); - } - command1.addBranch(list); - command1.addBranch(create); - command1.addBranch(delete); - } - - TreeCompleter.Node command2 = createStringNode("command2"); - { - TreeCompleter.Node find = createStringNode("find"); - { - TreeCompleter.Node files = new TreeCompleter.Node("fileName", new JLineFileNameCompleter()); - find.addBranch(files); - } - TreeCompleter.Node climb = createStringNode("climb"); - TreeCompleter.Node stop = createStringNode("stop"); - { - TreeCompleter.Node yes = createStringNode("yes"); - yes.setRestartNode(command2); - TreeCompleter.Node no = createStringNode("no"); - stop.addBranch(yes); - stop.addBranch(no); - } - command2.addBranch(find); - command2.addBranch(climb); - command2.addBranch(stop); - } - TreeCompleter.Node otherCommand = createStringNode("otherCommand"); - { - TreeCompleter.Node list = createStringNode("list"); - { - TreeCompleter.Node parts = createStringNode("parts"); - TreeCompleter.Node assemblies = createStringNode("assemblies"); - assemblies.setRestartNode(otherCommand); - TreeCompleter.Node degreesOfFreedom = createStringNode("degreesOfFreedom"); - degreesOfFreedom.setRestartNode(list); - TreeCompleter.Node bolts = createStringNode("bolts"); - bolts.setRestartNode(list); - TreeCompleter.Node tools = createStringNode("tools"); - tools.setRestartNode(list); - list.addBranch(parts); - list.addBranch(assemblies); - list.addBranch(degreesOfFreedom); - list.addBranch(bolts); - list.addBranch(tools); - } - TreeCompleter.Node create = createStringNode("create"); - { - TreeCompleter.Node a = createStringNode("a"); - TreeCompleter.Node longNode = createStringNode("long"); - TreeCompleter.Node chain = createStringNode("chain"); - TreeCompleter.Node of = createStringNode("of"); - TreeCompleter.Node tab = createStringNode("tab"); - TreeCompleter.Node completing = createStringNode("completing"); - TreeCompleter.Node words = createStringNode("words"); - completing.addBranch(words); - tab.addBranch(completing); - of.addBranch(tab); - chain.addBranch(of); - longNode.addBranch(chain); - a.addBranch(longNode); - create.addBranch(a); - } - TreeCompleter.Node start = createStringNode("start"); - { - TreeCompleter.Node yes = createStringNode("yes"); - yes.setRestartNode(otherCommand); - TreeCompleter.Node no = createStringNode("no"); - start.addBranch(yes); - start.addBranch(no); - } - otherCommand.addBranch(list); - otherCommand.addBranch(create); - otherCommand.addBranch(start); - } - TreeCompleter.Node anotherCommand = createStringNode("anotherCommand"); - { - TreeCompleter.Node list = createStringNode("list"); - { - TreeCompleter.Node everything = createStringNode("everything"); - TreeCompleter.Node nothing = createStringNode("nothing"); - TreeCompleter.Node firstHalf = createStringNode("firstHalf"); - TreeCompleter.Node secondHalf = createStringNode("secondHalf"); - TreeCompleter.Node twentyFifthToSeventyFifthPart = createStringNode("25to75part"); - list.addBranch(everything); - list.addBranch(nothing); - list.addBranch(firstHalf); - list.addBranch(secondHalf); - list.addBranch(twentyFifthToSeventyFifthPart); - } - anotherCommand.addBranch(list); - - TreeCompleter.Node grab = createStringNode("grab"); - { - TreeCompleter.Node pepsi = createStringNode("drink-pepsi"); - TreeCompleter.Node coke = createStringNode("drink-coke"); - TreeCompleter.Node fanta = createStringNode("drink-fanta"); - TreeCompleter.Node apple = createStringNode("drink-apple-juice"); - TreeCompleter.Node banana = createStringNode("drink-banana-juice"); - TreeCompleter.Node orange = createStringNode("drink-orange-juice"); - TreeCompleter.Node appleSoda = createStringNode("drink-apple-soda"); - TreeCompleter.Node grapeSoda = createStringNode("drink-grape-soda"); - TreeCompleter.Node pepsiZero = createStringNode("drink-pepsi-zero"); - TreeCompleter.Node pepsiDiet = createStringNode("drink-pepsi-diet"); - TreeCompleter.Node pepsiVanilla = createStringNode("drink-pepsi-vanilla"); - TreeCompleter.Node pepsiLime = createStringNode("drink-pepsi-lime"); - grab.addBranch(pepsi); - grab.addBranch(coke); - grab.addBranch(fanta); - grab.addBranch(apple); - grab.addBranch(banana); - grab.addBranch(orange); - grab.addBranch(appleSoda); - grab.addBranch(grapeSoda); - grab.addBranch(pepsiZero); - grab.addBranch(pepsiDiet); - grab.addBranch(pepsiVanilla); - grab.addBranch(pepsiLime); - } - anotherCommand.addBranch(grab); - } - - commands.add(command1); - commands.add(command2); - commands.add(otherCommand); - commands.add(anotherCommand); - - tree.addBranches(commands); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdCompleterServiceTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CliCommandOption; -import com.redhat.thermostat.common.cli.TabCompleter; -import org.junit.Before; -import org.junit.Test; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - -public class VmIdCompleterServiceTest { - - private VmIdCompleterService service; - - @Before - public void setup() { - service = new VmIdCompleterService(); - service.bindVmIdsFinder(mock(VmIdsFinder.class)); - } - - @Test - public void testCompleterAppliesToAllCommands() { - Set<String> commands = service.getCommands(); - Set<String> expected = TabCompletion.ALL_COMMANDS_COMPLETER; - assertThat(commands, is(equalTo(expected))); - } - - @Test - public void testProvidesCompleterForVmIdOptionOnly() { - Map<CliCommandOption, ? extends TabCompleter> map = service.getOptionCompleters(); - assertThat(map.keySet(), is(equalTo(Collections.singleton(VmIdCompleterService.VM_ID_OPTION)))); - } - - @Test - public void testVmIdCompleterIsProvided() { - TabCompleter completer = service.getOptionCompleters().get(VmIdCompleterService.VM_ID_OPTION); - assertThat(completer, is(not(equalTo(null)))); - } - -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdsFinderImplTest.java Tue May 23 14:10:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,238 +0,0 @@ -/* - * Copyright 2012-2017 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 com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.VmId; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; -import com.redhat.thermostat.storage.model.VmInfo; -import org.junit.Before; -import org.junit.Test; - -import java.text.Collator; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class VmIdsFinderImplTest { - - private VmIdsFinderImpl vmIdsFinder; - private final String id0 = "pequ-14as-54yt"; - private final String id1 = "412345-56789"; - private final String id2 = "111111-22222"; - private final String id3 = "98765-543210"; - private final String id4 = "abcdef-01234564-848156"; - private final String id5 = "456-879-4512"; - private final String id6 = "4101-1010-0111"; - private final String mainClass0 = "com.redhat.thermostat.main"; - private final String mainClass1 = "com.redhat.thermostat.blue.launcher.main"; - private final String mainClass2 = "com.redhat.thermostat.vmIdsFinder.main"; - private final String mainClass3 = "com.redhat.thermostat.distribution.main"; - private final String mainClass4 = "com.redhat.thermostat.orange.main"; - private final String mainClass5 = "com.redhat.thermostat.look.search.main"; - private final String mainClass6 = "com.redhat.thermostat.gui.chartspanel.chart.main"; - private final VmInfo.AliveStatus aliveStatus0 = VmInfo.AliveStatus.RUNNING; - private final VmInfo.AliveStatus aliveStatus1 = VmInfo.AliveStatus.RUNNING; - private final VmInfo.AliveStatus aliveStatus2 = VmInfo.AliveStatus.EXITED; - private final VmInfo.AliveStatus aliveStatus3 = VmInfo.AliveStatus.EXITED; - private final VmInfo.AliveStatus aliveStatus4 = VmInfo.AliveStatus.UNKNOWN; - private final VmInfo.AliveStatus aliveStatus5 = VmInfo.AliveStatus.RUNNING; - private final VmInfo.AliveStatus aliveStatus6 = VmInfo.AliveStatus.UNKNOWN; - - private VmIdsFinderImpl vmIdsFinderWithOnlyOneVm; - - @Before - public void setupVmIdsFinder() { - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); - VmInfoDAO vmsInfoDAO = mock(VmInfoDAO.class); - - vmIdsFinder = new VmIdsFinderImpl(); - vmIdsFinder.bindAgentInfoDao(agentInfoDAO); - vmIdsFinder.bindVmInfoDao(vmsInfoDAO); - - Set<AgentId> agentIds = new HashSet<>(); - AgentId agentId1 = mock(AgentId.class); - AgentId agentId2 = mock(AgentId.class); - agentIds.add(agentId1); - agentIds.add(agentId2); - - when(agentInfoDAO.getAgentIds()).thenReturn(agentIds); - AgentInformation agentInformation1 = mock(AgentInformation.class); - when(agentInfoDAO.getAgentInformation(agentId1)).thenReturn(agentInformation1); - AgentInformation agentInformation2 = mock(AgentInformation.class); - when(agentInfoDAO.getAgentInformation(agentId2)).thenReturn(agentInformation2); - - Set<VmId> vms1 = new HashSet<>(); - VmId vm0 = mock(VmId.class); - VmId vm1 = mock(VmId.class); - VmId vm2 = mock(VmId.class); - VmId vm3 = mock(VmId.class); - vms1.add(vm0); - vms1.add(vm1); - vms1.add(vm2); - vms1.add(vm3); - VmInfo info0 = mock(VmInfo.class); - VmInfo info1 = mock(VmInfo.class); - VmInfo info2 = mock(VmInfo.class); - VmInfo info3 = mock(VmInfo.class); - when(vmsInfoDAO.getVmInfo(vm0)).thenReturn(info0); - when(vmsInfoDAO.getVmInfo(vm1)).thenReturn(info1); - when(vmsInfoDAO.getVmInfo(vm2)).thenReturn(info2); - when(vmsInfoDAO.getVmInfo(vm3)).thenReturn(info3); - - Set<VmId> vms2 = new HashSet<>(); - VmId vm4 = mock(VmId.class); - VmId vm5 = mock(VmId.class); - VmId vm6 = mock(VmId.class); - vms2.add(vm4); - vms2.add(vm5); - vms2.add(vm6); - VmInfo info4 = mock(VmInfo.class); - VmInfo info5 = mock(VmInfo.class); - VmInfo info6 = mock(VmInfo.class); - when(vmsInfoDAO.getVmInfo(vm4)).thenReturn(info4); - when(vmsInfoDAO.getVmInfo(vm5)).thenReturn(info5); - when(vmsInfoDAO.getVmInfo(vm6)).thenReturn(info6); - - when(info0.getVmId()).thenReturn(id0); - when(info1.getVmId()).thenReturn(id1); - when(info2.getVmId()).thenReturn(id2); - when(info3.getVmId()).thenReturn(id3); - when(info4.getVmId()).thenReturn(id4); - when(info5.getVmId()).thenReturn(id5); - when(info6.getVmId()).thenReturn(id6); - - when(info0.getMainClass()).thenReturn(mainClass0); - when(info1.getMainClass()).thenReturn(mainClass1); - when(info2.getMainClass()).thenReturn(mainClass2); - when(info3.getMainClass()).thenReturn(mainClass3); - when(info4.getMainClass()).thenReturn(mainClass4); - when(info5.getMainClass()).thenReturn(mainClass5); - when(info6.getMainClass()).thenReturn(mainClass6); - - when(info0.isAlive(agentInformation1)).thenReturn(aliveStatus0); - when(info1.isAlive(agentInformation1)).thenReturn(aliveStatus1); - when(info2.isAlive(agentInformation1)).thenReturn(aliveStatus2); - when(info3.isAlive(agentInformation1)).thenReturn(aliveStatus3); - - when(info4.isAlive(agentInformation2)).thenReturn(aliveStatus4); - when(info5.isAlive(agentInformation2)).thenReturn(aliveStatus5); - when(info6.isAlive(agentInformation2)).thenReturn(aliveStatus6); - - when(vmsInfoDAO.getVmIds(agentId1)).thenReturn(vms1); - when(vmsInfoDAO.getVmIds(agentId2)).thenReturn(vms2); - - setupVmIdsFinderWithOnlyOneVm(); - } - - private void setupVmIdsFinderWithOnlyOneVm() { - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); - VmInfoDAO vmsInfoDAO = mock(VmInfoDAO.class); - - vmIdsFinderWithOnlyOneVm = new VmIdsFinderImpl(); - vmIdsFinderWithOnlyOneVm.bindAgentInfoDao(agentInfoDAO); - vmIdsFinderWithOnlyOneVm.bindVmInfoDao(vmsInfoDAO); - - Set<AgentId> agentIds = new HashSet<>(); - AgentId agentId = new AgentId(id0); - agentIds.add(agentId); - - when(agentInfoDAO.getAgentIds()).thenReturn(agentIds); - AgentInformation agentInformation1 = mock(AgentInformation.class); - when(agentInfoDAO.getAgentInformation(agentId)).thenReturn(agentInformation1); - - Set<VmId> vms1 = new HashSet<>(); - VmId vm1 = mock(VmId.class); - vms1.add(vm1); - VmInfo info1 = mock(VmInfo.class); - when(vmsInfoDAO.getVmInfo(vm1)).thenReturn(info1); - - when(info1.getVmId()).thenReturn(id0); - when(info1.getMainClass()).thenReturn(mainClass0); - when(info1.isAlive(agentInformation1)).thenReturn(aliveStatus0); - - when(vmsInfoDAO.getVmIds(agentId)).thenReturn(vms1); - AgentInformation agentInfo0 = mock(AgentInformation.class); - agentInfo0.setAgentId(id0); - } - - @Test - public void testFindIds() { - List<CompletionInfo> result = vmIdsFinder.findCompletions(); - - // Sort to get some predictability in result order - final Collator collator = Collator.getInstance(Locale.US); - - Collections.sort(result, new Comparator<CompletionInfo>() { - @Override - public int compare(CompletionInfo o1, CompletionInfo o2) { - return collator.compare(o1.getActualCompletion(), o2.getActualCompletion()); - } - }); - - assertEquals(7, result.size()); - assertEquals(formatExpected(id2, mainClass2, aliveStatus2), result.get(0).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id6, mainClass6, aliveStatus6), result.get(1).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id1, mainClass1, aliveStatus1), result.get(2).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id5, mainClass5, aliveStatus5), result.get(3).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id3, mainClass3, aliveStatus3), result.get(4).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id4, mainClass4, aliveStatus4), result.get(5).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id0, mainClass0, aliveStatus0), result.get(6).getCompletionWithUserVisibleText()); - } - - @Test - public void testFindsIdsWithOnlyOneVm() { - List<CompletionInfo> result = vmIdsFinderWithOnlyOneVm.findCompletions(); - assertEquals(1, result.size()); - assertEquals(formatExpected(id0, mainClass0, aliveStatus0), result.get(0).getCompletionWithUserVisibleText()); - } - - private String formatExpected(String id, String mainClass, VmInfo.AliveStatus aliveStatus) { - return id + " [" + mainClass + "(" + aliveStatus.toString() + ")]"; - } - -}