Skip to content

Commit

Permalink
Introduce config param for default test instance lifecycle
Browse files Browse the repository at this point in the history
WIP

Issue: #905
  • Loading branch information
sbrannen committed Aug 7, 2017
1 parent 6316bd0 commit 14ec8d7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@

/**
* Enumeration of test instance lifecycle <em>modes</em>.
*
* @see #PER_METHOD
* @see #PER_CLASS
*/
enum Lifecycle {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ public final class Constants {
*/
public static final String EXTENSIONS_AUTODETECTION_ENABLED_PROPERTY_NAME = "junit.jupiter.extensions.autodetection.enabled";

/**
* Property name used to set the default test instance lifecycle mode: {@value}
*
* <h3>Supported Values</h3>
*
* <p>Supported values include names of enum constants defined in
* {@link org.junit.jupiter.api.TestInstance.Lifecycle}, ignoring case.
*
* <p>If not specified, the default is "per_method" which corresponds to
* {@code @TestInstance(Lifecycle.PER_METHOD)}.
*
* @see org.junit.jupiter.api.TestInstance
*/
public static final String DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME = "junit.jupiter.testinstance.lifecycle.default";

private Constants() {
/* no-op */
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.junit.jupiter.engine.descriptor;

import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME;
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterAllMethods;
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterEachMethods;
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeAllMethods;
Expand All @@ -21,6 +22,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

Expand All @@ -43,6 +45,7 @@
import org.junit.platform.commons.util.AnnotationUtils;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.engine.ConfigurationParameters;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestTag;
import org.junit.platform.engine.UniqueId;
Expand All @@ -65,7 +68,8 @@ public class ClassTestDescriptor extends JupiterTestDescriptor {
private static final ExecutableInvoker executableInvoker = new ExecutableInvoker();

private final Class<?> testClass;
private final Lifecycle lifecycle;

private Lifecycle lifecycle;

private List<Method> beforeAllMethods;
private List<Method> afterAllMethods;
Expand All @@ -83,7 +87,6 @@ protected ClassTestDescriptor(UniqueId uniqueId, Function<Class<?>, String> defa
defaultDisplayNameGenerator), new ClassSource(testClass));

this.testClass = testClass;
this.lifecycle = getTestInstanceLifecycle(testClass);
}

// --- TestDescriptor ------------------------------------------------------
Expand Down Expand Up @@ -117,6 +120,8 @@ private static String generateDefaultDisplayName(Class<?> testClass) {

@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
this.lifecycle = getTestInstanceLifecycle(testClass, context.getConfigurationParameters());

this.beforeAllMethods = findBeforeAllMethods(testClass, this.lifecycle == Lifecycle.PER_METHOD);
this.afterAllMethods = findAfterAllMethods(testClass, this.lifecycle == Lifecycle.PER_METHOD);
this.beforeEachMethods = findBeforeEachMethods(testClass);
Expand Down Expand Up @@ -291,12 +296,33 @@ private void invokeMethodInExtensionContext(Method method, ExtensionContext cont
executableInvoker.invoke(method, testInstance, context, registry);
}

private static TestInstance.Lifecycle getTestInstanceLifecycle(Class<?> testClass) {
private static TestInstance.Lifecycle getTestInstanceLifecycle(Class<?> testClass,
ConfigurationParameters configurationParameters) {

// @formatter:off
return AnnotationUtils.findAnnotation(testClass, TestInstance.class)
.map(TestInstance::value)
.orElse(Lifecycle.PER_METHOD);
.orElseGet(() -> getDefaultTestInstanceLifecycle(configurationParameters));
// @formatter:on
}

private static TestInstance.Lifecycle getDefaultTestInstanceLifecycle(
ConfigurationParameters configurationParameters) {

Optional<String> optional = configurationParameters.get(DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME);
if (optional.isPresent()) {
try {
String constantName = optional.get().trim().toUpperCase();
Lifecycle lifecycle = TestInstance.Lifecycle.valueOf(constantName);
// TODO Log info message.
return lifecycle;
}
catch (Exception ex) {
// TODO Log error message.
}
}

return Lifecycle.PER_METHOD;
}

}

0 comments on commit 14ec8d7

Please sign in to comment.