-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FORGE-2574: Introduced CommandLine in ShellContext
- Loading branch information
Showing
9 changed files
with
279 additions
and
24 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
shell/api/src/main/java/org/jboss/forge/addon/shell/line/CommandLine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.shell.line; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* The command line | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public interface CommandLine | ||
{ | ||
/** | ||
* @return the arguments for a given command line | ||
*/ | ||
CommandOption getArgument(); | ||
|
||
/** | ||
* @return the parsed options from the command line | ||
*/ | ||
List<CommandOption> getOptions(); | ||
} |
23 changes: 23 additions & 0 deletions
23
shell/api/src/main/java/org/jboss/forge/addon/shell/line/CommandOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.shell.line; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public interface CommandOption | ||
{ | ||
String getName(); | ||
|
||
String getValue(); | ||
|
||
List<String> getValues(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
shell/impl/src/main/java/org/jboss/forge/addon/shell/aesh/line/CommandLineImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.shell.aesh.line; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.jboss.aesh.cl.CommandLine; | ||
import org.jboss.aesh.cl.internal.ProcessedOption; | ||
import org.jboss.forge.addon.shell.line.CommandOption; | ||
|
||
/** | ||
* Default {@link org.jboss.forge.addon.shell.line.CommandLine} implementation | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class CommandLineImpl implements org.jboss.forge.addon.shell.line.CommandLine | ||
{ | ||
private final CommandLine<?> cmdLine; | ||
|
||
public CommandLineImpl(CommandLine<?> cmdLine) | ||
{ | ||
super(); | ||
this.cmdLine = cmdLine; | ||
} | ||
|
||
@Override | ||
public CommandOption getArgument() | ||
{ | ||
ProcessedOption argument = cmdLine.getArgument(); | ||
return argument == null || argument.getValue() == null ? null : new CommandOptionImpl(argument); | ||
} | ||
|
||
@Override | ||
public List<CommandOption> getOptions() | ||
{ | ||
return cmdLine | ||
.getOptions() | ||
.stream() | ||
.map((option) -> new CommandOptionImpl(option)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "CommandLineImpl [cmdLine=" + cmdLine + "]"; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
shell/impl/src/main/java/org/jboss/forge/addon/shell/aesh/line/CommandOptionImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.shell.aesh.line; | ||
|
||
import java.util.List; | ||
|
||
import org.jboss.aesh.cl.internal.ProcessedOption; | ||
import org.jboss.forge.addon.shell.line.CommandOption; | ||
|
||
/** | ||
* Default {@link CommandOption} implementation | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
class CommandOptionImpl implements CommandOption | ||
{ | ||
private final ProcessedOption option; | ||
|
||
public CommandOptionImpl(ProcessedOption option) | ||
{ | ||
this.option = option; | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return option.getName(); | ||
} | ||
|
||
@Override | ||
public String getValue() | ||
{ | ||
return option.getValue(); | ||
} | ||
|
||
@Override | ||
public List<String> getValues() | ||
{ | ||
return option.getValues(); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "CommandOptionImpl [option=" + option + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
shell/tests/src/test/java/org/jboss/forge/addon/shell/ShellCommandLineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.shell; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.forge.addon.shell.line.CommandLine; | ||
import org.jboss.forge.addon.shell.line.CommandOption; | ||
import org.jboss.forge.addon.shell.mock.command.Career; | ||
import org.jboss.forge.addon.shell.mock.command.FooCommand; | ||
import org.jboss.forge.addon.shell.test.ShellTest; | ||
import org.jboss.forge.addon.shell.ui.ShellContext; | ||
import org.jboss.forge.addon.ui.command.AbstractCommandExecutionListener; | ||
import org.jboss.forge.addon.ui.command.UICommand; | ||
import org.jboss.forge.addon.ui.context.UIExecutionContext; | ||
import org.jboss.forge.arquillian.AddonDependencies; | ||
import org.jboss.forge.arquillian.AddonDependency; | ||
import org.jboss.forge.arquillian.archive.AddonArchive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* | ||
* @author <a href="ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
@RunWith(Arquillian.class) | ||
public class ShellCommandLineTest | ||
{ | ||
@Deployment | ||
@AddonDependencies({ | ||
@AddonDependency(name = "org.jboss.forge.addon:shell-test-harness"), | ||
@AddonDependency(name = "org.jboss.forge.furnace.container:cdi") | ||
|
||
}) | ||
public static AddonArchive getDeployment() | ||
{ | ||
AddonArchive archive = ShrinkWrap.create(AddonArchive.class) | ||
.addBeansXML() | ||
.addClasses(FooCommand.class, Career.class); | ||
|
||
return archive; | ||
} | ||
|
||
@Inject | ||
private ShellTest shellTest; | ||
|
||
@Before | ||
public void setUp() throws Exception | ||
{ | ||
shellTest.clearScreen(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception | ||
{ | ||
shellTest.close(); | ||
} | ||
|
||
@Test(timeout = 10000) | ||
public void testCommandLineShouldContainExecutionData() throws Exception | ||
{ | ||
final AtomicReference<CommandLine> ref = new AtomicReference<>(); | ||
shellTest.getShell().addCommandExecutionListener(new AbstractCommandExecutionListener() | ||
{ | ||
@Override | ||
public void preCommandExecuted(UICommand command, UIExecutionContext context) | ||
{ | ||
ShellContext uiContext = (ShellContext) context.getUIContext(); | ||
ref.set(uiContext.getCommandLine()); | ||
} | ||
}); | ||
shellTest.execute("foocommand --name George --help HALP", 10, TimeUnit.SECONDS); | ||
CommandLine commandLine = ref.get(); | ||
Assert.assertThat(commandLine, notNullValue()); | ||
Assert.assertThat(commandLine.getArgument(), nullValue()); | ||
List<CommandOption> options = commandLine.getOptions(); | ||
Assert.assertThat(options.size(), equalTo(2)); | ||
Assert.assertThat(options.get(0).getName(), equalTo("name")); | ||
Assert.assertThat(options.get(0).getValue(), equalTo("George")); | ||
Assert.assertThat(options.get(1).getName(), equalTo("help")); | ||
Assert.assertThat(options.get(1).getValue(), equalTo("HALP")); | ||
} | ||
} |