From a859f1ea683ecf31a01b79d07456a9c1c25f1437 Mon Sep 17 00:00:00 2001 From: "James R. Perkins" Date: Thu, 15 Aug 2024 13:56:57 -0700 Subject: [PATCH] [5] Only allow injection of the UriBuilder as a method parameter or constructor parameter. Signed-off-by: James R. Perkins --- README.adoc | 2 +- .../extension/annotations/RestBootstrap.java | 4 ++- .../UriBuilderParameterResolver.java | 36 +++++++++++++++++++ .../extensions/UriBuilderProducer.java | 36 ------------------- 4 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 extension/src/main/java/dev/resteasy/junit/extension/extensions/UriBuilderParameterResolver.java delete mode 100644 extension/src/main/java/dev/resteasy/junit/extension/extensions/UriBuilderProducer.java diff --git a/README.adoc b/README.adoc index 6cb3493..63f5258 100644 --- a/README.adoc +++ b/README.adoc @@ -159,7 +159,7 @@ This can be used with the `@RequestPath` qualifier. It creates a `WebTarget` fro * `@RestClientConfig` (optional) |`jakarta.ws.rs.core.UriBuilder` -|X +| |X |X | diff --git a/extension/src/main/java/dev/resteasy/junit/extension/annotations/RestBootstrap.java b/extension/src/main/java/dev/resteasy/junit/extension/annotations/RestBootstrap.java index f552399..faa28cd 100644 --- a/extension/src/main/java/dev/resteasy/junit/extension/annotations/RestBootstrap.java +++ b/extension/src/main/java/dev/resteasy/junit/extension/annotations/RestBootstrap.java @@ -21,6 +21,7 @@ import dev.resteasy.junit.extension.api.ConfigurationProvider; import dev.resteasy.junit.extension.extensions.InjectionProducerExtension; import dev.resteasy.junit.extension.extensions.SeBootstrapExtension; +import dev.resteasy.junit.extension.extensions.UriBuilderParameterResolver; /** * An annotation which starts a {@link jakarta.ws.rs.SeBootstrap.Instance} for unit testing. @@ -38,7 +39,8 @@ @Retention(RetentionPolicy.RUNTIME) @ExtendWith({ SeBootstrapExtension.class, - InjectionProducerExtension.class + InjectionProducerExtension.class, + UriBuilderParameterResolver.class, }) public @interface RestBootstrap { diff --git a/extension/src/main/java/dev/resteasy/junit/extension/extensions/UriBuilderParameterResolver.java b/extension/src/main/java/dev/resteasy/junit/extension/extensions/UriBuilderParameterResolver.java new file mode 100644 index 0000000..6f93aaf --- /dev/null +++ b/extension/src/main/java/dev/resteasy/junit/extension/extensions/UriBuilderParameterResolver.java @@ -0,0 +1,36 @@ +/* + * Copyright The RESTEasy Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package dev.resteasy.junit.extension.extensions; + +import jakarta.ws.rs.core.UriBuilder; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +/** + * @author James R. Perkins + */ +public class UriBuilderParameterResolver implements ParameterResolver { + @Override + public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext) + throws ParameterResolutionException { + return UriBuilder.class.isAssignableFrom(parameterContext.getParameter().getType()); + } + + @Override + public Object resolveParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext) + throws ParameterResolutionException { + if (UriBuilder.class.isAssignableFrom(parameterContext.getParameter().getType())) { + return InstanceManager.getInstance(extensionContext) + .map(im -> im.instance().configuration().baseUriBuilder()) + .orElseThrow(() -> new ParameterResolutionException("Failed to lookup URI builder instance.")); + } + throw new ParameterResolutionException(String.format("Type %s is not assignable to %s", + parameterContext.getParameter().getType().getName(), UriBuilder.class.getName())); + } +} diff --git a/extension/src/main/java/dev/resteasy/junit/extension/extensions/UriBuilderProducer.java b/extension/src/main/java/dev/resteasy/junit/extension/extensions/UriBuilderProducer.java deleted file mode 100644 index c5453d1..0000000 --- a/extension/src/main/java/dev/resteasy/junit/extension/extensions/UriBuilderProducer.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright The RESTEasy Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package dev.resteasy.junit.extension.extensions; - -import java.lang.annotation.Annotation; - -import jakarta.ws.rs.core.UriBuilder; - -import org.junit.jupiter.api.extension.ExtensionContext; -import org.kohsuke.MetaInfServices; - -import dev.resteasy.junit.extension.api.InjectionProducer; - -/** - * @author James R. Perkins - */ -@MetaInfServices -public class UriBuilderProducer implements InjectionProducer { - @Override - public boolean canInject(final ExtensionContext context, final Class clazz, final Annotation... qualifiers) { - return UriBuilder.class.isAssignableFrom(clazz); - } - - @Override - public Object produce(final ExtensionContext context, final Class clazz, final Annotation... qualifiers) { - if (UriBuilder.class.isAssignableFrom(clazz)) { - return InstanceManager.getInstance(context) - .map(im -> im.instance().configuration().baseUriBuilder()) - .orElse(null); - } - throw new IllegalArgumentException(String.format("Type %s is not assignable to %s", clazz.getName(), UriBuilder.class)); - } -}