Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update io module test to junit 5. Introduce common extensions.
Rewrite all, except parametrized tests, in junit 5. Introduce common test extensions. Introduce Inject annotation to inject test resources from custom extensions.
- Loading branch information
1 parent
63f0c76
commit cf0d91c
Showing
61 changed files
with
4,623 additions
and
4,115 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
community/common/src/test/java/org/neo4j/test/extension/Inject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target( FIELD ) | ||
@Retention( RUNTIME ) | ||
public @interface Inject | ||
{ | ||
} |
105 changes: 105 additions & 0 deletions
105
community/common/src/test/java/org/neo4j/test/extension/StatefullFieldExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* 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.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Store; | ||
import org.junit.jupiter.api.extension.TestInstancePostProcessor; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public abstract class StatefullFieldExtension<T> implements TestInstancePostProcessor, AfterAllCallback | ||
{ | ||
protected abstract String getFieldKey(); | ||
|
||
protected abstract Class<T> getFieldType(); | ||
|
||
protected abstract T createField( ExtensionContext extensionContext ); | ||
|
||
protected abstract Namespace getNameSpace(); | ||
|
||
@Override | ||
public void afterAll( ExtensionContext context ) | ||
{ | ||
removeStoredValue( context ); | ||
} | ||
|
||
@Override | ||
public void postProcessTestInstance( Object testInstance, ExtensionContext context ) throws Exception | ||
{ | ||
Class<?> clazz = testInstance.getClass(); | ||
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 ) ); | ||
} | ||
} | ||
} | ||
|
||
T getStoredValue( ExtensionContext context ) | ||
{ | ||
return getLocalStore( context ).get( getFieldKey(), getFieldType() ); | ||
} | ||
|
||
void removeStoredValue( ExtensionContext context ) | ||
{ | ||
getLocalStore( context ).remove( getFieldKey(), getFieldType() ); | ||
} | ||
|
||
Store getStore( ExtensionContext extensionContext, Namespace namespace ) | ||
{ | ||
return extensionContext.getRoot().getStore( namespace ); | ||
} | ||
|
||
private Store getLocalStore( ExtensionContext extensionContext ) | ||
{ | ||
return getStore( extensionContext, getNameSpace() ); | ||
} | ||
|
||
private Object createFieldInstance( ExtensionContext extensionContext ) | ||
{ | ||
Store store = getLocalStore( extensionContext ); | ||
return store.getOrComputeIfAbsent( getFieldKey(), (Function<String,Object>) s -> createField( extensionContext ) ); | ||
} | ||
|
||
private static List<Field> getAllFields( Class<?> baseClazz ) | ||
{ | ||
ArrayList<Field> fields = new ArrayList<>(); | ||
Class<?> clazz = baseClazz; | ||
do | ||
{ | ||
Collections.addAll( fields, clazz.getDeclaredFields() ); | ||
clazz = clazz.getSuperclass(); | ||
} | ||
while ( clazz != null ); | ||
return fields; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
community/common/src/test/java/org/neo4j/test/extension/SuppressOutputExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
|
||
import org.neo4j.test.rule.SuppressOutput; | ||
|
||
public class SuppressOutputExtension extends StatefullFieldExtension<SuppressOutput> implements BeforeEachCallback, AfterEachCallback | ||
{ | ||
private static final String SUPPRESS_OUTPUT = "suppressOutput"; | ||
private static final Namespace SUPPRESS_OUTPUT_NAMESPACE = Namespace.create( SUPPRESS_OUTPUT ); | ||
|
||
@Override | ||
protected String getFieldKey() | ||
{ | ||
return SUPPRESS_OUTPUT; | ||
} | ||
|
||
@Override | ||
protected Class<SuppressOutput> getFieldType() | ||
{ | ||
return SuppressOutput.class; | ||
} | ||
|
||
@Override | ||
protected SuppressOutput createField( ExtensionContext extensionContext ) | ||
{ | ||
return SuppressOutput.suppressAll(); | ||
} | ||
|
||
@Override | ||
protected Namespace getNameSpace() | ||
{ | ||
return SUPPRESS_OUTPUT_NAMESPACE; | ||
} | ||
|
||
@Override | ||
public void afterEach( ExtensionContext context ) | ||
{ | ||
getStoredValue( context ).releaseVoices( context.getExecutionException().isPresent() ); | ||
removeStoredValue( context ); | ||
} | ||
|
||
@Override | ||
public void beforeEach( ExtensionContext context ) | ||
{ | ||
getStoredValue( context ).captureVoices(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.