Skip to content

Commit

Permalink
fixed requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenag committed Feb 28, 2017
1 parent 5dec51c commit 49df904
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 44 deletions.
Expand Up @@ -34,6 +34,22 @@ public static AdminCommandSection general()
return GENERAL;
}

@Override
public int hashCode()
{
return this.printable().hashCode();
}

@Override
public boolean equals( Object other )
{
if ( other instanceof AdminCommandSection )
{
return this.printable().equals( ((AdminCommandSection) other).printable() );
}
return false;
}

public final void printAllCommandsUnderSection( Consumer<String> output, List<AdminCommand.Provider> providers )
{
output.accept( printable() );
Expand Down
Expand Up @@ -40,8 +40,6 @@ void printDetailed( Consumer<String> output )
{
for ( Arguments arguments : command.possibleArguments() )
{
//Arguments arguments = command.arguments();

String left = format( "usage: %s %s", scriptName, command.name() );

output.accept( Arguments.rightColumnFormatted( left, arguments.usage(), left.length() + 1 ) );
Expand Down
Expand Up @@ -72,7 +72,7 @@ private void printEnvironmentVariables( Consumer<String> output )

private void printCommands( Consumer<String> output )
{
Map<AdminCommandSection,List<AdminCommand.Provider>> groupedProviders = groupProvidersBySegment();
Map<AdminCommandSection,List<AdminCommand.Provider>> groupedProviders = groupProvidersBySection();

AdminCommandSection.general()
.printAllCommandsUnderSection( output, groupedProviders.remove( AdminCommandSection.general() ) );
Expand All @@ -82,10 +82,10 @@ private void printCommands( Consumer<String> output )
.forEach(entry -> entry.getKey().printAllCommandsUnderSection( output, entry.getValue() ) );
}

private Map<AdminCommandSection,List<AdminCommand.Provider>> groupProvidersBySegment()
private Map<AdminCommandSection,List<AdminCommand.Provider>> groupProvidersBySection()
{
List<AdminCommand.Provider> providers = new ArrayList<>();
commands.getAllProviders().forEach( providers::add );
return providers.stream().collect( Collectors.groupingBy( AdminCommand.Provider::commandSection ) );
return providers.stream().collect( Collectors.groupingBy( ( provider ) -> provider.commandSection() ) );
}
}
Expand Up @@ -25,10 +25,14 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -49,9 +53,9 @@ public void shouldPrintUsageForAllCommandsAlphabetically()
{
AdminCommandSection generalSection = AdminCommandSection.general();

List<AdminCommand.Provider> providers = asList( mockCommand( generalSection, "restore", "Restore" ),
mockCommand( generalSection, "bam", "A summary" ),
mockCommand( generalSection, "zzzz-last-one", "Another summary" ) );
List<AdminCommand.Provider> providers =
asList( mockCommand( "restore", "Restore" ), mockCommand( "bam", "A summary" ),
mockCommand( "zzzz-last-one", "Another summary" ) );
generalSection.printAllCommandsUnderSection( out, providers );

InOrder ordered = inOrder( out );
Expand All @@ -65,12 +69,55 @@ public void shouldPrintUsageForAllCommandsAlphabetically()
ordered.verifyNoMoreInteractions();
}

private AdminCommand.Provider mockCommand( AdminCommandSection section, String name, String summary )
@Test
public void equalsUsingReflection() throws Exception
{
assertTrue( AdminCommandSection.general().equals( new TestGeneralSection() ) );
assertFalse( AdminCommandSection.general().equals( new TestAnotherGeneralSection() ) );
}

@Test
public void hashCodeUsingReflection() throws Exception
{
TestGeneralSection testGeneralSection = new TestGeneralSection();
TestAnotherGeneralSection testAnotherGeneralSection = new TestAnotherGeneralSection();
HashMap<AdminCommandSection,String> map = new HashMap<>();
map.put( AdminCommandSection.general(), "General-Original" );
map.put( testGeneralSection, "General-Test" );
map.put( testAnotherGeneralSection, "General-AnotherTest" );

assertEquals( 2, map.size() );
assertEquals( "General-Test", map.get( AdminCommandSection.general() ) );
assertEquals( "General-Test", map.get( testGeneralSection ) );
assertEquals( "General-AnotherTest", map.get( testAnotherGeneralSection ) );
}

private class TestGeneralSection extends AdminCommandSection
{

@Override
public String printable()
{
return "General";
}
}

private class TestAnotherGeneralSection extends AdminCommandSection
{

@Override
public String printable()
{
return "Another Section";
}
}

private AdminCommand.Provider mockCommand( String name, String summary )
{
AdminCommand.Provider commandProvider = mock( AdminCommand.Provider.class );
when( commandProvider.name() ).thenReturn( name );
when( commandProvider.summary() ).thenReturn( summary );
when( commandProvider.commandSection() ).thenReturn( AdminCommandSection.general() );
return commandProvider;
AdminCommand.Provider commandProvider = mock( AdminCommand.Provider.class );
when( commandProvider.name() ).thenReturn( name );
when( commandProvider.summary() ).thenReturn( summary );
when( commandProvider.commandSection() ).thenReturn( AdminCommandSection.general() );
return commandProvider;
}
}
Expand Up @@ -139,14 +139,6 @@ public void testAdminUsage() throws Exception
}
}

