Repository: karaf Updated Branches: refs/heads/master 2154f29d6 -> 6e253c938
KARAF-2772 Moving some util classes and test to simplify separation Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/6e253c93 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/6e253c93 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/6e253c93 Branch: refs/heads/master Commit: 6e253c9384df432dafb901df26ed2dd592edbfa4 Parents: 2154f29 Author: Christian Schneider <[email protected]> Authored: Fri Feb 21 14:19:10 2014 +0100 Committer: Christian Schneider <[email protected]> Committed: Fri Feb 21 14:19:10 2014 +0100 ---------------------------------------------------------------------- .../karaf/shell/commands/ansi/SimpleAnsi.java | 30 ++++ .../karaf/shell/util/CommandSessionUtil.java | 28 --- .../karaf/shell/util/IndentFormatter.java | 58 ------ .../org/apache/karaf/shell/util/SimpleAnsi.java | 30 ---- .../apache/karaf/shell/commands/Context.java | 67 ------- .../karaf/shell/commands/SimpleSubShell.java | 38 ---- .../karaf/shell/commands/TestCommands.java | 178 ------------------- .../karaf/shell/commands/basic/Context.java | 67 +++++++ .../shell/commands/basic/SimpleSubShell.java | 38 ++++ .../shell/commands/basic/TestCommands.java | 178 +++++++++++++++++++ .../shell/commands/meta/TestFormatting.java | 36 ++++ .../shell/console/ExampleSubclassMain.java | 104 ----------- .../shell/console/impl/ExampleSubclassMain.java | 104 +++++++++++ .../apache/karaf/shell/util/TestFormatting.java | 36 ---- 14 files changed, 453 insertions(+), 539 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java b/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java new file mode 100644 index 0000000..b4dc23b --- /dev/null +++ b/shell/console/src/main/java/org/apache/karaf/shell/commands/ansi/SimpleAnsi.java @@ -0,0 +1,30 @@ +/* + * 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.karaf.shell.util; + +import org.fusesource.jansi.Ansi; +import org.fusesource.jansi.Ansi.Color; + +public class SimpleAnsi { + public static String COLOR_RED = Ansi.ansi().fg(Color.RED).toString(); + public static String COLOR_DEFAULT = Ansi.ansi().fg(Color.DEFAULT).toString(); + + public static String INTENSITY_BOLD = Ansi.ansi().bold().toString(); + public static String INTENSITY_NORMAL = Ansi.ansi().boldOff().toString(); +} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/main/java/org/apache/karaf/shell/util/CommandSessionUtil.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/util/CommandSessionUtil.java b/shell/console/src/main/java/org/apache/karaf/shell/util/CommandSessionUtil.java deleted file mode 100644 index cd3d5c6..0000000 --- a/shell/console/src/main/java/org/apache/karaf/shell/util/CommandSessionUtil.java +++ /dev/null @@ -1,28 +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.karaf.shell.util; - -import org.apache.felix.service.command.CommandSession; - -public class CommandSessionUtil { - public static int getWidth(CommandSession session) { - Object cols = session.get("#COLUMNS"); - return (cols != null && cols instanceof Integer) ? (Integer)cols : 80; - } -} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/main/java/org/apache/karaf/shell/util/IndentFormatter.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/util/IndentFormatter.java b/shell/console/src/main/java/org/apache/karaf/shell/util/IndentFormatter.java deleted file mode 100644 index 792cadc..0000000 --- a/shell/console/src/main/java/org/apache/karaf/shell/util/IndentFormatter.java +++ /dev/null @@ -1,58 +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.karaf.shell.util; - -import java.io.PrintStream; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class IndentFormatter { - - public static void printFormatted(String prefix, String str, int termWidth, PrintStream out, boolean prefixFirstLine) { - int pfxLen = prefix.length(); - int maxwidth = termWidth - pfxLen; - Pattern wrap = Pattern.compile("(\\S\\S{" + maxwidth + ",}|.{1," + maxwidth + "})(\\s+|$)"); - int cur = 0; - while (cur >= 0) { - int lst = str.indexOf('\n', cur); - String s = (lst >= 0) ? str.substring(cur, lst) : str.substring(cur); - if (s.length() == 0) { - out.println(); - } else { - Matcher m = wrap.matcher(s); - while (m.find()) { - if (cur > 0 || prefixFirstLine) { - out.print(prefix); - } - out.println(m.group()); - } - } - if (lst >= 0) { - cur = lst + 1; - } else { - break; - } - } - } - - public static void printFormatted(String prefix, String str, int termWidth, PrintStream out) { - printFormatted(prefix, str, termWidth, out, true); - } - -} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/main/java/org/apache/karaf/shell/util/SimpleAnsi.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/util/SimpleAnsi.java b/shell/console/src/main/java/org/apache/karaf/shell/util/SimpleAnsi.java deleted file mode 100644 index b4dc23b..0000000 --- a/shell/console/src/main/java/org/apache/karaf/shell/util/SimpleAnsi.java +++ /dev/null @@ -1,30 +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.karaf.shell.util; - -import org.fusesource.jansi.Ansi; -import org.fusesource.jansi.Ansi.Color; - -public class SimpleAnsi { - public static String COLOR_RED = Ansi.ansi().fg(Color.RED).toString(); - public static String COLOR_DEFAULT = Ansi.ansi().fg(Color.DEFAULT).toString(); - - public static String INTENSITY_BOLD = Ansi.ansi().bold().toString(); - public static String INTENSITY_NORMAL = Ansi.ansi().boldOff().toString(); -} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/commands/Context.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/commands/Context.java b/shell/console/src/test/java/org/apache/karaf/shell/commands/Context.java deleted file mode 100644 index 7737c11..0000000 --- a/shell/console/src/test/java/org/apache/karaf/shell/commands/Context.java +++ /dev/null @@ -1,67 +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.karaf.shell.commands; - -import org.apache.felix.gogo.runtime.CommandProcessorImpl; -import org.apache.felix.gogo.runtime.CommandSessionImpl; -import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl; -import org.apache.felix.service.command.CommandSession; - -public class Context extends CommandProcessorImpl -{ - public static final String EMPTY = ""; - CommandSessionImpl session; - static ThreadIOImpl threadio; - - static - { - threadio = new ThreadIOImpl(); - threadio.start(); - } - - public Context() - { - super(threadio); - addCommand("shell", this, "addCommand"); - addCommand("shell", this, "removeCommand"); - addCommand("shell", this, "eval"); - session = (CommandSessionImpl) createSession(System.in, System.out, System.err); - } - - - public Object execute(CharSequence source) throws Exception - { - return session.execute(source); - } - - public void set(String name, Object value) - { - session.put(name, value); - } - - public Object get(String name) - { - return session.get(name); - } - - public CommandSession getSession() { - return session; - } - -} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/commands/SimpleSubShell.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/commands/SimpleSubShell.java b/shell/console/src/test/java/org/apache/karaf/shell/commands/SimpleSubShell.java deleted file mode 100644 index 59fe9a2..0000000 --- a/shell/console/src/test/java/org/apache/karaf/shell/commands/SimpleSubShell.java +++ /dev/null @@ -1,38 +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.karaf.shell.commands; - -import org.apache.karaf.shell.commands.basic.SimpleCommand; -import org.apache.karaf.shell.console.SubShellAction; - -public class SimpleSubShell extends SimpleCommand { - - private final String subshell; - - public SimpleSubShell(String subshell) { - super(SubShellAction.class); - this.subshell = subshell; - } - - @Override - public Action createNewAction() { - SubShellAction action = (SubShellAction) super.createNewAction(); - action.setSubShell(subshell); - return action; - } -} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/commands/TestCommands.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/commands/TestCommands.java b/shell/console/src/test/java/org/apache/karaf/shell/commands/TestCommands.java deleted file mode 100644 index 7ad5b75..0000000 --- a/shell/console/src/test/java/org/apache/karaf/shell/commands/TestCommands.java +++ /dev/null @@ -1,178 +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.karaf.shell.commands; - -import java.util.List; -import java.util.Arrays; -import java.io.IOException; -import java.io.StringWriter; -import java.io.BufferedReader; -import java.io.InputStreamReader; - -import junit.framework.TestCase; -import org.apache.karaf.shell.commands.basic.SimpleCommand; -import org.apache.felix.service.command.CommandSession; -import org.apache.karaf.shell.console.ExitAction; - -public class TestCommands extends TestCase { - - public void testSubShellScope() throws Exception { - Context c = new Context(); - c.set("SCOPE", "*"); - c.addCommand("*", new SimpleSubShell("foo"), "foo"); - c.addCommand("*", new SimpleCommand(ExitAction.class), "exit"); - - String scope = (String) c.get("SCOPE"); - c.execute("foo"); - assertEquals("foo:" + scope, c.get("SCOPE")); - c.execute("exit"); - assertEquals(scope, c.get("SCOPE")); - } - - public void testPrompt() throws Exception { - Context c = new Context(); - c.addCommand("echo", this); - c.set("USER", "test"); - c.set("APPLICATION", "karaf"); - //c.set("SCOPE", ""); - Object p = c.execute("echo \"@|bold ${USER}|@${APPLICATION}:@|bold ${SCOPE}|> \""); - System.out.println("Prompt: " + p); - } - - public void testCommand() throws Exception { - Context c = new Context(); - c.addCommand("*", this, "capture"); - c.addCommand("*", new SimpleCommand(MyAction.class), "my-action"); - - // Test help - Object help = c.execute("my-action --help | capture"); - assertTrue(help instanceof String); - assertTrue(((String) help).indexOf("My Action") >= 0); - assertTrue(((String) help).indexOf("First option") >= 0); - assertTrue(((String) help).indexOf("Bundle ids") >= 0); - - // Test required argument - try { - c.execute("my-action"); - fail("Action should have thrown an exception because of a missing argument"); - } catch (CommandException e) { - // ignore - } - - // Test required argument - assertEquals(Arrays.asList(3), c.execute("my-action 3")); - - // Test required argument - assertEquals(Arrays.asList(3), c.execute("my-action 3")); - - // Test required argument - assertEquals(Arrays.asList(3, 5), c.execute("my-action 3 5")); - - // Test option - assertEquals(Arrays.asList(4), c.execute("my-action -i 3")); - - // Test option alias - assertEquals(Arrays.asList(4), c.execute("my-action --increment 3")); - } - - public void testCommandTwoArguments() throws Exception { - Context c = new Context(); - c.addCommand("*", new SimpleCommand(MyActionTwoArguments.class), "my-action-two-arguments"); - - // test required arguments - try { - c.execute("my-action-two-arguments"); - fail("Action should have thrown an exception because of a missing argument"); - } catch (CommandException e) { - assertEquals("Argument one is required", e.getMessage()); - } - - try { - c.execute("my-action-two-arguments 1"); - fail("Action should have thrown an exception because of a missing argument"); - } catch (CommandException e) { - assertEquals("Argument two is required", e.getMessage()); - } - - c.execute("my-action-two-arguments 1 2"); - } - - public String capture() throws IOException { - StringWriter sw = new StringWriter(); - BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in)); - String s = rdr.readLine(); - while (s != null) { - sw.write(s); - s = rdr.readLine(); - } - return sw.toString(); - } - - public CharSequence echo(Object args[]) { - if (args == null) { - return ""; - } - - StringBuilder sb = new StringBuilder(); - String del = ""; - for (Object arg : args) { - sb.append(del); - if (arg != null) { - sb.append(arg); - del = " "; - } - } - return sb; - } - - @Command(scope = "test", name = "my-action", description = "My Action") - public static class MyAction implements Action { - - @Option(name = "-i", aliases = {"--increment"}, description = "First option") - private boolean increment; - - @Argument(name = "ids", description = "Bundle ids", required = true, multiValued = true) - private List<Integer> ids; - - public Object execute(CommandSession session) throws Exception { - if (increment) { - for (int i = 0; i < ids.size(); i++) { - ids.set(i, ids.get(i) + 1); - } - } - return ids; - } - } - - @Command(scope = "test", name = "my-action-two-arguments", description = "My Action with two arguments") - public static class MyActionTwoArguments implements Action { - - @Argument(index = 0, name = "one", description = "one description", required = true, multiValued = false) - private String one; - - @Argument(index = 1, name = "two", description = "two description", required = true, multiValued = false) - private String two; - - public Object execute(CommandSession session) throws Exception { - return null; - } - - } - -} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/Context.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/Context.java b/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/Context.java new file mode 100644 index 0000000..7737c11 --- /dev/null +++ b/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/Context.java @@ -0,0 +1,67 @@ +/* + * 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.karaf.shell.commands; + +import org.apache.felix.gogo.runtime.CommandProcessorImpl; +import org.apache.felix.gogo.runtime.CommandSessionImpl; +import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl; +import org.apache.felix.service.command.CommandSession; + +public class Context extends CommandProcessorImpl +{ + public static final String EMPTY = ""; + CommandSessionImpl session; + static ThreadIOImpl threadio; + + static + { + threadio = new ThreadIOImpl(); + threadio.start(); + } + + public Context() + { + super(threadio); + addCommand("shell", this, "addCommand"); + addCommand("shell", this, "removeCommand"); + addCommand("shell", this, "eval"); + session = (CommandSessionImpl) createSession(System.in, System.out, System.err); + } + + + public Object execute(CharSequence source) throws Exception + { + return session.execute(source); + } + + public void set(String name, Object value) + { + session.put(name, value); + } + + public Object get(String name) + { + return session.get(name); + } + + public CommandSession getSession() { + return session; + } + +} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/SimpleSubShell.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/SimpleSubShell.java b/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/SimpleSubShell.java new file mode 100644 index 0000000..59fe9a2 --- /dev/null +++ b/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/SimpleSubShell.java @@ -0,0 +1,38 @@ +/** + * + * 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.karaf.shell.commands; + +import org.apache.karaf.shell.commands.basic.SimpleCommand; +import org.apache.karaf.shell.console.SubShellAction; + +public class SimpleSubShell extends SimpleCommand { + + private final String subshell; + + public SimpleSubShell(String subshell) { + super(SubShellAction.class); + this.subshell = subshell; + } + + @Override + public Action createNewAction() { + SubShellAction action = (SubShellAction) super.createNewAction(); + action.setSubShell(subshell); + return action; + } +} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/TestCommands.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/TestCommands.java b/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/TestCommands.java new file mode 100644 index 0000000..7ad5b75 --- /dev/null +++ b/shell/console/src/test/java/org/apache/karaf/shell/commands/basic/TestCommands.java @@ -0,0 +1,178 @@ +/* + * 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.karaf.shell.commands; + +import java.util.List; +import java.util.Arrays; +import java.io.IOException; +import java.io.StringWriter; +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import junit.framework.TestCase; +import org.apache.karaf.shell.commands.basic.SimpleCommand; +import org.apache.felix.service.command.CommandSession; +import org.apache.karaf.shell.console.ExitAction; + +public class TestCommands extends TestCase { + + public void testSubShellScope() throws Exception { + Context c = new Context(); + c.set("SCOPE", "*"); + c.addCommand("*", new SimpleSubShell("foo"), "foo"); + c.addCommand("*", new SimpleCommand(ExitAction.class), "exit"); + + String scope = (String) c.get("SCOPE"); + c.execute("foo"); + assertEquals("foo:" + scope, c.get("SCOPE")); + c.execute("exit"); + assertEquals(scope, c.get("SCOPE")); + } + + public void testPrompt() throws Exception { + Context c = new Context(); + c.addCommand("echo", this); + c.set("USER", "test"); + c.set("APPLICATION", "karaf"); + //c.set("SCOPE", ""); + Object p = c.execute("echo \"@|bold ${USER}|@${APPLICATION}:@|bold ${SCOPE}|> \""); + System.out.println("Prompt: " + p); + } + + public void testCommand() throws Exception { + Context c = new Context(); + c.addCommand("*", this, "capture"); + c.addCommand("*", new SimpleCommand(MyAction.class), "my-action"); + + // Test help + Object help = c.execute("my-action --help | capture"); + assertTrue(help instanceof String); + assertTrue(((String) help).indexOf("My Action") >= 0); + assertTrue(((String) help).indexOf("First option") >= 0); + assertTrue(((String) help).indexOf("Bundle ids") >= 0); + + // Test required argument + try { + c.execute("my-action"); + fail("Action should have thrown an exception because of a missing argument"); + } catch (CommandException e) { + // ignore + } + + // Test required argument + assertEquals(Arrays.asList(3), c.execute("my-action 3")); + + // Test required argument + assertEquals(Arrays.asList(3), c.execute("my-action 3")); + + // Test required argument + assertEquals(Arrays.asList(3, 5), c.execute("my-action 3 5")); + + // Test option + assertEquals(Arrays.asList(4), c.execute("my-action -i 3")); + + // Test option alias + assertEquals(Arrays.asList(4), c.execute("my-action --increment 3")); + } + + public void testCommandTwoArguments() throws Exception { + Context c = new Context(); + c.addCommand("*", new SimpleCommand(MyActionTwoArguments.class), "my-action-two-arguments"); + + // test required arguments + try { + c.execute("my-action-two-arguments"); + fail("Action should have thrown an exception because of a missing argument"); + } catch (CommandException e) { + assertEquals("Argument one is required", e.getMessage()); + } + + try { + c.execute("my-action-two-arguments 1"); + fail("Action should have thrown an exception because of a missing argument"); + } catch (CommandException e) { + assertEquals("Argument two is required", e.getMessage()); + } + + c.execute("my-action-two-arguments 1 2"); + } + + public String capture() throws IOException { + StringWriter sw = new StringWriter(); + BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in)); + String s = rdr.readLine(); + while (s != null) { + sw.write(s); + s = rdr.readLine(); + } + return sw.toString(); + } + + public CharSequence echo(Object args[]) { + if (args == null) { + return ""; + } + + StringBuilder sb = new StringBuilder(); + String del = ""; + for (Object arg : args) { + sb.append(del); + if (arg != null) { + sb.append(arg); + del = " "; + } + } + return sb; + } + + @Command(scope = "test", name = "my-action", description = "My Action") + public static class MyAction implements Action { + + @Option(name = "-i", aliases = {"--increment"}, description = "First option") + private boolean increment; + + @Argument(name = "ids", description = "Bundle ids", required = true, multiValued = true) + private List<Integer> ids; + + public Object execute(CommandSession session) throws Exception { + if (increment) { + for (int i = 0; i < ids.size(); i++) { + ids.set(i, ids.get(i) + 1); + } + } + return ids; + } + } + + @Command(scope = "test", name = "my-action-two-arguments", description = "My Action with two arguments") + public static class MyActionTwoArguments implements Action { + + @Argument(index = 0, name = "one", description = "one description", required = true, multiValued = false) + private String one; + + @Argument(index = 1, name = "two", description = "two description", required = true, multiValued = false) + private String two; + + public Object execute(CommandSession session) throws Exception { + return null; + } + + } + +} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/commands/meta/TestFormatting.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/commands/meta/TestFormatting.java b/shell/console/src/test/java/org/apache/karaf/shell/commands/meta/TestFormatting.java new file mode 100644 index 0000000..d709e9c --- /dev/null +++ b/shell/console/src/test/java/org/apache/karaf/shell/commands/meta/TestFormatting.java @@ -0,0 +1,36 @@ +/* + * 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.karaf.shell.util; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import junit.framework.TestCase; + +import org.apache.karaf.shell.util.IndentFormatter; + +public class TestFormatting extends TestCase { + + public void testFormat() throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + IndentFormatter.printFormatted(" ", + " This is a test with a long paragraph\n\n with an indented paragraph\nAnd another one\n", 20, new PrintStream(baos, true)); + System.err.println(baos.toString()); + } +} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java b/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java deleted file mode 100644 index 8229d08..0000000 --- a/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java +++ /dev/null @@ -1,104 +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.karaf.shell.console; - -import java.io.InputStream; -import java.io.PrintStream; - -import jline.Terminal; -import org.apache.felix.gogo.runtime.CommandProcessorImpl; -import org.apache.felix.service.threadio.ThreadIO; -import org.apache.karaf.shell.console.impl.Main; -import org.apache.karaf.shell.console.impl.jline.ConsoleImpl; - -import java.util.Properties; - -/** - * This class is mostly here so that folks can see an example of how you can extend the Karaf Main shell. Also - * lets Karaf developers see how changes the Main class can affect the interface comparability - * with sub classes. - */ -public class ExampleSubclassMain extends Main { - - public static void main(String args[]) throws Exception { - ExampleSubclassMain main = new ExampleSubclassMain(); - main.run(args); - } - - public void ExampleSubclassMain() { - // Sets the name of the shell and the current user. - setApplication("example"); - setUser("unknown"); - } - - @Override - protected ConsoleImpl createConsole(CommandProcessorImpl commandProcessor, ThreadIO threadIO, InputStream in, PrintStream out, PrintStream err, Terminal terminal) throws Exception { - return new ConsoleImpl(commandProcessor, threadIO, in, out, err, terminal, null, null, null) { - - /** - * If you don't overwrite, then karaf will use the welcome message found in the - * following resource files: - * <ul> - * <li>org/apache/karaf/shell/console/branding.properties</li> - * <li>org/apache/karaf/branding/branding.properties</li> - * <ul> - */ - @Override - protected void welcome(Properties brandingProps) { - session.getConsole().println("==============================================="); - session.getConsole().println(" Example Shell "); - session.getConsole().println("==============================================="); - } - - /** - * If you don't overwrite then Karaf builds a prompt based on the current app and user. - * @return - */ - @Override - protected String getPrompt() { - return "example>"; - } - - /** - * If you don't overwrite, then karaf automatically adds session properties - * found in the following resource files: - * <ul> - * <li>org/apache/karaf/shell/console/branding.properties</li> - * <li>org/apache/karaf/branding/branding.properties</li> - * <ul> - */ - @Override - protected void setSessionProperties(Properties brandingProps) { - // we won't add any session properties. - } - - }; - } - - /** - * if you don't override, then Karaf will discover the commands listed in the - * "META-INF/services/org/apache/karaf/shell/commands" resource file. - * - * @return - */ - @Override - public String getDiscoveryResource() { - return "META-INF/services/org/example/commands.index"; - } - - -} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/console/impl/ExampleSubclassMain.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/console/impl/ExampleSubclassMain.java b/shell/console/src/test/java/org/apache/karaf/shell/console/impl/ExampleSubclassMain.java new file mode 100644 index 0000000..8229d08 --- /dev/null +++ b/shell/console/src/test/java/org/apache/karaf/shell/console/impl/ExampleSubclassMain.java @@ -0,0 +1,104 @@ +/** + * 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.karaf.shell.console; + +import java.io.InputStream; +import java.io.PrintStream; + +import jline.Terminal; +import org.apache.felix.gogo.runtime.CommandProcessorImpl; +import org.apache.felix.service.threadio.ThreadIO; +import org.apache.karaf.shell.console.impl.Main; +import org.apache.karaf.shell.console.impl.jline.ConsoleImpl; + +import java.util.Properties; + +/** + * This class is mostly here so that folks can see an example of how you can extend the Karaf Main shell. Also + * lets Karaf developers see how changes the Main class can affect the interface comparability + * with sub classes. + */ +public class ExampleSubclassMain extends Main { + + public static void main(String args[]) throws Exception { + ExampleSubclassMain main = new ExampleSubclassMain(); + main.run(args); + } + + public void ExampleSubclassMain() { + // Sets the name of the shell and the current user. + setApplication("example"); + setUser("unknown"); + } + + @Override + protected ConsoleImpl createConsole(CommandProcessorImpl commandProcessor, ThreadIO threadIO, InputStream in, PrintStream out, PrintStream err, Terminal terminal) throws Exception { + return new ConsoleImpl(commandProcessor, threadIO, in, out, err, terminal, null, null, null) { + + /** + * If you don't overwrite, then karaf will use the welcome message found in the + * following resource files: + * <ul> + * <li>org/apache/karaf/shell/console/branding.properties</li> + * <li>org/apache/karaf/branding/branding.properties</li> + * <ul> + */ + @Override + protected void welcome(Properties brandingProps) { + session.getConsole().println("==============================================="); + session.getConsole().println(" Example Shell "); + session.getConsole().println("==============================================="); + } + + /** + * If you don't overwrite then Karaf builds a prompt based on the current app and user. + * @return + */ + @Override + protected String getPrompt() { + return "example>"; + } + + /** + * If you don't overwrite, then karaf automatically adds session properties + * found in the following resource files: + * <ul> + * <li>org/apache/karaf/shell/console/branding.properties</li> + * <li>org/apache/karaf/branding/branding.properties</li> + * <ul> + */ + @Override + protected void setSessionProperties(Properties brandingProps) { + // we won't add any session properties. + } + + }; + } + + /** + * if you don't override, then Karaf will discover the commands listed in the + * "META-INF/services/org/apache/karaf/shell/commands" resource file. + * + * @return + */ + @Override + public String getDiscoveryResource() { + return "META-INF/services/org/example/commands.index"; + } + + +} http://git-wip-us.apache.org/repos/asf/karaf/blob/6e253c93/shell/console/src/test/java/org/apache/karaf/shell/util/TestFormatting.java ---------------------------------------------------------------------- diff --git a/shell/console/src/test/java/org/apache/karaf/shell/util/TestFormatting.java b/shell/console/src/test/java/org/apache/karaf/shell/util/TestFormatting.java deleted file mode 100644 index d709e9c..0000000 --- a/shell/console/src/test/java/org/apache/karaf/shell/util/TestFormatting.java +++ /dev/null @@ -1,36 +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.karaf.shell.util; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -import junit.framework.TestCase; - -import org.apache.karaf.shell.util.IndentFormatter; - -public class TestFormatting extends TestCase { - - public void testFormat() throws Exception { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - IndentFormatter.printFormatted(" ", - " This is a test with a long paragraph\n\n with an indented paragraph\nAnd another one\n", 20, new PrintStream(baos, true)); - System.err.println(baos.toString()); - } -}
