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
{
Class<?> clazz = testInstance.getClass();
Object instance = createInstance( context );
List<Field> declaredFields = getAllFields( clazz );
for ( Field declaredField : declaredFields )
{
if ( declaredField.isAnnotationPresent( Inject.class ) &&
getFieldType().equals( declaredField.getType() ) )
{
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() );
}

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

private Object createFieldInstance( ExtensionContext extensionContext )
private Object createInstance( ExtensionContext extensionContext )
{
Store store = getLocalStore( 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 IncorrectFormat( Path archive, IOException cause )
IncorrectFormat( Path archive, IOException cause )
{
super( archive.toString(), cause );
}
Expand Down
Expand Up @@ -19,8 +19,8 @@
*/
package org.neo4j.commandline.dbms;

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

import java.io.ByteArrayInputStream;
import java.io.File;
Expand All @@ -34,21 +34,22 @@
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Args;
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 static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.neo4j.helpers.collection.MapUtil.stringMap;

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

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

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.File;
Expand All @@ -43,30 +42,32 @@
import org.neo4j.commandline.admin.RealOutsideWorld;
import org.neo4j.diagnostics.DiagnosticsOfflineReportProvider;
import org.neo4j.diagnostics.DiagnosticsReportSource;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
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.fs.DefaultFileSystemRule;

import static org.hamcrest.CoreMatchers.containsString;
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.when;
import static org.neo4j.commandline.dbms.DiagnosticsReportCommand.DEFAULT_CLASSIFIERS;
import static org.neo4j.commandline.dbms.DiagnosticsReportCommand.describeClassifier;

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

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

@Before
public void setUp() throws Exception
@BeforeEach
void setUp() throws Exception
{
homeDir = testDirectory.directory( "home-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() );
}

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

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

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

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

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

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

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

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

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

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

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

@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
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
public void errorOnInvalidPid() throws Exception
void errorOnInvalidPid() throws Exception
{
expected.expect( CommandFailed.class );
expected.expectMessage( "Unable to parse --pid" );
String[] args = {"--pid=a", "all"};
try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() )
{
DiagnosticsReportCommand
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.