Skip to content

Commit

Permalink
Added truly asychronous ShellTest.execute() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Jan 15, 2014
1 parent 002e0e0 commit 39d5410
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import org.jboss.forge.addon.ui.result.Result;

/**
* Utility for interacting with the Forge shell in a synchronous manner.
* Utility for interacting with the Forge {@link Shell} in an asynchronous manner, providing methods to perform blocking
* writes, reads, and other test functions.
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
Expand All @@ -34,42 +35,47 @@ public interface ShellTest
*/
Shell getShell();

/**
* Execute the given line without waiting for it to run to completion.
*/
void execute(String line);

/**
* Execute the given line and return the {@link Result}. Fail if not complete within the given quantity of
* {@link TimeUnit}. Clears STDOUT and STDERR before execution. (Appends the newline character to the given input if
* necessary, and calls {@link OutputStream#flush()} on {@link #getStdIn()}).
* {@link TimeUnit}. (Appends the newline character to the given input if necessary, and calls
* {@link OutputStream#flush()} on {@link #getStdIn()}).
*
* @throws TimeoutException if the given command was not executed successfully within the allotted timeout.
*/
Result execute(String line, int quantity, TimeUnit unit) throws TimeoutException;

/**
* Wait for the console {@link Buffer} to be updated the given task is executed.
* Wait for the console {@link Buffer} to be updated after the given task is executed.
*
* @throws TimeoutException if the timeout is reached without detecting a buffer value change.
*/
void waitForBufferChanged(Callable<?> task, int quantity, TimeUnit unit) throws TimeoutException;

/**
* Wait for the console {@link Buffer} to be updated the given task is executed, and assert that it matches the given
* {@link String}.
* Wait for the console {@link Buffer} to be updated after the given task is executed, and assert that it matches the
* given {@link String}.
*
* @throws TimeoutException if the timeout is reached without detecting the appropriate value.
*/
void waitForBufferValue(Callable<?> task, int quantity, TimeUnit unit, String expected) throws TimeoutException;
void waitForBufferValue(Callable<?> task, String expected, int quantity, TimeUnit unit) throws TimeoutException;

/**
* Clear and wait for the next write to STDOUT. Send the provided line to STDIN. Fail if no write occurs within the
* given quantity of {@link TimeUnit}
* Clear and wait for the next write to STDOUT. Send the provided line to STDIN.
*
* @throws TimeoutException if the timeout is reached without detecting a write to STDOUT.
* @throws TimeoutException if the timeout is reached without detecting the expected write to STDOUT.
*/
void waitForStdOutChanged(String input, int quantity, TimeUnit unit) throws TimeoutException;

/**
* Execute the given {@link Callable} task, waiting for STDOUT and returning the resultant output.
* lear and wait for the next write to STDOUT. Execute the given {@link Callable} task, waiting for STDOUT and
* returning the resultant output.
*
* @throws TimeoutException if the timeout is reached without detecting a write to STDOUT.
* @throws TimeoutException if the timeout is reached without detecting the expected write to STDOUT.
*/
String waitForStdOutChanged(Callable<?> task, int quantity, TimeUnit unit) throws TimeoutException;

Expand All @@ -79,7 +85,7 @@ public interface ShellTest
*
* @throws TimeoutException if the timeout is reached without detecting the expected write to STDOUT.
*/
void waitForStdOutValue(Callable<Void> task, int timeout, TimeUnit unit, String expected) throws TimeoutException;
void waitForStdOutValue(Callable<Void> task, String expected, int timeout, TimeUnit unit) throws TimeoutException;

/**
* Clear and wait for the next write to STDOUT. Send the provided line to STDIN. Fail if no write occurs within the
Expand All @@ -90,19 +96,33 @@ public interface ShellTest
void waitForStdErrChanged(String input, int quantity, TimeUnit unit) throws TimeoutException;

/**
* Execute the given {@link Callable} task, waiting for STDERR and returning the resultant output.
* Wait for STDOUT to write the expected value. Does not clear STDOUT before waiting.
*
* @throws TimeoutException if the timeout is reached without detecting the expected write to STDOUT.
*/
void waitForStdOutValue(String expected, int timeout, TimeUnit unit) throws TimeoutException;

/**
* Wait for STDERR to write the expected value. Does not clear STDERR before waiting.
*
* @throws TimeoutException if the timeout is reached without detecting the expected write to STDERR.
*/
void waitForStdErrValue(String expected, int timeout, TimeUnit unit) throws TimeoutException;

