Skip to content

Commit

Permalink
Merge pull request #3747 from jamezp/RESTEASY-3366
Browse files Browse the repository at this point in the history
RESTEASY-3366 Start migrating @context injection in tests to @Inject
  • Loading branch information
jamezp committed Aug 15, 2023
2 parents 5b7a255 + 91d3cc6 commit 082852c
Show file tree
Hide file tree
Showing 58 changed files with 451 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.jboss.resteasy.cdi;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;
Expand All @@ -45,10 +46,10 @@
@Singleton
public class ContextProducers {

@ApplicationScoped
@Dependent
@Produces
public Configuration configuration() {
return getProviderFactory();
return getProviderFactory().getContextData(Configuration.class);
}

@RequestScoped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
* Sesion Bean JAX-RS components.
*
* @author Jozef Hartinger
*
*/
public class ResteasyCdiExtension implements Extension {
private static boolean active;
Expand All @@ -73,6 +72,7 @@ public static boolean isCDIActive() {
private final Map<Class<?>, Type> sessionBeanInterface = new HashMap<>();
private boolean generateClientBean = true;
private boolean addContextProducers = true;
private boolean noApplicationFound = true;

/**
* Obtain BeanManager reference for future use.
Expand Down Expand Up @@ -135,6 +135,11 @@ public void addContextProducer(@Observes final AfterTypeDiscovery event, final B
.createAnnotatedType(ContextProducers.class);
event.addAnnotatedType(producersAnnotatedType, ContextProducers.class.getCanonicalName());
}
if (noApplicationFound) {
// Add a generic Application if no application was defined
final AnnotatedType<Application> annotatedType = beanManager.createAnnotatedType(Application.class);
event.addAnnotatedType(annotatedType, Application.class.getCanonicalName());
}
}

/**
Expand Down Expand Up @@ -190,6 +195,7 @@ public <T> void observeProviders(@WithAnnotations({ Provider.class }) @Observes
*/
public <T extends Application> void observeApplications(@Observes ProcessAnnotatedType<T> event,
BeanManager beanManager) {
noApplicationFound = false;
if (!Utils.isScopeDefined(event.getAnnotatedType(), beanManager)) {
event.configureAnnotatedType().add(applicationScopedLiteral);
}
Expand Down Expand Up @@ -220,7 +226,6 @@ protected <T> InjectionTarget<T> wrapInjectionTarget(ProcessInjectionTarget<T> e
*
* @param <T> type
* @param event event
*
*/
public <T> void observeSessionBeans(@Observes ProcessSessionBean<T> event) {
Bean<Object> sessionBean = event.getBean();
Expand Down
24 changes: 24 additions & 0 deletions testsuite/arquillian-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,28 @@
<artifactId>aether-transport-http</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<!--
Disable Annotation processor on project compile
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.resteasy.test;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

import org.jboss.resteasy.test.annotations.FollowUpRequired;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
@SupportedOptions({
"dev.resteasy.test.follow.up.level"
})
public class FollowUpRequiredProcessor extends AbstractProcessor {
private final Set<String> supportedAnnotations;

public FollowUpRequiredProcessor() {
supportedAnnotations = Set.of(FollowUpRequired.class.getName());
}

@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
if (!roundEnv.processingOver() && !annotations.isEmpty()) {
final Messager messager = processingEnv.getMessager();
final String kindLevel = processingEnv.getOptions().getOrDefault("dev.resteasy.test.follow.up.level", "WARNING");
Diagnostic.Kind kind;
try {
kind = Diagnostic.Kind.valueOf(kindLevel);
} catch (IllegalArgumentException e) {
kind = Diagnostic.Kind.WARNING;
messager.printMessage(Diagnostic.Kind.MANDATORY_WARNING,
"Failed to parse follow-up level " + kindLevel + ". Defaulting to " + kind + ".");
}
for (TypeElement annotation : annotations) {
final Set<? extends Element> annotated = roundEnv.getElementsAnnotatedWith(annotation);
for (Element e : annotated) {
// Get the message for the annotation
final var msg = e.getAnnotation(FollowUpRequired.class).value();
// Print a warning message since this requires some kind of action
messager.printMessage(kind, "Follow up required: " + msg, e);
}
}
}
return true;
}

@Override
public Set<String> getSupportedAnnotationTypes() {
return supportedAnnotations;
}

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.resteasy.test.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* An annotation for tests that simply indicates a follow-up is required.
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
@Inherited
@Target({
ElementType.CONSTRUCTOR,
ElementType.FIELD,
ElementType.METHOD,
ElementType.MODULE,
ElementType.PARAMETER,
ElementType.RECORD_COMPONENT,
ElementType.TYPE
})
@Documented
@Retention(RetentionPolicy.CLASS)
public @interface FollowUpRequired {

/**
* A simple message indicating information about the follow-up.
*
* @return the follow-up information.
*/
String value();

/**
* Defines the version the follow-up should be done by.
*
* @return the version the follow-up should be done by or an empty string if no version was specified
*/
String version() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,9 @@ public static Asset createBeansXml() {
return new StringAsset("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<beans xmlns=\"https://jakarta.ee/xml/ns/jakartaee\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd\"\n"
" xsi:schemaLocation=\"https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd\"\n"
+
" version=\"3.0\" bean-discovery-mode=\"all\">\n" +
" version=\"4.0\" bean-discovery-mode=\"all\">\n" +
"</beans>");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.resteasy.test.FollowUpRequiredProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import jakarta.ejb.EJB;
import jakarta.ejb.EJBException;
import jakarta.ejb.Singleton;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;

@Singleton
Expand All @@ -30,7 +30,7 @@ public SingletonLocalIF getLocalSub() {
return rl;
}

@Context
@Inject
private Application injectedApplication;
private boolean isJaxrsInjectedPriorToPostConstruct = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import jakarta.ejb.Local;
import jakarta.ejb.Stateless;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.UriInfo;

@Stateless(name = "SingletonTestBean")
Expand All @@ -16,7 +16,7 @@ public SingletonTestBean() {
public void remove() {
}

@Context
@Inject
private UriInfo ui;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jboss.resteasy.test.cdi.injection.resource.LazyInitUriInfoInjectionSingletonResource;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -31,7 +32,8 @@ public class LazyInitUriInfoInjectionTest {

@Deployment
public static Archive<?> deploySimpleResource() {
WebArchive war = TestUtil.prepareArchive(LazyInitUriInfoInjectionTest.class.getSimpleName());
WebArchive war = TestUtil.prepareArchive(LazyInitUriInfoInjectionTest.class.getSimpleName())
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return TestUtil.finishContainerPrepare(war, null, LazyInitUriInfoInjectionSingletonResource.class,
LazyInitUriInfoInjectionResource.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.jboss.resteasy.test.cdi.injection.resource;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.UriInfo;

import org.jboss.logging.Logger;
Expand All @@ -14,7 +14,7 @@ public class LazyInitUriInfoInjectionResource {

private UriInfo info;

@Context
@Inject
public void setUriInfo(UriInfo i) {
this.info = i;
logger.info(i.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import jakarta.inject.Inject;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;

import org.jboss.resteasy.test.annotations.FollowUpRequired;

@Provider
public class ProviderOneArgConstructorStringHandlerBodyWriter
implements MessageBodyWriter<ProviderOneArgConstructorStringHandler> {

@Inject
@SuppressWarnings("unused")
public ProviderOneArgConstructorStringHandlerBodyWriter(@Context final Application application) {
public ProviderOneArgConstructorStringHandlerBodyWriter(final Application application) {
}

@FollowUpRequired("This should not be required, but currently RESTEasy requires it")
@SuppressWarnings("unused")
private ProviderOneArgConstructorStringHandlerBodyWriter() {
public ProviderOneArgConstructorStringHandlerBodyWriter() {
}

public boolean isWriteable(Class<?> type, Type genericType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import java.io.IOException;

import jakarta.inject.Inject;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Context;

@NameBoundProxiesAnnotation
public class NameBoundCDIProxiesInterceptor implements ContainerRequestFilter, ContainerResponseFilter {

private static String in = "";

/** The application context, used for retrieving the {@link ApplicationPath} value. */
@Context
@Inject
Application application;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/**
* Tests that a {@link DynamicFeature} can inject {@link jakarta.ws.rs.core.UriInfo} via
* {@link jakarta.ws.rs.core.Context @Context} and in a CDI bean. The feature is registered as a provider with
* {@link jakarta.inject.Inject @Inject} and in a CDI bean. The feature is registered as a provider with
* {@link Provider @Provider}.
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
Expand Down

0 comments on commit 082852c

Please sign in to comment.