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

Commit 4a213c3

Browse files
authored
Merge pull request #142 from henriknyman/2.0-use-database
Support database selection with 2.x driver
2 parents a38a6b1 + 6dd1c96 commit 4a213c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+631
-349
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ ext {
7272
argparse4jVersion = '0.7.0'
7373
junitVersion = '4.12'
7474
evaluatorVersion = '3.5.4'
75-
neo4jJavaDriverVersion = '1.7.0'
75+
neo4jJavaDriverVersion = '2.0.0-alpha01'
7676
findbugsVersion = '3.0.0'
7777
jansiVersion = '1.13'
7878
jlineVersion = '2.14.6'

cypher-shell/src/integration-test/java/org/neo4j/shell/MainIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void connectInteractivelyPromptsOnWrongAuthentication() throws Exception
4040
cliArgs.getPort(),
4141
cliArgs.getUsername(),
4242
cliArgs.getPassword(),
43-
cliArgs.getEncryption());
43+
cliArgs.getEncryption(),
44+
cliArgs.getDatabase());
4445

4546
CypherShell shell = new CypherShell(logger, prettyConfig);
4647

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
import org.junit.Rule;
66
import org.junit.Test;
77
import org.junit.rules.ExpectedException;
8-
import org.neo4j.driver.v1.exceptions.AuthenticationException;
8+
9+
import org.neo4j.driver.exceptions.AuthenticationException;
910
import org.neo4j.shell.ConnectionConfig;
1011
import org.neo4j.shell.CypherShell;
1112
import org.neo4j.shell.StringLinePrinter;
1213
import org.neo4j.shell.cli.Format;
1314
import org.neo4j.shell.exception.CommandException;
1415
import org.neo4j.shell.prettyprint.PrettyConfig;
1516

17+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
18+
1619
public class CypherShellFailureIntegrationTest {
1720
@Rule
1821
public final ExpectedException thrown = ExpectedException.none();
@@ -28,9 +31,9 @@ public void setUp() throws Exception {
2831

2932
@Test
3033
public void cypherWithNoPasswordShouldReturnValidError() throws CommandException {
31-
thrown.expect( AuthenticationException.class );
34+
thrown.expect(AuthenticationException.class);
3235
thrown.expectMessage("The client is unauthorized due to authentication failure.");
3336

34-
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "", true));
37+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "", true, ABSENT_DB_NAME));
3538
}
3639
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.neo4j.shell.commands;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.ExpectedException;
8+
9+
import org.neo4j.driver.exceptions.ClientException;
10+
import org.neo4j.shell.ConnectionConfig;
11+
import org.neo4j.shell.CypherShell;
12+
import org.neo4j.shell.StringLinePrinter;
13+
import org.neo4j.shell.cli.Format;
14+
import org.neo4j.shell.exception.CommandException;
15+
import org.neo4j.shell.prettyprint.PrettyConfig;
16+
17+
import static org.hamcrest.CoreMatchers.is;
18+
import static org.hamcrest.MatcherAssert.assertThat;
19+
import static org.junit.Assume.assumeTrue;
20+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
21+
import static org.neo4j.shell.DatabaseManager.DEFAULT_DEFAULT_DB_NAME;
22+
import static org.neo4j.shell.DatabaseManager.SYSTEM_DB_NAME;
23+
import static org.neo4j.shell.Versions.majorVersion;
24+
25+
public class CypherShellMultiDatabaseIntegrationTest
26+
{
27+
@Rule
28+
public final ExpectedException thrown = ExpectedException.none();
29+
30+
private StringLinePrinter linePrinter = new StringLinePrinter();
31+
private Command useCommand;
32+
private Command beginCommand;
33+
private Command rollbackCommand;
34+
private CypherShell shell;
35+
36+
@Before
37+
public void setUp() throws Exception {
38+
linePrinter.clear();
39+
shell = new CypherShell(linePrinter, new PrettyConfig(Format.PLAIN, true, 1000));
40+
useCommand = new Use(shell);
41+
beginCommand = new Begin(shell);
42+
rollbackCommand = new Rollback(shell);
43+
44+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true, ABSENT_DB_NAME));
45+
46+
// Multiple databases are only available from 4.0
47+
assumeTrue( majorVersion( shell.getServerVersion() ) >= 4 );
48+
}
49+
50+
@After
51+
public void tearDown() throws Exception {
52+
}
53+
54+
@Test
55+
public void switchingToSystemDatabaseWorks() throws CommandException {
56+
useCommand.execute(SYSTEM_DB_NAME);
57+
58+
assertThat(linePrinter.output(), is(""));
59+
}
60+
61+
@Test
62+
public void switchingToSystemDatabaseAndBackToDefaultWorks() throws CommandException {
63+
useCommand.execute(SYSTEM_DB_NAME);
64+
useCommand.execute(DEFAULT_DEFAULT_DB_NAME);
65+
66+
assertThat(linePrinter.output(), is(""));
67+
}
68+
69+
@Test
70+
public void switchingDatabaseInOpenTransactionShouldFail() throws CommandException {
71+
thrown.expect(CommandException.class);
72+
thrown.expectMessage("There is an open transaction.");
73+
74+
beginCommand.execute("");
75+
useCommand.execute("another_database");
76+
}
77+
78+
@Test
79+
public void switchingDatabaseAfterRollbackTransactionWorks() throws CommandException {
80+
beginCommand.execute("");
81+
rollbackCommand.execute("");
82+
useCommand.execute(SYSTEM_DB_NAME);
83+
84+
assertThat(linePrinter.output(), is(""));
85+
}
86+
87+
@Test
88+
public void switchingToNonExistingDatabaseShouldGiveErrorResponseFromServer() throws CommandException {
89+
thrown.expect(ClientException.class);
90+
thrown.expectMessage("The database requested does not exist.");
91+
92+
useCommand.execute("this_database_name_does_not_exist_in_test_container");
93+
}
94+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static org.hamcrest.CoreMatchers.containsString;
1717
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
1819
import static org.neo4j.shell.prettyprint.OutputFormatter.NEWLINE;
1920

