SHL-103 - Create simple way to test the execution of shell commands

This commit is contained in:
mpollack
2013-07-25 20:19:34 -04:00
parent 43e328c3ba
commit d6e0cfe34c
3 changed files with 106 additions and 11 deletions

View File

@@ -157,7 +157,55 @@ public class HelloWorldCommands implements CommandMarker {
is returned, the shell will display its <literal>toString()</literal>
representation.</para>
</section>
<section>
<title>Testing shell commands</title>
<para>To perform a test of the shell commands you can instantiate the shell inside a test case,
execute the command and then perform assertions on the return value CommandResult.
A simple base class to set this up is shown below.</para>
<programlisting language="java">public abstract class AbstractShellIntegrationTest {
private static JLineShellComponent shell;
@BeforeClass
public static void startUp() throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
shell = bootstrap.getJLineShellComponent();
}
@AfterClass
public static void shutdown() {
shell.stop();
}
public static JLineShellComponent getShell() {
return shell;
}
}</programlisting>
<para>Here is an example testing the Date command</para>
<programlisting language="java">public class BuiltInCommandTests extends AbstractShellIntegrationTest {
@Test
public void dateTest() throws ParseException {
//Execute command
CommandResult cr = getShell().executeCommand("date");
//Get result
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,Locale.US);
Date result = df.parse(cr.getResult().toString());
//Make assertions - DateMaters is an external dependency not shown here.
Date now = new Date();
MatcherAssert.assertThat(now, DateMatchers.within(5, TimeUnit.SECONDS, result));
}
}</programlisting>
<para>The java.lang.Class of CommandResult's getResult method will match that of the return value of
the method annotated with @CliCommand. You should cast to the appropriate type to help perform your assertions.
</para>
</section>
<section>
<title>Building and running the shell</title>

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed 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.springframework.shell.commands;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.shell.Bootstrap;
import org.springframework.shell.core.JLineShellComponent;
/**
* Superclass for performing integration tests of shell commands.
*
* JUnit's BeforeClass and AfterClass annotations are used to start and stop the shell.
* in local mode with the default store configured to use in-memory storage.
*
* @author Mark Pollack
*
*/
public abstract class AbstractShellIntegrationTest {
private static JLineShellComponent shell;
@BeforeClass
public static void startUp() throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
shell = bootstrap.getJLineShellComponent();
}
@AfterClass
public static void shutdown() {
shell.stop();
}
public static JLineShellComponent getShell() {
return shell;
}
}

View File

@@ -23,29 +23,24 @@ import java.util.concurrent.TimeUnit;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.springframework.shell.Bootstrap;
import org.springframework.shell.core.CommandResult;
import org.springframework.shell.core.JLineShellComponent;
import uk.co.it.modular.hamcrest.date.DateMatchers;
public class BuiltInCommandTests {
public class BuiltInCommandTests extends AbstractShellIntegrationTest {
@Test
public void dateTest() throws ParseException {
Bootstrap bootstrap = new Bootstrap();
JLineShellComponent shell = bootstrap.getJLineShellComponent();
CommandResult cr = shell.executeCommand("date");
//Execute command
CommandResult cr = getShell().executeCommand("date");
//Get result
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,Locale.US);
Date result = df.parse(cr.getResult().toString());
//Make assertions
Date now = new Date();
MatcherAssert.assertThat(now, DateMatchers.within(5, TimeUnit.SECONDS, result));
}
}