Skip to content

Commit

Permalink
Make exit codes into constants
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed Mar 17, 2017
1 parent 0c4907d commit 101a085
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
Expand Up @@ -32,6 +32,10 @@

public class AdminTool
{

static final int STATUS_SUCCESS = 0;
public static final int STATUS_ERROR = 1;

public static void main( String[] args ) throws IOException
{
Path homeDir = Paths.get( System.getenv().getOrDefault( "NEO4J_HOME", "" ) );
Expand Down Expand Up @@ -159,7 +163,7 @@ private void failure()

private void failure( String message, Exception e )
{
failure( message, e, 1 );
failure( message, e, STATUS_ERROR );
}

private void failure( String message, Exception e, int code )
Expand All @@ -179,6 +183,6 @@ private void failure( String message, int code )

private void success()
{
outsideWorld.exit( 0 );
outsideWorld.exit( STATUS_SUCCESS );
}
}
Expand Up @@ -38,6 +38,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.neo4j.commandline.admin.AdminTool.STATUS_ERROR;
import static org.neo4j.commandline.admin.AdminTool.STATUS_SUCCESS;

public class AdminToolTest
{
Expand All @@ -56,7 +58,7 @@ public void shouldExit0WhenEverythingWorks()
OutsideWorld outsideWorld = mock( OutsideWorld.class );
new AdminTool( new CannedLocator( new NullCommandProvider() ), new NullBlockerLocator(), outsideWorld, false )
.execute( null, null, "null" );
verify( outsideWorld ).exit( 0 );
verify( outsideWorld ).exit( STATUS_SUCCESS );
}

@Test
Expand All @@ -75,7 +77,7 @@ public void shouldProvideFeedbackWhenNoCommandIsProvided()
new AdminTool( new NullCommandLocator(), new NullBlockerLocator(), outsideWorld, false ).execute( null, null );
verify( outsideWorld ).stdErrLine( "you must provide a command" );
verify( outsideWorld ).stdErrLine( "usage: neo4j-admin <command>" );
verify( outsideWorld ).exit( 1 );
verify( outsideWorld ).exit( STATUS_ERROR );
}

@Test
Expand All @@ -89,7 +91,7 @@ public void shouldProvideFeedbackIfTheCommandThrowsARuntimeException()
new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false )
.execute( null, null, "exception" );
verify( outsideWorld ).stdErrLine( "unexpected error: the-exception-message" );
verify( outsideWorld ).exit( 1 );
verify( outsideWorld ).exit( STATUS_ERROR );
}

@Test
Expand Down Expand Up @@ -131,7 +133,7 @@ public void shouldProvideFeedbackIfTheCommandFails()
new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false )
.execute( null, null, "exception" );
verify( outsideWorld ).stdErrLine( "command failed: the-failure-message" );
verify( outsideWorld ).exit( 1 );
verify( outsideWorld ).exit( STATUS_ERROR );
}

@Test
Expand Down Expand Up @@ -174,7 +176,7 @@ public void shouldProvideFeedbackIfTheCommandReportsAUsageProblem()
.execute( null, null, "exception" );
InOrder inOrder = inOrder( outsideWorld );
inOrder.verify( outsideWorld ).stdErrLine( "the-usage-message" );
verify( outsideWorld ).exit( 1 );
verify( outsideWorld ).exit( STATUS_ERROR );
}

@Test
Expand All @@ -195,7 +197,7 @@ public void shouldBlockDumpIfABlockerSaysSo() throws IncorrectUsage
.execute( null, null, "command" );

verify( outsideWorld ).stdErrLine( "command failed: the explanation" );
verify( outsideWorld ).exit( 1 );
verify( outsideWorld ).exit( STATUS_ERROR );
}

@Test
Expand All @@ -220,7 +222,7 @@ public void shouldBlockDumpIfOneBlockerOutOfManySaysSo() throws IncorrectUsage
.execute( null, null, "command" );

verify( outsideWorld ).stdErrLine( "command failed: trueBlocker explanation" );
verify( outsideWorld ).exit( 1 );
verify( outsideWorld ).exit( STATUS_ERROR );
}

@Test
Expand Down Expand Up @@ -253,7 +255,7 @@ public void helpArgumentShouldAlwaysPrintHelp() throws CommandFailed, IncorrectU
verifyNoMoreInteractions( command );
verify( outsideWorld ).stdErrLine( "unknown argument: --help" );
verify( outsideWorld ).stdErrLine( "usage: neo4j-admin command " );
verify( outsideWorld ).exit( 1 );
verify( outsideWorld ).exit( STATUS_ERROR );
}

private CannedLocator cannedCommand( final String name, AdminCommand command )
Expand Down
Expand Up @@ -85,6 +85,8 @@ public static Arguments arguments()
return arguments;
}

