Fix other NPE in expression logging, for #2264

This commit is contained in:
Antonin Delpeuch 2020-01-06 06:30:56 +01:00
parent a4c4a20767
commit 904129d0f7
5 changed files with 42 additions and 7 deletions

View File

@ -84,7 +84,11 @@ public class GetExpressionHistoryCommand extends Command {
try { try {
List<String> expressions = toExpressionList(ProjectManager.singleton.getPreferenceStore().get("scripting.expressions")); List<String> expressions = toExpressionList(ProjectManager.singleton.getPreferenceStore().get("scripting.expressions"));
Set<String> starredExpressions = new HashSet<String>(((TopList)ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions")).getList()); TopList topList = (TopList)ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions");
if (topList == null) {
topList = new TopList(ProjectManager.EXPRESSION_HISTORY_MAX);
}
Set<String> starredExpressions = new HashSet<String>(topList.getList());
ExpressionsList expressionsList = new ExpressionsList(expressions.stream() ExpressionsList expressionsList = new ExpressionsList(expressions.stream()
.map(s -> new ExpressionState(s, starredExpressions.contains(s))) .map(s -> new ExpressionState(s, starredExpressions.contains(s)))
.collect(Collectors.toList())); .collect(Collectors.toList()));

View File

@ -27,6 +27,7 @@
package com.google.refine.commands.expr; package com.google.refine.commands.expr;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -59,7 +60,8 @@ public class GetStarredExpressionsCommand extends Command {
} }
public static ExpressionList getExpressionsList() { public static ExpressionList getExpressionsList() {
List<String> starredExpressions = ((TopList)ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions")).getList(); TopList topList = (TopList)ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions");
List<String> starredExpressions = topList == null ? Collections.emptyList() : topList.getList();
return new ExpressionList(starredExpressions.stream().map(e -> new Expression(e)).collect(Collectors.toList())); return new ExpressionList(starredExpressions.stream().map(e -> new Expression(e)).collect(Collectors.toList()));
} }

View File

@ -68,17 +68,22 @@ public class ExpressionCommandTestBase {
String starred = starredExpressionsJson == null ? "{\"class\":\"com.google.refine.preference.TopList\",\"top\":2147483647," + String starred = starredExpressionsJson == null ? "{\"class\":\"com.google.refine.preference.TopList\",\"top\":2147483647," +
"\"list\":[]}" : starredExpressionsJson; "\"list\":[]}" : starredExpressionsJson;
String expressions = expressionsJson == null ? "{\"class\":\"com.google.refine.preference.TopList\",\"top\":100,\"list\":[]}" : expressionsJson; String expressions = expressionsJson == null ? "{\"class\":\"com.google.refine.preference.TopList\",\"top\":100,\"list\":[]}" : expressionsJson;
try { String jsonData = "{\"projectIDs\":[]\n" +
",\"preferences\":{\"entries\":{\"scripting.starred-expressions\":" + starred +
",\"scripting.expressions\":"+expressions+"}}}";
initWorkspace(jsonData);
}
public void initWorkspace(String jsonData) {
try {
File workspaceDir = TestUtils.createTempDirectory("openrefine-test-workspace-dir"); File workspaceDir = TestUtils.createTempDirectory("openrefine-test-workspace-dir");
File jsonPath = new File(workspaceDir, "workspace.json"); File jsonPath = new File(workspaceDir, "workspace.json");
FileUtils.writeStringToFile(jsonPath, "{\"projectIDs\":[]\n" + FileUtils.writeStringToFile(jsonPath, jsonData);
",\"preferences\":{\"entries\":{\"scripting.starred-expressions\":" + starred +
",\"scripting.expressions\":"+expressions+"}}}");
FileProjectManager.initialize(workspaceDir); FileProjectManager.initialize(workspaceDir);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void assertResponseJsonIs(String expectedJson) { public void assertResponseJsonIs(String expectedJson) {
String actualJson = writer.toString(); String actualJson = writer.toString();

View File

@ -78,4 +78,16 @@ public class GetExpressionHistoryCommandTests extends ExpressionCommandTestBase
assertResponseJsonIs(json); assertResponseJsonIs(json);
} }
@Test
public void testUninitialized() throws ServletException, IOException {
initWorkspace("{}");
String json = "{\n" +
" \"expressions\" : []\n" +
" }";
command.doGet(request, response);
assertResponseJsonIs(json);
}
} }

View File

@ -67,4 +67,16 @@ public class GetStarredExpressionsCommandTests extends ExpressionCommandTestBase
command.doGet(request, response); command.doGet(request, response);
assertResponseJsonIs(json); assertResponseJsonIs(json);
} }
@Test
public void testUninitialized() throws ServletException, IOException {
initWorkspace("{}");
String json = "{\n" +
" \"expressions\" : []\n" +
" }";
command.doGet(request, response);
assertResponseJsonIs(json);
}
} }