Repository: geode Updated Branches: refs/heads/GEODE-1912 [created] ce9e138cc
http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/test/java/org/apache/geode/management/internal/cli/annotations/CliArgumentJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/annotations/CliArgumentJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/annotations/CliArgumentJUnitTest.java deleted file mode 100644 index 161f7c6..0000000 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/annotations/CliArgumentJUnitTest.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.management.internal.cli.annotations; - -import static org.junit.Assert.*; - -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import org.apache.geode.management.internal.cli.annotation.CliArgument; -import org.apache.geode.test.junit.categories.UnitTest; - -/** - * Includes tests for checking assignment of {@link CliArgument} - */ -@Category(UnitTest.class) -public class CliArgumentJUnitTest { - - private static final String ARGUMENT_NAME = "ARGUMENT_NAME"; - private static final String ARGUMENT_HELP = "ARGUMENT_HELP"; - private static final boolean ARGUMENT_MANDATORY = true; - private static final String ARGUMENT_CONTEXT = "ARGUMENT_CONTEXT"; - private static final boolean SYSTEM_PROVIDED = true; - private static final String ARGUMENT_UNSPECIFIED_DEFAULT_VALUE = - "ARGUMENT_UNSPECIFIED_DEFAULT_VALUE"; - private static final String MESSAGE_FOR_DEFAULT_ARGUMENT = "Testing for argument with defaults"; - private static final String MESSAGE_FOR_ARGUMENT = "Testing for argument without defaults"; - - /** - * Test for {@link CliArgument#name()} - */ - @Test - public void testName() throws Exception { - String name = ((CliArgument) (ArgumentTestingClass.class - .getMethod("defaultArgumentTestingMethod", String.class).getParameterAnnotations()[0][0])) - .name(); - assertNotNull(name); - assertEquals(MESSAGE_FOR_DEFAULT_ARGUMENT, name, ARGUMENT_NAME); - name = ((CliArgument) (ArgumentTestingClass.class - .getMethod("argumentTestingMethod", String.class).getParameterAnnotations()[0][0])).name(); - assertNotNull(name); - assertEquals(MESSAGE_FOR_ARGUMENT, name, ARGUMENT_NAME); - } - - /** - * Test for {@link CliArgument#help()} - */ - @Test - public void testHelp() throws Exception { - String help = ((CliArgument) (ArgumentTestingClass.class - .getMethod("defaultArgumentTestingMethod", String.class).getParameterAnnotations()[0][0])) - .help(); - assertNotNull(help); - assertEquals(MESSAGE_FOR_DEFAULT_ARGUMENT, help, ""); - help = ((CliArgument) (ArgumentTestingClass.class - .getMethod("argumentTestingMethod", String.class).getParameterAnnotations()[0][0])).help(); - assertNotNull(help); - assertEquals(MESSAGE_FOR_ARGUMENT, help, ARGUMENT_HELP); - } - - /** - * Test for {@link CliArgument#mandatory()} - */ - @Test - public void testMandatory() throws Exception { - boolean mandatory = ((CliArgument) (ArgumentTestingClass.class - .getMethod("defaultArgumentTestingMethod", String.class).getParameterAnnotations()[0][0])) - .mandatory(); - assertEquals(MESSAGE_FOR_DEFAULT_ARGUMENT, mandatory, false); - mandatory = - ((CliArgument) (ArgumentTestingClass.class.getMethod("argumentTestingMethod", String.class) - .getParameterAnnotations()[0][0])).mandatory(); - assertEquals(MESSAGE_FOR_ARGUMENT, mandatory, ARGUMENT_MANDATORY); - } - - /** - * Test for {@link CliArgument#argumentContext()} - */ - @Test - public void testArgumentContext() throws Exception { - String argumentContext = ((CliArgument) (ArgumentTestingClass.class - .getMethod("defaultArgumentTestingMethod", String.class).getParameterAnnotations()[0][0])) - .argumentContext(); - assertNotNull(argumentContext); - assertEquals(MESSAGE_FOR_DEFAULT_ARGUMENT, argumentContext, ""); - argumentContext = - ((CliArgument) (ArgumentTestingClass.class.getMethod("argumentTestingMethod", String.class) - .getParameterAnnotations()[0][0])).argumentContext(); - assertNotNull(argumentContext); - assertEquals(MESSAGE_FOR_ARGUMENT, argumentContext, ARGUMENT_CONTEXT); - } - - /** - * Test for {@link CliArgument#systemProvided()} - */ - @Test - public void testSystemProvided() throws Exception { - boolean systemProvided = ((CliArgument) (ArgumentTestingClass.class - .getMethod("defaultArgumentTestingMethod", String.class).getParameterAnnotations()[0][0])) - .systemProvided(); - assertEquals(MESSAGE_FOR_DEFAULT_ARGUMENT, systemProvided, false); - systemProvided = - ((CliArgument) (ArgumentTestingClass.class.getMethod("argumentTestingMethod", String.class) - .getParameterAnnotations()[0][0])).systemProvided(); - assertEquals(MESSAGE_FOR_ARGUMENT, systemProvided, SYSTEM_PROVIDED); - } - - /** - * Test for {@link CliArgument#unspecifiedDefaultValue()} - */ - @Test - public void testUnspecifiedDefaultValue() throws Exception { - String unspecifiedDefaultValue = ((CliArgument) (ArgumentTestingClass.class - .getMethod("defaultArgumentTestingMethod", String.class).getParameterAnnotations()[0][0])) - .unspecifiedDefaultValue(); - assertEquals(MESSAGE_FOR_DEFAULT_ARGUMENT, unspecifiedDefaultValue, "__NULL__"); - unspecifiedDefaultValue = - ((CliArgument) (ArgumentTestingClass.class.getMethod("argumentTestingMethod", String.class) - .getParameterAnnotations()[0][0])).unspecifiedDefaultValue(); - assertEquals(MESSAGE_FOR_ARGUMENT, unspecifiedDefaultValue, ARGUMENT_UNSPECIFIED_DEFAULT_VALUE); - } - - /** - * Class used by the tests - */ - private static class ArgumentTestingClass { - - @SuppressWarnings("unused") - public static Object defaultArgumentTestingMethod( - @CliArgument(name = ARGUMENT_NAME) String defaultArgument) { - return null; - } - - @SuppressWarnings("unused") - public static Object argumentTestingMethod( - @CliArgument(name = ARGUMENT_NAME, help = ARGUMENT_HELP, mandatory = ARGUMENT_MANDATORY, - argumentContext = ARGUMENT_CONTEXT, systemProvided = SYSTEM_PROVIDED, - unspecifiedDefaultValue = ARGUMENT_UNSPECIFIED_DEFAULT_VALUE) String argument) { - return null; - } - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CliCommandTestBase.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CliCommandTestBase.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CliCommandTestBase.java index 21426d6..de2fba1 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CliCommandTestBase.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/CliCommandTestBase.java @@ -37,14 +37,27 @@ import org.apache.geode.management.ManagementService; import org.apache.geode.management.internal.cli.CommandManager; import org.apache.geode.management.internal.cli.HeadlessGfsh; import org.apache.geode.management.internal.cli.i18n.CliStrings; -import org.apache.geode.management.internal.cli.parser.CommandTarget; import org.apache.geode.management.internal.cli.result.CommandResult; import org.apache.geode.management.internal.cli.shell.Gfsh; import org.apache.geode.management.internal.cli.util.CommandStringBuilder; +import org.apache.geode.security.templates.SampleSecurityManager; import org.apache.geode.test.dunit.Host; import org.apache.geode.test.dunit.IgnoredException; import org.apache.geode.test.dunit.cache.internal.JUnit4CacheTestCase; import org.apache.geode.test.dunit.rules.DistributedRestoreSystemProperties; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; +import org.springframework.shell.core.CommandMarker; + +import java.io.IOException; +import java.io.PrintStream; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Base class for all the CLI/gfsh command dunit tests. @@ -52,24 +65,45 @@ import org.apache.geode.test.dunit.rules.DistributedRestoreSystemProperties; public abstract class CliCommandTestBase extends JUnit4CacheTestCase { public static final String USE_HTTP_SYSTEM_PROPERTY = "useHTTP"; - - private boolean useHttpOnConnect = Boolean.getBoolean(USE_HTTP_SYSTEM_PROPERTY); - - private ManagementService managementService; - - private transient HeadlessGfsh shell; - + @Rule + public transient DistributedRestoreSystemProperties restoreSystemProperties = + new DistributedRestoreSystemProperties(); + @Rule + public transient TemporaryFolder temporaryFolder = new TemporaryFolder(); protected transient int httpPort; protected transient int jmxPort; protected transient String jmxHost; protected transient String gfshDir; + private boolean useHttpOnConnect = Boolean.getBoolean(USE_HTTP_SYSTEM_PROPERTY); + private ManagementService managementService; + private transient HeadlessGfsh shell; - @Rule - public transient DistributedRestoreSystemProperties restoreSystemProperties = - new DistributedRestoreSystemProperties(); + public static boolean checkIfCommandsAreLoadedOrNot() { + CommandManager manager; + try { + manager = CommandManager.getInstance(); + List<CommandMarker> commands = manager.getCommandMarkers(); + return commands.size() >= 1; + } catch (ClassNotFoundException | IOException e) { + throw new RuntimeException("Could not load commands", e); + } + } - @Rule - public transient TemporaryFolder temporaryFolder = new TemporaryFolder(); + protected static String commandResultToString(final CommandResult commandResult) { + assertNotNull(commandResult); + + commandResult.resetToFirstLine(); + + StringBuilder buffer = new StringBuilder(commandResult.getHeader()); + + while (commandResult.hasNextLine()) { + buffer.append(commandResult.nextLine()); + } + + buffer.append(commandResult.getFooter()); + + return buffer.toString(); + } @Override public final void postSetUp() throws Exception { @@ -195,20 +229,6 @@ public abstract class CliCommandTestBase extends JUnit4CacheTestCase { assertTrue(checkIfCommandsAreLoadedOrNot()); } - public static boolean checkIfCommandsAreLoadedOrNot() { - CommandManager manager; - try { - manager = CommandManager.getInstance(); - Map<String, CommandTarget> commands = manager.getCommands(); - if (commands.size() < 1) { - return false; - } - return true; - } catch (ClassNotFoundException | IOException e) { - throw new RuntimeException("Could not load commands", e); - } - } - /** * Stop the default management service. */ @@ -387,22 +407,6 @@ public abstract class CliCommandTestBase extends JUnit4CacheTestCase { printStream.print(commandResultToString(commandResult)); } - protected static String commandResultToString(final CommandResult commandResult) { - assertNotNull(commandResult); - - commandResult.resetToFirstLine(); - - StringBuilder buffer = new StringBuilder(commandResult.getHeader()); - - while (commandResult.hasNextLine()) { - buffer.append(commandResult.nextLine()); - } - - buffer.append(commandResult.getFooter()); - - return buffer.toString(); - } - /** * Utility method for finding the CommandResult object in the Map of CommandOutput objects. * http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/HelpCommandsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/HelpCommandsIntegrationTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/HelpCommandsIntegrationTest.java deleted file mode 100644 index 1483aad..0000000 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/HelpCommandsIntegrationTest.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.management.internal.cli.commands; - -import org.apache.geode.cache.CacheFactory; -import org.apache.geode.distributed.internal.InternalDistributedSystem; -import org.apache.geode.internal.AvailablePortHelper; -import org.apache.geode.management.internal.cli.CommandManager; -import org.apache.geode.management.internal.cli.parser.CommandTarget; -import org.apache.geode.management.internal.cli.result.CommandResult; -import org.apache.geode.management.internal.cli.shell.Gfsh; -import org.apache.geode.management.internal.cli.shell.GfshConfig; -import org.apache.geode.test.junit.categories.IntegrationTest; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.contrib.java.lang.system.ProvideSystemProperty; -import org.junit.experimental.categories.Category; - -import java.util.Map; -import java.util.Properties; - -import static org.apache.geode.distributed.ConfigurationProperties.*; -import static org.apache.geode.management.internal.cli.commands.CliCommandTestBase.commandResultToString; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -@Category(IntegrationTest.class) -public class HelpCommandsIntegrationTest { - - private int jmxPort; - - private Gfsh gfsh; - - @ClassRule - public static final ProvideSystemProperty isGfsh = new ProvideSystemProperty("gfsh", "true"); - - @Before - public void setup() throws Exception { - jmxPort = AvailablePortHelper.getRandomAvailableTCPPort(); - - Properties localProps = new Properties(); - localProps.setProperty(LOCATORS, ""); - localProps.setProperty(MCAST_PORT, "0"); - localProps.setProperty(JMX_MANAGER, "true"); - localProps.setProperty(JMX_MANAGER_START, "true"); - localProps.setProperty(JMX_MANAGER_PORT, String.valueOf(jmxPort)); - - new CacheFactory(localProps).create(); - - gfsh = Gfsh.getInstance(false, new String[0], new GfshConfig()); - } - - @After - public void teardown() { - InternalDistributedSystem ids = InternalDistributedSystem.getConnectedInstance(); - if (ids != null) { - ids.disconnect(); - } - } - - /** - * TODO:GEODE-1466: update golden file to geode.properties TODO:GEODE-1566: update golden file to - * GeodeRedisServer - */ - @Test - public void testOfflineHelp() throws Exception { - Properties helpProps = new Properties(); - helpProps.load( - HelpCommandsIntegrationTest.class.getResourceAsStream("golden-help-offline.properties")); - - CommandManager cm = CommandManager.getInstance(); - for (Map.Entry<String, CommandTarget> e : cm.getCommands().entrySet()) { - // Mock commands may have been produced in the VM by other tests - // 'quit' is an alias for 'exit' and doesn't produce help - if (e.getKey().contains("mock") || e.getKey().contains("quit")) { - continue; - } - - CommandResult cr = (CommandResult) gfsh.executeCommand("help " + e.getKey()).getResult(); - String gfshResult = commandResultToString(cr); - - String goldParam = e.getKey().replace(" ", "-") + ".help"; - String goldResult = helpProps.getProperty(goldParam); - assertNotNull("No golden text for: " + goldParam, goldResult); - assertEquals(goldResult.trim(), gfshResult.trim()); - - helpProps.remove(goldParam); - } - - // No help should remain unchecked - assertEquals(0, helpProps.size()); - } - - @Test - public void testOnlineHelp() throws Exception { - Properties helpProps = new Properties(); - helpProps.load( - HelpCommandsIntegrationTest.class.getResourceAsStream("golden-help-online.properties")); - - gfsh.executeCommand("connect --jmx-manager=localhost[" + jmxPort + "]"); - - CommandManager cm = CommandManager.getInstance(); - for (Map.Entry<String, CommandTarget> e : cm.getCommands().entrySet()) { - // Mock commands may have been produced in the VM by other tests - // 'quit' is an alias for 'exit' and doesn't produce help - if (e.getKey().contains("mock") || e.getKey().contains("quit")) { - continue; - } - - CommandResult cr = (CommandResult) gfsh.executeCommand("help " + e.getKey()).getResult(); - String gfshResult = commandResultToString(cr); - - String goldParam = e.getKey().replace(" ", "-") + ".help"; - String goldResult = helpProps.getProperty(goldParam); - assertNotNull("No golden text for: " + goldParam, goldResult); - - String[] lines = gfshResult.split("\n"); - gfshResult = String.join("\n", lines[0], lines[1], lines[2], lines[3]); - - assertEquals(goldResult.trim(), gfshResult.trim()); - - helpProps.remove(goldParam); - } - - // No help should remain unchecked - assertEquals(0, helpProps.size()); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/ParserUtilsJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/ParserUtilsJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/ParserUtilsJUnitTest.java deleted file mode 100644 index 9b22e64..0000000 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/ParserUtilsJUnitTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.management.internal.cli.parser; - -import static org.junit.Assert.*; - -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import org.apache.geode.test.junit.categories.UnitTest; - -/** - * Includes tests for all utility methods in {@link ParserUtils} - */ -@Category(UnitTest.class) -public class ParserUtilsJUnitTest { - - /** - * Test for {@link ParserUtils#split(String, String)} - */ - @Test - public void testSplit() { - String input = "something::{::}::nothing"; - String[] split = ParserUtils.split(input, "::"); - assertEquals("Size of the split", 3, split.length); - assertEquals("First string", "something", split[0]); - assertEquals("Second string", "{::}", split[1]); - assertEquals("Third string", "nothing", split[2]); - } - - /** - * Test for {@link ParserUtils#splitValues(String, String)} - */ - @Test - public void testSplitValues() { - String input = "something::{::}::nothing::"; - String[] split = ParserUtils.splitValues(input, "::"); - assertEquals("Size of the split", 4, split.length); - assertEquals("First string", "something", split[0]); - assertEquals("Second string", "{::}", split[1]); - assertEquals("Third string", "nothing", split[2]); - assertEquals("Fourth string", "", split[3]); - } - - /** - * Test for {@link ParserUtils#contains(String, String)} - */ - @Test - public void testContains() { - String input = "something::{::}::nothing::"; - assertTrue("Check Boolean", ParserUtils.contains(input, "::")); - input = "{something::{::}::nothing::}"; - assertFalse("Check Boolean", ParserUtils.contains(input, "::")); - } - - /** - * Test for {@link ParserUtils#lastIndexOf(String, String)} - */ - @Test - public void testLastIndexOf() { - String input = "something::{::}::nothing::"; - assertEquals("lastIndex", 24, ParserUtils.lastIndexOf(input, "::")); - input = "something::{::}::\"nothing::\""; - assertEquals("lastIndex", 15, ParserUtils.lastIndexOf(input, "::")); - input = "{something::{::}::\"nothing::\"}"; - assertEquals("lastIndex", -1, ParserUtils.lastIndexOf(input, "::")); - } - -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/preprocessor/PreprocessorJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/preprocessor/PreprocessorJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/preprocessor/PreprocessorJUnitTest.java deleted file mode 100644 index 97325cb..0000000 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/preprocessor/PreprocessorJUnitTest.java +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.management.internal.cli.parser.preprocessor; - -import static org.junit.Assert.*; - -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import org.apache.geode.test.junit.categories.UnitTest; - -/** - * Test for Preprocessor - */ -@Category(UnitTest.class) -public class PreprocessorJUnitTest { - - @Test - public void test1Arg() { - String input = "arg1"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 1, split.length); - assertEquals("First string", "arg1", split[0]); - } - - @Test - public void test2Args() { - String input = "arg1?arg2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "arg1", split[0]); - assertEquals("Second string", "arg2", split[1]); - } - - @Test - public void test1SpacedArg() { - String input = "arg1-1 arg1-2 "; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 1, split.length); - assertEquals("First string", "arg1-1 arg1-2", split[0]); - } - - @Test - public void test1SpacedArg1Option() { - String input = "arg1-1 arg1-2 --option1=value1"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 3, split.length); - assertEquals("First string", "arg1-1 arg1-2", split[0]); - assertEquals("Second string", "--option1", split[1]); - assertEquals("Third string", "value1", split[2]); - } - - @Test - public void test1OptionNoValue() { - String input = "--option1"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 1, split.length); - assertEquals("First string", "--option1", split[0]); - } - - @Test - public void test2OptionsNoValue() { - String input = "--option1 --option2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "--option2", split[1]); - } - - @Test - public void test2Options1Value() { - String input = "--option1=value1 --option2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 3, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "value1", split[1]); - assertEquals("Third string", "--option2", split[2]); - } - - @Test - public void test1OptionHasValue() { - String input = "--option1=value1"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "value1", split[1]); - } - - @Test - public void test1Arg1OptionHasValue() { - String input = "arg1 --option1=value1"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 3, split.length); - assertEquals("First string", "arg1", split[0]); - assertEquals("Second string", "--option1", split[1]); - assertEquals("Third string", "value1", split[2]); - } - - @Test - public void test1OptionMissingValue() { - String input = "--option1="; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "__NULL__", split[1]); - } - - @Test - public void test2OptionsMissingFirstValue() { - String input = "--option1= --option2=value2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 4, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "__NULL__", split[1]); - assertEquals("Third string", "--option2", split[2]); - assertEquals("Fourth string", "value2", split[3]); - } - - @Test - public void testSingleQuotedArg() { - String input = "\'arg1-1= arg1-2\'?arg2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "\'arg1-1= arg1-2\'", split[0]); - assertEquals("Second string", "arg2", split[1]); - } - - @Test - public void testDoubleQuotedArg() { - String input = "\" \'arg1-1 =arg1-2 \"?arg2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "\" \'arg1-1 =arg1-2 \"", split[0]); - assertEquals("Second string", "arg2", split[1]); - } - - @Test - public void testSingleQuotedOption() { - String input = "--option1=\'value1-1 =value1-2\"\' --option2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 3, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "\'value1-1 =value1-2\"\'", split[1]); - assertEquals("Third string", "--option2", split[2]); - } - - @Test - public void testDoubleQuotedOption() { - String input = "--option1= --option2=\"value2\""; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 4, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "__NULL__", split[1]); - assertEquals("Third string", "--option2", split[2]); - assertEquals("Fourth string", "\"value2\"", split[3]); - } - - @Test - public void testSingleQuoteInsideDoubleQuote() { - String input = "--option1=\" \' value1 \' \""; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "\" \' value1 \' \"", split[1]); - } - - @Test - public void testQuotedStringWithAdditonalData() { - String input = "--option1=\" \' value1 \' \",moreData,\" even more data\""; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "\" \' value1 \' \",moreData,\" even more data\"", - split[1]); - } - - @Test - public void testBadOption() { - String input = "--option1=value1 -option2=value2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 4, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "value1", split[1]); - assertEquals("Third string", "-option2", split[2]); - assertEquals("Third string", "value2", split[3]); - } - - @Test - public void testBadOptions() { - String input = "--option1=value1 -option3 -option4"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 4, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "value1", split[1]); - assertEquals("Third string", "-option3", split[2]); - assertEquals("Third string", "-option4", split[3]); - } - - @Test - public void testExtraArgSpaces() { - String input = " arg1? arg2 "; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "arg1", split[0]); - assertEquals("Second string", "arg2", split[1]); - } - - @Test - public void testExtraOptionSpaces() { - String input = " --option1=value1 --option2=value2 "; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 4, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "value1", split[1]); - assertEquals("Third string", "--option2", split[2]); - assertEquals("Fourth string", "value2", split[3]); - } - - @Test - public void testExtraArgAndOptionSpaces() { - String input = " arg1 --option1=value1 "; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 3, split.length); - assertEquals("First string", "arg1", split[0]); - assertEquals("Second string", "--option1", split[1]); - assertEquals("Third string", "value1", split[2]); - } - - @Test - public void testValueSpecifierAsPartOfValue() { - String input = "--option1=-Dprop1=value1 --option2=-Dprop2=value2 --option3"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 5, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "-Dprop1=value1", split[1]); - assertEquals("Third string", "--option2", split[2]); - assertEquals("Fourth string", "-Dprop2=value2", split[3]); - assertEquals("Fifth string", "--option3", split[4]); - } - - @Test - public void testMissingOption() { - String input = "--option1=value1 value2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 3, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "value1", split[1]); - assertEquals("Third string", "value2", split[2]); - } - - @Test - public void testUnclosedQuoteArg() { - String input = "\"arg1-1 arg1-2 --option1=value1 --option2=value2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 1, split.length); - assertEquals("First string", "\"arg1-1 arg1-2 --option1=value1 --option2=value2", split[0]); - } - - @Test - public void testUnclosedQuoteOption() { - String input = "--option1=\"value1 --option2=value2"; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second string", "\"value1 --option2=value2", split[1]); - } - - @Test - public void testArgWithQuotedLongOptionSpec() { - String input = "\"--arg=value\""; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 1, split.length); - assertEquals("First string", "\"--arg=value\"", split[0]); - } - - @Test - public void testOptionWithQuotedLongOptionSpec() { - String input = "--option1=\"--arg=value\""; - String[] split = Preprocessor.split(input); - assertEquals("Size of the split", 2, split.length); - assertEquals("First string", "--option1", split[0]); - assertEquals("Second", "\"--arg=value\"", split[1]); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/preprocessor/PreprocessorUtilsJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/preprocessor/PreprocessorUtilsJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/preprocessor/PreprocessorUtilsJUnitTest.java deleted file mode 100644 index b56cff2..0000000 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/parser/preprocessor/PreprocessorUtilsJUnitTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.management.internal.cli.parser.preprocessor; - -import static org.junit.Assert.*; - -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import org.apache.geode.internal.lang.SystemUtils; -import org.apache.geode.test.junit.categories.UnitTest; - -/** - * Includes tests for all utility methods in {@link PreprocessorUtils} - */ -@Category(UnitTest.class) -public class PreprocessorUtilsJUnitTest { - - /** - * Test for {@link PreprocessorUtils#simpleTrim(String)} - */ - @Test - public void testSimpleTrim() { - String input = " 1 2 3 "; - TrimmedInput simpleTrim = PreprocessorUtils.simpleTrim(input); - assertEquals("No of spaces removed", 1, simpleTrim.getNoOfSpacesRemoved()); - assertEquals("input after trimming", "1 2 3", simpleTrim.getString()); - - input = " 1 2 3 "; - simpleTrim = PreprocessorUtils.simpleTrim(input); - assertEquals("No of spaces removed", 1, simpleTrim.getNoOfSpacesRemoved()); - assertEquals("input after trimming", "1 2 3", simpleTrim.getString()); - } - - /** - * Test for {@link PreprocessorUtils#trim(String)} - */ - @Test - public void testTrim() { - String input = " command argument1 argument2 "; - TrimmedInput trim = PreprocessorUtils.trim(input); - assertEquals("No of spaces removed", 1, trim.getNoOfSpacesRemoved()); - assertEquals("input after trimming", "command argument1 argument2", trim.getString()); - - input = " command argument1 argument2 "; - trim = PreprocessorUtils.trim(input); - assertEquals("No of spaces removed", 7, trim.getNoOfSpacesRemoved()); - assertEquals("input after trimming", "command argument1 argument2", trim.getString()); - - input = "command argument1 argument2 -- -- - - - -- -- -- -- -- --- --------- - - - --- --"; - trim = PreprocessorUtils.trim(input); - assertEquals("No of spaces removed", 0, trim.getNoOfSpacesRemoved()); - assertEquals("input after trimming", "command argument1 argument2", trim.getString()); - - input = "command argument1 argument2 --"; - trim = PreprocessorUtils.trim(input); - assertEquals("No of spaces removed", 0, trim.getNoOfSpacesRemoved()); - assertEquals("input after trimming", "command argument1 argument2", trim.getString()); - - input = "command argument1 argument2 -"; - trim = PreprocessorUtils.trim(input); - assertEquals("No of spaces removed", 0, trim.getNoOfSpacesRemoved()); - assertEquals("input after trimming", "command argument1 argument2", trim.getString()); - } - - /** - * Test for {@link PreprocessorUtils#removeWhiteSpaces(String)} - */ - @Test - public void testRemoveWhiteSpaces() { - String input = "1 2 3 "; - String output = PreprocessorUtils.removeWhiteSpaces(input); - assertEquals("Output after removing white spaces", "123", output); - } - - /** - * Test for {@link PreprocessorUtils#isSyntaxValid(String)} - */ - @Test - public void testIsSyntaxValid() { - assertTrue(PreprocessorUtils.isSyntaxValid("{}")); - assertFalse(PreprocessorUtils.isSyntaxValid("{{]}")); - assertTrue(PreprocessorUtils.isSyntaxValid("\"\"")); - assertTrue(PreprocessorUtils.isSyntaxValid("\"{\'[]\'}\"")); - assertFalse(PreprocessorUtils.isSyntaxValid("{\"}\"")); - } - - /** - * Test for {@link PreprocessorUtils#containsOnlyWhiteSpaces(String)} - */ - @Test - public void testContainsOnlyWhiteSpaces() { - assertTrue(PreprocessorUtils - .containsOnlyWhiteSpaces(" ")); - assertFalse(PreprocessorUtils.containsOnlyWhiteSpaces(" d ")); - } - - /** - * Test for {@link PreprocessorUtils#isWhitespace(char)} - */ - @Test - public void testIsWhitespace() { - assertTrue(PreprocessorUtils.isWhitespace(' ')); - assertTrue(PreprocessorUtils.isWhitespace('\t')); - assertTrue(PreprocessorUtils.isWhitespace('\n')); - assertEquals(SystemUtils.isWindows(), PreprocessorUtils.isWhitespace('\r')); - } - -} http://git-wip-us.apache.org/repos/asf/geode/blob/ce9e138c/geode-core/src/test/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategyJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategyJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategyJUnitTest.java index 54c7cf7..cca9299 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategyJUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/shell/GfshExecutionStrategyJUnitTest.java @@ -14,22 +14,19 @@ */ package org.apache.geode.management.internal.cli.shell; -import static org.junit.Assert.*; - -import java.util.List; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import org.apache.geode.management.cli.CliMetaData; import org.apache.geode.management.cli.ConverterHint; import org.apache.geode.management.cli.Result; import org.apache.geode.management.internal.cli.CommandManager; import org.apache.geode.management.internal.cli.GfshParser; -import org.apache.geode.management.internal.cli.annotation.CliArgument; import org.apache.geode.management.internal.cli.result.ResultBuilder; import org.apache.geode.management.internal.security.ResourceOperation; import org.apache.geode.security.ResourcePermission.Operation; import org.apache.geode.security.ResourcePermission.Resource; import org.apache.geode.test.junit.categories.UnitTest; - import org.junit.After; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -38,6 +35,8 @@ import org.springframework.shell.core.annotation.CliCommand; import org.springframework.shell.core.annotation.CliOption; import org.springframework.shell.event.ParseResult; +import java.util.List; + /** * GfshExecutionStrategyTest - Includes tests to for GfshExecutionStrategyTest */ @@ -64,10 +63,9 @@ public class GfshExecutionStrategyJUnitTest { CommandManager commandManager = CommandManager.getInstance(); assertNotNull("CommandManager should not be null.", commandManager); commandManager.add(Commands.class.newInstance()); - GfshParser parser = new GfshParser(commandManager); + GfshParser parser = GfshParser.getInstance(); String[] command1Names = - ((CliCommand) Commands.class.getMethod(COMMAND1_NAME).getAnnotation(CliCommand.class)) - .value(); + Commands.class.getMethod(COMMAND1_NAME).getAnnotation(CliCommand.class).value(); String input = command1Names[0]; ParseResult parseResult = null; parseResult = parser.parse(input); @@ -89,8 +87,7 @@ public class GfshExecutionStrategyJUnitTest { assertNotNull("CommandManager should not be null.", commandManager); commandManager.add(Commands.class.newInstance()); String[] command1Names = - ((CliCommand) Commands.class.getMethod(COMMAND1_NAME).getAnnotation(CliCommand.class)) - .value(); + Commands.class.getMethod(COMMAND1_NAME).getAnnotation(CliCommand.class).value(); String[] args = new String[] {command1Names[0]}; Gfsh gfsh = Gfsh.getInstance(false, args, new GfshConfig()); GfshExecutionStrategy gfshExecutionStrategy = new GfshExecutionStrategy(gfsh); @@ -130,8 +127,8 @@ public class GfshExecutionStrategyJUnitTest { @CliCommand(value = {"testMultiWordArg"}) @ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ) - public static Result testMultiWordArg(@CliArgument(name = "arg1") String arg1, - @CliArgument(name = "arg2") String arg2) { + public static Result testMultiWordArg(@CliOption(key = "arg1") String arg1, + @CliOption(key = "arg2") String arg2) { return null; } }
