doGet and doPost methods of GridworksServlet now unit tested. @Ignore attribute added to GridworksServletStub to prevent ./gridworks server_test failing.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@769 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-05-14 17:34:40 +00:00
parent 6450921c02
commit 624b421da9
2 changed files with 115 additions and 12 deletions

View File

@ -6,6 +6,8 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Ignore;
import com.metaweb.gridworks.GridworksServlet;
import com.metaweb.gridworks.commands.Command;
@ -13,27 +15,39 @@ import com.metaweb.gridworks.commands.Command;
* Exposes protected methods of com.metaweb.gridworks.GridworksServlet as public for unit testing
*
*/
@Ignore
public class GridworksServletStub extends GridworksServlet{
//requirement of extending HttpServlet, not required for testing
private static final long serialVersionUID = 1L;
public void wrapDoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
super.doGet(request, response);
}
public void wrapDoPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
super.doPost(request, response);
}
public String wrapGetCommandName(HttpServletRequest request){
return super.getCommandName(request);
}
//-------------------helper methods--------------
/**
* Helper method for inserting a mock object
* @param commandName
* @param command
*/
static public void InsertTestCommand( Command command ){
_commands.put("test-command", command);
static public void InsertCommand( String commandName, Command command ){
_commands.put(commandName, command);
}
/**
* Helper method for clearing up after testing
* @param commandName
*/
static public void RemoveTestCommand( ){
_commands.remove("test-command");
}
public void wrapDoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
super.doGet(request, response);
static public void RemoveCommand( String commandName ){
_commands.remove(commandName);
}
}

View File

@ -27,6 +27,11 @@ public class GridworksServletTests {
//System under test
GridworksServletStub SUT = null;
//variables
String TEST_COMMAND_NAME = "test-command";
String TEST_COMMAND_PATH = "/test-command/foobar";
String BAD_COMMAND_PATH = "/command-does-not-exist";
//mocks
HttpServletRequest request = null;
HttpServletResponse response = null;
@ -40,7 +45,7 @@ public class GridworksServletTests {
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
command = mock(Command.class);
GridworksServletStub.InsertTestCommand(command); //inject mock into command container
GridworksServletStub.InsertCommand(TEST_COMMAND_NAME,command); //inject mock into command container
}
@After
@ -49,13 +54,13 @@ public class GridworksServletTests {
request = null;
response = null;
command = null;
GridworksServletStub.RemoveTestCommand(); //remove mock to clean command container
GridworksServletStub.RemoveCommand(TEST_COMMAND_NAME); //remove mock to clean command container
}
//--------------------doGet tests---------------------
@Test
public void doGetRegressionTest(){
when(request.getPathInfo()).thenReturn("/test-command/foobar"); //called in getCommandName method
whenGetCommandNameThenReturn(TEST_COMMAND_PATH);
try {
SUT.wrapDoGet(request, response);
@ -65,7 +70,7 @@ public class GridworksServletTests {
Assert.fail();
}
verify(request,times(1)).getPathInfo();
verifyGetCommandNameCalled();
try {
verify(command,times(1)).doGet(request, response);
} catch (ServletException e) {
@ -74,4 +79,88 @@ public class GridworksServletTests {
Assert.fail();
}
}
@Test
public void doGetReturnsError404WhenCommandNotFound(){
whenGetCommandNameThenReturn(BAD_COMMAND_PATH);
try {
SUT.wrapDoGet(request, response);
} catch (ServletException e) {
Assert.fail();
} catch (IOException e) {
Assert.fail();
}
verifyGetCommandNameCalled();
verifyError404Called();
}
//----------------doPost tests-------------------------
@Test
public void doPostRegressionTest(){
whenGetCommandNameThenReturn(TEST_COMMAND_PATH);
try {
SUT.wrapDoPost(request, response);
} catch (ServletException e) {
Assert.fail();
} catch (IOException e) {
Assert.fail();
}
verifyGetCommandNameCalled();
try {
verify(command,times(1)).doPost(request, response);
} catch (ServletException e) {
Assert.fail();
} catch (IOException e) {
Assert.fail();
}
}
@Test
public void doPostReturns404WhenCommandNotFound(){
whenGetCommandNameThenReturn(BAD_COMMAND_PATH);
try {
SUT.wrapDoPost(request, response);
} catch (ServletException e) {
Assert.fail();
} catch (IOException e) {
Assert.fail();
}
verifyGetCommandNameCalled();
verifyError404Called();
}
//----------------getCommandName tests----------------
@Test
public void getCommandNameHandlesBadCommandName(){
when(request.getPathInfo()).thenReturn("/this-command-has-no-trailing-slash");
Assert.assertEquals("this-command-has-no-trailing-slash", SUT.wrapGetCommandName(request));
verify(request, times(1)).getPathInfo();
}
//helpers
protected void whenGetCommandNameThenReturn(String commandName){
when(request.getPathInfo()).thenReturn(commandName);
}
protected void verifyGetCommandNameCalled(){
verify(request,times(1)).getPathInfo();
}
protected void verifyError404Called(){
try {
verify(response,times(1)).sendError(404);
} catch (IOException e) {
Assert.fail();
}
}
}