You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2018/03/21 16:40:29 UTC
[cxf] 02/02: tests for default injections
This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git
commit effefb0a8f0902ef5d0da165ef2402dcfcb861b3
Author: Romain Manni-Bucau <rm...@apache.org>
AuthorDate: Mon Mar 19 09:35:10 2018 +0100
tests for default injections
---
.../apache/cxf/cdi/JAXRSCdiResourceExtension.java | 11 +-
.../cdi/base/AbstractCdiSingleAppTest.java | 29 ++++
.../apache/cxf/systests/cdi/base/BookStore.java | 11 +-
.../apache/cxf/systests/cdi/base/Injections.java | 154 +++++++++++++++++++++
4 files changed, 201 insertions(+), 4 deletions(-)
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
index 04121a6..d2b76db 100644
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
@@ -67,6 +67,7 @@ import org.apache.cxf.jaxrs.utils.ResourceUtils;
import static java.util.Arrays.asList;
import static java.util.Optional.ofNullable;
+import static org.apache.cxf.cdi.AbstractCXFBean.DEFAULT;
/**
* Apache CXF portable CDI extension to support initialization of JAX-RS resources.
@@ -196,7 +197,7 @@ public class JAXRSCdiResourceExtension implements Extension {
} else if (CdiBusBean.CXF.equals(event.getBean().getName())
&& Bus.class.isAssignableFrom(event.getBean().getBeanClass())) {
hasBus = true;
- } else {
+ } else if (event.getBean().getQualifiers().contains(DEFAULT)) {
event.getBean().getTypes().stream()
.filter(e -> Object.class != e && InjectionUtils.STANDARD_CONTEXT_CLASSES.contains(e.getTypeName()))
.findFirst()
@@ -263,11 +264,16 @@ public class JAXRSCdiResourceExtension implements Extension {
beanManager.createInjectionTarget(busAnnotatedType);
event.addBean(new CdiBusBean(busInjectionTarget));
}
+
if (applicationBeans.isEmpty() && !serviceBeans.isEmpty()) {
final DefaultApplicationBean applicationBean = new DefaultApplicationBean();
applicationBeans.add(applicationBean);
event.addBean(applicationBean);
+ } else {
+ // otherwise will be ambiguous since we scanned it with default qualifier already
+ existingStandardClasses.add(Application.class.getName());
}
+
// always add the standard context classes
InjectionUtils.STANDARD_CONTEXT_CLASSES.stream()
.map(this::toClass)
@@ -334,8 +340,7 @@ public class JAXRSCdiResourceExtension implements Extension {
* @return JAXRSServerFactoryBean instance
*/
private JAXRSServerFactoryBean createFactoryInstance(final Application application, final BeanManager beanManager) {
-
- final JAXRSServerFactoryBean instance =
+ final JAXRSServerFactoryBean instance =
ResourceUtils.createApplication(application, false, false, false, bus);
final ClassifiedClasses classified = classes2singletons(application, beanManager);
diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java
index 36eb0dc..edc0c54 100644
--- a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java
+++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java
@@ -36,6 +36,35 @@ import org.junit.Test;
public abstract class AbstractCdiSingleAppTest extends AbstractBusClientServerTestBase {
@Test
+ public void testAvailableInjections() {
+ assertEquals("configuration=Configuration/"
+ + "contextResolver=ContextResolver/"
+ + "cxfApplication=Application/"
+ + "cxfConfiguration=Configuration/"
+ + "cxfContextResolver=ContextResolver/"
+ + "cxfHttpHeaders=HttpHeaders/"
+ + "cxfHttpServletRequest=HttpServletRequest/"
+ + "cxfProviders=Providers/"
+ + "cxfRequest=Request/"
+ + "cxfResourceContext=ResourceContext/"
+ + "cxfResourceInfo=ResourceInfo/"
+ + "cxfSecurityContext=SecurityContext/"
+ + "cxfServletContext=ServletContext/"
+ + "cxfUriInfo=UriInfo/"
+ + "cxfhttpServletResponse=HttpServletRequest/"
+ + "httpHeaders=HttpHeaders/"
+ + "httpServletRequest=HttpServletRequest/"
+ + "httpServletResponse=HttpServletResponse/"
+ + "providers=Providers/request=Request/"
+ + "resourceContext=ResourceContext/"
+ + "resourceInfo=ResourceInfo/"
+ + "securityContext=SecurityContext/"
+ + "servletContext=ServletContext/"
+ + "uriInfo=UriInfo",
+ createWebClient(getBasePath() + "/injections", MediaType.TEXT_PLAIN).get(String.class).trim());
+ }
+
+ @Test
public void testInjectedVersionIsProperlyReturned() {
Response r = createWebClient(getBasePath() + "/version", MediaType.TEXT_PLAIN).get();
String pathInfo = r.getHeaderString(Message.PATH_INFO);
diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStore.java b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStore.java
index bb458af..63cbc3a 100644
--- a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStore.java
+++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStore.java
@@ -39,15 +39,24 @@ public class BookStore {
private BookStoreService service;
private BookStoreVersion bookStoreVersion;
private UriInfo uriInfo;
+ private Injections injections;
public BookStore() {
}
@Inject
- public BookStore(BookStoreService service, BookStoreVersion bookStoreVersion, UriInfo uriInfo) {
+ public BookStore(BookStoreService service, BookStoreVersion bookStoreVersion, UriInfo uriInfo,
+ Injections injections) {
this.service = service;
this.bookStoreVersion = bookStoreVersion;
this.uriInfo = uriInfo;
+ this.injections = injections;
+ }
+
+ @GET
+ @Path("injections")
+ public String injections() {
+ return injections.state();
}
@Path("/version")
diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/Injections.java b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/Injections.java
new file mode 100644
index 0000000..7e2391c
--- /dev/null
+++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/Injections.java
@@ -0,0 +1,154 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.cxf.systests.cdi.base;
+
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.container.ResourceContext;
+import javax.ws.rs.container.ResourceInfo;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Configuration;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.Providers;
+
+import org.apache.cxf.cdi.ContextResolved;
+
+@ApplicationScoped
+public class Injections {
+ /* this one is not supposed to work in the systests
+ @Inject
+ private Application application;
+ */
+
+ @Inject
+ @ContextResolved
+ private Application cxfApplication; //NOPMD
+
+ @Inject
+ private UriInfo uriInfo; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private UriInfo cxfUriInfo; //NOPMD
+
+ @Inject
+ private HttpHeaders httpHeaders; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private HttpHeaders cxfHttpHeaders; //NOPMD
+
+ @Inject
+ private Request request; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private Request cxfRequest; //NOPMD
+
+ @Inject
+ private SecurityContext securityContext; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private SecurityContext cxfSecurityContext; //NOPMD
+
+ @Inject
+ private Providers providers; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private Providers cxfProviders; //NOPMD
+
+ @Inject
+ private ContextResolver contextResolver; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private ContextResolver cxfContextResolver; //NOPMD
+
+ @Inject
+ private HttpServletRequest httpServletRequest; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private HttpServletRequest cxfHttpServletRequest; //NOPMD
+
+ @Inject
+ private HttpServletResponse httpServletResponse; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private HttpServletRequest cxfhttpServletResponse; //NOPMD
+
+ @Inject
+ private ServletContext servletContext; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private ServletContext cxfServletContext; //NOPMD
+
+ @Inject
+ private ResourceContext resourceContext; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private ResourceContext cxfResourceContext; //NOPMD
+
+ @Inject
+ private ResourceInfo resourceInfo; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private ResourceInfo cxfResourceInfo; //NOPMD
+
+ @Inject
+ private Configuration configuration; //NOPMD
+
+ @Inject
+ @ContextResolved
+ private Configuration cxfConfiguration; //NOPMD
+
+ public String state() {
+ return Stream.of(Injections.class.getDeclaredFields())
+ .filter(f -> f.isAnnotationPresent(Inject.class))
+ .map(f -> {
+ if (!f.isAccessible()) {
+ f.setAccessible(true);
+ }
+ try {
+ // the standard name otherwise not portable
+ return f.get(Injections.this) != null ? f.getName() + "=" + f.getType().getSimpleName() : "";
+ } catch (final IllegalAccessException e) {
+ return "";
+ }
+ })
+ .sorted() // be deterministic in the test, java reflection is no more since java7
+ .collect(Collectors.joining("/"));
+ }
+}
--
To stop receiving notification emails like this one, please contact
dkulp@apache.org.