Skip to content

Commit

Permalink
Respect requests to keep failed test directory in TestDirectoryExtens…
Browse files Browse the repository at this point in the history
…ion(s)

Also, verify test extension lifecycle by starting custom junit runs
that validate that extension did the right thing on test failure/success.
  • Loading branch information
MishaDemianenko committed Oct 12, 2018
1 parent eacdd90 commit de47415
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 2 deletions.
6 changes: 6 additions & 0 deletions community/io/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ the relevant Commercial Agreement.
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.extension;

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

import java.io.File;

import org.neo4j.test.rule.TestDirectory;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.neo4j.test.extension.ExecutionSharedContext.FAILED_TEST_FILE_KEY;
import static org.neo4j.test.extension.ExecutionSharedContext.INSTANCE;
import static org.neo4j.test.extension.ExecutionSharedContext.SUCCESSFUL_TEST_FILE_KEY;

/**
* This test class name should not match default test name pattern since it should not be executed by default test launcher
* Its executed by custom test junit launcher to test extensions lifecycle
*/
@ExtendWith( {DefaultFileSystemExtension.class, TestDirectoryExtension.class} )
class DirectoryExtensionLifecycleVerification
{
@Inject
TestDirectory directory;

@Test
void executeAndCleanupDirectory()
{
File file = directory.createFile( "a" );
assertTrue( file.exists() );
INSTANCE.setValue( SUCCESSFUL_TEST_FILE_KEY, file );
}

@Test
void failAndKeepDirectory()
{
File file = directory.createFile( "b" );
INSTANCE.setValue( FAILED_TEST_FILE_KEY, file );
throw new RuntimeException( "simulate test failure" );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.extension;

import java.util.concurrent.ConcurrentHashMap;

public class ExecutionSharedContext
{
public static final String FAILED_TEST_FILE_KEY = "failedFileName";
public static final String SUCCESSFUL_TEST_FILE_KEY = "successfulFileName";
private static final ConcurrentHashMap<String,Object> context = new ConcurrentHashMap();

public static final ExecutionSharedContext INSTANCE = new ExecutionSharedContext();

private ExecutionSharedContext()
{
}

public <T> T getValue( String key )
{
return (T) context.get( key );
}

public void setValue( String key, Object value )
{
context.put( key, value );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void afterAll( ExtensionContext context )
{
try
{
testDirectory.complete( context.getExecutionException().isPresent() );
testDirectory.complete( !context.getExecutionException().isPresent() );
}
catch ( IOException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void beforeEach( ExtensionContext context ) throws Exception
public void afterEach( ExtensionContext context ) throws Exception
{
TestDirectory testDirectory = getStoredValue( context );
testDirectory.complete( context.getExecutionException().isPresent() );
testDirectory.complete( !context.getExecutionException().isPresent() );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

import java.io.File;
import java.nio.file.Path;
Expand All @@ -30,9 +34,15 @@
import org.neo4j.test.rule.TestDirectory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import static org.neo4j.test.extension.ExecutionSharedContext.FAILED_TEST_FILE_KEY;
import static org.neo4j.test.extension.ExecutionSharedContext.INSTANCE;
import static org.neo4j.test.extension.ExecutionSharedContext.SUCCESSFUL_TEST_FILE_KEY;

@ExtendWith( {DefaultFileSystemExtension.class, TestDirectoryExtension.class} )
class TestDirectoryExtensionTest
Expand Down Expand Up @@ -71,4 +81,32 @@ void createTestFile()
assertEquals( "a", file.getName() );
assertTrue( fileSystem.fileExists( file ) );
}

@Test
void failedTestShouldKeepDirectory()
{
assertNull( INSTANCE.getValue( FAILED_TEST_FILE_KEY ) );
execute( "failAndKeepDirectory" );
File failedFile = INSTANCE.getValue( FAILED_TEST_FILE_KEY );
assertNotNull( failedFile );
assertTrue( failedFile.exists() );
}

@Test
void successfulTestShouldCleanupDirectory()
{
assertNull( INSTANCE.getValue( SUCCESSFUL_TEST_FILE_KEY ) );
execute( "executeAndCleanupDirectory" );
File greenTestFail = INSTANCE.getValue( SUCCESSFUL_TEST_FILE_KEY );
assertNotNull( greenTestFail );
assertFalse( greenTestFail.exists() );
}

private static void execute( String failAndKeepDirectory )
{
LauncherDiscoveryRequest discoveryRequest = LauncherDiscoveryRequestBuilder.request().selectors(
selectMethod( DirectoryExtensionLifecycleVerification.class, failAndKeepDirectory ) ).build();
Launcher launcher = LauncherFactory.create();
launcher.execute( discoveryRequest );
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<scala.java.additional.args/>
<jersey.version>1.19.3</jersey.version>
<junit.version>5.2.0</junit.version>
<junit.platform.version>1.2.0</junit.platform.version>
<currentYear>2018</currentYear>
<cypher.compatibility.23>2.3.12</cypher.compatibility.23>
<cypher.compatibility.31>3.1.9</cypher.compatibility.31>
Expand Down

0 comments on commit de47415

Please sign in to comment.