Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit a94b929

Browse files
committed
Rename VariableHolder -> ParameterMap
1 parent 3beb5e3 commit a94b929

File tree

9 files changed

+60
-63
lines changed

9 files changed

+60
-63
lines changed

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellVerboseIntegrationTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ public void commitScenario() throws CommandException {
167167

168168
@Test
169169
public void paramsAndListVariables() throws CommandException {
170-
assertTrue(shell.getAll().isEmpty());
170+
assertTrue(shell.allParameterValues().isEmpty());
171171

172172
long randomLong = System.currentTimeMillis();
173173
String stringInput = "\"randomString\"";
174-
shell.set("string", stringInput);
175-
Optional result = shell.set("bob", String.valueOf(randomLong));
174+
shell.setParameter("string", stringInput);
175+
Optional result = shell.setParameter("bob", String.valueOf(randomLong));
176176
assertTrue(result.isPresent());
177177
assertEquals(randomLong, result.get());
178178

@@ -184,16 +184,16 @@ public void paramsAndListVariables() throws CommandException {
184184
List<String> queryResult = captor.getAllValues();
185185
assertThat(queryResult.get(0), containsString("| { bob }"));
186186
assertThat(queryResult.get(0), containsString("| " + randomLong + " | " + stringInput + " |"));
187-
assertEquals(randomLong, shell.getAll().get("bob"));
188-
assertEquals("randomString", shell.getAll().get("string"));
187+
assertEquals(randomLong, shell.allParameterValues().get("bob"));
188+
assertEquals("randomString", shell.allParameterValues().get("string"));
189189
}
190190

191191
@Test
192192
public void paramsAndListVariablesWithSpecialCharacters() throws CommandException {
193-
assertTrue(shell.getAll().isEmpty());
193+
assertTrue(shell.allParameterValues().isEmpty());
194194

195195
long randomLong = System.currentTimeMillis();
196-
Optional result = shell.set("`bob`", String.valueOf(randomLong));
196+
Optional result = shell.setParameter("`bob`", String.valueOf(randomLong));
197197
assertTrue(result.isPresent());
198198
assertEquals(randomLong, result.get());
199199

@@ -205,6 +205,6 @@ public void paramsAndListVariablesWithSpecialCharacters() throws CommandExceptio
205205
List<String> queryResult = captor.getAllValues();
206206
assertThat(queryResult.get(0), containsString("| { `bob` }"));
207207
assertThat(queryResult.get(0), containsString("\n| " + randomLong+ " |\n"));
208-
assertEquals(randomLong, shell.getAll().get("bob"));
208+
assertEquals(randomLong, shell.allParameterValues().get("bob"));
209209
}
210210
}

cypher-shell/src/main/java/org/neo4j/shell/CypherShell.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.neo4j.shell.state.ParamValue;
1414

1515
import javax.annotation.Nonnull;
16-
import java.util.AbstractMap;
1716
import java.util.HashMap;
1817
import java.util.List;
1918
import java.util.Map;
@@ -25,7 +24,7 @@
2524
/**
2625
* A possibly interactive shell for evaluating cypher statements.
2726
*/
28-
public class CypherShell implements StatementExecuter, Connector, TransactionHandler, VariableHolder {
27+
public class CypherShell implements StatementExecuter, Connector, TransactionHandler, ParameterMap {
2928
// Final space to catch newline
3029
protected static final Pattern cmdNamePattern = Pattern.compile("^\\s*(?<name>[^\\s]+)\\b(?<args>.*)\\s*$");
3130
protected final Map<String, ParamValue> queryParams = new HashMap<>();
@@ -83,7 +82,7 @@ public void execute(@Nonnull final String cmdString) throws ExitException, Comma
8382
* @param cypher non-empty cypher text to executeLine
8483
*/
8584
protected void executeCypher(@Nonnull final String cypher) throws CommandException {
86-
final Optional<BoltResult> result = boltStateHandler.runCypher(cypher, getAll());
85+
final Optional<BoltResult> result = boltStateHandler.runCypher(cypher, allParameterValues());
8786
result.ifPresent(boltResult -> logger.printOut(prettyPrinter.format(boltResult)));
8887
}
8988

@@ -155,7 +154,7 @@ public boolean isTransactionOpen() {
155154

156155
@Override
157156
@Nonnull
158-
public Optional set(@Nonnull String name, @Nonnull String valueString) throws CommandException {
157+
public Optional setParameter(@Nonnull String name, @Nonnull String valueString) throws CommandException {
159158
final BoltResult result = setParamsAndValidate(name, valueString);
160159
String parameterName = CypherVariablesFormatter.unescapedCypherVariable(name);
161160
final Object value = result.getRecords().get(0).get(parameterName).asObject();
@@ -165,7 +164,7 @@ public Optional set(@Nonnull String name, @Nonnull String valueString) throws Co
165164

166165
private BoltResult setParamsAndValidate(@Nonnull String name, @Nonnull String valueString) throws CommandException {
167166
String cypher = "RETURN " + valueString + " as " + name;
168-
final Optional<BoltResult> result = boltStateHandler.runCypher(cypher, getAll());
167+
final Optional<BoltResult> result = boltStateHandler.runCypher(cypher, allParameterValues());
169168
if (!result.isPresent() || result.get().getRecords().isEmpty()) {
170169
throw new CommandException("Failed to set value of parameter");
171170
}
@@ -174,7 +173,7 @@ private BoltResult setParamsAndValidate(@Nonnull String name, @Nonnull String va
174173

175174
@Override
176175
@Nonnull
177-
public Map<String, Object> getAll() {
176+
public Map<String, Object> allParameterValues() {
178177
return queryParams.entrySet()
179178
.stream()
180179
.collect(Collectors.toMap(

cypher-shell/src/main/java/org/neo4j/shell/VariableHolder.java renamed to cypher-shell/src/main/java/org/neo4j/shell/ParameterMap.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
import java.util.Optional;
99

1010
/**
11-
* An object which keeps variables and allows them them to be set/unset.
11+
* An object which keeps named parameters and allows them them to be set/unset.
1212
*/
13-
public interface VariableHolder {
13+
public interface ParameterMap {
1414
/**
15-
* @param name of variable to set value for
15+
* @param name of variable to set value for
1616
* @param valueString to interpret the value from
1717
*/
18-
Optional set(@Nonnull String name, @Nonnull String valueString) throws CommandException;
18+
Optional setParameter(@Nonnull String name, @Nonnull String valueString) throws CommandException;
1919

2020
/**
21-
*
2221
* @return map of all currently set variables and their values
2322
*/
2423
@Nonnull
25-
Map<String, Object> getAll();
24+
Map<String, Object> allParameterValues();
2625

2726
/**
2827
* @return map of all currently set variables and their values corresponding to the user valueString

cypher-shell/src/main/java/org/neo4j/shell/commands/CommandHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.neo4j.shell.CypherShell;
44
import org.neo4j.shell.Historian;
55
import org.neo4j.shell.TransactionHandler;
6-
import org.neo4j.shell.VariableHolder;
6+
import org.neo4j.shell.ParameterMap;
77
import org.neo4j.shell.exception.CommandException;
88
import org.neo4j.shell.exception.DuplicateCommandException;
99
import org.neo4j.shell.log.AnsiFormattedText;
@@ -26,15 +26,15 @@ public CommandHelper(Logger logger, Historian historian, CypherShell cypherShell
2626
}
2727

2828
private void registerAllCommands(Logger logger, Historian historian,
29-
TransactionHandler transactionHandler, VariableHolder variableHolder) {
29+
TransactionHandler transactionHandler, ParameterMap parameterMap) {
3030
registerCommand(new Exit(logger));
3131
registerCommand(new Help(logger, this));
3232
registerCommand(new History(logger, historian));
3333
registerCommand(new Begin(transactionHandler));
3434
registerCommand(new Commit(transactionHandler));
3535
registerCommand(new Rollback(transactionHandler));
36-
registerCommand(new Param(variableHolder));
37-
registerCommand(new Params(logger, variableHolder));
36+
registerCommand(new Param(parameterMap));
37+
registerCommand(new Params(logger, parameterMap));
3838
}
3939

4040
private void registerCommand(@Nonnull final Command command) throws DuplicateCommandException {

cypher-shell/src/main/java/org/neo4j/shell/commands/Param.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.neo4j.shell.commands;
22

3-
import org.neo4j.shell.VariableHolder;
3+
import org.neo4j.shell.ParameterMap;
44
import org.neo4j.shell.exception.CommandException;
55
import org.neo4j.shell.log.AnsiFormattedText;
66

@@ -24,10 +24,10 @@ public class Param implements Command {
2424
private static final Pattern lambdaMapPattern = Pattern.compile("^\\s*(?<key>[\\p{L}_][\\p{L}0-9_]*):\\s*=>\\s*(?<value>.+)$");
2525

2626
public static final String COMMAND_NAME = ":param";
27-
private final VariableHolder variableHolder;
27+
private final ParameterMap parameterMap;
2828

29-
public Param(@Nonnull final VariableHolder variableHolder) {
30-
this.variableHolder = variableHolder;
29+
public Param(@Nonnull final ParameterMap parameterMap) {
30+
this.parameterMap = parameterMap;
3131
}
3232

3333
@Nonnull
@@ -84,7 +84,7 @@ private boolean setParameterIfItMatchesPattern(@Nonnull String argString, Patter
8484
BiPredicate<String, Matcher> matchingFunction) throws CommandException {
8585
Matcher matcher = pattern.matcher(argString);
8686
if (matchingFunction.test(argString, matcher)) {
87-
variableHolder.set(matcher.group("key"), matcher.group("value"));
87+
parameterMap.setParameter(matcher.group("key"), matcher.group("value"));
8888
return true;
8989
} else {
9090
return false;

cypher-shell/src/main/java/org/neo4j/shell/commands/Params.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.neo4j.shell.commands;
22

3-
import org.neo4j.shell.VariableHolder;
3+
import org.neo4j.shell.ParameterMap;
44
import org.neo4j.shell.exception.CommandException;
55
import org.neo4j.shell.exception.ExitException;
66
import org.neo4j.shell.log.Logger;
@@ -22,12 +22,12 @@
2222
public class Params implements Command {
2323
public static final String COMMAND_NAME = ":params";
2424
private final Logger logger;
25-
private final VariableHolder variableHolder;
25+
private final ParameterMap parameterMap;
2626
private static final Pattern backtickPattern = Pattern.compile("^\\s*(?<key>(`([^`])*`)+?)\\s*");
2727

28-
public Params(@Nonnull Logger logger, @Nonnull VariableHolder variableHolder) {
28+
public Params(@Nonnull Logger logger, @Nonnull ParameterMap parameterMap) {
2929
this.logger = logger;
30-
this.variableHolder = variableHolder;
30+
this.parameterMap = parameterMap;
3131
}
3232

3333
@Nonnull
@@ -78,21 +78,21 @@ public void execute(@Nonnull final String argString) throws ExitException, Comma
7878

7979
private void listParam(@Nonnull String name) throws CommandException {
8080
String parameterName = CypherVariablesFormatter.unescapedCypherVariable(name);
81-
if (!this.variableHolder.getAllAsUserInput().containsKey(parameterName)) {
81+
if (!this.parameterMap.getAllAsUserInput().containsKey(parameterName)) {
8282
throw new CommandException("Unknown parameter: " + name);
8383
}
84-
listParam(name.length(), name, this.variableHolder.getAllAsUserInput().get(parameterName).getValueAsString());
84+
listParam(name.length(), name, this.parameterMap.getAllAsUserInput().get(parameterName).getValueAsString());
8585
}
8686

8787
private void listParam(int leftColWidth, @Nonnull String key, @Nonnull Object value) {
8888
logger.printOut(String.format(":param %-" + leftColWidth + "s => %s", key, value));
8989
}
9090

9191
private void listAllParams() {
92-
List<String> keys = variableHolder.getAllAsUserInput().keySet().stream().sorted().collect(Collectors.toList());
92+
List<String> keys = parameterMap.getAllAsUserInput().keySet().stream().sorted().collect(Collectors.toList());
9393

9494
int leftColWidth = keys.stream().map((s) -> escape(s).length()).reduce(0, Math::max);
9595

96-
keys.forEach(key -> listParam(leftColWidth, escape(key), variableHolder.getAllAsUserInput().get(key).getValueAsString()));
96+
keys.forEach(key -> listParam(leftColWidth, escape(key), parameterMap.getAllAsUserInput().get(key).getValueAsString()));
9797
}
9898
}

cypher-shell/src/test/java/org/neo4j/shell/CypherShellTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void setWhenOfflineShouldThrow() throws CommandException {
117117

118118
when(mockedBoltStateHandler.runCypher(anyString(), anyMap())).thenThrow(new CommandException("not connected"));
119119

120-
shell.set("bob", "99");
120+
shell.setParameter("bob", "99");
121121
}
122122

123123
@Test
@@ -142,11 +142,11 @@ public void setParamShouldAddParamWithSpecialCharactersAndValue() throws Command
142142
when(recordMock.get("bo`b")).thenReturn(value);
143143
when(value.asObject()).thenReturn("99");
144144

145-
assertTrue(offlineTestShell.getAll().isEmpty());
145+
assertTrue(offlineTestShell.allParameterValues().isEmpty());
146146

147-
Optional result = offlineTestShell.set("`bo``b`", "99");
147+
Optional result = offlineTestShell.setParameter("`bo``b`", "99");
148148
assertEquals("99", result.get());
149-
assertEquals("99", offlineTestShell.getAll().get("bo`b"));
149+
assertEquals("99", offlineTestShell.allParameterValues().get("bo`b"));
150150
}
151151

152152
@Test
@@ -160,11 +160,11 @@ public void setParamShouldAddParam() throws CommandException {
160160
when(recordMock.get("bob")).thenReturn(value);
161161
when(value.asObject()).thenReturn("99");
162162

163-
assertTrue(offlineTestShell.getAll().isEmpty());
163+
assertTrue(offlineTestShell.allParameterValues().isEmpty());
164164

165-
Optional result = offlineTestShell.set("`bob`", "99");
165+
Optional result = offlineTestShell.setParameter("`bob`", "99");
166166
assertEquals("99", result.get());
167-
assertEquals("99", offlineTestShell.getAll().get("bob"));
167+
assertEquals("99", offlineTestShell.allParameterValues().get("bob"));
168168
}
169169

170170
@Test
@@ -279,6 +279,6 @@ public void setWithSomeBoltError() throws CommandException {
279279
CypherShell shell = new CypherShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
280280

281281
// when
282-
shell.set("bob", "99");
282+
shell.setParameter("bob", "99");
283283
}
284284
}

cypher-shell/src/test/java/org/neo4j/shell/commands/ParamTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.junit.Rule;
66
import org.junit.Test;
77
import org.junit.rules.ExpectedException;
8-
import org.neo4j.shell.VariableHolder;
8+
import org.neo4j.shell.ParameterMap;
99
import org.neo4j.shell.exception.CommandException;
1010

1111
import static junit.framework.TestCase.assertEquals;
@@ -18,7 +18,7 @@ public class ParamTest {
1818
@Rule
1919
public final ExpectedException thrown = ExpectedException.none();
2020

21-
private VariableHolder mockShell = mock(VariableHolder.class);
21+
private ParameterMap mockShell = mock( ParameterMap.class);
2222
private Command cmd;
2323

2424
@Before
@@ -48,49 +48,49 @@ public void shouldFailIfOneArg() throws CommandException {
4848
public void setParam() throws CommandException {
4949
cmd.execute("bob 9");
5050

51-
verify(mockShell).set("bob", "9");
51+
verify(mockShell).setParameter("bob", "9");
5252
}
5353

5454
@Test
5555
public void setLambdasAsParam() throws CommandException {
5656
cmd.execute("bob => 9");
5757

58-
verify(mockShell).set("bob", "9");
58+
verify(mockShell).setParameter("bob", "9");
5959
}
6060

6161
@Test
6262
public void setLambdasAsParamWithBackticks() throws CommandException {
6363
cmd.execute("`bob` => 9");
6464

65-
verify(mockShell).set("`bob`", "9");
65+
verify(mockShell).setParameter("`bob`", "9");
6666
}
6767

6868
@Test
6969
public void setSpecialCharacterParameter() throws CommandException {
7070
cmd.execute("bØb 9");
7171

72-
verify(mockShell).set("bØb", "9");
72+
verify(mockShell).setParameter("bØb", "9");
7373
}
7474

7575
@Test
7676
public void setSpecialCharacterParameterForLambdaExpressions() throws CommandException {
7777
cmd.execute("`first=>Name` => \"Bruce\"");
7878

79-
verify(mockShell).set("`first=>Name`", "\"Bruce\"");
79+
verify(mockShell).setParameter("`first=>Name`", "\"Bruce\"");
8080
}
8181

8282
@Test
8383
public void setParamWithSpecialCharacters() throws CommandException {
8484
cmd.execute("`bob#` 9");
8585

86-
verify(mockShell).set("`bob#`", "9");
86+
verify(mockShell).setParameter("`bob#`", "9");
8787
}
8888

8989
@Test
9090
public void setParamWithOddNoOfBackTicks() throws CommandException {
9191
cmd.execute(" `bo `` sömething ``` 9");
9292

93-
verify(mockShell).set("`bo `` sömething ```", "9");
93+
verify(mockShell).setParameter("`bo `` sömething ```", "9");
9494
}
9595

9696
@Test
@@ -146,34 +146,34 @@ public void shouldFailForVariablesWithoutText() throws CommandException {
146146
@Test
147147
public void shouldNotSplitOnSpace() throws CommandException {
148148
cmd.execute("bob 'one two'");
149-
verify(mockShell).set("bob", "'one two'");
149+
verify(mockShell).setParameter("bob", "'one two'");
150150
}
151151

152152
@Test
153153
public void shouldAcceptUnicodeAlphaNumeric() throws CommandException {
154154
cmd.execute("böb 'one two'");
155-
verify(mockShell).set("böb", "'one two'");
155+
verify(mockShell).setParameter("böb", "'one two'");
156156
}
157157

158158
@Test
159159
public void shouldAcceptColonFormOfParams() throws CommandException {
160160
cmd.execute("bob: one");
161-
verify(mockShell).set("bob", "one");
161+
verify(mockShell).setParameter("bob", "one");
162162
}
163163

164164
@Test
165165
public void shouldAcceptForTwoColonsFormOfParams() throws CommandException {
166166
cmd.execute("`bob:`: one");
167-
verify(mockShell).set("`bob:`", "one");
167+
verify(mockShell).setParameter("`bob:`", "one");
168168

169169
cmd.execute("`t:om` two");
170-
verify(mockShell).set("`t:om`", "two");
170+
verify(mockShell).setParameter("`t:om`", "two");
171171
}
172172

173173
@Test
174174
public void shouldNotExecuteEscapedCypher() throws CommandException {
175175
cmd.execute("bob \"RETURN 5 as bob\"");
176-
verify(mockShell).set("bob", "\"RETURN 5 as bob\"");
176+
verify(mockShell).setParameter("bob", "\"RETURN 5 as bob\"");
177177
}
178178

179179
@Test

0 commit comments

Comments
 (0)