Skip to content

Commit

Permalink
Aesh improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Mar 22, 2013
1 parent 3ff72c9 commit 2b04118
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 63 deletions.
13 changes: 9 additions & 4 deletions aesh/impl/src/main/java/org/jboss/forge/aesh/ForgeShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public class ForgeShell
@Inject
private AddonRegistry registry;

public void observe(@Observes PostStartup startup) throws Exception
void observe(@Observes PostStartup startup) throws Exception
{
initShell();
startShell();
}

public void stop(@Observes PreShutdown shutdown) throws Exception
void stop(@Observes PreShutdown shutdown) throws Exception
{
if (console != null)
if (console != null && console.isRunning())
console.stop();
}

Expand Down Expand Up @@ -164,6 +164,7 @@ private boolean isCommandFound(String name, Set<ExportedInstance<UICommand>> uiC

public void startShell() throws Exception
{
initShell();
console.start();
}

Expand All @@ -184,7 +185,11 @@ public Console getConsole()

public void stopShell() throws IOException
{
forge.stop();
if (console != null && console.isRunning())
{
console.stop();
console.reset();
}
}

private Prompt createPrompt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import javax.inject.Inject;

import org.jboss.forge.aesh.ForgeShell;
import org.jboss.forge.aesh.ShellContext;
import org.jboss.forge.container.Forge;
import org.jboss.forge.ui.UICommand;
import org.jboss.forge.ui.context.UIBuilder;
import org.jboss.forge.ui.context.UIContext;
Expand All @@ -26,7 +26,7 @@ public abstract class BaseExitCommand implements UICommand
{

@Inject
private ForgeShell aeshell;
private Forge forge;

@Override
public UICommandMetadata getMetadata()
Expand All @@ -53,7 +53,7 @@ public void validate(UIValidationContext context)
@Override
public Result execute(UIContext context) throws Exception
{
aeshell.stopShell();
forge.stop();
return Results.success("");
}

Expand Down
122 changes: 66 additions & 56 deletions aesh/tests/src/test/java/org/jboss/forge/aesh/AeshAddonTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package org.jboss.forge.aesh;

import javax.inject.Inject;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import junit.framework.TestCase;
import javax.inject.Inject;

import org.jboss.aesh.console.Config;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.edit.KeyOperation;
import org.jboss.aesh.edit.actions.Operation;
import org.jboss.aesh.terminal.Key;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.arquillian.Addon;
Expand All @@ -12,7 +21,7 @@
import org.jboss.forge.container.addons.AddonId;
import org.jboss.forge.container.repositories.AddonDependencyEntry;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -22,29 +31,22 @@
*/
@Ignore
@RunWith(Arquillian.class)
public class AeshAddonTest extends TestCase
public class AeshAddonTest
{

// private KeyOperation completeChar = new KeyOperation(Key.CTRL_I, Operation.COMPLETE);

public AeshAddonTest()
{
}
private KeyOperation completeChar = new KeyOperation(Key.CTRL_I, Operation.COMPLETE);

@Deployment
@Dependencies(@Addon(name = "org.jboss.forge:ui", version = "2.0.0-SNAPSHOT"))
@Dependencies({ @Addon(name = "org.jboss.forge:ui", version = "2.0.0-SNAPSHOT"),
@Addon(name = "org.jboss.forge:aesh", version = "2.0.0-SNAPSHOT") })
public static ForgeArchive getDeployment()
{
ForgeArchive archive = ShrinkWrap
.create(ForgeArchive.class)
.addPackages(true, ForgeShell.class.getPackage())
.addAsLibraries(Maven.resolver().loadPomFromFile("pom.xml")
.resolve("org.jboss.aesh:aesh:0.33").withTransitivity().asFile())
.addAsLibraries(Maven.resolver().loadPomFromFile("pom.xml")
.resolve("org.jboss.aesh:aesh-extensions:0.33").withTransitivity().asFile())
ForgeArchive archive = ShrinkWrap.create(ForgeArchive.class)
.addClasses(FooCommand.class)
.addBeansXML()
.addAsAddonDependencies(
AddonDependencyEntry.create(AddonId.from("org.jboss.forge:ui", "2.0.0-SNAPSHOT")));
AddonDependencyEntry.create(AddonId.from("org.jboss.forge:ui", "2.0.0-SNAPSHOT")),
AddonDependencyEntry.create(AddonId.from("org.jboss.forge:aesh", "2.0.0-SNAPSHOT"))
);

return archive;
}
Expand All @@ -56,45 +58,53 @@ public static ForgeArchive getDeployment()
private FooCommand fooCommand;

@Test
public void testContainerInjection()
public void testContainerInjection() throws Exception
{
/*
* try {
*
* Assert.assertNotNull(shell);
*
* PipedOutputStream outputStream = new PipedOutputStream(); PipedInputStream pipedInputStream = new
* PipedInputStream(outputStream); ByteArrayOutputStream out = new ByteArrayOutputStream();
*
* setupSettings(pipedInputStream, out);
*
* shell.initShell(); shell.addCommand(new ShellCommand(fooCommand));
*
* outputStream.write(("foo\n").getBytes()); shell.startShell(); String outString = out.toString();
* assertEquals("boo", outString.substring(shell.getPrompt().length() + "foo\n".length()));
*
* outputStream.write("fo".getBytes()); outputStream.write(completeChar.getFirstValue());
* outputStream.write("\n".getBytes()); shell.startShell(); outString = out.toString();
*
* outputStream.write(("list-services\n").getBytes()); shell.startShell(); // System.out.println("OUT:"+
* out.toString());
*
* outputStream.write(("exit\n").getBytes()); shell.startShell();
*
* // shell.stopShell(); } catch (Exception ioe) { ioe.printStackTrace(); }
*/
Assert.assertNotNull(shell);

PipedOutputStream outputStream = new PipedOutputStream();
PipedInputStream pipedInputStream = new PipedInputStream(outputStream);
ByteArrayOutputStream out = new ByteArrayOutputStream();

setupSettings(pipedInputStream, out);

shell.addCommand(new ShellCommand(fooCommand, shell));

outputStream.write(("foo\n").getBytes());
shell.stopShell();
shell.startShell();
String outString = out.toString();
Assert.assertEquals("boo", outString.substring(shell.getPrompt().length() + "foo\n".length()));

outputStream.write("fo".getBytes());
outputStream.write(completeChar.getFirstValue());
outputStream.write("\n".getBytes());
shell.stopShell();
shell.startShell();
outString = out.toString();

outputStream.write(("list-services\n").getBytes());
shell.stopShell();
shell.startShell();
System.out.println("OUT:" + out.toString());

outputStream.write(("exit\n").getBytes());
shell.stopShell();
shell.startShell();

shell.stopShell();
}

// private void setupSettings(InputStream input, OutputStream out)
// {
// Settings.getInstance().setName("test");
// Settings.getInstance().setInputStream(input);
// Settings.getInstance().setStdOut(out);
// // aeshProducer.getSettings().setStdOut(new ByteArrayOutputStream());
// if (!Config.isOSPOSIXCompatible())
// Settings.getInstance().setAnsiConsole(false);
//
// Settings.getInstance().getOperationManager().addOperation(new KeyOperation(10, Operation.NEW_LINE));
// }
private void setupSettings(InputStream input, OutputStream out)
{
Settings.getInstance().setName("test");
Settings.getInstance().setInputStream(input);
Settings.getInstance().setStdOut(out);
// aeshProducer.getSettings().setStdOut(new ByteArrayOutputStream());
if (!Config.isOSPOSIXCompatible())
Settings.getInstance().setAnsiConsole(false);

Settings.getInstance().getOperationManager().addOperation(new KeyOperation(Key.ENTER, Operation.NEW_LINE));
}

}

0 comments on commit 2b04118

Please sign in to comment.