Skip to content

Commit

Permalink
Migrate dbms module tests to junit 5.
Browse files Browse the repository at this point in the history
Tests cleanup.
Put annoying import report into correct test directory.

Allow junit extensions to instantiate and store a value in the context
without test defined fields.
For example, suppress output rule can be transparent and catch/print
output without any field in a particular test.
  • Loading branch information
MishaDemianenko committed Jul 20, 2018
1 parent 5bb1a4c commit daec76d
Show file tree
Hide file tree
Showing 14 changed files with 361 additions and 565 deletions.
Expand Up @@ -52,14 +52,15 @@ public void afterAll( ExtensionContext context )
public void postProcessTestInstance( Object testInstance, ExtensionContext context ) throws Exception public void postProcessTestInstance( Object testInstance, ExtensionContext context ) throws Exception
{ {
Class<?> clazz = testInstance.getClass(); Class<?> clazz = testInstance.getClass();
Object instance = createInstance( context );
List<Field> declaredFields = getAllFields( clazz ); List<Field> declaredFields = getAllFields( clazz );
for ( Field declaredField : declaredFields ) for ( Field declaredField : declaredFields )
{ {
if ( declaredField.isAnnotationPresent( Inject.class ) && if ( declaredField.isAnnotationPresent( Inject.class ) &&
getFieldType().equals( declaredField.getType() ) ) getFieldType().equals( declaredField.getType() ) )
{ {
declaredField.setAccessible( true ); declaredField.setAccessible( true );
declaredField.set( testInstance, createFieldInstance( context ) ); declaredField.set( testInstance, instance );
} }
} }
} }
Expand All @@ -74,7 +75,7 @@ void removeStoredValue( ExtensionContext context )
getLocalStore( context ).remove( getFieldKey(), getFieldType() ); getLocalStore( context ).remove( getFieldKey(), getFieldType() );
} }


Store getStore( ExtensionContext extensionContext, Namespace namespace ) static Store getStore( ExtensionContext extensionContext, Namespace namespace )
{ {
return extensionContext.getRoot().getStore( namespace ); return extensionContext.getRoot().getStore( namespace );
} }
Expand All @@ -84,7 +85,7 @@ private Store getLocalStore( ExtensionContext extensionContext )
return getStore( extensionContext, getNameSpace() ); return getStore( extensionContext, getNameSpace() );
} }


