@Test void shouldBlockDumpIfABlockerSaysSo() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = mock( AdminCommand.class ); AdminCommand.Blocker blocker = mock( AdminCommand.Blocker.class ); when( blocker.doesBlock( any(), any() ) ).thenReturn( true ); when( blocker.commands() ).thenReturn( Collections.singleton( "command" ) ); when( blocker.explanation() ).thenReturn( "the explanation" ); BlockerLocator blockerLocator = mock( BlockerLocator.class ); when( blockerLocator.findBlockers( "command" ) ).thenReturn( Collections.singletonList( blocker ) ); new AdminTool( cannedCommand( "command", command ), blockerLocator, outsideWorld, false ) .execute( null, null, "command" ); verify( outsideWorld ).stdErrLine( "command failed: the explanation" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldNotBlockIfNoneOfTheBlockersBlock() throws CommandFailed, IncorrectUsage { AdminCommand command = mock( AdminCommand.class ); AdminCommand.Blocker falseBlocker = mock( AdminCommand.Blocker.class ); when( falseBlocker.doesBlock( any(), any() ) ).thenReturn( false ); when( falseBlocker.explanation() ).thenReturn( "falseBlocker explanation" ); BlockerLocator blockerLocator = mock( BlockerLocator.class ); when( blockerLocator.findBlockers( "command" ) ) .thenReturn( Arrays.asList( falseBlocker, falseBlocker, falseBlocker ) ); new AdminTool( cannedCommand( "command", command ), blockerLocator, new NullOutsideWorld(), false ) .execute( null, null, "command", "the", "other", "args" ); verify( command ).execute( new String[]{"the", "other", "args"} ); }
@Test void shouldBlockDumpIfOneBlockerOutOfManySaysSo() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = mock( AdminCommand.class ); AdminCommand.Blocker trueBlocker = mock( AdminCommand.Blocker.class ); when( trueBlocker.doesBlock( any(), any() ) ).thenReturn( true ); when( trueBlocker.explanation() ).thenReturn( "trueBlocker explanation" ); AdminCommand.Blocker falseBlocker = mock( AdminCommand.Blocker.class ); when( falseBlocker.doesBlock( any(), any() ) ).thenReturn( false ); when( falseBlocker.explanation() ).thenReturn( "falseBlocker explanation" ); BlockerLocator blockerLocator = mock( BlockerLocator.class ); when( blockerLocator.findBlockers( "command" ) ) .thenReturn( Arrays.asList( falseBlocker, trueBlocker, falseBlocker ) ); new AdminTool( cannedCommand( "command", command ), blockerLocator, outsideWorld, false ) .execute( null, null, "command" ); verify( outsideWorld ).stdErrLine( "command failed: trueBlocker explanation" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@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 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 ); }
@Test void shouldNotPrintTheStacktraceWhenTheCommandThrowsARuntimeExceptionIfTheDebugFlagIsNotSet() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); RuntimeException exception = new RuntimeException( "" ); 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 versionArgumentPrintsVersion() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "--version" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdOutLine( "neo4j-admin " + neo4jVersion() ); verify( outsideWorld ).exit( STATUS_SUCCESS ); }
@Test void versionArgumentPrintsVersionEvenWithCommand() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "command", "--version" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdOutLine( "neo4j-admin " + neo4jVersion() ); verify( outsideWorld ).exit( STATUS_SUCCESS ); }
@Test void shouldProvideFeedbackIfTheCommandThrowsARuntimeException() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = args -> { throw new RuntimeException( "the-exception-message" ); }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); verify( outsideWorld ).stdErrLine( "unexpected error: the-exception-message" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldPrintTheStacktraceWhenTheCommandThrowsARuntimeExceptionIfTheDebugFlagIsSet() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); RuntimeException exception = new RuntimeException( "" ); AdminCommand command = args -> { throw exception; }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, true ) .execute( null, null, "exception" ); verify( outsideWorld ).printStacktrace( exception ); }
@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 shouldProvideFeedbackIfTheCommandReportsAUsageProblem() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = args -> { throw new IncorrectUsage( "the-usage-message" ); }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); InOrder inOrder = inOrder( outsideWorld ); inOrder.verify( outsideWorld ).stdErrLine( "the-usage-message" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@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 ); }
@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"} ); }