2021
public class CypherShellPlainIntegrationTest {
@@ -28,7 +29,7 @@ public class CypherShellPlainIntegrationTest {
2829
public void setUp() throws Exception {
2930
linePrinter.clear();
3031
shell = new CypherShell(linePrinter, new PrettyConfig(Format.PLAIN, true, 1000));
31-
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true));
32+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true, ABSENT_DB_NAME));
3233
}
3334

3435
@After

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.junit.Assert.assertEquals;
1818
import static org.junit.Assert.assertTrue;
1919
import static org.junit.Assume.assumeTrue;
20+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
2021
import static org.neo4j.shell.Versions.majorVersion;
2122
import static org.neo4j.shell.Versions.minorVersion;
2223

@@ -38,7 +39,7 @@ public void setUp() throws Exception {
3839
commitCommand = new Commit(shell);
3940
beginCommand = new Begin(shell);
4041

41-
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true));
42+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true, ABSENT_DB_NAME));
4243
}
4344

4445
@After
@@ -72,7 +73,7 @@ public void connectTwiceThrows() throws CommandException {
7273
thrown.expect(CommandException.class);
7374
thrown.expectMessage("Already connected");
7475

75-
ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true);
76+
ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true, ABSENT_DB_NAME);
7677
assertTrue("Shell should already be connected", shell.isConnected());
7778
shell.connect(config);
7879
}

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

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

3-
import org.neo4j.driver.v1.Config;
3+
import org.neo4j.driver.Config;
44

55
import javax.annotation.Nonnull;
66

77
public class ConnectionConfig {
88
private final String scheme;
99
private final String host;
1010
private final int port;
11-
private final Config.EncryptionLevel encryption;
11+
private final boolean encryption;
1212
private String username;
1313
private String password;
14+
private String database;
1415

1516
public ConnectionConfig(@Nonnull String scheme, @Nonnull String host, int port,
16-
@Nonnull String username, @Nonnull String password, boolean encryption) {
17+
@Nonnull String username, @Nonnull String password, boolean encryption,
18+
@Nonnull String database) {
1719
this.host = host;
1820
this.port = port;
1921
this.username = fallbackToEnvVariable(username, "NEO4J_USERNAME");
2022
this.password = fallbackToEnvVariable(password, "NEO4J_PASSWORD");
21-
this.encryption = encryption ? Config.EncryptionLevel.REQUIRED : Config.EncryptionLevel.NONE;
23+
this.encryption = encryption;
2224
this.scheme = scheme;
25+
this.database = database;
2326
}
2427

2528
/**
@@ -64,10 +67,15 @@ public String driverUrl() {
6467
}
6568

6669
@Nonnull
67-
public Config.EncryptionLevel encryption() {
70+
public boolean encryption() {
6871
return encryption;
6972
}
7073

74+
@Nonnull
75+
public String database() {
76+
return database;
77+
}
78+
7179
public void setUsername(@Nonnull String username) {
7280
this.username = username;
7381
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* A possibly interactive shell for evaluating cypher statements.
3030
*/
31-
public class CypherShell implements StatementExecuter, Connector, TransactionHandler, ParameterMap {
31+
public class CypherShell implements StatementExecuter, Connector, TransactionHandler, ParameterMap, DatabaseManager {
3232
// Final space to catch newline
3333
protected static final Pattern cmdNamePattern = Pattern.compile("^\\s*(?<name>[^\\s]+)\\b(?<args>.*)\\s*$");
3434
protected final Map<String, ParamValue> queryParams = new HashMap<>();
@@ -199,4 +199,15 @@ protected void addRuntimeHookToResetShell() {
199199
Runtime.getRuntime().addShutdownHook(new Thread(this::reset));
200200
}
201201

202+
@Override
203+
public void setActiveDatabase(String databaseName) throws CommandException
204+
{
205+
boltStateHandler.setActiveDatabase(databaseName);
206+
}
207+
208+
@Override
209+
public String getActiveDatabase()
210+
{
211+
return boltStateHandler.getActiveDatabase();
212+
}
202213
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.neo4j.shell;
2+
3+
import org.neo4j.shell.exception.CommandException;
4+
5+
/**
6+
* An object capable of tracking the active database.
7+
*/
8+
public interface DatabaseManager
9+
{
10+
String DEFAULT_DEFAULT_DB_NAME = "neo4j";
11+
String SYSTEM_DB_NAME = "system";
12+
13+
void setActiveDatabase(String databaseName) throws CommandException;
14+
15+
String getActiveDatabase();
16+
}

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

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

33
import jline.console.ConsoleReader;
4-
import org.neo4j.driver.v1.exceptions.AuthenticationException;
4+
import org.neo4j.driver.exceptions.AuthenticationException;
55
import org.neo4j.shell.build.Build;
66
import org.neo4j.shell.cli.CliArgHelper;
77
import org.neo4j.shell.cli.CliArgs;
@@ -67,7 +67,8 @@ void startShell(@Nonnull CliArgs cliArgs) {
6767
cliArgs.getPort(),
6868
cliArgs.getUsername(),
6969
cliArgs.getPassword(),
70-
cliArgs.getEncryption());
70+
cliArgs.getEncryption(),
71+
cliArgs.getDatabase());
7172

7273
try {
7374
CypherShell shell = new CypherShell(logger, prettyConfig);

0 commit comments

Comments
 (0)