Skip to content

Commit

Permalink
Make OutsideWorld closeable to be able to close used file system corr…
Browse files Browse the repository at this point in the history
…ectly.

Add closing verification test for case of standard close and exit.
  • Loading branch information
MishaDemianenko committed Nov 21, 2016
1 parent bb578e3 commit ba5524b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
7 changes: 7 additions & 0 deletions community/command-line/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.neo4j.commandline.admin;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
Expand All @@ -29,14 +30,17 @@

public class AdminTool
{
public static void main( String[] args )
public static void main( String[] args ) throws IOException
{
Path homeDir = Paths.get( System.getenv().getOrDefault( "NEO4J_HOME", "" ) );
Path configDir = Paths.get( System.getenv().getOrDefault( "NEO4J_CONF", "" ) );
boolean debug = System.getenv( "NEO4J_DEBUG" ) != null;

new AdminTool( CommandLocator.fromServiceLocator(), BlockerLocator.fromServiceLocator(), new RealOutsideWorld(),
debug ).execute( homeDir, configDir, args );
try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() )
{
new AdminTool( CommandLocator.fromServiceLocator(), BlockerLocator.fromServiceLocator(), outsideWorld,
debug ).execute( homeDir, configDir, args );
}
}

private final String scriptName = "neo4j-admin";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
*/
package org.neo4j.commandline.admin;

import java.io.Closeable;
import java.io.PrintStream;

import org.neo4j.io.fs.FileSystemAbstraction;

public interface OutsideWorld
public interface OutsideWorld extends Closeable
{
void stdOutLine( String text );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
*/
package org.neo4j.commandline.admin;

import java.io.IOException;
import java.io.PrintStream;

import org.neo4j.io.IOUtils;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;

Expand All @@ -43,6 +45,7 @@ public void stdErrLine( String text )
@Override
public void exit( int status )
{
IOUtils.closeAllSilently( this );
System.exit( status );
}

Expand All @@ -63,4 +66,10 @@ public PrintStream errorStream()
{
return System.err;
}

@Override
public void close() throws IOException
{
fileSystemAbstraction.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.neo4j.commandline.admin;

import java.io.IOException;
import java.io.PrintStream;

import org.neo4j.io.fs.FileSystemAbstraction;
Expand Down Expand Up @@ -56,4 +57,9 @@ public PrintStream errorStream()
{
throw new UnsupportedOperationException( "not implemented" );
}

@Override
public void close() throws IOException
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2002-2016 "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.commandline.admin;

import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;

import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.test.rule.system.SystemExitRule;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class RealOutsideWorldTest
{

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

@Test
public void closeFileSystemOnClose() throws Exception
{
RealOutsideWorld outsideWorld = new RealOutsideWorld();
FileSystemAbstraction fileSystemMock = mock( FileSystemAbstraction.class );
outsideWorld.fileSystemAbstraction = fileSystemMock;

outsideWorld.close();

verify( fileSystemMock ).close();
}

@Test
public void closeFilesystemOnExit() throws IOException
{
RealOutsideWorld outsideWorld = new RealOutsideWorld();
FileSystemAbstraction fileSystemMock = mock( FileSystemAbstraction.class );
outsideWorld.fileSystemAbstraction = fileSystemMock;

systemExitRule.expectExit( 0 );

outsideWorld.exit( 0 );

verify( fileSystemMock ).close();
}

}

0 comments on commit ba5524b

Please sign in to comment.