/**
* Clear STDERR, execute the given {@link Callable} task, waiting for STDERR and returning the resultant output.
*
* @throws TimeoutException if the timeout is reached without detecting a write to STDERR.
*/
String waitForStdErrChanged(Callable<?> callable, int quantity, TimeUnit unit) throws TimeoutException;

/**
* Execute the given {@link Callable} task, waiting for STDERR to be updated, and assert that it matches the given
* value.
* Clear STDERR, execute the given {@link Callable} task, waiting for STDERR to be updated, and assert that it
* matches the given value.
*
* @throws TimeoutException if the timeout is reached without detecting the expected write to STDERR.
*/
void waitForStdErrValue(Callable<Void> task, int timeout, TimeUnit unit, String expected) throws TimeoutException;
void waitForStdErrValue(Callable<Void> task, String expected, int timeout, TimeUnit unit) throws TimeoutException;

/**
* Get the STDIN {@link OutputStream} for writing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public class DefaultShellTest implements ShellTest
private ShellFactory factory;
private Shell shell;

private final Callable<?> nullCallable = new Callable<Void>()
{
@Override
public Void call() throws Exception
{
return null;
}
};

@Override
public Shell getShell()
{
Expand All @@ -75,6 +84,23 @@ public String getBuffer()
return console.getBuffer();
}

@Override
public void execute(String line)
{
Assert.notNull(line, "Line to execute cannot be null.");

try
{
if (!line.trim().endsWith(OperatingSystemUtils.getLineSeparator()))
line = line + OperatingSystemUtils.getLineSeparator();
provider.getStdIn().write(line.getBytes());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}

@Override
public Result execute(String line, int quantity, TimeUnit unit) throws TimeoutException
{
Expand All @@ -83,8 +109,8 @@ public Result execute(String line, int quantity, TimeUnit unit) throws TimeoutEx
Result result;
try
{
if (!line.trim().endsWith("\n"))
line = line + "\n";
if (!line.trim().endsWith(OperatingSystemUtils.getLineSeparator()))
line = line + OperatingSystemUtils.getLineSeparator();
listener.reset();
provider.getStdIn().write(line.getBytes());
long start = System.currentTimeMillis();
Expand Down Expand Up @@ -116,7 +142,7 @@ public Result execute(String line, int quantity, TimeUnit unit) throws TimeoutEx
@Override
public void waitForStdOutChanged(final String value, int quantity, TimeUnit unit) throws TimeoutException
{
waitForStream(new Callable<Void>()
waitForStreamChanged(new Callable<Void>()
{
@Override
public Void call() throws Exception
Expand All @@ -131,7 +157,7 @@ public Void call() throws Exception
@Override
public void waitForStdErrChanged(final String value, int quantity, TimeUnit unit) throws TimeoutException
{
waitForStream(new Callable<Void>()
waitForStreamChanged(new Callable<Void>()
{
@Override
public Void call() throws Exception
Expand All @@ -146,35 +172,46 @@ public Void call() throws Exception
@Override
public String waitForStdOutChanged(Callable<?> task, int quantity, TimeUnit unit) throws TimeoutException
{
waitForStream(task, provider.getStdOut(), quantity, unit);
waitForStreamChanged(task, provider.getStdOut(), quantity, unit);
return new String(provider.getStdOut().toByteArray());
}

@Override
public String waitForStdErrChanged(Callable<?> task, int quantity, TimeUnit unit) throws TimeoutException
{
waitForStream(task, provider.getStdErr(), quantity, unit);
waitForStreamChanged(task, provider.getStdErr(), quantity, unit);
return new String(provider.getStdErr().toByteArray());
}

@Override
public void waitForStdOutValue(Callable<Void> task, int timeout, TimeUnit unit, String expected)
public void waitForStdOutValue(String expected, int timeout, TimeUnit unit) throws TimeoutException
{
waitForStreamValue(nullCallable, provider.getStdOut(), expected, timeout, unit);
}

@Override
public void waitForStdErrValue(String expected, int timeout, TimeUnit unit) throws TimeoutException
{
waitForStreamValue(nullCallable, provider.getStdErr(), expected, timeout, unit);
}

@Override
public void waitForStdOutValue(Callable<Void> task, String expected, int timeout, TimeUnit unit)
throws TimeoutException
{
waitForStreamValue(task, provider.getStdOut(), timeout, unit, expected);
clearAndWaitForStreamValue(task, provider.getStdOut(), expected, timeout, unit);
}

@Override
public void waitForStdErrValue(Callable<Void> task, int timeout, TimeUnit unit, String expected)
public void waitForStdErrValue(Callable<Void> task, String expected, int timeout, TimeUnit unit)
throws TimeoutException
{
waitForStreamValue(task, provider.getStdErr(), timeout, unit, expected);
clearAndWaitForStreamValue(task, provider.getStdErr(), expected, timeout, unit);
}

private void waitForStream(Callable<?> task, ByteArrayOutputStream stream, int quantity, TimeUnit unit)
private void waitForStreamChanged(Callable<?> task, ByteArrayOutputStream stream, int quantity, TimeUnit unit)
throws TimeoutException
{
stream.reset();
final int size = stream.size();
try
{
Expand Down Expand Up @@ -205,10 +242,16 @@ private void waitForStream(Callable<?> task, ByteArrayOutputStream stream, int q
}
}

private void waitForStreamValue(Callable<?> task, ByteArrayOutputStream stream, int quantity, TimeUnit unit,
String expected) throws TimeoutException
private void clearAndWaitForStreamValue(Callable<?> task, ByteArrayOutputStream stream, String expected,
int quantity, TimeUnit unit) throws TimeoutException
{
stream.reset();
waitForStreamValue(task, stream, expected, quantity, unit);
}

private void waitForStreamValue(Callable<?> task, ByteArrayOutputStream stream, String expected, int quantity,
TimeUnit unit) throws TimeoutException
{
try
{
task.call();
Expand Down Expand Up @@ -273,7 +316,7 @@ public void waitForBufferChanged(Callable<?> task, int quantity, TimeUnit unit)
}

@Override
public void waitForBufferValue(Callable<?> task, int quantity, TimeUnit unit, String expected)
public void waitForBufferValue(Callable<?> task, String expected, int quantity, TimeUnit unit)
throws TimeoutException
{
try
Expand Down Expand Up @@ -430,9 +473,17 @@ public void sendCompletionSignal() throws IOException

private TimeoutException throwTimeout(String message) throws TimeoutException
{
return new TimeoutException(message + "\n\nSTDOUT: " + provider.getStdOut().toString()
+ "\n\nSTDERR: " + provider.getStdErr().toString()
+ "\n\nBUFFER: [" + getBuffer() + "]\n");
return new TimeoutException(message
+ OperatingSystemUtils.getLineSeparator()
+ OperatingSystemUtils.getLineSeparator()
+ "STDOUT: " + provider.getStdOut().toString()
+ OperatingSystemUtils.getLineSeparator()
+ OperatingSystemUtils.getLineSeparator()
+ "STDERR: " + provider.getStdErr().toString()
+ OperatingSystemUtils.getLineSeparator()
+ OperatingSystemUtils.getLineSeparator()
+ "BUFFER: [" + getBuffer() + "]"
+ OperatingSystemUtils.getLineSeparator());
};

@Override
Expand All @@ -445,12 +496,12 @@ public void clearScreen() throws IOException
@Override
public String call() throws Exception
{
getStdIn().write((Key.CTRL_U.getAsChar() + "\n").getBytes());
getStdIn().write((Key.CTRL_U.getAsChar() + OperatingSystemUtils.getLineSeparator()).getBytes());
provider.getStdOut().reset();
provider.getStdErr().reset();
return null;
}
}, 10, TimeUnit.SECONDS, "");
}, "", 10, TimeUnit.SECONDS);
}
catch (TimeoutException e)
{
Expand All @@ -476,10 +527,10 @@ public String call() throws Exception
sendCompletionSignal();
return null;
}
}, quantity, unit, expected);
}, expected, quantity, unit);
return null;
}
}, quantity, unit, expected);
}, expected, quantity, unit);

return getStdOut();
}
Expand All @@ -501,10 +552,10 @@ public String call() throws Exception
sendCompletionSignal();
return null;
}
}, quantity, unit, buffer);
}, buffer, quantity, unit);
return null;
}
}, quantity, unit, buffer);
}, buffer, quantity, unit);

return getStdOut();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class MockCommandExecutionListener extends AbstractCommandExecutionListen
{
private boolean pre;
private boolean post;
private Result result;

@Override
public void preCommandExecuted(UICommand command, UIExecutionContext context)
Expand All @@ -35,6 +36,7 @@ public void postCommandExecuted(UICommand command, UIExecutionContext context, R
Assert.assertNotNull(context);
Assert.assertNotNull(result);
this.post = true;
this.result = result;
}

public boolean isPreExecuted()
Expand All @@ -46,4 +48,9 @@ public boolean isPostExecuted()
{
return post;
}

public Result getResult()
{
return result;
}
}
Loading

0 comments on commit 39d5410

Please sign in to comment.