Skip to content

Commit

Permalink
Cleanup powermock dependencies
Browse files Browse the repository at this point in the history
Remove powermock usages, switch to mockito in all cases.
Introduce SystemExitRule to be able to verify System.exit calls.
Remove exit handles.
  • Loading branch information
MishaDemianenko committed Oct 3, 2015
1 parent 0aacb36 commit 5ba13ab
Show file tree
Hide file tree
Showing 16 changed files with 664 additions and 181 deletions.
Expand Up @@ -52,24 +52,10 @@ public class ConsistencyCheckTool
private static final String CONFIG = "config";
private static final String PROP_OWNER = "propowner";

public interface ExitHandle
{
ExitHandle SYSTEM_EXIT = new ExitHandle()
{
@Override
public void pull()
{
System.exit( 1 );
}
};

void pull();
}

public static void main( String[] args ) throws IOException
{
ConsistencyCheckTool tool = new ConsistencyCheckTool( new ConsistencyCheckService(), new GraphDatabaseFactory(),
new DefaultFileSystemAbstraction(), System.err, ExitHandle.SYSTEM_EXIT );
new DefaultFileSystemAbstraction(), System.err );
try
{
tool.run( args );
Expand All @@ -83,17 +69,15 @@ public static void main( String[] args ) throws IOException
private final ConsistencyCheckService consistencyCheckService;
private final GraphDatabaseFactory dbFactory;
private final PrintStream systemError;
private final ExitHandle exitHandle;
private final FileSystemAbstraction fs;

public ConsistencyCheckTool( ConsistencyCheckService consistencyCheckService,
GraphDatabaseFactory dbFactory, FileSystemAbstraction fs, PrintStream systemError, ExitHandle exitHandle )
GraphDatabaseFactory dbFactory, FileSystemAbstraction fs, PrintStream systemError )
{
this.consistencyCheckService = consistencyCheckService;
this.dbFactory = dbFactory;
this.fs = fs;
this.systemError = systemError;
this.exitHandle = exitHandle;
}

public void run( String... args ) throws ToolFailureException, IOException
Expand Down Expand Up @@ -134,7 +118,7 @@ private void attemptRecoveryOrCheckStateOfLogicalLogs( Args arguments, File stor
"Consistency checking will continue, abort if you wish to perform recovery first.",
"To perform recovery before checking consistency, use the '--recovery' flag." ) );

exitHandle.pull();
exit();
}
}
catch ( IOException e )
Expand Down Expand Up @@ -211,7 +195,12 @@ void exitTool()
getCause().printStackTrace( System.err );
}

exitHandle.pull();
exit();
}
}

private static void exit()
{
System.exit( 1 );
}
}
Expand Up @@ -44,6 +44,7 @@
import org.neo4j.legacy.consistency.checking.full.TaskExecutionOrder;
import org.neo4j.logging.LogProvider;
import org.neo4j.test.EphemeralFileSystemRule;
import org.neo4j.test.SystemExitRule;
import org.neo4j.test.TargetDirectory;
import org.neo4j.test.TestGraphDatabaseFactory;

Expand All @@ -65,6 +66,10 @@

public class ConsistencyCheckToolTest
{

@Rule
public SystemExitRule systemExitRule = SystemExitRule.none();

@Test
public void runsConsistencyCheck() throws Exception
{
Expand Down Expand Up @@ -185,7 +190,7 @@ public void shouldExecuteRecoveryWhenStoreWasNonCleanlyShutdown() throws Excepti
monitors.addMonitorListener( listener );

ConsistencyCheckTool consistencyCheckTool =
newConsistencyCheckToolWith( monitors, mock( ConsistencyCheckTool.ExitHandle.class ), fs.get() );
newConsistencyCheckToolWith( monitors, fs.get() );

// When
consistencyCheckTool.run( "-recovery", storeDirectory.graphDbDir().getAbsolutePath() );
Expand All @@ -199,22 +204,21 @@ public void shouldExecuteRecoveryWhenStoreWasNonCleanlyShutdown() throws Excepti
public void shouldExitWhenRecoveryNeededButRecoveryFalseOptionSpecified() throws Exception
{
// Given
systemExitRule.expectExit( 1 );
File storeDir = storeDirectory.graphDbDir();
EphemeralFileSystemAbstraction fileSystem = createDataBaseWithStateThatNeedsRecovery( storeDir );

Monitors monitors = new Monitors();
PhysicalLogFile.Monitor listener = mock( PhysicalLogFile.Monitor.class );
monitors.addMonitorListener( listener );

ConsistencyCheckTool.ExitHandle exitHandle = mock( ConsistencyCheckTool.ExitHandle.class );
ConsistencyCheckTool consistencyCheckTool = newConsistencyCheckToolWith( monitors, exitHandle, fileSystem );
ConsistencyCheckTool consistencyCheckTool = newConsistencyCheckToolWith( monitors, fileSystem );

// When
consistencyCheckTool.run( "-recovery=false", storeDir.getAbsolutePath() );

// Then
verifyZeroInteractions( listener );
verify( exitHandle ).pull();
}

private EphemeralFileSystemAbstraction createDataBaseWithStateThatNeedsRecovery( File storeDir )
Expand Down Expand Up @@ -252,7 +256,7 @@ private void createGraphDbAndKillIt()
}

private ConsistencyCheckTool newConsistencyCheckToolWith( Monitors monitors,
ConsistencyCheckTool.ExitHandle exitHandle, FileSystemAbstraction fileSystem ) throws IOException
FileSystemAbstraction fileSystem ) throws IOException
{
GraphDatabaseFactory graphDbFactory = new TestGraphDatabaseFactory()
{
Expand All @@ -264,14 +268,14 @@ public GraphDatabaseService newEmbeddedDatabase( File storeDir )
}.setFileSystem( fileSystem ).setMonitors( monitors );

return new ConsistencyCheckTool( mock( ConsistencyCheckService.class ),
graphDbFactory, fileSystem, mock( PrintStream.class ), exitHandle );
graphDbFactory, fileSystem, mock( PrintStream.class ) );
}

