diff --git a/community/shell/src/main/java/org/neo4j/shell/ShellLobby.java b/community/shell/src/main/java/org/neo4j/shell/ShellLobby.java index c7cff6718381d..bc1b474417d75 100644 --- a/community/shell/src/main/java/org/neo4j/shell/ShellLobby.java +++ b/community/shell/src/main/java/org/neo4j/shell/ShellLobby.java @@ -28,6 +28,7 @@ import org.neo4j.shell.impl.RemoteClient; import org.neo4j.shell.impl.RmiLocation; import org.neo4j.shell.impl.SameJvmClient; +import org.neo4j.shell.impl.SystemOutput; /** * A convenience class for creating servers clients as well as finding remote @@ -95,7 +96,24 @@ public static ShellClient newClient( ShellServer server ) throws ShellException public static ShellClient newClient( ShellServer server, Map initialSession, CtrlCHandler signalHandler ) throws ShellException { - return new SameJvmClient( initialSession, server, signalHandler ); + return newClient( server, initialSession, new SystemOutput(), signalHandler ); + } + + /** + * Creates a client and "starts" it, i.e. grabs the console prompt. + * @param server the server (in the same JVM) which the client will + * communicate with. + * @param initialSession the initial session variables the shell will have, + * in addition to those provided by the server initially. + * @param output the output to write to. + * @param signalHandler the ctrl-c handler to use + * @throws ShellException if the execution fails + * @return the new shell client. + */ + public static ShellClient newClient( ShellServer server, Map initialSession, + Output output, CtrlCHandler signalHandler ) throws ShellException + { + return new SameJvmClient( initialSession, server, output, signalHandler ); } /** diff --git a/community/shell/src/main/java/org/neo4j/shell/StartClient.java b/community/shell/src/main/java/org/neo4j/shell/StartClient.java index 92f603af6a5d5..991020214323e 100644 --- a/community/shell/src/main/java/org/neo4j/shell/StartClient.java +++ b/community/shell/src/main/java/org/neo4j/shell/StartClient.java @@ -41,6 +41,7 @@ import org.neo4j.shell.impl.RmiLocation; import org.neo4j.shell.impl.ShellBootstrap; import org.neo4j.shell.impl.SimpleAppServer; +import org.neo4j.shell.impl.SystemOutput; import org.neo4j.shell.kernel.GraphDatabaseShellServer; import static org.neo4j.io.fs.FileUtils.newBufferedFileReader; @@ -299,7 +300,8 @@ public void run() { out.println( "NOTE: Local Neo4j graph database service at '" + dbPath + "'" ); } - ShellClient client = ShellLobby.newClient( server, getSessionVariablesFromArgs( args ), signalHandler ); + ShellClient client = ShellLobby.newClient( server, getSessionVariablesFromArgs( args ), + new SystemOutput( out ), signalHandler ); grabPromptOrJustExecuteCommand( client, args ); shutdownIfNecessary( server ); diff --git a/community/shell/src/main/java/org/neo4j/shell/impl/SystemOutput.java b/community/shell/src/main/java/org/neo4j/shell/impl/SystemOutput.java index 6e407ea28ccbc..35734d6b0abae 100644 --- a/community/shell/src/main/java/org/neo4j/shell/impl/SystemOutput.java +++ b/community/shell/src/main/java/org/neo4j/shell/impl/SystemOutput.java @@ -19,11 +19,11 @@ */ package org.neo4j.shell.impl; +import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.neo4j.shell.Output; @@ -35,13 +35,14 @@ public class SystemOutput implements Output { private PrintWriter out; - public SystemOutput() { - try { - out = new PrintWriter(new OutputStreamWriter(System.out,"UTF-8")); - } catch (UnsupportedEncodingException e) { - System.err.println("Unsupported encoding UTF-8, using "+Charset.defaultCharset()+", error: "+e.getMessage()); - out = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset())); - } + public SystemOutput() + { + this( System.out ); + } + + public SystemOutput( OutputStream out ) + { + this.out = new PrintWriter( new OutputStreamWriter( out, StandardCharsets.UTF_8 ) ); } public void print( Serializable object ) diff --git a/community/shell/src/test/java/org/neo4j/shell/StartClientTest.java b/community/shell/src/test/java/org/neo4j/shell/StartClientTest.java index 27b24e69e1ac0..6d9bfb3c2fb41 100644 --- a/community/shell/src/test/java/org/neo4j/shell/StartClientTest.java +++ b/community/shell/src/test/java/org/neo4j/shell/StartClientTest.java @@ -174,6 +174,25 @@ protected GraphDatabaseShellServer getGraphDatabaseShellServer( String dbPath, b verify( databaseShellServer ).shutdown(); } + @Test + public void shouldReportEditionThroughDbInfoApp() throws Exception + { + // given + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + CtrlCHandler ctrlCHandler = mock( CtrlCHandler.class ); + StartClient client = new StartClient( + new PrintStream( out ), new PrintStream( err ) ); + + // when + client.start( new String[]{"-path", db.getGraphDatabaseAPI().getStoreDir(), + "-c", "dbinfo -g Configuration edition"}, ctrlCHandler ); + + // then + assertEquals( 0, err.size() ); + assertThat( out.toString(), containsString( "\"edition\": \"Community\"" ) ); + } + @Test public void shouldPrintVersionAndExit() throws Exception { diff --git a/enterprise/kernel/pom.xml b/enterprise/kernel/pom.xml index 8547811c484a6..ec9f6de042a6a 100644 --- a/enterprise/kernel/pom.xml +++ b/enterprise/kernel/pom.xml @@ -143,6 +143,12 @@ ${project.version} test + + org.neo4j + neo4j-jmx + ${project.version} + test + org.neo4j neo4j-io diff --git a/enterprise/kernel/src/test/java/org/neo4j/shell/EnterpriseVersionTest.java b/enterprise/kernel/src/test/java/org/neo4j/shell/EnterpriseVersionTest.java index de7a92673273f..ac9a0451f2fc3 100644 --- a/enterprise/kernel/src/test/java/org/neo4j/shell/EnterpriseVersionTest.java +++ b/enterprise/kernel/src/test/java/org/neo4j/shell/EnterpriseVersionTest.java @@ -22,15 +22,23 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import org.junit.Rule; import org.junit.Test; +import org.neo4j.test.TargetDirectory; + +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; +import static org.neo4j.test.TargetDirectory.testDirForTest; public class EnterpriseVersionTest { + @Rule + public final TargetDirectory.TestDirectory dir = testDirForTest( EnterpriseVersionTest.class ); + @Test public void shouldPrintVersionAndExit() throws Exception { @@ -48,4 +56,22 @@ public void shouldPrintVersionAndExit() throws Exception String version = out.toString(); assertThat( version, startsWith( "Neo4j Enterprise, version " ) ); } + + @Test + public void shouldReportEditionThroughDbInfoApp() throws Exception + { + // given + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + CtrlCHandler ctrlCHandler = mock( CtrlCHandler.class ); + StartClient client = new StartClient( new PrintStream( out ), new PrintStream( err ) ); + + // when + client.start( new String[]{"-path", dir.absolutePath(), + "-c", "dbinfo -g Configuration edition"}, ctrlCHandler ); + + // then + assertEquals( 0, err.size() ); + assertThat( out.toString(), containsString( "\"edition\": \"Enterprise\"" ) ); + } }