Fix failing tests after JSONWriter replacement

This commit is contained in:
Antonin Delpeuch 2018-10-11 19:57:41 +01:00
parent 790b1fbc60
commit bc6f1b0146
3 changed files with 20 additions and 12 deletions

View File

@ -348,18 +348,17 @@ public abstract class Command {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json"); response.setHeader("Content-Type", "application/json");
Writer w = response.getWriter();
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
writer.writeStartObject();
writer.writeStringField("code", "error");
writer.writeStringField("message", e.getMessage());
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw); e.printStackTrace(pw);
pw.flush(); pw.flush();
sw.flush(); sw.flush();
Writer w = response.getWriter();
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
writer.writeStartObject();
writer.writeStringField("code", "error");
writer.writeStringField("message", e.getMessage());
writer.writeStringField("stack", sw.toString()); writer.writeStringField("stack", sw.toString());
writer.writeEndObject(); writer.writeEndObject();
writer.flush(); writer.flush();

View File

@ -13,6 +13,6 @@ public class EngineTests {
public void serializeEngine() { public void serializeEngine() {
Project project = mock(Project.class); Project project = mock(Project.class);
Engine engine = new Engine(project); Engine engine = new Engine(project);
TestUtils.isSerializedTo(engine, "{\"mode\":\"row-based\",\"facets\":[]}"); TestUtils.isSerializedTo(engine, "{\"engine-mode\":\"row-based\",\"facets\":[]}");
} }
} }

View File

@ -41,6 +41,7 @@ import static org.mockito.Mockito.when;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -58,6 +59,7 @@ import com.google.refine.commands.history.CancelProcessesCommand;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.process.ProcessManager; import com.google.refine.process.ProcessManager;
import com.google.refine.tests.RefineTest; import com.google.refine.tests.RefineTest;
import com.google.refine.tests.util.TestUtils;
public class CancelProcessesCommandTests extends RefineTest { public class CancelProcessesCommandTests extends RefineTest {
@ -80,6 +82,7 @@ public class CancelProcessesCommandTests extends RefineTest {
ProjectManager projMan = null; ProjectManager projMan = null;
Project proj = null; Project proj = null;
ProcessManager processMan = null; ProcessManager processMan = null;
StringWriter sw = null;
PrintWriter pw = null; PrintWriter pw = null;
@BeforeMethod @BeforeMethod
@ -88,7 +91,8 @@ public class CancelProcessesCommandTests extends RefineTest {
ProjectManager.singleton = projMan; ProjectManager.singleton = projMan;
proj = mock(Project.class); proj = mock(Project.class);
processMan = mock(ProcessManager.class); processMan = mock(ProcessManager.class);
pw = mock(PrintWriter.class); sw = new StringWriter();
pw = new PrintWriter(sw);
request = mock(HttpServletRequest.class); request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class); response = mock(HttpServletResponse.class);
@ -102,7 +106,7 @@ public class CancelProcessesCommandTests extends RefineTest {
projMan = null; projMan = null;
ProjectManager.singleton = null; ProjectManager.singleton = null;
proj = null; proj = null;
pw = null; sw = null;
request = null; request = null;
response = null; response = null;
} }
@ -186,7 +190,7 @@ public class CancelProcessesCommandTests extends RefineTest {
} catch (IOException e) { } catch (IOException e) {
Assert.fail(); Assert.fail();
} }
verify(pw, times(1)).write("{ \"code\" : \"ok\" }"); TestUtils.assertEqualAsJson("{ \"code\" : \"ok\" }", sw.toString());
} }
@Test @Test
@ -195,6 +199,11 @@ public class CancelProcessesCommandTests extends RefineTest {
when(request.getParameter("project")).thenReturn(PROJECT_ID); when(request.getParameter("project")).thenReturn(PROJECT_ID);
when(projMan.getProject(anyLong())) when(projMan.getProject(anyLong()))
.thenReturn(null); .thenReturn(null);
try {
when(response.getWriter()).thenReturn(pw);
} catch (IOException e1) {
Assert.fail();
}
// run // run
try { try {
@ -238,7 +247,7 @@ public class CancelProcessesCommandTests extends RefineTest {
verify(projMan, times(1)).getProject(PROJECT_ID_LONG); verify(projMan, times(1)).getProject(PROJECT_ID_LONG);
verify(processMan, times(1)).cancelAll(); verify(processMan, times(1)).cancelAll();
verify(response, times(3)).setCharacterEncoding("UTF-8"); verify(response, times(2)).setCharacterEncoding("UTF-8");
//omitted other verifications for brevity. //omitted other verifications for brevity.
//assumption is that expecting response.setCharacterEncoding times(3) //assumption is that expecting response.setCharacterEncoding times(3)
//implies it has Command.respondException has been called as expected //implies it has Command.respondException has been called as expected