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

View File

@ -27,6 +27,11 @@ public class GridworksServletTests {
//System under test //System under test
GridworksServletStub SUT = null; 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 //mocks
HttpServletRequest request = null; HttpServletRequest request = null;
HttpServletResponse response = null; HttpServletResponse response = null;
@ -40,7 +45,7 @@ public class GridworksServletTests {
request = mock(HttpServletRequest.class); request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class); response = mock(HttpServletResponse.class);
command = mock(Command.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 @After
@ -49,13 +54,13 @@ public class GridworksServletTests {
request = null; request = null;
response = null; response = null;
command = null; command = null;
GridworksServletStub.RemoveTestCommand(); //remove mock to clean command container GridworksServletStub.RemoveCommand(TEST_COMMAND_NAME); //remove mock to clean command container
} }
//--------------------doGet tests--------------------- //--------------------doGet tests---------------------
@Test @Test
public void doGetRegressionTest(){ public void doGetRegressionTest(){
when(request.getPathInfo()).thenReturn("/test-command/foobar"); //called in getCommandName method whenGetCommandNameThenReturn(TEST_COMMAND_PATH);
try { try {
SUT.wrapDoGet(request, response); SUT.wrapDoGet(request, response);
@ -65,7 +70,7 @@ public class GridworksServletTests {
Assert.fail(); Assert.fail();
} }
verify(request,times(1)).getPathInfo(); verifyGetCommandNameCalled();
try { try {
verify(command,times(1)).doGet(request, response); verify(command,times(1)).doGet(request, response);
} catch (ServletException e) { } catch (ServletException e) {
@ -74,4 +79,88 @@ public class GridworksServletTests {
Assert.fail(); 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();
}
}
} }