This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit c798d642603479ebfaead918ca64af32a9a932c5
Author: Willian Antunes <willian.lima.antu...@gmail.com>
AuthorDate: Sun May 27 13:20:53 2018 -0300

    CAMEL-12541: Use configured bean instead of creating a new
    
    When the endpoint is created the bean is always available and is used
    for the consumer approach (newJAXRSServerFactoryBean). Now the same
    behavior is used for producer approach (newJAXRSClientFactoryBean).
---
 .../component/cxf/jaxrs/CxfRsSpringEndpoint.java   |  3 +-
 .../cxf/jaxrs/CxfRsSpringEndpointTest.java         | 51 ++++++++++++++++++++--
 2 files changed, 50 insertions(+), 4 deletions(-)

diff --git 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java
 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java
index 954d984..bd51da1 100644
--- 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java
+++ 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java
@@ -69,7 +69,8 @@ public class CxfRsSpringEndpoint extends CxfRsEndpoint 
implements BeanIdAware {
 
     @Override
     protected JAXRSClientFactoryBean newJAXRSClientFactoryBean() {
-        return new SpringJAXRSClientFactoryBean();
+        checkBeanType(bean, JAXRSClientFactoryBean.class);
+        return (JAXRSClientFactoryBean)bean;
     }
 
     @Override
diff --git 
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
 
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
index 977d7dc..08ed017 100644
--- 
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
+++ 
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
@@ -24,11 +24,18 @@ import 
org.apache.camel.component.cxf.spring.SpringJAXRSServerFactoryBean;
 import org.apache.camel.test.spring.CamelSpringTestSupport;
 import org.apache.cxf.version.Version;
 import org.junit.Test;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
 import org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 public class CxfRsSpringEndpointTest extends CamelSpringTestSupport {
     
+    private static final String BEAN_SERVICE_ENDPOINT_NAME = "serviceEndpoint";
+    private static final String BEAN_SERVICE_ADDRESS = 
"http://localhost/programmatically";;
+    private static final String BEAN_SERVICE_USERNAME = 
"BEAN_SERVICE_USERNAME";
+    private static final String BEAN_SERVICE_PASSWORD = 
"BEAN_SERVICE_PASSWORD";
+    
     @Test
     public void testCreateCxfRsServerFactoryBean() {
         CxfRsEndpoint endpoint = 
resolveMandatoryEndpoint("cxfrs://bean://rsServer", CxfRsEndpoint.class);
@@ -62,13 +69,51 @@ public class CxfRsSpringEndpointTest extends 
CamelSpringTestSupport {
 
     }
     
+    @Test
+    public void testCreateCxfRsClientFactoryBeanProgrammatically() {
+        
+        CxfRsEndpoint endpoint = resolveMandatoryEndpoint("cxfrs://bean://" + 
BEAN_SERVICE_ENDPOINT_NAME, CxfRsEndpoint.class);
+        SpringJAXRSClientFactoryBean cfb = 
(SpringJAXRSClientFactoryBean)endpoint.createJAXRSClientFactoryBean();
+        
+        assertEquals("Got the wrong address", BEAN_SERVICE_ADDRESS, 
cfb.getAddress());
+        assertNotNull("Service class must not be null", cfb.getServiceClass());
+        assertEquals("Got the wrong ServiceClass", CustomerService.class, 
cfb.getServiceClass());
+        assertEquals("Got the wrong username", BEAN_SERVICE_USERNAME, 
cfb.getUsername());
+        assertEquals("Got the wrong password", BEAN_SERVICE_PASSWORD, 
cfb.getPassword());
+    }
+
+    public static SpringJAXRSClientFactoryBean serviceEndpoint() {
+
+        SpringJAXRSClientFactoryBean clientFactoryBean = new 
SpringJAXRSClientFactoryBean();
+        clientFactoryBean.setAddress(BEAN_SERVICE_ADDRESS);
+        clientFactoryBean.setServiceClass(CustomerService.class);
+        clientFactoryBean.setUsername(BEAN_SERVICE_USERNAME);
+        clientFactoryBean.setPassword(BEAN_SERVICE_PASSWORD);
+
+        return clientFactoryBean;
+    }    
+    
     @Override
     protected AbstractXmlApplicationContext createApplicationContext() {
         String version = Version.getCurrentVersion();
+        
+        ClassPathXmlApplicationContext applicationContext = null;
+        
         if (version.startsWith("2") && (version.contains("2.5") || 
version.contains("2.4"))) {
-            return new ClassPathXmlApplicationContext(new 
String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml"));
+            applicationContext = new ClassPathXmlApplicationContext(new 
String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml"));
+        } else {
+            applicationContext = new ClassPathXmlApplicationContext(new 
String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml"));
    
         }
-        return new ClassPathXmlApplicationContext(new 
String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml"));
+        
+        emulateBeanRegistrationProgrammatically(applicationContext);
+        
+        return applicationContext;
     }
 
-}
+    private void 
emulateBeanRegistrationProgrammatically(ClassPathXmlApplicationContext 
applicationContext) {
+        
+        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) 
applicationContext.getBeanFactory();
+        BeanDefinitionBuilder definitionBuilder = 
BeanDefinitionBuilder.rootBeanDefinition(CxfRsSpringEndpointTest.class.getName()).setFactoryMethod("serviceEndpoint");
+        beanFactory.registerBeanDefinition(BEAN_SERVICE_ENDPOINT_NAME, 
definitionBuilder.getBeanDefinition());
+    }
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
ningji...@apache.org.

Reply via email to