private Object createFieldInstance( ExtensionContext extensionContext ) private Object createInstance( ExtensionContext extensionContext )
{ {
Store store = getLocalStore( extensionContext ); Store store = getLocalStore( extensionContext );
return store.getOrComputeIfAbsent( getFieldKey(), (Function<String,Object>) s -> createField( extensionContext ) ); return store.getOrComputeIfAbsent( getFieldKey(), (Function<String,Object>) s -> createField( extensionContext ) );
Expand Down
Expand Up @@ -24,7 +24,7 @@


public class IncorrectFormat extends Exception public class IncorrectFormat extends Exception
{ {
public IncorrectFormat( Path archive, IOException cause ) IncorrectFormat( Path archive, IOException cause )
{ {
super( archive.toString(), cause ); super( archive.toString(), cause );
} }
Expand Down
Expand Up @@ -19,8 +19,8 @@
*/ */
package org.neo4j.commandline.dbms; package org.neo4j.commandline.dbms;


import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith;


import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
Expand All @@ -34,21 +34,22 @@
import org.neo4j.graphdb.factory.GraphDatabaseSettings; import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Args; import org.neo4j.helpers.Args;
import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.Config;
import org.neo4j.test.rule.SuppressOutput; import org.neo4j.test.extension.Inject;
import org.neo4j.test.extension.SuppressOutputExtension;
import org.neo4j.test.extension.TestDirectoryExtension;
import org.neo4j.test.rule.TestDirectory; import org.neo4j.test.rule.TestDirectory;


import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.neo4j.helpers.collection.MapUtil.stringMap; import static org.neo4j.helpers.collection.MapUtil.stringMap;


public class CsvImporterTest @ExtendWith( {TestDirectoryExtension.class, SuppressOutputExtension.class} )
class CsvImporterTest
{ {
@Rule @Inject
public final TestDirectory testDir = TestDirectory.testDirectory(); private TestDirectory testDir;
@Rule
public final SuppressOutput suppressOutput = SuppressOutput.suppressAll();


@Test @Test
public void writesReportToSpecifiedReportFile() throws Exception void writesReportToSpecifiedReportFile() throws Exception
{ {
File dbDir = testDir.directory( "db" ); File dbDir = testDir.directory( "db" );
File logDir = testDir.directory( "logs" ); File logDir = testDir.directory( "logs" );
Expand Down
Expand Up @@ -19,12 +19,11 @@
*/ */
package org.neo4j.commandline.dbms; package org.neo4j.commandline.dbms;


import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.jupiter.api.condition.JRE; import org.junit.jupiter.api.condition.JRE;
import org.junit.rules.ExpectedException; import org.junit.jupiter.api.extension.ExtendWith;


import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
Expand All @@ -43,30 +42,32 @@
import org.neo4j.commandline.admin.RealOutsideWorld; import org.neo4j.commandline.admin.RealOutsideWorld;
import org.neo4j.diagnostics.DiagnosticsOfflineReportProvider; import org.neo4j.diagnostics.DiagnosticsOfflineReportProvider;
import org.neo4j.diagnostics.DiagnosticsReportSource; import org.neo4j.diagnostics.DiagnosticsReportSource;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.Config;
import org.neo4j.test.rule.SuppressOutput; import org.neo4j.test.extension.DefaultFileSystemExtension;
import org.neo4j.test.extension.Inject;
import org.neo4j.test.extension.SuppressOutputExtension;
import org.neo4j.test.extension.TestDirectoryExtension;
import org.neo4j.test.rule.TestDirectory; import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;


import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.neo4j.commandline.dbms.DiagnosticsReportCommand.DEFAULT_CLASSIFIERS; import static org.neo4j.commandline.dbms.DiagnosticsReportCommand.DEFAULT_CLASSIFIERS;
import static org.neo4j.commandline.dbms.DiagnosticsReportCommand.describeClassifier; import static org.neo4j.commandline.dbms.DiagnosticsReportCommand.describeClassifier;


@ExtendWith( {TestDirectoryExtension.class, DefaultFileSystemExtension.class, SuppressOutputExtension.class} )
public class DiagnosticsReportCommandTest public class DiagnosticsReportCommandTest
{ {
@Rule @Inject
public SuppressOutput suppressOutput = SuppressOutput.suppressAll(); private TestDirectory testDirectory;
@Rule @Inject
public TestDirectory testDirectory = TestDirectory.testDirectory(); private DefaultFileSystemAbstraction fs;
@Rule
public ExpectedException expected = ExpectedException.none();
@Rule
public DefaultFileSystemRule fsRule = new DefaultFileSystemRule();


private Path homeDir; private Path homeDir;
private Path configDir; private Path configDir;
Expand All @@ -93,8 +94,8 @@ protected List<DiagnosticsReportSource> provideSources( Set<String> classifiers
} }
} }


@Before @BeforeEach
public void setUp() throws Exception void setUp() throws Exception
{ {
homeDir = testDirectory.directory( "home-dir" ).toPath(); homeDir = testDirectory.directory( "home-dir" ).toPath();
configDir = testDirectory.directory( "config-dir" ).toPath(); configDir = testDirectory.directory( "config-dir" ).toPath();
Expand All @@ -107,84 +108,77 @@ public void setUp() throws Exception
originalUserDir = System.setProperty( "user.dir", testDirectory.absolutePath().getAbsolutePath() ); originalUserDir = System.setProperty( "user.dir", testDirectory.absolutePath().getAbsolutePath() );
} }


@After @AfterEach
public void tearDown() void tearDown()
{ {
// Restore directory // Restore directory
System.setProperty( "user.dir", originalUserDir ); System.setProperty( "user.dir", originalUserDir );
} }


@Test @Test
public void exitIfConfigFileIsMissing() throws IOException, CommandFailed, IncorrectUsage void exitIfConfigFileIsMissing() throws IOException
{ {
Files.delete( configFile ); Files.delete( configFile );
String[] args = {"--list"}; String[] args = {"--list"};
try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() )
{ {
DiagnosticsReportCommand DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld );
diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld );


expected.expect( CommandFailed.class ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> diagnosticsReportCommand.execute( args ) );
expected.expectMessage( containsString( "Unable to find config file, tried: " ) ); assertThat( commandFailed.getMessage(), containsString( "Unable to find config file, tried: " ) );
diagnosticsReportCommand.execute( args );
} }
} }


@Test @Test
public void allHasToBeOnlyClassifier() throws Exception void allHasToBeOnlyClassifier() throws Exception
{ {
String[] args = {"all", "logs", "tx"}; String[] args = {"all", "logs", "tx"};
try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() )
{ {
DiagnosticsReportCommand DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld );
diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld );


expected.expect( IncorrectUsage.class ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> diagnosticsReportCommand.execute( args ) );
expected.expectMessage( assertEquals( "If you specify 'all' this has to be the only classifier. Found ['logs','tx'] as well.", incorrectUsage.getMessage() );
"If you specify 'all' this has to be the only classifier. Found ['logs','tx'] as well." );
diagnosticsReportCommand.execute( args );
} }
} }


@Test @Test
public void printUnrecognizedClassifiers() throws Exception void printUnrecognizedClassifiers() throws Exception
{ {
String[] args = {"logs", "tx", "invalid"}; String[] args = {"logs", "tx", "invalid"};
try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() )
{ {
DiagnosticsReportCommand DiagnosticsReportCommand
diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld );


expected.expect( IncorrectUsage.class ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> diagnosticsReportCommand.execute( args ) );
expected.expectMessage( "Unknown classifier: invalid" ); assertEquals( "Unknown classifier: invalid", incorrectUsage.getMessage() );
diagnosticsReportCommand.execute( args );
} }
} }


