-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce config param for default test instance lifecycle
WIP Issue: #905
- Loading branch information
Showing
9 changed files
with
490 additions
and
20 deletions.
There are no files selected for viewing
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
83 changes: 83 additions & 0 deletions
83
...-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestInstanceLifecycleUtils.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,83 @@ | ||
/* | ||
* Copyright 2015-2017 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.descriptor; | ||
|
||
import static java.util.logging.Level.WARNING; | ||
import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME; | ||
|
||
import java.util.Optional; | ||
import java.util.logging.Logger; | ||
|
||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.TestInstance.Lifecycle; | ||
import org.junit.platform.commons.util.AnnotationUtils; | ||
import org.junit.platform.commons.util.Preconditions; | ||
import org.junit.platform.engine.ConfigurationParameters; | ||
|
||
/** | ||
* Collection of utilities for retrieving the test instance lifecycle mode. | ||
* | ||
* @since 5.0 | ||
* @see TestInstance | ||
* @see TestInstance.Lifecycle | ||
*/ | ||
final class TestInstanceLifecycleUtils { | ||
|
||
private static final Logger LOG = Logger.getLogger(TestInstanceLifecycleUtils.class.getName()); | ||
|
||
///CLOVER:OFF | ||
private TestInstanceLifecycleUtils() { | ||
/* no-op */ | ||
} | ||
///CLOVER:ON | ||
|
||
static TestInstance.Lifecycle getTestInstanceLifecycle(Class<?> testClass, ConfigurationParameters configParams) { | ||
Preconditions.notNull(testClass, "testClass must not be null"); | ||
Preconditions.notNull(configParams, "ConfigurationParameters must not be null"); | ||
|
||
// @formatter:off | ||
return AnnotationUtils.findAnnotation(testClass, TestInstance.class) | ||
.map(TestInstance::value) | ||
.orElseGet(() -> getDefaultTestInstanceLifecycle(configParams)); | ||
// @formatter:on | ||
} | ||
|
||
// TODO Consider looking up the default test instance lifecycle mode once per test plan execution. | ||
static TestInstance.Lifecycle getDefaultTestInstanceLifecycle(ConfigurationParameters configParams) { | ||
Preconditions.notNull(configParams, "ConfigurationParameters must not be null"); | ||
String propertyName = DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME; | ||
|
||
Optional<String> optional = configParams.get(propertyName); | ||
String constantName = null; | ||
if (optional.isPresent()) { | ||
try { | ||
constantName = optional.get().trim().toUpperCase(); | ||
Lifecycle lifecycle = TestInstance.Lifecycle.valueOf(constantName); | ||
LOG.info(() -> String.format( | ||
"Using default test instance lifecycle mode '%s' set via the '%s' configuration parameter.", | ||
lifecycle, propertyName)); | ||
return lifecycle; | ||
} | ||
catch (Exception ex) { | ||
// local copy necessary for use in lambda expression | ||
String constant = constantName; | ||
LOG.log(WARNING, ex, | ||
() -> String.format( | ||
"Invalid test instance lifecycle mode '%s' set via the '%s' configuration parameter. " | ||
+ "Falling back to %s lifecycle semantics.", | ||
constant, propertyName, Lifecycle.PER_METHOD.name())); | ||
} | ||
} | ||
|
||
return Lifecycle.PER_METHOD; | ||
} | ||
|
||
} |
Oops, something went wrong.