private AdminCommand.Provider mockCommand( String name )
{
AdminCommand.Provider commandProvider = mock( AdminCommand.Provider.class );
when( commandProvider.name() ).thenReturn( name );
when( commandProvider.commandSection() ).thenReturn( AdminCommandSection.general() );
return commandProvider;
}

@Test
public void showsArgumentsAndDescriptionForSpecifiedCommand() throws Exception
{
Expand Down Expand Up @@ -176,4 +168,12 @@ public void showsArgumentsAndDescriptionForSpecifiedCommand() throws Exception
baos.toString() );
}
}

private AdminCommand.Provider mockCommand( String name )
{
AdminCommand.Provider commandProvider = mock( AdminCommand.Provider.class );
when( commandProvider.name() ).thenReturn( name );
when( commandProvider.commandSection() ).thenReturn( AdminCommandSection.general() );
return commandProvider;
}
}
Expand Up @@ -49,7 +49,7 @@ public void setUp()
public void shouldPrintUsageForACommand() throws Exception
{
// given
AdminCommand.Provider commandProvier = mockCommand();
AdminCommand.Provider commandProvier = mockCommand( "bam", "A summary", AdminCommandSection.general() );
AdminCommand.Provider[] commands = new AdminCommand.Provider[]{commandProvier};
final Usage usage = new Usage( "neo4j-admin", new CannedLocator( commands ) );

Expand All @@ -66,7 +66,8 @@ public void shouldPrintUsageForACommand() throws Exception
@Test
public void shouldPrintUsageWithConfiguration()
{
AdminCommand.Provider[] commands = new AdminCommand.Provider[]{mockCommand()};
AdminCommand.Provider[] commands =
new AdminCommand.Provider[]{mockCommand( "bam", "A summary", AdminCommandSection.general() )};
final Usage usage = new Usage( "neo4j-admin", new CannedLocator( commands ) );
usage.print( out );

Expand All @@ -91,15 +92,57 @@ public void shouldPrintUsageWithConfiguration()
ordered.verifyNoMoreInteractions();
}

private AdminCommand.Provider mockCommand()
@Test
public void commandsUnderSameAdminCommandSectionPrintableSectionShouldAppearTogether()
{
AdminCommand.Provider[] commands = new AdminCommand.Provider[]{
mockCommand( "first-command", "first-command", AdminCommandSection.general() ),
mockCommand( "second-command", "second-command", new TestGeneralSection() )};
final Usage usage = new Usage( "neo4j-admin", new CannedLocator( commands ) );
usage.print( out );

InOrder ordered = inOrder( out );
ordered.verify( out ).accept( "usage: neo4j-admin <command>" );
ordered.verify( out ).accept( "" );
ordered.verify( out ).accept( "Manage your Neo4j instance." );
ordered.verify( out ).accept( "" );

ordered.verify( out ).accept( "environment variables:" );
ordered.verify( out ).accept( " NEO4J_CONF Path to directory which contains neo4j.conf." );
ordered.verify( out ).accept( " NEO4J_DEBUG Set to anything to enable debug output." );
ordered.verify( out ).accept( " NEO4J_HOME Neo4j home directory." );
ordered.verify( out ).accept( "" );

ordered.verify( out ).accept( "available commands:" );
ordered.verify( out ).accept( "General" );
ordered.verify( out ).accept( " first-command" );
ordered.verify( out ).accept( " first-command" );
ordered.verify( out ).accept( " second-command" );
ordered.verify( out ).accept( " second-command" );
ordered.verify( out ).accept( "" );
ordered.verify( out ).accept( "Use neo4j-admin help <command> for more details." );
ordered.verifyNoMoreInteractions();
}

private class TestGeneralSection extends AdminCommandSection
{

@Override
public String printable()
{
return "General";
}
}

private AdminCommand.Provider mockCommand( String name, String summary, AdminCommandSection section )
{
AdminCommand.Provider commandProvider = mock( AdminCommand.Provider.class );
when( commandProvider.name() ).thenReturn( "bam" );
when( commandProvider.summary() ).thenReturn( "A summary" );
when( commandProvider.name() ).thenReturn( name );
when( commandProvider.summary() ).thenReturn( summary );
when( commandProvider.allArguments() ).thenReturn( Arguments.NO_ARGS );
when( commandProvider.possibleArguments() ).thenReturn( Collections.singletonList( Arguments.NO_ARGS ) );
when( commandProvider.description() ).thenReturn( "description" );
when( commandProvider.commandSection() ).thenReturn( AdminCommandSection.general() );
when( commandProvider.commandSection() ).thenReturn( section );
return commandProvider;
}
}
Expand Up @@ -57,7 +57,7 @@ public String summary()
@Override
public AdminCommandSection commandSection()
{
return ManageOffineBackupCommandSection.instance();
return OffineBackupCommandSection.instance();
}

