-
-
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.
Document best practices for implementing ParameterResolvers (#3829)
Resolves #1190. Co-authored-by: Marc Philipp <[email protected]>
- Loading branch information
1 parent
2cf42df
commit 0ca1754
Showing
5 changed files
with
316 additions
and
0 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
62 changes: 62 additions & 0 deletions
62
documentation/src/test/java/example/extensions/ParameterResolverConflictDemo.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,62 @@ | ||
/* | ||
* Copyright 2015-2024 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 v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.extensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import extensions.ExpectToFail; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
// tag::user_guide[] | ||
public class ParameterResolverConflictDemo { | ||
|
||
// end::user_guide[] | ||
@ExpectToFail | ||
// tag::user_guide[] | ||
@Test | ||
@ExtendWith({ FirstIntegerResolver.class, SecondIntegerResolver.class }) | ||
void testInt(int i) { | ||
// Test will not run due to ParameterResolutionException | ||
assertEquals(1, i); | ||
} | ||
|
||
static class FirstIntegerResolver implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameterContext.getParameter().getType() == int.class; | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return 1; | ||
} | ||
} | ||
|
||
static class SecondIntegerResolver implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameterContext.getParameter().getType() == int.class; | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return 2; | ||
} | ||
} | ||
} | ||
// end::user_guide[] |
74 changes: 74 additions & 0 deletions
74
documentation/src/test/java/example/extensions/ParameterResolverCustomAnnotationDemo.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,74 @@ | ||
/* | ||
* Copyright 2015-2024 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 v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.extensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
// tag::user_guide[] | ||
public class ParameterResolverCustomAnnotationDemo { | ||
|
||
@Test | ||
void testInt(@FirstInteger Integer first, @SecondInteger Integer second) { | ||
assertEquals(1, first); | ||
assertEquals(2, second); | ||
} | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ExtendWith(FirstInteger.Extension.class) | ||
public @interface FirstInteger { | ||
|
||
class Extension implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameterContext.getParameter().getType().equals(Integer.class) | ||
&& !parameterContext.isAnnotated(SecondInteger.class); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ExtendWith(SecondInteger.Extension.class) | ||
public @interface SecondInteger { | ||
|
||
class Extension implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameterContext.isAnnotated(SecondInteger.class); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return 2; | ||
} | ||
} | ||
} | ||
} | ||
// end::user_guide[] |
67 changes: 67 additions & 0 deletions
67
documentation/src/test/java/example/extensions/ParameterResolverCustomTypeDemo.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,67 @@ | ||
/* | ||
* Copyright 2015-2024 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 v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.extensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
// tag::user_guide[] | ||
public class ParameterResolverCustomTypeDemo { | ||
|
||
@Test | ||
@ExtendWith({ FirstIntegerResolver.class, SecondIntegerResolver.class }) | ||
void testInt(Integer i, WrappedInteger wrappedInteger) { | ||
assertEquals(1, i); | ||
assertEquals(2, wrappedInteger.value); | ||
} | ||
|
||
static class FirstIntegerResolver implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameterContext.getParameter().getType().equals(Integer.class); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return 1; | ||
} | ||
} | ||
|
||
static class SecondIntegerResolver implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameterContext.getParameter().getType().equals(WrappedInteger.class); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return new WrappedInteger(2); | ||
} | ||
} | ||
|
||
static class WrappedInteger { | ||
|
||
private final int value; | ||
|
||
public WrappedInteger(int value) { | ||
this.value = value; | ||
} | ||
|
||
} | ||
} | ||
// end::user_guide[] |
62 changes: 62 additions & 0 deletions
62
documentation/src/test/java/example/extensions/ParameterResolverNoConflictDemo.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,62 @@ | ||
/* | ||
* Copyright 2015-2024 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 v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.extensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
// tag::user_guide[] | ||
public class ParameterResolverNoConflictDemo { | ||
|
||
@Test | ||
@ExtendWith(FirstIntegerResolver.class) | ||
void firstResolution(int i) { | ||
assertEquals(1, i); | ||
} | ||
|
||
@Test | ||
@ExtendWith(SecondIntegerResolver.class) | ||
void secondResolution(int i) { | ||
assertEquals(2, i); | ||
} | ||
|
||
static class FirstIntegerResolver implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameterContext.getParameter().getType() == int.class; | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return 1; | ||
} | ||
} | ||
|
||
static class SecondIntegerResolver implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameterContext.getParameter().getType() == int.class; | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return 2; | ||
} | ||
} | ||
} | ||
// end::user_guide[] |