private void badUsage( String message ) { outsideWorld.stdErrLine( message ); usage.print( outsideWorld::stdErrLine ); failure(); }
private void badUsage( AdminCommand.Provider command, IncorrectUsage e ) { outsideWorld.stdErrLine( e.getMessage() ); outsideWorld.stdErrLine( "" ); usage.printUsageForCommand( command, outsideWorld::stdErrLine ); failure(); }
public AdminTool( CommandLocator commandLocator, BlockerLocator blockerLocator, OutsideWorld outsideWorld, boolean debug ) { this.commandLocator = CommandLocator.withAdditionalCommand( help(), commandLocator ); this.blockerLocator = blockerLocator; this.outsideWorld = outsideWorld; this.debug = debug; this.usage = new Usage( scriptName, this.commandLocator ); }
@Test void shouldProvideFeedbackIfTheCommandFails() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = args -> { throw new CommandFailed( "the-failure-message" ); }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); verify( outsideWorld ).stdErrLine( "command failed: the-failure-message" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldPrintTheStacktraceWhenTheCommandFailsIfTheDebugFlagIsSet() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); CommandFailed exception = new CommandFailed( "" ); AdminCommand command = args -> { throw exception; }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, true ) .execute( null, null, "exception" ); verify( outsideWorld ).printStacktrace( exception ); }
public static void main( String[] args ) throws IOException { Path homeDir = Paths.get( NEO4J_HOME ); Path configDir = Paths.get( NEO4J_CONF ); boolean debug = NEO4J_DEBUG != null; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { new AdminTool( CommandLocator.fromServiceLocator(), BlockerLocator.fromServiceLocator(), outsideWorld, debug ).execute( homeDir, configDir, args ); } }
@Test void shouldExecuteTheCommand() throws CommandFailed, IncorrectUsage { AdminCommand command = mock( AdminCommand.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), new NullOutsideWorld(), false ) .execute( null, null, "command", "the", "other", "args" ); verify( command ).execute( new String[]{"the", "other", "args"} ); }
@Test void shouldAddTheHelpCommandToThoseProvidedByTheLocator() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( new NullCommandLocator(), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "help" ); verify( outsideWorld ).stdOutLine( " help" ); }
@Test void shouldProvideFeedbackWhenNoCommandIsProvided() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( new NullCommandLocator(), new NullBlockerLocator(), outsideWorld, false ).execute( null, null ); verify( outsideWorld ).stdErrLine( "you must provide a command" ); verify( outsideWorld ).stdErrLine( "usage: neo4j-admin <command>" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
DiagnosticsReportCommand( Path homeDir, Path configDir, OutsideWorld outsideWorld ) { this.homeDir = homeDir; this.configDir = configDir; this.fs = outsideWorld.fileSystem(); this.out = outsideWorld.outStream(); err = outsideWorld.errorStream(); }
@Test void shouldExit0WhenEverythingWorks() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( new CannedLocator( new NullCommandProvider() ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "null" ); verify( outsideWorld ).exit( STATUS_SUCCESS ); }
private void failure( String message, int code ) { outsideWorld.stdErrLine( message ); outsideWorld.exit( code ); }
@Override @Nonnull public AdminCommand create( Path homeDir, Path configDir, OutsideWorld outsideWorld ) { return new HelpCommand( usage, outsideWorld::stdOutLine, CommandLocator.fromServiceLocator() ); } }
@Override public AdminCommandSection commandSection() { return AdminCommandSection.general(); }
private void success() { outsideWorld.exit( STATUS_SUCCESS ); } }
public void printUsageForCommand( AdminCommand.Provider command, Consumer<String> output ) { final CommandUsage commandUsage = new CommandUsage( command, scriptName ); commandUsage.printDetailed( output ); }
@Test void helpArgumentPrintsHelpForCommand() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "command", "--help" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdErrLine( "unknown argument: --help" ); verify( outsideWorld ).stdErrLine( "usage: neo4j-admin command " ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldNotPrintTheStacktraceWhenTheCommandFailsIfTheDebugFlagIsNotSet() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); CommandFailed exception = new CommandFailed( "" ); AdminCommand command = args -> { throw exception; }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); verify( outsideWorld, never() ).printStacktrace( exception ); }
@Test void helpArgumentPrintsHelp() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "--help" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdErrLine( "unrecognized command: --help" ); verify( outsideWorld ).stdErrLine( "usage: neo4j-admin <command>" ); verify( outsideWorld ).exit( STATUS_ERROR ); }