cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [cxf] branch 3.2.x-fixes updated: CXF-7908 - Allow to customise JAXRS server in spring boot when using cxf-spring-boot-autoconfigure (#477)
Date Tue, 04 Dec 2018 02:53:34 GMT
This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.2.x-fixes by this push:
     new 0a0780b  CXF-7908 - Allow to customise JAXRS server in spring boot when using cxf-spring-boot-autoconfigure
(#477)
0a0780b is described below

commit 0a0780bfa8a85b86fea60ae5b2ffbbb28bfd1cbe
Author: Maciej Swiderski <swiderski.maciej@gmail.com>
AuthorDate: Mon Dec 3 12:47:59 2018 +0100

    CXF-7908 - Allow to customise JAXRS server in spring boot when using cxf-spring-boot-autoconfigure
(#477)
---
 .../boot/autoconfigure/CxfAutoConfiguration.java   |  3 +
 ...ionTests.java => CxfAutoConfigurationTest.java} | 56 ++++++++++++++++---
 .../cxf/spring/boot/jaxrs/CustomJaxRSServer.java   | 65 ++++++++++++++++++++++
 .../cxf/spring/boot/jaxrs/SampleJaxRSResource.java | 31 +++++++++++
 4 files changed, 147 insertions(+), 8 deletions(-)

diff --git a/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
index c8fe687..7923ffc 100644
--- a/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
+++ b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
@@ -21,6 +21,7 @@ package org.apache.cxf.spring.boot.autoconfigure;
 import java.util.Map;
 
 import org.apache.cxf.bus.spring.SpringBus;
+import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.spring.SpringComponentScanServer;
 import org.apache.cxf.jaxrs.spring.SpringJaxrsClassesScanServer;
@@ -81,6 +82,7 @@ public class CxfAutoConfiguration {
     @Configuration
     @ConditionalOnClass(JAXRSServerFactoryBean.class)
     @ConditionalOnExpression("'${cxf.jaxrs.component-scan}'=='true' && '${cxf.jaxrs.classes-scan}'!='true'")
+    @ConditionalOnMissingBean(Server.class)
     @Import(SpringComponentScanServer.class)
     protected static class JaxRsComponentConfiguration {
 
@@ -89,6 +91,7 @@ public class CxfAutoConfiguration {
     @Configuration
     @ConditionalOnClass(JAXRSServerFactoryBean.class)
     @ConditionalOnExpression("'${cxf.jaxrs.classes-scan}'=='true' && '${cxf.jaxrs.component-scan}'!='true'")
+    @ConditionalOnMissingBean(Server.class)
     @Import(SpringJaxrsClassesScanServer.class)
     protected static class JaxRsClassesConfiguration {
 
diff --git a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTests.java
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTest.java
similarity index 65%
rename from integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTests.java
rename to integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTest.java
index 34d1283..77b7415 100644
--- a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTests.java
+++ b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTest.java
@@ -18,6 +18,11 @@
  */
 package org.apache.cxf.spring.boot.autoconfigure;
 
+import java.util.Map;
+
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.endpoint.ServerImpl;
+import org.apache.cxf.spring.boot.jaxrs.CustomJaxRSServer;
 import org.hamcrest.Matcher;
 import org.springframework.beans.factory.BeanCreationException;
 import org.springframework.boot.test.util.EnvironmentTestUtils;
@@ -34,14 +39,16 @@ import org.junit.rules.ExpectedException;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasEntry;
 import static org.hamcrest.Matchers.hasItem;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
 /**
  * Tests for {@link CxfAutoConfiguration}.
  *
  * @author Vedran Pavic
  */
-public class CxfAutoConfigurationTests {
+public class CxfAutoConfigurationTest {
 
     @Rule
     public ExpectedException thrown = ExpectedException.none();
@@ -72,8 +79,9 @@ public class CxfAutoConfigurationTests {
     @Test
     public void customPathWithTrailingSlash() {
         load(CxfAutoConfiguration.class, "cxf.path=/valid/");
-        assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings(),
-                (Matcher<Iterable<? super String>>) hasItem("/valid/*"));
+        ServletRegistrationBean registrationBean = this.context.getBean(ServletRegistrationBean.class);
+        Matcher<java.lang.Iterable<? super String>> v = hasItem("/valid/*");
+        assertThat(registrationBean.getUrlMappings(), v);
     }
 
     @Test
@@ -81,8 +89,9 @@ public class CxfAutoConfigurationTests {
         load(CxfAutoConfiguration.class, "cxf.path=/valid");
         assertThat(this.context.getBeansOfType(ServletRegistrationBean.class).size(),
                 equalTo(1));
-        assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings(),
-                (Matcher<Iterable<? super String>>) hasItem("/valid/*"));
+        ServletRegistrationBean registrationBean = this.context.getBean(ServletRegistrationBean.class);
+        Matcher<java.lang.Iterable<? super String>> v = hasItem("/valid/*");
+        assertThat(registrationBean.getUrlMappings(), v);
     }
 
     @Test
@@ -97,20 +106,51 @@ public class CxfAutoConfigurationTests {
     @Test
     public void customInitParameters() {
         load(CxfAutoConfiguration.class, "cxf.servlet.init.key1=value1",
-                "spring.cxf.servlet.init.key2=value2");
+                "cxf.servlet.init.key2=value2");
         ServletRegistrationBean registrationBean = this.context
                 .getBean(ServletRegistrationBean.class);
         assertThat(registrationBean.getInitParameters(), hasEntry("key1", "value1"));
         assertThat(registrationBean.getInitParameters(), hasEntry("key2", "value2"));
     }
+    
+    @Test
+    public void customizedJaxRsServer() {
+        load(new Class<?>[] {CxfAutoConfiguration.class, CustomJaxRSServer.class},

+                "cxf.jaxrs.classes-scan=true", 
+                "cxf.jaxrs.classes-scan-packages=org.apache.cxf.spring.boot.jaxrs");
+        Map<String, Server> beans = 
+                this.context.getBeansOfType(Server.class);
+        assertThat(beans.size(),
+                equalTo(1));
+        
+        Object serverInstance = beans.values().iterator().next();
+        assertFalse(serverInstance instanceof ServerImpl);
+    }
+    
+    @Test
+    public void defaultJaxRsServer() {
+        load(CxfAutoConfiguration.class, 
+                "cxf.jaxrs.classes-scan=true", 
+                "cxf.jaxrs.classes-scan-packages=org.apache.cxf.spring.boot.jaxrs");
+        Map<String, Server> beans = 
+                this.context.getBeansOfType(Server.class);
+        assertThat(beans.size(),
+                equalTo(1));
+        
+        Object serverInstance = beans.values().iterator().next();
+        assertTrue(serverInstance instanceof ServerImpl);
+    }
 
     private void load(Class<?> config, String... environment) {
+        load(new Class<?>[] {config}, environment);
+    }
+
+    private void load(Class<?>[] configs, String... environment) {
         AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
         ctx.setServletContext(new MockServletContext());
         EnvironmentTestUtils.addEnvironment(ctx, environment);
-        ctx.register(config);
+        ctx.register(configs);
         ctx.refresh();
         this.context = ctx;
     }
-
 }
diff --git a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/CustomJaxRSServer.java
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/CustomJaxRSServer.java
new file mode 100644
index 0000000..78f6298
--- /dev/null
+++ b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/CustomJaxRSServer.java
@@ -0,0 +1,65 @@
+/**
+ * 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.spring.boot.jaxrs;
+
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.jaxrs.spring.AbstractJaxrsClassesScanServer;
+import org.apache.cxf.transport.Destination;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class CustomJaxRSServer extends AbstractJaxrsClassesScanServer {
+
+    @Bean
+    public Server jaxRsServer() {
+        return new Server() {
+            
+            @Override
+            public void stop() {                
+            }
+            
+            @Override
+            public void start() {                
+            }
+            
+            @Override
+            public boolean isStarted() {
+                return false;
+            }
+            
+            @Override
+            public Endpoint getEndpoint() {
+                return null;
+            }
+            
+            @Override
+            public Destination getDestination() {
+                return null;
+            }
+            
+            @Override
+            public void destroy() {
+                
+            }
+        };
+    }
+
+}
diff --git a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/SampleJaxRSResource.java
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/SampleJaxRSResource.java
new file mode 100644
index 0000000..a6ee994
--- /dev/null
+++ b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/SampleJaxRSResource.java
@@ -0,0 +1,31 @@
+/**
+ * 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.spring.boot.jaxrs;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+@Path("/sample")
+public class SampleJaxRSResource {
+
+    @GET
+    public String test() {
+        return "";
+    }
+}


Mime
View raw message