Merge pull request #2264 from OpenRefine/issue-2086-expression-logging-npe

Fix NPE in expression logging.
This commit is contained in:
Antonin Delpeuch 2019-12-30 21:52:58 +01:00 committed by GitHub
commit 14dd4c0112
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 5 deletions

View File

@ -68,7 +68,7 @@ import com.google.refine.util.ParsingUtilities;
*/ */
public abstract class ProjectManager { public abstract class ProjectManager {
// last n expressions used across all projects // 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 // If a project has been idle this long, flush it from memory
static protected final int PROJECT_FLUSH_DELAY = 1000 * 60 * 15; // 15 minutes static protected final int PROJECT_FLUSH_DELAY = 1000 * 60 * 15; // 15 minutes
@ -610,7 +610,7 @@ public abstract class ProjectManager {
* @param ps * @param ps
*/ */
static protected void preparePreferenceStore(PreferenceStore 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)); ps.put("scripting.starred-expressions", new TopList(Integer.MAX_VALUE));
} }
} }

View File

@ -41,6 +41,7 @@ import javax.servlet.http.HttpServletResponse;
import com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.preference.PreferenceStore;
import com.google.refine.preference.TopList; import com.google.refine.preference.TopList;
public class LogExpressionCommand extends Command { public class LogExpressionCommand extends Command {
@ -56,8 +57,13 @@ public class LogExpressionCommand extends Command {
try { try {
String expression = request.getParameter("expression"); String expression = request.getParameter("expression");
((TopList) ProjectManager.singleton.getPreferenceStore().get("scripting.expressions")) PreferenceStore prefStore = ProjectManager.singleton.getPreferenceStore();
.add(expression); 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.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json"); response.setHeader("Content-Type", "application/json");

View File

@ -1,18 +1,33 @@
package com.google.refine.commands.expr; 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.commands.CommandTestBase;
import com.google.refine.preference.PreferenceStore;
import com.google.refine.preference.TopList;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
public class LogExpressionCommandTests extends CommandTestBase { public class LogExpressionCommandTests extends CommandTestBase {
PreferenceStore prefStore;
@BeforeMethod @BeforeMethod
public void setUpCommand() { public void setUpCommand() {
command = new LogExpressionCommand(); command = new LogExpressionCommand();
ProjectManager.singleton = mock(ProjectManager.class);
prefStore = new PreferenceStore();
when(ProjectManager.singleton.getPreferenceStore()).thenReturn(prefStore);
} }
@Test @Test
@ -20,4 +35,16 @@ public class LogExpressionCommandTests extends CommandTestBase {
command.doPost(request, response); command.doPost(request, response);
assertCSRFCheckFailed(); 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'"));
}
} }