private void badUsage( AdminCommand.Provider command, IncorrectUsage e ) { outsideWorld.stdErrLine( e.getMessage() ); outsideWorld.stdErrLine( "" ); usage.printUsageForCommand( command, outsideWorld::stdErrLine ); failure(); }
@Test void printsUnknownCommandWhenUnknownCommandIsProvided() { CommandLocator commandLocator = mock( CommandLocator.class ); when( commandLocator.getAllProviders() ).thenReturn( Collections.emptyList() ); when( commandLocator.findProvider( "foobar" ) ).thenThrow( new NoSuchElementException( "foobar" ) ); HelpCommand helpCommand = new HelpCommand( mock( Usage.class ), out, commandLocator ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> helpCommand.execute( "foobar" ) ); assertThat( incorrectUsage.getMessage(), containsString( "Unknown command: foobar" ) ); }
@Test void printsAvailableCommandsWhenUnknownCommandIsProvided() { CommandLocator commandLocator = mock( CommandLocator.class ); List<AdminCommand.Provider> mockCommands = asList( mockCommand( "foo" ), mockCommand( "bar" ), mockCommand( "baz" ) ); when( commandLocator.getAllProviders() ).thenReturn( mockCommands ); when( commandLocator.findProvider( "foobar" ) ).thenThrow( new NoSuchElementException( "foobar" ) ); HelpCommand helpCommand = new HelpCommand( mock( Usage.class ), out, commandLocator ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> helpCommand.execute( "foobar" ) ); assertThat( incorrectUsage.getMessage(), containsString( "Available commands are: foo bar baz" ) ); }
@Test void databaseAndBackupAreMutuallyExclusive() throws Exception { ConsistencyCheckService consistencyCheckService = mock( ConsistencyCheckService.class ); Path homeDir = testDir.directory( "home" ).toPath(); CheckConsistencyCommand checkConsistencyCommand = new CheckConsistencyCommand( homeDir, testDir.directory( "conf" ).toPath(), consistencyCheckService ); when( consistencyCheckService.runFullConsistencyCheck( any(), any(), any(), any(), any(), anyBoolean(), any( ConsistencyFlags.class ) ) ) .thenReturn( ConsistencyCheckService.Result.success( null ) ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> checkConsistencyCommand.execute( new String[]{"--database=foo", "--backup=bar"} ) ); assertEquals( "Only one of '--database' and '--backup' can be specified.", incorrectUsage.getMessage() ); }
@Test void failIfInvalidModeSpecified() { try ( NullOutsideWorld outsideWorld = new NullOutsideWorld() ) { ImportCommand importCommand = new ImportCommand( testDir.directory( "home" ).toPath(), testDir.directory( "conf" ).toPath(), outsideWorld ); String[] arguments = {"--mode=foo", "--database=bar", "--from=baz"}; IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> importCommand.execute( arguments ) ); assertThat( incorrectUsage.getMessage(), containsString( "foo" ) ); } }
@Test void requiresDatabaseArgument() { try ( NullOutsideWorld outsideWorld = new NullOutsideWorld() ) { ImportCommand importCommand = new ImportCommand( testDir.directory( "home" ).toPath(), testDir.directory( "conf" ).toPath(), outsideWorld ); String[] arguments = {"--mode=database", "--from=bar"}; IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> importCommand.execute( arguments ) ); assertThat( incorrectUsage.getMessage(), containsString( "database" ) ); } }
if ( e.getMessage().equals( "Unknown classifier: threads" ) )
@Test void throwsOnUnexpectedShortArgument() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withDatabase().parse( new String[]{ "-f" } ) ); assertEquals( "unrecognized option: 'f'", incorrectUsage.getMessage() ); }
@Test void throwsOnUnexpectedPositionalArgument() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withDatabase().parse( new String[]{ "bob", "sob" } ) ); assertEquals( "unrecognized arguments: 'bob sob'", incorrectUsage.getMessage() ); }
@Test void throwsOnUnexpectedLongArgument() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withDatabase().parse( new String[]{"--stacktrace"} ) ); assertEquals( "unrecognized option: 'stacktrace'", incorrectUsage.getMessage() ); }
@Test void throwsOnUnexpectedShortArgumentWithValue() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withDatabase().parse( new String[]{ "-f=bob" } ) ); assertEquals( "unrecognized option: 'f'", incorrectUsage.getMessage() ); }
@Test void throwsOnUnexpectedLongArgumentWithValue() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withDatabase().parse( new String[]{ "--stacktrace=true" } ) ); assertEquals( "unrecognized option: 'stacktrace'", incorrectUsage.getMessage() ); }
@Test public void failIfSourceIsNotAStore() { File from = testDir.directory( "empty" ); String[] arguments = {"--mode=database", "--database=bar", "--from=" + from.getAbsolutePath()}; try { new DatabaseImporter( Args.parse( arguments ), Config.defaults(), new NullOutsideWorld() ); fail( "Should have thrown an exception." ); } catch ( IncorrectUsage e ) { assertThat( e.getMessage(), containsString( "does not contain a database" ) ); } }
@Test void printUnrecognizedClassifiers() throws Exception { String[] args = {"logs", "tx", "invalid"}; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> diagnosticsReportCommand.execute( args ) ); assertEquals( "Unknown classifier: invalid", incorrectUsage.getMessage() ); } }
@Test void allHasToBeOnlyClassifier() throws Exception { String[] args = {"all", "logs", "tx"}; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> diagnosticsReportCommand.execute( args ) ); assertEquals( "If you specify 'all' this has to be the only classifier. Found ['logs','tx'] as well.", incorrectUsage.getMessage() ); } }
@Test void throwsOnUnexpectedPositionalArgumentWhenExpectingSome() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withMandatoryPositionalArgument( 0, "first" ) .withOptionalPositionalArgument( 1, "second" ) .parse( new String[]{ "one", "two", "three", "four" } ) ); assertEquals( "unrecognized arguments: 'three four'", incorrectUsage.getMessage() ); }
@Test void throwsOnTooFewPositionalArguments() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withMandatoryPositionalArgument( 0, "first" ) .withOptionalPositionalArgument( 1, "second" ) .parse( new String[]{} ) ); assertEquals( "not enough arguments", incorrectUsage.getMessage() ); }
@Test public void requiresFromArgument() { String[] arguments = {"--mode=database", "--database=bar"}; try { new DatabaseImporter( Args.parse( arguments ), Config.defaults(), new NullOutsideWorld() ); fail( "Should have thrown an exception." ); } catch ( IncorrectUsage e ) { assertThat( e.getMessage(), containsString( "from" ) ); } }