static final int STATUS_CC_ERROR = 2;
static final int STATUS_CC_INCONSISTENT = 3;
static final int MAX_OLD_BACKUPS = 1000;
private final BackupService backupService;
private final Path homeDir;
Expand Down Expand Up @@ -249,7 +251,7 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed
if ( !ccResult.isSuccessful() )
{
throw new CommandFailed( String.format( "Inconsistencies found. See '%s' for details.",
ccResult.reportFile() ), 3 );
ccResult.reportFile() ), STATUS_CC_INCONSISTENT );
}
}
catch ( Throwable e )
Expand All @@ -258,7 +260,8 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed
{
throw (CommandFailed) e;
}
throw new CommandFailed( "Failed to do consistency check on backup: " + e.getMessage(), e, 2 );
throw new CommandFailed( "Failed to do consistency check on backup: " + e.getMessage(), e,
STATUS_CC_ERROR );
}
}

Expand Down
Expand Up @@ -68,6 +68,9 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.neo4j.backup.OnlineBackupCommand.MAX_OLD_BACKUPS;
import static org.neo4j.backup.OnlineBackupCommand.STATUS_CC_ERROR;
import static org.neo4j.backup.OnlineBackupCommand.STATUS_CC_INCONSISTENT;
import static org.neo4j.commandline.admin.AdminTool.STATUS_ERROR;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.cypher_planner;

public class OnlineBackupCommandTest
Expand Down Expand Up @@ -172,7 +175,7 @@ public void nonExistingBackupDirThrows() throws CommandFailed, IncorrectUsage, B
final String path = path( "/Idontexist/sasdfasdfa" );
expected.expect( CommandFailed.class );
expected.expectMessage( "Directory '" + path + "' does not exist." );
expected.expect( exitCode( 1 ) );
expected.expect( exitCode( STATUS_ERROR ) );
execute( backupDir( path ), "--name=mybackup" );
}

Expand Down Expand Up @@ -255,7 +258,7 @@ public void inconsistentCCIsReportedWithExitCode3() throws Exception

expected.expect( CommandFailed.class );
expected.expectMessage( "Inconsistencies found. See '" + path + "' for details." );
expected.expect( exitCode( 3 ) );
expected.expect( exitCode( STATUS_CC_INCONSISTENT ) );
execute( "--check-consistency=true", backupDir(), "--name=mybackup" );
}

Expand All @@ -269,7 +272,7 @@ public void errorInCCIsReportedWithExitCode2() throws Exception

expected.expect( CommandFailed.class );
expected.expectMessage( "Failed to do consistency check on backup: craassh" );
expected.expect( exitCode( 2 ) );
expected.expect( exitCode( STATUS_CC_ERROR ) );
execute( "--check-consistency=true", backupDir(), "--name=mybackup" );
}

Expand Down Expand Up @@ -370,7 +373,7 @@ public void failToRenameIsReported() throws Exception

expected.expectMessage( "Failed to move old backup out of the way: kaboom" );
expected.expect( CommandFailed.class );
expected.expect( exitCode( 1 ) );
expected.expect( exitCode( STATUS_ERROR ) );

execute( "--cc-report-dir=" + dir.getParent(), backupDir( dir.getParent() ),
"--name=" + dir.getName() );
Expand Down Expand Up @@ -424,7 +427,7 @@ public void shouldNotFallbackToFullIfSpecified() throws Exception

expected.expectMessage( "Backup failed: nah-ah" );
expected.expect( CommandFailed.class );
expected.expect( exitCode( 1 ) );
expected.expect( exitCode( STATUS_ERROR ) );

execute( "--fallback-to-full=false", backupDir( dir.getParent() ), "--name=" + dir.getName() );
}
Expand Down Expand Up @@ -474,7 +477,7 @@ public void renamingOldBackupIncrementsOnlySoFar() throws Exception

expected.expect( CommandFailed.class );
expected.expectMessage( "ailed to move old backup out of the way: too many old backups." );
expected.expect( exitCode( 1 ) );
expected.expect( exitCode( STATUS_ERROR ) );
execute( "--cc-report-dir=" + dir.getParent(), backupDir( dir.getParent() ), "--name=" + dir.getName() );
}

Expand Down Expand Up @@ -504,7 +507,7 @@ public void fullFailureIsReported() throws Exception

expected.expect( CommandFailed.class );
expected.expectMessage( "Backup failed: nope" );
expected.expect( exitCode( 1 ) );
expected.expect( exitCode( STATUS_ERROR ) );
execute( backupDir( dir.getParent() ), "--name=" + dir.getName() );
}

Expand All @@ -523,7 +526,7 @@ public void reportDirMustExist() throws Exception
final String path = path( "/aalivnmoimzlckmvPDK" );
expected.expect( CommandFailed.class );
expected.expectMessage( "Directory '" + path + "' does not exist." );
expected.expect( exitCode( 1 ) );
expected.expect( exitCode( STATUS_ERROR ) );
execute( "--check-consistency", backupDir(), "--name=mybackup",
"--cc-report-dir=" + path );
}
Expand Down

0 comments on commit 101a085

Please sign in to comment.