Skip to content

Commit

Permalink
Allow @RegisterExtension fields to be private
Browse files Browse the repository at this point in the history
Prior to this commit, an exception was thrown if a @RegisterExtension
field was declared private.

This commit removes this restriction.

Closes #2688
See #864, #2680
  • Loading branch information
sbrannen committed Aug 17, 2021
1 parent 4ae957d commit 3480fc8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
10 changes: 5 additions & 5 deletions documentation/src/docs/asciidoc/user-guide/extensions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ registered last and _after_ callback extensions to be registered first, relative
programmatically registered extensions.
====

NOTE: `@RegisterExtension` fields must not be `private` or `null` (at evaluation time) but
may be either `static` or non-static.
NOTE: `@RegisterExtension` fields must not be `null` (at evaluation time) but may be
either `static` or non-static.

[[extensions-registration-programmatic-static-fields]]
===== Static Fields
Expand Down Expand Up @@ -159,9 +159,9 @@ include::{testDir}/example/registration/WebServerDemo.java[tags=user_guide]

The Kotlin programming language does not have the concept of a `static` field. However,
the compiler can be instructed to generate static fields using annotations. Since, as
stated earlier, `@RegisterExtension` fields must not be `private` nor `null`, one
**cannot** use the `@JvmStatic` annotation in Kotlin as it generates `private` fields.
Rather, the `@JvmField` annotation must be used.
stated earlier, `@RegisterExtension` fields must not be `null`, one **cannot** use the
`@JvmStatic` annotation in Kotlin as it generates `private` fields. Rather, the
`@JvmField` annotation must be used.

The following example is a version of the `WebServerDemo` from the previous section that
has been ported to Kotlin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* to pass arguments to the extension's constructor, {@code static} factory
* method, or builder API.
*
* <p>{@code @RegisterExtension} fields must not be {@code private} or
* {@code null} (when evaluated) but may be either {@code static} or non-static.
* <p>{@code @RegisterExtension} fields must not be {@code null} (when evaluated)
* but may be either {@code static} or non-static.
*
* <h3>Static Fields</h3>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.TOP_DOWN;
import static org.junit.platform.commons.util.ReflectionUtils.findFields;
import static org.junit.platform.commons.util.ReflectionUtils.getDeclaredConstructor;
import static org.junit.platform.commons.util.ReflectionUtils.isNotPrivate;
import static org.junit.platform.commons.util.ReflectionUtils.tryToReadFieldValue;

import java.lang.reflect.AnnotatedElement;
Expand Down Expand Up @@ -108,9 +107,6 @@ static void registerExtensionsFromFields(ExtensionRegistrar registrar, Class<?>
fields.stream()
.filter(field -> isAnnotated(field, RegisterExtension.class))
.forEach(field -> {
Preconditions.condition(isNotPrivate(field), () -> String.format(
"Failed to register extension via @RegisterExtension field [%s]: field must not be private.",
field));
tryToReadFieldValue(field, instance).ifSuccess(value -> {
Preconditions.condition(value instanceof Extension, () -> String.format(
"Failed to register extension via @RegisterExtension field [%s]: field value's type [%s] must implement an [%s] API.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private static TestTemplateInvocationContext emptyTestTemplateInvocationContext(
static class StaticFieldTestCase {

@MagicField
static String staticField1;
private static String staticField1;

@MagicField
static String staticField2;
Expand All @@ -560,7 +560,7 @@ static class InstanceFieldTestCase {
String instanceField1;

@MagicField
String instanceField2;
private String instanceField2;

@Test
void test() {
Expand Down Expand Up @@ -606,15 +606,15 @@ static class AllInOneWithTestInstancePerMethodTestCase {

@RegisterExtension
@Order(1)
static Extension classLevelExtension1 = new ClassLevelExtension1();
private static Extension classLevelExtension1 = new ClassLevelExtension1();

@RegisterExtension
@Order(2)
static Extension classLevelExtension2 = new ClassLevelExtension2();

@RegisterExtension
@Order(1)
Extension instanceLevelExtension1 = new InstanceLevelExtension1();
private Extension instanceLevelExtension1 = new InstanceLevelExtension1();

@RegisterExtension
@Order(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ void classLevelWithFieldThatDoesNotImplementAnExtensionApi() {
@Test
void instanceLevelWithPrivateField() {
Class<?> testClass = InstanceLevelExtensionRegistrationWithPrivateFieldTestCase.class;
executeTestsForClass(testClass).testEvents().assertThatEvents().haveExactly(1, finishedWithFailure(
instanceOf(PreconditionViolationException.class), message(expectedNotPrivateMessage(testClass))));
executeTestsForClass(testClass).testEvents().assertStatistics(stats -> stats.succeeded(1));
}

/**
Expand All @@ -177,8 +176,7 @@ void instanceLevelWithPrivateField() {
@Test
void classLevelWithPrivateField() {
Class<?> testClass = ClassLevelExtensionRegistrationWithPrivateFieldTestCase.class;
executeTestsForClass(testClass).containerEvents().assertThatEvents().haveExactly(1, finishedWithFailure(
instanceOf(PreconditionViolationException.class), message(expectedNotPrivateMessage(testClass))));
executeTestsForClass(testClass).testEvents().assertStatistics(stats -> stats.succeeded(1));
}

@Test
Expand Down Expand Up @@ -219,11 +217,6 @@ void classLevelWithNonExtensionFieldValue() {
instanceOf(PreconditionViolationException.class), message(expectedMessage(testClass, String.class))));
}

private String expectedNotPrivateMessage(Class<?> testClass) {
return "Failed to register extension via @RegisterExtension field [" + field(testClass)
+ "]: field must not be private.";
}

private String expectedMessage(Class<?> testClass, Class<?> valueType) {
return "Failed to register extension via @RegisterExtension field [" + field(testClass)
+ "]: field value's type [" + (valueType != null ? valueType.getName() : null) + "] must implement an ["
Expand Down Expand Up @@ -602,14 +595,16 @@ void test() {
static class InstanceLevelExtensionRegistrationWithPrivateFieldTestCase extends AbstractTestCase {

@RegisterExtension
private Extension extension;
private Extension extension = new Extension() {
};

}

static class ClassLevelExtensionRegistrationWithPrivateFieldTestCase extends AbstractTestCase {

@RegisterExtension
private static Extension extension;
private static Extension extension = new Extension() {
};

}

Expand Down

0 comments on commit 3480fc8

Please sign in to comment.