@SuppressWarnings( "ResultOfMethodCallIgnored" ) @SuppressWarnings( "ResultOfMethodCallIgnored" )
@Test @Test
public void defaultValuesShouldBeValidClassifiers() void defaultValuesShouldBeValidClassifiers()
{ {
for ( String classifier : DEFAULT_CLASSIFIERS ) for ( String classifier : DEFAULT_CLASSIFIERS )
{ {
describeClassifier( classifier ); describeClassifier( classifier );
} }


// Make sure the above actually catches bad classifiers // Make sure the above actually catches bad classifiers
expected.expect( IllegalArgumentException.class ); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, () -> describeClassifier( "invalid" ) );
expected.expectMessage( "Unknown classifier: invalid" ); assertEquals( "Unknown classifier: invalid", exception.getMessage() );
describeClassifier( "invalid" );
} }


@Test @Test
public void listShouldDisplayAllClassifiers() throws Exception void listShouldDisplayAllClassifiers() throws Exception
{ {
try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() )
{ {
PrintStream ps = new PrintStream( baos ); PrintStream ps = new PrintStream( baos );
String[] args = {"--list"}; String[] args = {"--list"};
OutsideWorld outsideWorld = mock( OutsideWorld.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class );
when( outsideWorld.fileSystem() ).thenReturn( fsRule.get() ); when( outsideWorld.fileSystem() ).thenReturn( fs );
when( outsideWorld.outStream() ).thenReturn( ps ); when( outsideWorld.outStream() ).thenReturn( ps );


DiagnosticsReportCommand DiagnosticsReportCommand
Expand All @@ -206,7 +200,7 @@ public void listShouldDisplayAllClassifiers() throws Exception
} }


@Test @Test
public void overrideDestination() throws Exception void overrideDestination() throws Exception
{ {
// because of https://bugs.openjdk.java.net/browse/JDK-8202127 and current surefire behaviour we need to have custom value for JRE >= 11 // because of https://bugs.openjdk.java.net/browse/JDK-8202127 and current surefire behaviour we need to have custom value for JRE >= 11
String toArgument = JRE.JAVA_11.isCurrentVersion() ? "--to=" + System.getProperty( "user.dir" ) + "/other/" : "--to=other/"; String toArgument = JRE.JAVA_11.isCurrentVersion() ? "--to=" + System.getProperty( "user.dir" ) + "/other/" : "--to=other/";
Expand All @@ -229,16 +223,15 @@ public void overrideDestination() throws Exception
} }


@Test @Test
public void errorOnInvalidPid() throws Exception void errorOnInvalidPid() throws Exception
{ {
expected.expect( CommandFailed.class );
expected.expectMessage( "Unable to parse --pid" );
String[] args = {"--pid=a", "all"}; String[] args = {"--pid=a", "all"};
try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() )
{ {
DiagnosticsReportCommand DiagnosticsReportCommand
diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld );
diagnosticsReportCommand.execute( args ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> diagnosticsReportCommand.execute( args ) );
assertEquals( "Unable to parse --pid", commandFailed.getMessage() );
} }
} }
} }

0 comments on commit daec76d

Please sign in to comment.