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

Commit 6eb8ffc

Browse files
authored
Merge pull request #56 from spacecowboy/neo4jversionprint
Added version of connected Neo4j to welcome message
2 parents 03b8507 + ecda3b3 commit 6eb8ffc

File tree

6 files changed

+119
-6
lines changed

6 files changed

+119
-6
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
public interface Connector {
1111

1212
/**
13-
*
1413
* @return true if connected, false otherwise
1514
*/
1615
boolean isConnected();
@@ -20,4 +19,13 @@ public interface Connector {
2019
* @throws CommandException if connection failed
2120
*/
2221
void connect(@Nonnull ConnectionConfig connectionConfig) throws CommandException;
22+
23+
/**
24+
* Returns the version of Neo4j which the shell is connected to. If the version is before 3.1.0-M09, or we are not
25+
* connected yet, this returns the empty string.
26+
*
27+
* @return the version of neo4j (like '3.1.0') if connected and available, an empty string otherwise
28+
*/
29+
@Nonnull
30+
String getServerVersion();
2331
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ public void connect(@Nonnull ConnectionConfig connectionConfig) throws CommandEx
123123
boltStateHandler.connect(connectionConfig);
124124
}
125125

126+
@Nonnull
127+
@Override
128+
public String getServerVersion() {
129+
return boltStateHandler.getServerVersion();
130+
}
131+
126132
@Override
127133
public void beginTransaction() throws CommandException {
128134
boltStateHandler.beginTransaction();

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,15 @@ public static void main(String[] args) {
4848
}
4949

5050
private static void printWelcomeMessage(@Nonnull Logger logger,
51-
@Nonnull ConnectionConfig connectionConfig) {
52-
AnsiFormattedText welcomeMessage = AnsiFormattedText.from("Connected to Neo4j at ")
51+
@Nonnull ConnectionConfig connectionConfig,
52+
@Nonnull String serverVersion) {
53+
String neo4j = "Neo4j";
54+
if (!serverVersion.isEmpty()) {
55+
neo4j += " " + serverVersion;
56+
}
57+
AnsiFormattedText welcomeMessage = AnsiFormattedText.from("Connected to ")
58+
.append(neo4j)
59+
.append(" at ")
5360
.bold().append(connectionConfig.driverUrl()).boldOff();
5461

5562
if (!connectionConfig.username().isEmpty()) {
@@ -90,7 +97,7 @@ void startShell(@Nonnull CliArgs cliArgs) {
9097

9198
shell.setCommandHelper(commandHelper);
9299

93-
printWelcomeMessage(logger, connectionConfig);
100+
printWelcomeMessage(logger, connectionConfig, shell.getServerVersion());
94101

95102
int code = shellRunner.runUntilEnd();
96103
System.exit(code);

cypher-shell/src/main/java/org/neo4j/shell/state/BoltStateHandler.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ public void connect(@Nonnull ConnectionConfig connectionConfig) throws CommandEx
114114
}
115115
}
116116

117+
@Nonnull
118+
@Override
119+
public String getServerVersion() {
120+
if (isConnected()) {
121+
String version = session.server();
122+
if (version == null) {
123+
// On versions before 3.1.0-M09
124+
version = "";
125+
}
126+
if (version.startsWith("Neo4j/")) {
127+
// Want to return '3.1.0' and not 'Neo4j/3.1.0'
128+
version = version.substring(6);
129+
}
130+
return version;
131+
}
132+
return "";
133+
}
134+
117135
@Nonnull
118136
public Optional<BoltResult> runCypher(@Nonnull String cypher,
119137
@Nonnull Map<String, Object> queryParams) throws CommandException {
@@ -181,8 +199,8 @@ private StatementRunner getStatementRunner() throws CommandException {
181199

182200
private Driver getDriver(@Nonnull ConnectionConfig connectionConfig, @Nullable AuthToken authToken) {
183201
Config config = Config.build()
184-
.withLogging(new ConsoleLogging(Level.OFF))
185-
.withEncryptionLevel(connectionConfig.encryption()).toConfig();
202+
.withLogging(new ConsoleLogging(Level.OFF))
203+
.withEncryptionLevel(connectionConfig.encryption()).toConfig();
186204
return driverProvider.apply(connectionConfig.driverUrl(), authToken, config);
187205
}
188206
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ public void verifyDelegationOfResetMethod() throws CommandException {
7474
verify(mockedBoltStateHandler).reset();
7575
}
7676

77+
@Test
78+
public void verifyDelegationOfGetServerVersionMethod() throws CommandException {
79+
CypherShell shell = new CypherShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
80+
81+
shell.getServerVersion();
82+
verify(mockedBoltStateHandler).getServerVersion();
83+
}
84+
85+
@Test
86+
public void verifyDelegationOfIsTransactionOpenMethod() throws CommandException {
87+
CypherShell shell = new CypherShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
88+
89+
shell.isTransactionOpen();
90+
verify(mockedBoltStateHandler).isTransactionOpen();
91+
}
92+
7793
@Test
7894
public void verifyDelegationOfTransactionMethods() throws CommandException {
7995
CypherShell shell = new CypherShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);

cypher-shell/src/test/java/org/neo4j/shell/state/BoltStateHandlerTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,64 @@ public void setup() {
3838
doReturn(System.out).when(logger).getOutputStream();
3939
}
4040

41+
@Test
42+
public void versionIsEmptyBeforeConnect() throws CommandException {
43+
assertFalse(boltStateHandler.isConnected());
44+
assertEquals("", boltStateHandler.getServerVersion());
45+
}
46+
47+
@Test
48+
public void versionIsEmptyIfDriverReturnsNull() throws CommandException {
49+
RecordingDriverProvider provider = new RecordingDriverProvider() {
50+
@Override
51+
public Driver apply(String uri, AuthToken authToken, Config config) {
52+
super.apply(uri, authToken, config);
53+
return new FakeDriver() {
54+
@Override
55+
public Session session() {
56+
return new FakeSession() {
57+
@Override
58+
public String server() {
59+
return null;
60+
}
61+
};
62+
}
63+
};
64+
}
65+
};
66+
BoltStateHandler handler = new BoltStateHandler(provider);
67+
ConnectionConfig config = new ConnectionConfig("", -1, "", "", false);
68+
handler.connect(config);
69+
70+
assertEquals("", handler.getServerVersion());
71+
}
72+
73+
@Test
74+
public void versionIsNotEmptyAfterConnect() throws CommandException {
75+
RecordingDriverProvider provider = new RecordingDriverProvider() {
76+
@Override
77+
public Driver apply(String uri, AuthToken authToken, Config config) {
78+
super.apply(uri, authToken, config);
79+
return new FakeDriver() {
80+
@Override
81+
public Session session() {
82+
return new FakeSession() {
83+
@Override
84+
public String server() {
85+
return "Neo4j/9.4.1-ALPHA";
86+
}
87+
};
88+
}
89+
};
90+
}
91+
};
92+
BoltStateHandler handler = new BoltStateHandler(provider);
93+
ConnectionConfig config = new ConnectionConfig("", -1, "", "", false);
94+
handler.connect(config);
95+
96+
assertEquals("9.4.1-ALPHA", handler.getServerVersion());
97+
}
98+
4199
@Test
42100
public void closeTransactionAfterRollback() throws CommandException {
43101
boltStateHandler.connect();

0 commit comments

Comments
 (0)