private ConsistencyCheckTool newConsistencyCheckToolWith ( ConsistencyCheckService
consistencyCheckService, PrintStream systemError )
{
return new ConsistencyCheckTool( consistencyCheckService, new GraphDatabaseFactory(),
new DefaultFileSystemAbstraction(), systemError, ConsistencyCheckTool.ExitHandle.SYSTEM_EXIT );
new DefaultFileSystemAbstraction(), systemError );
}

@Rule
Expand Down
Expand Up @@ -39,12 +39,10 @@
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.pagecache.StandalonePageCacheFactory;
import org.neo4j.kernel.impl.recovery.RecoveryRequiredChecker;
import org.neo4j.legacy.consistency.ConsistencyCheckTool.ExitHandle;
import org.neo4j.logging.FormattedLogProvider;
import org.neo4j.logging.LogProvider;

import static java.lang.System.currentTimeMillis;

import static org.neo4j.helpers.Args.jarUsage;
import static org.neo4j.helpers.Strings.joinAsLines;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
Expand All @@ -66,7 +64,7 @@ public class ConsistencyCheckTool
public static void main( String[] args ) throws IOException
{
ConsistencyCheckTool tool = new ConsistencyCheckTool( new ConsistencyCheckService(), new GraphDatabaseFactory(),
new DefaultFileSystemAbstraction(), System.err, ExitHandle.SYSTEM_EXIT );
new DefaultFileSystemAbstraction(), System.err );
try
{
tool.run( args );
Expand All @@ -80,17 +78,15 @@ public static void main( String[] args ) throws IOException
private final ConsistencyCheckService consistencyCheckService;
private final GraphDatabaseFactory dbFactory;
private final PrintStream systemError;
private final ExitHandle exitHandle;
private final FileSystemAbstraction fs;

ConsistencyCheckTool( ConsistencyCheckService consistencyCheckService,
GraphDatabaseFactory dbFactory, FileSystemAbstraction fs, PrintStream systemError, ExitHandle exitHandle )
GraphDatabaseFactory dbFactory, FileSystemAbstraction fs, PrintStream systemError )
{
this.consistencyCheckService = consistencyCheckService;
this.dbFactory = dbFactory;
this.fs = fs;
this.systemError = systemError;
this.exitHandle = exitHandle;
}

void run( String... args ) throws ToolFailureException, IOException
Expand Down Expand Up @@ -131,10 +127,7 @@ private void runLegacyConsistencyChecker( String[] args ) throws ToolFailureExce
{
org.neo4j.legacy.consistency.ConsistencyCheckTool legacyTool = new org.neo4j.legacy.consistency.ConsistencyCheckTool(
new org.neo4j.legacy.consistency.ConsistencyCheckService(),
dbFactory,
fs,
systemError,
exitHandle );
dbFactory, fs, systemError);
legacyTool.run( args );
}
catch ( org.neo4j.legacy.consistency.ConsistencyCheckTool.ToolFailureException e )
Expand Down Expand Up @@ -182,7 +175,7 @@ private void attemptRecoveryOrCheckStateOfLogicalLogs( Args arguments, File stor
"Consistency checking will continue, abort if you wish to perform recovery first.",
"To perform recovery before checking consistency, use the '--recovery' flag." ) );

exitHandle.pull();
exit();
}
}
catch ( IOException e )
Expand Down Expand Up @@ -264,7 +257,12 @@ void exitTool()
getCause().printStackTrace( System.err );
}

exitHandle.pull();
exit();
}
}

private static void exit()
{
System.exit( 1 );
}
}
Expand Up @@ -47,9 +47,9 @@
import org.neo4j.kernel.impl.transaction.log.PhysicalLogFile;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.kernel.recovery.Recovery;
import org.neo4j.legacy.consistency.ConsistencyCheckTool.ExitHandle;
import org.neo4j.logging.LogProvider;
import org.neo4j.test.EphemeralFileSystemRule;
import org.neo4j.test.SystemExitRule;
import org.neo4j.test.TargetDirectory;
import org.neo4j.test.TestGraphDatabaseFactory;

