Fix order dependent NPE in LoadLanguage test (#2922)
* Ensure ProjectManager is initialized before test - fixes #2895 * Fix indentation (detabify)
This commit is contained in:
parent
c5e6ac9f90
commit
561619399c
@ -11,6 +11,7 @@ import java.io.IOException;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
@ -19,80 +20,87 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.refine.RefineServlet;
|
||||
import com.google.refine.commands.CommandTestBase;
|
||||
import com.google.refine.io.FileProjectManager;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
import com.google.refine.util.TestUtils;
|
||||
|
||||
import edu.mit.simile.butterfly.ButterflyModule;
|
||||
|
||||
public class LoadLanguageCommandTests extends CommandTestBase {
|
||||
|
||||
@BeforeMethod
|
||||
public void setUpCommand() {
|
||||
command = new LoadLanguageCommand();
|
||||
ButterflyModule coreModule = mock(ButterflyModule.class);
|
||||
|
||||
when(coreModule.getName()).thenReturn("core");
|
||||
when(coreModule.getPath()).thenReturn(new File("webapp/modules/core"));
|
||||
RefineServlet servlet = mock(RefineServlet.class);
|
||||
when(servlet.getModule("core")).thenReturn(coreModule);
|
||||
command.init(servlet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadLanguages() throws ServletException, IOException {
|
||||
when(request.getParameter("module")).thenReturn("core");
|
||||
when(request.getParameterValues("lang")).thenReturn(new String[] {"en"});
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
|
||||
assertTrue(response.has("dictionary"));
|
||||
assertTrue(response.has("lang"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadUnknownLanguage() throws ServletException, IOException {
|
||||
when(request.getParameter("module")).thenReturn("core");
|
||||
when(request.getParameterValues("lang")).thenReturn(new String[] {"foobar"});
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
|
||||
assertTrue(response.has("dictionary"));
|
||||
assertEquals(response.get("lang").asText(), "en");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadNoLanguage() throws JsonParseException, JsonMappingException, IOException, ServletException {
|
||||
when(request.getParameter("module")).thenReturn("core");
|
||||
when(request.getParameter("lang")).thenReturn("");
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
|
||||
|
||||
@BeforeTest
|
||||
public void setUpTest() throws IOException {
|
||||
FileProjectManager.initialize(TestUtils.createTempDirectory("openrefine-test-workspace-dir"));
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void setUpCommand() {
|
||||
command = new LoadLanguageCommand();
|
||||
ButterflyModule coreModule = mock(ButterflyModule.class);
|
||||
|
||||
when(coreModule.getName()).thenReturn("core");
|
||||
when(coreModule.getPath()).thenReturn(new File("webapp/modules/core"));
|
||||
RefineServlet servlet = mock(RefineServlet.class);
|
||||
when(servlet.getModule("core")).thenReturn(coreModule);
|
||||
command.init(servlet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadLanguages() throws ServletException, IOException {
|
||||
when(request.getParameter("module")).thenReturn("core");
|
||||
when(request.getParameterValues("lang")).thenReturn(new String[] {"en"});
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
|
||||
assertTrue(response.has("dictionary"));
|
||||
assertTrue(response.has("lang"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadUnknownLanguage() throws ServletException, IOException {
|
||||
when(request.getParameter("module")).thenReturn("core");
|
||||
when(request.getParameterValues("lang")).thenReturn(new String[] {"foobar"});
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
|
||||
assertTrue(response.has("dictionary"));
|
||||
assertEquals(response.get("lang").asText(), "en");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLanguageFallback() throws JsonParseException, JsonMappingException, IOException {
|
||||
String fallbackJson = "{"
|
||||
+ "\"foo\":\"hello\","
|
||||
+ "\"bar\":\"world\""
|
||||
+ "}";
|
||||
String preferredJson = "{"
|
||||
+ "\"foo\":\"hallo\""
|
||||
+ "}";
|
||||
String expectedJson = "{"
|
||||
+ "\"foo\":\"hallo\","
|
||||
+ "\"bar\":\"world\""
|
||||
+ "}";
|
||||
ObjectNode fallback = ParsingUtilities.mapper.readValue(fallbackJson, ObjectNode.class);
|
||||
ObjectNode preferred = ParsingUtilities.mapper.readValue(preferredJson, ObjectNode.class);
|
||||
ObjectNode expected = ParsingUtilities.mapper.readValue(expectedJson, ObjectNode.class);
|
||||
|
||||
ObjectNode merged = LoadLanguageCommand.mergeLanguages(preferred, fallback);
|
||||
|
||||
assertEquals(merged, expected);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadNoLanguage() throws JsonParseException, JsonMappingException, IOException, ServletException {
|
||||
when(request.getParameter("module")).thenReturn("core");
|
||||
when(request.getParameter("lang")).thenReturn("");
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
|
||||
assertTrue(response.has("dictionary"));
|
||||
assertEquals(response.get("lang").asText(), "en");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLanguageFallback() throws JsonParseException, JsonMappingException, IOException {
|
||||
String fallbackJson = "{"
|
||||
+ "\"foo\":\"hello\","
|
||||
+ "\"bar\":\"world\""
|
||||
+ "}";
|
||||
String preferredJson = "{"
|
||||
+ "\"foo\":\"hallo\""
|
||||
+ "}";
|
||||
String expectedJson = "{"
|
||||
+ "\"foo\":\"hallo\","
|
||||
+ "\"bar\":\"world\""
|
||||
+ "}";
|
||||
ObjectNode fallback = ParsingUtilities.mapper.readValue(fallbackJson, ObjectNode.class);
|
||||
ObjectNode preferred = ParsingUtilities.mapper.readValue(preferredJson, ObjectNode.class);
|
||||
ObjectNode expected = ParsingUtilities.mapper.readValue(expectedJson, ObjectNode.class);
|
||||
|
||||
ObjectNode merged = LoadLanguageCommand.mergeLanguages(preferred, fallback);
|
||||
|
||||
assertEquals(merged, expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user