From 561619399cbb0e66e698c76d6aa1154ad39f1e45 Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Tue, 14 Jul 2020 12:06:04 -0400 Subject: [PATCH] Fix order dependent NPE in LoadLanguage test (#2922) * Ensure ProjectManager is initialized before test - fixes #2895 * Fix indentation (detabify) --- .../lang/LoadLanguageCommandTests.java | 144 +++++++++--------- 1 file changed, 76 insertions(+), 68 deletions(-) diff --git a/main/tests/server/src/com/google/refine/commands/lang/LoadLanguageCommandTests.java b/main/tests/server/src/com/google/refine/commands/lang/LoadLanguageCommandTests.java index a8737aa13..f1185ce99 100644 --- a/main/tests/server/src/com/google/refine/commands/lang/LoadLanguageCommandTests.java +++ b/main/tests/server/src/com/google/refine/commands/lang/LoadLanguageCommandTests.java @@ -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); + } }