Expand All @@ -66,7 +66,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;

import static org.neo4j.consistency.ConsistencyCheckTool.EXPERIMENTAL;
import static org.neo4j.graphdb.DynamicLabel.label;
import static org.neo4j.helpers.ArrayUtil.concat;
Expand All @@ -77,6 +76,9 @@ public class ConsistencyCheckToolTest
{
private final boolean experimental;

@Rule
public SystemExitRule systemExitRule = SystemExitRule.none();

@Parameters( name = "Experimental:{0}" )
public static Collection<Object[]> data()
{
Expand Down Expand Up @@ -217,7 +219,7 @@ public void shouldExecuteRecoveryWhenStoreWasNonCleanlyShutdown() throws Excepti
monitors.addMonitorListener( listener );

// When
runConsistencyCheckToolWith( monitors, mock( ExitHandle.class ), fs.get(),
runConsistencyCheckToolWith( monitors, fs.get(),
"-recovery", storeDirectory.graphDbDir().getAbsolutePath() );

// Then
Expand All @@ -229,22 +231,19 @@ public void shouldExecuteRecoveryWhenStoreWasNonCleanlyShutdown() throws Excepti
public void shouldExitWhenRecoveryNeededButRecoveryFalseOptionSpecified() throws Exception
{
// Given
systemExitRule.expectExit( 1 );
File storeDir = storeDirectory.graphDbDir();
EphemeralFileSystemAbstraction fileSystem = createDataBaseWithStateThatNeedsRecovery( storeDir );

Monitors monitors = new Monitors();
PhysicalLogFile.Monitor listener = mock( PhysicalLogFile.Monitor.class );
monitors.addMonitorListener( listener );

ExitHandle exitHandle = mock( ExitHandle.class );

// When
runConsistencyCheckToolWith( monitors, exitHandle, fileSystem,
"-recovery=false", storeDir.getAbsolutePath() );
runConsistencyCheckToolWith( monitors, fileSystem, "-recovery=false", storeDir.getAbsolutePath() );

// Then
verifyZeroInteractions( listener );
verify( exitHandle ).pull();
}

private EphemeralFileSystemAbstraction createDataBaseWithStateThatNeedsRecovery( File storeDir )
Expand Down Expand Up @@ -281,8 +280,7 @@ private void createGraphDbAndKillIt()
fs.snapshot( shutdownDbAction( db ) );
}

private void runConsistencyCheckToolWith( Monitors monitors,
ExitHandle exitHandle, FileSystemAbstraction fileSystem, String... args )
private void runConsistencyCheckToolWith( Monitors monitors, FileSystemAbstraction fileSystem, String... args )
throws IOException, ToolFailureException
{
GraphDatabaseFactory graphDbFactory = new TestGraphDatabaseFactory()
Expand All @@ -295,7 +293,7 @@ public GraphDatabaseService newEmbeddedDatabase( File storeDir )
}.setFileSystem( fileSystem ).setMonitors( monitors );

new ConsistencyCheckTool( mock( ConsistencyCheckService.class ),
graphDbFactory, fileSystem, mock( PrintStream.class ), exitHandle ).run( augment( args ) );
graphDbFactory, fileSystem, mock( PrintStream.class ) ).run( augment( args ) );
}

private String[] augment( String[] args )
Expand All @@ -307,7 +305,7 @@ private void runConsistencyCheckToolWith ( ConsistencyCheckService
consistencyCheckService, PrintStream systemError, String... args ) throws ToolFailureException, IOException
{
new ConsistencyCheckTool( consistencyCheckService, new GraphDatabaseFactory(),
new DefaultFileSystemAbstraction(), systemError, ExitHandle.SYSTEM_EXIT )
new DefaultFileSystemAbstraction(), systemError )
.run( augment( args ) );
}

Expand Down
5 changes: 0 additions & 5 deletions community/kernel/pom.xml
Expand Up @@ -336,11 +336,6 @@ the relevant Commercial Agreement.
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>geronimo-spec</groupId>
Expand Down
Expand Up @@ -30,9 +30,10 @@

import static org.junit.Assert.fail;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.kernel.impl.store.record.UniquePropertyConstraintRule.uniquenessConstraintRule;
import static org.powermock.api.mockito.PowerMockito.mock;


public class IntegrityValidatorTest
{
Expand Down
41 changes: 41 additions & 0 deletions community/kernel/src/test/java/org/neo4j/test/SystemExitError.java
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.test;

/**
* Error that used by {@link TestSecurityManager} as notification to {@link SystemExitRule}
* that System.exit call has happened.
*
* Should not be used anywhere except tests.
*/
class SystemExitError extends Error
{
private int statusCode;

public SystemExitError( int statusCode )
{
this.statusCode = statusCode;
}

public int getStatusCode()
{
return statusCode;
}
}

0 comments on commit 5ba13ab

Please sign in to comment.