From 08e175dc66c58f393ed9b8c720ad8faf503cfdbe Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Wed, 25 Dec 2019 12:33:42 +0100 Subject: [PATCH] Fix NPE in expresion logging. Closes #2086. --- .../src/com/google/refine/ProjectManager.java | 4 +-- .../commands/expr/LogExpressionCommand.java | 12 ++++++--- .../expr/LogExpressionCommandTests.java | 27 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/main/src/com/google/refine/ProjectManager.java b/main/src/com/google/refine/ProjectManager.java index 89070ac29..2447ecd5e 100644 --- a/main/src/com/google/refine/ProjectManager.java +++ b/main/src/com/google/refine/ProjectManager.java @@ -68,7 +68,7 @@ import com.google.refine.util.ParsingUtilities; */ public abstract class ProjectManager { // last n expressions used across all projects - static protected final int s_expressionHistoryMax = 100; + static public final int EXPRESSION_HISTORY_MAX = 100; // If a project has been idle this long, flush it from memory static protected final int PROJECT_FLUSH_DELAY = 1000 * 60 * 15; // 15 minutes @@ -610,7 +610,7 @@ public abstract class ProjectManager { * @param ps */ static protected void preparePreferenceStore(PreferenceStore ps) { - ps.put("scripting.expressions", new TopList(s_expressionHistoryMax)); + ps.put("scripting.expressions", new TopList(EXPRESSION_HISTORY_MAX)); ps.put("scripting.starred-expressions", new TopList(Integer.MAX_VALUE)); } } diff --git a/main/src/com/google/refine/commands/expr/LogExpressionCommand.java b/main/src/com/google/refine/commands/expr/LogExpressionCommand.java index 71df8ca24..6ccad3a93 100644 --- a/main/src/com/google/refine/commands/expr/LogExpressionCommand.java +++ b/main/src/com/google/refine/commands/expr/LogExpressionCommand.java @@ -41,6 +41,7 @@ import javax.servlet.http.HttpServletResponse; import com.google.refine.ProjectManager; import com.google.refine.commands.Command; +import com.google.refine.preference.PreferenceStore; import com.google.refine.preference.TopList; public class LogExpressionCommand extends Command { @@ -55,9 +56,14 @@ public class LogExpressionCommand extends Command { try { String expression = request.getParameter("expression"); - - ((TopList) ProjectManager.singleton.getPreferenceStore().get("scripting.expressions")) - .add(expression); + + PreferenceStore prefStore = ProjectManager.singleton.getPreferenceStore(); + TopList topList = (TopList) prefStore.get("scripting.expressions"); + if (topList == null) { + topList = new TopList(ProjectManager.EXPRESSION_HISTORY_MAX); + prefStore.put("scripting.expressions", topList); + } + topList.add(expression); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json"); diff --git a/main/tests/server/src/com/google/refine/commands/expr/LogExpressionCommandTests.java b/main/tests/server/src/com/google/refine/commands/expr/LogExpressionCommandTests.java index 1283820ef..9222a76e2 100644 --- a/main/tests/server/src/com/google/refine/commands/expr/LogExpressionCommandTests.java +++ b/main/tests/server/src/com/google/refine/commands/expr/LogExpressionCommandTests.java @@ -1,18 +1,33 @@ package com.google.refine.commands.expr; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import com.google.refine.ProjectManager; +import com.google.refine.ProjectManagerStub; +import com.google.refine.commands.Command; import com.google.refine.commands.CommandTestBase; +import com.google.refine.preference.PreferenceStore; +import com.google.refine.preference.TopList; + import java.io.IOException; +import java.util.Collections; import javax.servlet.ServletException; +import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class LogExpressionCommandTests extends CommandTestBase { + PreferenceStore prefStore; + @BeforeMethod public void setUpCommand() { command = new LogExpressionCommand(); + ProjectManager.singleton = mock(ProjectManager.class); + prefStore = new PreferenceStore(); + when(ProjectManager.singleton.getPreferenceStore()).thenReturn(prefStore); } @Test @@ -20,4 +35,16 @@ public class LogExpressionCommandTests extends CommandTestBase { command.doPost(request, response); assertCSRFCheckFailed(); } + + @Test + public void testNullExpressions() throws ServletException, IOException { + prefStore.put("scripting.expressions", null); + when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken()); + when(request.getParameter("expression")).thenReturn("grel:value+'a'"); + + command.doPost(request, response); + + TopList topList = (TopList)prefStore.get("scripting.expressions"); + Assert.assertEquals(topList.getList(), Collections.singletonList("grel:value+'a'")); + } }