Applying patch for Issue 21 from iainsproat

git-svn-id: http://google-refine.googlecode.com/svn/trunk@722 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Stefano Mazzocchi 2010-05-12 08:59:05 +00:00
parent fe0afa0bc3
commit 11da70d223
7 changed files with 167 additions and 6 deletions

View File

@ -107,6 +107,9 @@ licenses/icu4j.LICENSE.txt
licenses/json.LICENSE.txt
json
licenses/mockito.LICENSE.txt
mockito
Others
------

View File

@ -146,14 +146,18 @@ public abstract class Command {
}
static protected void respond(HttpServletResponse response, String content)
throws IOException {
throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpServletResponse.SC_OK);
Writer w = response.getWriter();
w.write(content);
w.flush();
w.close();
if (w != null) {
w.write(content);
w.flush();
w.close();
} else {
throw new ServletException("response returned a null writer");
}
}
static protected void respond(HttpServletResponse response, String status, String message)
@ -191,10 +195,14 @@ public abstract class Command {
}
static protected void respondException(HttpServletResponse response, Exception e)
throws IOException {
throws IOException, ServletException {
logger.warn("Exception caught", e);
if (response == null) {
throw new ServletException("Response object can't be null");
}
try {
JSONObject o = new JSONObject();
o.put("code", "error");

View File

@ -17,7 +17,7 @@ public class CancelProcessesCommand extends Command {
try {
Project project = getProject(request);
project.processManager.cancelAll();
project.getProcessManager().cancelAll();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");

View File

@ -375,4 +375,10 @@ public class Project {
row.contextRowSlots[cellIndex] = contextRowIndex;
row.contextCellSlots[cellIndex] = contextCellIndex;
}
//wrapper of processManager variable to allow unit testing
//TODO make the processManager variable private, and force all calls through this method
public ProcessManager getProcessManager() {
return this.processManager;
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,144 @@
package com.metaweb.gridworks.tests.commands.util;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.util.CancelProcessesCommand;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.process.ProcessManager;
public class CancelProcessesCommandTests {
// logging
final static protected Logger logger = LoggerFactory.getLogger("CancelProcessesCommandTests");
// System Under Test
CancelProcessesCommand SUT = null;
// variables
long PROJECT_ID_LONG = 1234;
String PROJECT_ID = "1234";
// mocks
HttpServletRequest request = null;
HttpServletResponse response = null;
ProjectManager projMan = null;
Project p = null;
ProcessManager processMan = null;
@Before
public void SetUp() {
projMan = mock(ProjectManager.class);
ProjectManager.singleton = projMan;
p = mock(Project.class);
processMan = mock(ProcessManager.class);
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
SUT = new CancelProcessesCommand();
}
@After
public void TearDown() {
SUT = null;
projMan = null;
ProjectManager.singleton = null;
p = null;
request = null;
response = null;
}
@Test
public void doPostFailsThrowsWithNullParameters() {
// both parameters null
try {
SUT.doPost(null, null);
Assert.fail(); // should have thrown exception by this point
} catch (ServletException e) {
// expected
} catch (Exception e) {
Assert.fail();
}
// request is null
try {
SUT.doPost(null, response);
Assert.fail(); // should have thrown exception by this point
} catch (ServletException e) {
// expected
} catch (Exception e) {
Assert.fail();
}
// response parameter null
try {
SUT.doPost(request, null);
Assert.fail(); // should have thrown exception by this point
} catch (ServletException e) {
// expected
} catch (Exception e) {
Assert.fail();
}
}
// runs through a complete working post
@Test
public void doPost() {
// mock dependencies
when(request.getParameter("project")).thenReturn(PROJECT_ID);
when(projMan.getProject(anyLong())).thenReturn(p);
when(p.getProcessManager()).thenReturn(processMan);
PrintWriter pw = mock(PrintWriter.class);
try {
when(response.getWriter()).thenReturn(pw);
} catch (IOException e1) {
Assert.fail();
}
// run
try {
SUT.doPost(request, response);
} catch (ServletException e) {
Assert.fail();
} catch (IOException e) {
Assert.fail();
}
// verify
verify(request, times(1)).getParameter("project");
verify(projMan, times(1)).getProject(PROJECT_ID_LONG);
verify(p, times(1)).getProcessManager();
try {
verify(response, times(1)).getWriter();
} catch (IOException e) {
Assert.fail();
}
verify(processMan, times(1)).cancelAll();
verify(response, times(1)).setCharacterEncoding("UTF-8");
verify(response, times(1))
.setHeader("Content-Type", "application/json");
verify(pw, times(1)).write("{ \"code\" : \"ok\" }");
}
}