@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 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 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" ) ); } }
@Test public void removesOldMessagesLog() throws Exception { File home = testDir.directory(); File from = provideStoreDirectory(); File oldMessagesLog = new File( from, "messages.log" ); assertTrue( oldMessagesLog.createNewFile() ); File destination = testDir.databaseDir(); String[] arguments = {"--mode=database", "--database=bar", "--from=" + from.getAbsolutePath()}; DatabaseImporter importer = new DatabaseImporter( Args.parse( arguments ), getConfigWith( home, "bar" ), new NullOutsideWorld() ); File messagesLog = new File( destination, "messages.log" ); importer.doImport(); assertFalse( messagesLog.exists() ); }
@Test public void copiesDatabaseFromOldLocationToNewLocation() throws Exception { File home = testDir.directory( "home" ); File from = provideStoreDirectory(); File destination = new File( new File( new File( home, "data" ), "databases" ), "bar" ); String[] arguments = {"--mode=database", "--database=bar", "--from=" + from.getAbsolutePath()}; DatabaseImporter importer = new DatabaseImporter( Args.parse( arguments ), getConfigWith( home, "bar" ), new NullOutsideWorld() ); assertThat( destination, not( isExistingDatabase() ) ); importer.doImport(); assertThat( destination, isExistingDatabase() ); }
@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 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 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" ) ); } }