private void validateClassifiers( Set<String> availableClassifiers, Set<String> orphans ) throws IncorrectUsage { for ( String classifier : orphans ) { if ( !availableClassifiers.contains( classifier ) ) { throw new IncorrectUsage( "Unknown classifier: " + classifier ); } } }
private void badUsage( AdminCommand.Provider command, IncorrectUsage e ) { outsideWorld.stdErrLine( e.getMessage() ); outsideWorld.stdErrLine( "" ); usage.printUsageForCommand( command, outsideWorld::stdErrLine ); failure(); }
private void validate() throws IncorrectUsage { for ( String o : parsedArgs.asMap().keySet() ) { if ( !namedArgs.containsKey( o ) ) { throw new IncorrectUsage( format( "unrecognized option: '%s'", o ) ); } } long mandatoryPositionalArgs = positionalArgs.stream() .filter( o -> o instanceof MandatoryPositionalArgument ) .count(); if ( parsedArgs.orphans().size() < mandatoryPositionalArgs ) { throw new IncorrectUsage( "not enough arguments" ); } String excessArgs = parsedArgs.orphans().stream() .skip( positionalArgs.size() ) .collect( Collectors.joining( " " ) ); if ( !excessArgs.isEmpty() ) { throw new IncorrectUsage( format( "unrecognized arguments: '%s'", excessArgs ) ); } }
@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" ) ); }
DatabaseImporter( Args args, Config config, OutsideWorld outsideWorld ) throws IncorrectUsage { this.config = config; try { this.from = args.interpretOption( "from", Converters.mandatory(), Converters.toFile(), Validators.CONTAINS_EXISTING_DATABASE ); } catch ( IllegalArgumentException e ) { throw new IncorrectUsage( e.getMessage() ); } }
@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" ) ); }
@Override public void execute( String... args ) throws IncorrectUsage { if ( args.length > 0 ) { try { AdminCommand.Provider commandProvider = this.locator.findProvider( args[0] ); usage.printUsageForCommand( commandProvider, output ); } catch ( NoSuchElementException e ) { StringBuilder validCommands = new StringBuilder(); locator.getAllProviders() .forEach( commandProvider -> validCommands.append( commandProvider.name() ).append( " " ) ); throw new IncorrectUsage( format( "Unknown command: %s. Available commands are: %s\n", args[0], validCommands ) ); } } else { usage.print( output ); } } }
@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() ); }
throw new IncorrectUsage( "If you specify 'all' this has to be the only classifier. Found ['" + String.join( "','", classifiers ) + "'] as well." );
@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" ) ); } }
throw new IncorrectUsage( e.getMessage() ); throw new IncorrectUsage( e.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" ) ); } }
CsvImporter( Args args, Config databaseConfig, OutsideWorld outsideWorld ) throws IncorrectUsage { this.args = args; this.outsideWorld = outsideWorld; nodesFiles = extractInputFiles( args, "nodes", outsideWorld.errorStream() ); relationshipsFiles = extractInputFiles( args, "relationships", outsideWorld.errorStream() ); reportFileName = args.interpretOption( "report-file", withDefault( ImportCommand.DEFAULT_REPORT_FILE_NAME ), s -> s ); ignoreExtraColumns = args.getBoolean( "ignore-extra-columns", false ); ignoreDuplicateNodes = args.getBoolean( "ignore-duplicate-nodes", false ); ignoreBadRelationships = args.getBoolean( "ignore-missing-nodes", false ); try { validateInputFiles( nodesFiles, relationshipsFiles ); } catch ( IllegalArgumentException e ) { throw new IncorrectUsage( e.getMessage() ); } idType = args.interpretOption( "id-type", withDefault( IdType.STRING ), from -> IdType.valueOf( from.toUpperCase() ) ); inputEncoding = Charset.forName( args.get( "input-encoding", defaultCharset().name() ) ); highIO = args.getBoolean( "high-io", null, true ); // intentionally left as null if not specified this.databaseConfig = databaseConfig; }
if ( e.getMessage().equals( "Unknown classifier: threads" ) )
@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 throwsOnUnexpectedShortArgument() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withDatabase().parse( new String[]{ "-f" } ) ); assertEquals( "unrecognized option: 'f'", incorrectUsage.getMessage() ); }
throw new IncorrectUsage( e.getMessage() ); throw new IncorrectUsage( "Only one of '--" + ARG_DATABASE + "' and '--backup' can be specified." ); throw new IncorrectUsage( e.getMessage() );
@Test void throwsOnUnexpectedLongArgumentWithValue() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withDatabase().parse( new String[]{ "--stacktrace=true" } ) ); assertEquals( "unrecognized option: 'stacktrace'", incorrectUsage.getMessage() ); }
private void validateClassifiers( Set<String> availableClassifiers, Set<String> orphans ) throws IncorrectUsage { for ( String classifier : orphans ) { if ( !availableClassifiers.contains( classifier ) ) { throw new IncorrectUsage( "Unknown classifier: " + classifier ); } } }
@Test void throwsOnUnexpectedLongArgument() { IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> builder.withDatabase().parse( new String[]{"--stacktrace"} ) ); assertEquals( "unrecognized option: 'stacktrace'", incorrectUsage.getMessage() ); }