Fix order dependent NPE in LoadLanguage test (#2922)

* Ensure ProjectManager is initialized before test - fixes #2895

* Fix indentation (detabify)
This commit is contained in:
Tom Morris 2020-07-14 12:06:04 -04:00 committed by GitHub
parent c5e6ac9f90
commit 561619399c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,7 @@ import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.fasterxml.jackson.core.JsonParseException; 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.fasterxml.jackson.databind.node.ObjectNode;
import com.google.refine.RefineServlet; import com.google.refine.RefineServlet;
import com.google.refine.commands.CommandTestBase; import com.google.refine.commands.CommandTestBase;
import com.google.refine.io.FileProjectManager;
import com.google.refine.util.ParsingUtilities; import com.google.refine.util.ParsingUtilities;
import com.google.refine.util.TestUtils;
import edu.mit.simile.butterfly.ButterflyModule; import edu.mit.simile.butterfly.ButterflyModule;
public class LoadLanguageCommandTests extends CommandTestBase { public class LoadLanguageCommandTests extends CommandTestBase {
@BeforeMethod @BeforeTest
public void setUpCommand() { public void setUpTest() throws IOException {
command = new LoadLanguageCommand(); FileProjectManager.initialize(TestUtils.createTempDirectory("openrefine-test-workspace-dir"));
ButterflyModule coreModule = mock(ButterflyModule.class); }
when(coreModule.getName()).thenReturn("core"); @BeforeMethod
when(coreModule.getPath()).thenReturn(new File("webapp/modules/core")); public void setUpCommand() {
RefineServlet servlet = mock(RefineServlet.class); command = new LoadLanguageCommand();
when(servlet.getModule("core")).thenReturn(coreModule); ButterflyModule coreModule = mock(ButterflyModule.class);
command.init(servlet);
} when(coreModule.getName()).thenReturn("core");
when(coreModule.getPath()).thenReturn(new File("webapp/modules/core"));
@Test RefineServlet servlet = mock(RefineServlet.class);
public void testLoadLanguages() throws ServletException, IOException { when(servlet.getModule("core")).thenReturn(coreModule);
when(request.getParameter("module")).thenReturn("core"); command.init(servlet);
when(request.getParameterValues("lang")).thenReturn(new String[] {"en"}); }
command.doPost(request, response); @Test
public void testLoadLanguages() throws ServletException, IOException {
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class); when(request.getParameter("module")).thenReturn("core");
assertTrue(response.has("dictionary")); when(request.getParameterValues("lang")).thenReturn(new String[] {"en"});
assertTrue(response.has("lang"));
} command.doPost(request, response);
@Test JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
public void testLoadUnknownLanguage() throws ServletException, IOException { assertTrue(response.has("dictionary"));
when(request.getParameter("module")).thenReturn("core"); assertTrue(response.has("lang"));
when(request.getParameterValues("lang")).thenReturn(new String[] {"foobar"}); }
command.doPost(request, response); @Test
public void testLoadUnknownLanguage() throws ServletException, IOException {
JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class); when(request.getParameter("module")).thenReturn("core");
assertTrue(response.has("dictionary")); when(request.getParameterValues("lang")).thenReturn(new String[] {"foobar"});
assertEquals(response.get("lang").asText(), "en");
} command.doPost(request, response);
@Test JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
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")); assertTrue(response.has("dictionary"));
assertEquals(response.get("lang").asText(), "en"); assertEquals(response.get("lang").asText(), "en");
} }
@Test @Test
public void testLanguageFallback() throws JsonParseException, JsonMappingException, IOException { public void testLoadNoLanguage() throws JsonParseException, JsonMappingException, IOException, ServletException {
String fallbackJson = "{" when(request.getParameter("module")).thenReturn("core");
+ "\"foo\":\"hello\"," when(request.getParameter("lang")).thenReturn("");
+ "\"bar\":\"world\""
+ "}"; command.doPost(request, response);
String preferredJson = "{"
+ "\"foo\":\"hallo\"" JsonNode response = ParsingUtilities.mapper.readValue(writer.toString(), JsonNode.class);
+ "}"; assertTrue(response.has("dictionary"));
String expectedJson = "{" assertEquals(response.get("lang").asText(), "en");
+ "\"foo\":\"hallo\"," }
+ "\"bar\":\"world\""
+ "}"; @Test
ObjectNode fallback = ParsingUtilities.mapper.readValue(fallbackJson, ObjectNode.class); public void testLanguageFallback() throws JsonParseException, JsonMappingException, IOException {
ObjectNode preferred = ParsingUtilities.mapper.readValue(preferredJson, ObjectNode.class); String fallbackJson = "{"
ObjectNode expected = ParsingUtilities.mapper.readValue(expectedJson, ObjectNode.class); + "\"foo\":\"hello\","
+ "\"bar\":\"world\""
ObjectNode merged = LoadLanguageCommand.mergeLanguages(preferred, fallback); + "}";
String preferredJson = "{"
assertEquals(merged, expected); + "\"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);
}
} }