@Override
Expand Down
Expand Up @@ -58,7 +58,7 @@ public String summary()
@Override
public AdminCommandSection commandSection()
{
return ManageOffineBackupCommandSection.instance();
return OffineBackupCommandSection.instance();
}

@Override
Expand Down
Expand Up @@ -21,18 +21,18 @@

import org.neo4j.commandline.admin.AdminCommandSection;

public class ManageOffineBackupCommandSection extends AdminCommandSection
public class OffineBackupCommandSection extends AdminCommandSection
{
private static final ManageOffineBackupCommandSection manageOffineBackupCommandSection = new ManageOffineBackupCommandSection();
private static final OffineBackupCommandSection OFFINE_BACKUP_COMMAND_SECTION = new OffineBackupCommandSection();

public static AdminCommandSection instance()
{
return manageOffineBackupCommandSection;
return OFFINE_BACKUP_COMMAND_SECTION;
}

@Override
public String printable()
{
return "Manage offline backup";
return "Offline backup";
}
}
Expand Up @@ -21,19 +21,19 @@

import org.neo4j.commandline.admin.AdminCommandSection;

public class ManageOnlineBackupCommandSection extends AdminCommandSection
public class OnlineBackupCommandSection extends AdminCommandSection
{
private static final ManageOnlineBackupCommandSection manageOnlineBackupCommandSection = new ManageOnlineBackupCommandSection();
private static final OnlineBackupCommandSection ONLINE_BACKUP_COMMAND_SECTION = new OnlineBackupCommandSection();

public static AdminCommandSection instance()
{
return manageOnlineBackupCommandSection;
return ONLINE_BACKUP_COMMAND_SECTION;
}

@Override
public String printable()
{
return "Manage online backup";
return "Online backup";
}

}
Expand Up @@ -21,7 +21,7 @@

import java.nio.file.Path;

import org.neo4j.ManageOnlineBackupCommandSection;
import org.neo4j.OnlineBackupCommandSection;
import org.neo4j.commandline.admin.AdminCommand;
import org.neo4j.commandline.admin.AdminCommandSection;
import org.neo4j.commandline.admin.OutsideWorld;
Expand Down Expand Up @@ -58,7 +58,7 @@ public String summary()
@Override
public AdminCommandSection commandSection()
{
return ManageOnlineBackupCommandSection.instance();
return OnlineBackupCommandSection.instance();
}

@Override
Expand Down
Expand Up @@ -21,7 +21,7 @@

import java.nio.file.Path;

import org.neo4j.ManageOnlineBackupCommandSection;
import org.neo4j.OnlineBackupCommandSection;
import org.neo4j.commandline.admin.AdminCommand;
import org.neo4j.commandline.admin.AdminCommandSection;
import org.neo4j.commandline.admin.OutsideWorld;
Expand Down Expand Up @@ -55,7 +55,7 @@ public String summary()
@Override
public AdminCommandSection commandSection()
{
return ManageOnlineBackupCommandSection.instance();
return OnlineBackupCommandSection.instance();
}

@Override
Expand Down
Expand Up @@ -65,12 +65,12 @@ public void verifyUsageMatchesExpectedCommands() throws Exception
"Clustering\n" +
" unbind\n" +
" Removes cluster state data for the specified database.\n" +
"Manage offline backup\n" +
"Offline backup\n" +
" dump\n" +
" Dump a database into a single-file archive.\n" +
" load\n" +
" Load a database from an archive created with the dump command.\n" +
"Manage online backup\n" +
"Online backup\n" +
" backup\n" +
" Perform an online backup from a running Neo4j enterprise server.\n" +
" restore\n" +
Expand Down

0 comments on commit 49df904

Please sign in to comment.