You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by tl...@apache.org on 2006/12/15 08:24:18 UTC

svn commit: r487472 - in /incubator/cxf/trunk: common/common/src/main/java/org/apache/cxf/jaxb/ systests/src/test/java/org/apache/cxf/systest/jaxws/ testutils/ testutils/src/main/java/org/apache/cxf/jaxb_element_test/ testutils/src/main/resources/wsdl/

Author: tli
Date: Thu Dec 14 23:24:17 2006
New Revision: 487472

URL: http://svn.apache.org/viewvc?view=rev&rev=487472
Log:
CXF-305 support JAXBElement<T> param in wrap mode with unit test

Added:
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ServerMisc.java   (with props)
    incubator/cxf/trunk/testutils/src/main/java/org/apache/cxf/jaxb_element_test/
    incubator/cxf/trunk/testutils/src/main/java/org/apache/cxf/jaxb_element_test/JaxbElementTestImpl.java   (with props)
    incubator/cxf/trunk/testutils/src/main/resources/wsdl/jaxb_element_test.wsdl   (with props)
Modified:
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/WrapperHelper.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java
    incubator/cxf/trunk/testutils/pom.xml

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/WrapperHelper.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/WrapperHelper.java?view=diff&rev=487472&r1=487471&r2=487472
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/WrapperHelper.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/jaxb/WrapperHelper.java Thu Dec 14 23:24:17 2006
@@ -19,46 +19,42 @@
 
 package org.apache.cxf.jaxb;
 
-
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Collection;
 import java.util.List;
 
+import javax.xml.bind.JAXBElement;
 import javax.xml.bind.annotation.XmlElement;
 
-
 public final class WrapperHelper {
 
     private WrapperHelper() {
-        //complete
+        // complete
     }
 
-
-    public static void setWrappedPart(String partName, Object wrapperType, Object part) 
+    public static void setWrappedPart(String partName, Object wrapperType, Object part)
         throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
-        
+
         if (part instanceof List) {
             setWrappedListProperty(partName, wrapperType, part);
         } else {
             String fieldName = partName;
             if (JAXBUtils.isJavaKeyword(partName)) {
-                fieldName = JAXBUtils.nameToIdentifier(
-                                partName, 
-                               JAXBUtils.IdentifierType.VARIABLE);
+                fieldName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.VARIABLE);
             }
-            
+
             XmlElement el = null;
             for (Field field : wrapperType.getClass().getDeclaredFields()) {
-              
+
                 if (field.getName().equals(fieldName)) {
-                    //JAXB Type get XmlElement Annotation
+                    // JAXB Type get XmlElement Annotation
                     el = field.getAnnotation(XmlElement.class);
-                   // assert el != null;
-                } 
+                    // assert el != null;
+                }
             }
-            
+
             if (part == null) {
                 if (el != null && !el.nillable()) {
                     throw new IllegalArgumentException("null value for field not permitted.");
@@ -66,36 +62,62 @@
                 return;
             }
 
-            String modifier = 
-                JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER);            
-            
+            String modifier = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER);
+
             boolean setInvoked = false;
             for (Method method : wrapperType.getClass().getMethods()) {
-                if (method.getParameterTypes() != null 
-                    && method.getParameterTypes().length == 1
+                if (method.getParameterTypes() != null && method.getParameterTypes().length == 1
                     && modifier.equals(method.getName())) {
-
-                    method.invoke(wrapperType, part);
+                    if ("javax.xml.bind.JAXBElement".equals(method.getParameterTypes()[0].getName())) {
+                        if (!setJAXBElementValueIntoWrapType(method, wrapperType, part)) {
+                            throw new RuntimeException("Failed to set the part value (" + part 
+                                + ") to wrapper type (" + wrapperType.getClass() + ")");
+                        }
+                    } else {
+                        method.invoke(wrapperType, part);
+                    }
                     setInvoked = true;
                     break;
                 }
             }
-            
+
             if (!setInvoked) {
-                throw new IllegalArgumentException("Could not find a modifier method on Wrapper Type for " 
+                throw new IllegalArgumentException("Could not find a modifier method on Wrapper Type for "
                                                    + partName);
             }
         }
     }
+    @SuppressWarnings("unchecked")
+    private static boolean setJAXBElementValueIntoWrapType(Method method, Object wrapType, Object value) {
+        String typeClassName = wrapType.getClass().getCanonicalName();
+        String objectFactoryClassName = typeClassName.substring(0, typeClassName.lastIndexOf('.'))
+                                        + ".ObjectFactory";
+        try {
+            Object objectFactory = wrapType.getClass().getClassLoader().loadClass(objectFactoryClassName)
+                .newInstance();
+            String methodName = "create" + wrapType.getClass().getSimpleName()
+                                + method.getName().substring(3);
+            Method objectFactoryMethod = objectFactory.getClass().getMethod(methodName, value.getClass());
+            if (objectFactoryMethod != null) {
+                JAXBElement je = (JAXBElement)objectFactoryMethod.invoke(objectFactory, value);
+                method.invoke(wrapType, je);
+            } else {
+                return false;
+            }
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+
+    }
 
-    private static void setWrappedListProperty(String partName, Object wrapperType, Object part) 
+    private static void setWrappedListProperty(String partName, Object wrapperType, Object part)
         throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
 
         String accessorName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
         for (Method method : wrapperType.getClass().getMethods()) {
             if (accessorName.equals(method.getName()) 
-                && List.class.isAssignableFrom(method.getReturnType())) { 
-                
+                && List.class.isAssignableFrom(method.getReturnType())) {
                 Object ret = method.invoke(wrapperType);
                 Method addAll = ret.getClass().getMethod("addAll", Collection.class);
                 addAll.invoke(ret, part);
@@ -103,22 +125,21 @@
             }
         }
     }
-    
-    public static Object getWrappedPart(String partName, Object wrapperType, Class<?> partClazz) 
+
+    public static Object getWrappedPart(String partName, Object wrapperType, Class<?> partClazz)
         throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
 
         String accessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
-        
+
         if (partClazz.equals(boolean.class) || partClazz.equals(Boolean.class)) {
-            //JAXB Exception to get the Boolean property
+            // JAXB Exception to get the Boolean property
             accessor = accessor.replaceFirst("get", "is");
         }
-        
+
         for (Method method : wrapperType.getClass().getMethods()) {
-            if (method.getParameterTypes().length == 0
-                && accessor.equals(method.getName())) {
+            if (method.getParameterTypes().length == 0 && accessor.equals(method.getName())) {
 
-                return method.invoke(wrapperType);
+                return getValue(method, wrapperType);
             }
         }
         return null;
@@ -137,30 +158,38 @@
         for (Method method : wrapperType.getClass().getMethods()) {
             if (method.getParameterTypes().length == 0 && accessor.equals(method.getName())) {
 
-                return method.invoke(wrapperType);
+                return getValue(method, wrapperType);
             }
         }
         return null;
     }
 
-    
-
-    public static Object getWrappedPart(String partName, Object wrapperType)
-        throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
+    public static Object getWrappedPart(String partName, Object wrapperType) throws IllegalAccessException,
+        NoSuchMethodException, InvocationTargetException {
         String accessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
 
-        // TODO: There must be a way to determine the class by inspecting wrapperType
+        // TODO: There must be a way to determine the class by inspecting
+        // wrapperType
         // if (partClazz.equals(boolean.class) ||
         // partClazz.equals(Boolean.class)) {
         // //JAXB Exception to get the Boolean property
         // accessor = accessor.replaceFirst("get", "is");
-        //        }
+        // }
         for (Method method : wrapperType.getClass().getMethods()) {
-            if (method.getParameterTypes().length == 0
-                && accessor.equals(method.getName())) {
-                return method.invoke(wrapperType);
+            if (method.getParameterTypes().length == 0 && accessor.equals(method.getName())) {
+                return getValue(method, wrapperType);
             }
         }
         return null;
+    }
+
+    private static Object getValue(Method method, Object in) throws IllegalAccessException,
+        InvocationTargetException {
+        if ("javax.xml.bind.JAXBElement".equals(method.getReturnType().getCanonicalName())) {
+            JAXBElement je = (JAXBElement)method.invoke(in);
+            return je == null ? je : je.getValue();
+        } else {
+            return method.invoke(in);
+        }
     }
 }

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java?view=diff&rev=487472&r1=487471&r2=487472
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java (original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerMiscTest.java Thu Dec 14 23:24:17 2006
@@ -22,52 +22,30 @@
 import java.lang.reflect.UndeclaredThrowableException;
 
 import javax.xml.namespace.QName;
-import javax.xml.ws.Endpoint;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 import org.apache.cxf.anonymous_complex_type.AnonymousComplexType;
-import org.apache.cxf.anonymous_complex_type.AnonymousComplexTypeImpl;
 import org.apache.cxf.anonymous_complex_type.AnonymousComplexTypeService;
 import org.apache.cxf.anonymous_complex_type.SplitName;
 import org.apache.cxf.anonymous_complex_type.SplitNameResponse;
 import org.apache.cxf.anonymous_complex_type.SplitNameResponse.Names;
+import org.apache.cxf.jaxb_element_test.JaxbElementTest;
+import org.apache.cxf.jaxb_element_test.JaxbElementTest_Service;
 import org.apache.cxf.systest.common.ClientServerSetupBase;
 import org.apache.cxf.systest.common.ClientServerTestBase;
-import org.apache.cxf.systest.common.TestServerBase;
 
 public class ClientServerMiscTest extends ClientServerTestBase {
 
     private final QName portName = new QName("http://cxf.apache.org/anonymous_complex_type/",
             "anonymous_complex_typeSOAP");
 
-    public static class Server extends TestServerBase {
-
-        protected void run() {
-            Object implementor = new AnonymousComplexTypeImpl();
-            String address = "http://localhost:9000/anonymous_complex_typeSOAP";
-            Endpoint.publish(address, implementor);
-        }
-
-        public static void main(String[] args) {
-            try {
-                Server s = new Server();
-                s.start();
-            } catch (Exception ex) {
-                ex.printStackTrace();
-                System.exit(-1);
-            } finally {
-                System.out.println("done!");
-            }
-        }
-    }
-
     public static Test suite() throws Exception {
         TestSuite suite = new TestSuite(ClientServerMiscTest.class);
         return new ClientServerSetupBase(suite) {
             public void startServers() throws Exception {
-                assertTrue("server did not launch correctly", launchServer(Server.class));
+                assertTrue("server did not launch correctly", launchServer(ServerMisc.class));
             }
         };
     }
@@ -77,7 +55,7 @@
         AnonymousComplexTypeService actService = new AnonymousComplexTypeService();
         assertNotNull(actService);
         AnonymousComplexType act = actService.getPort(portName, AnonymousComplexType.class);
-        
+
         try {
             Names reply = act.splitName("Tom Li");
             assertNotNull("no response received from service", reply);
@@ -93,7 +71,7 @@
         AnonymousComplexTypeService actService = new AnonymousComplexTypeService();
         assertNotNull(actService);
         AnonymousComplexType act = actService.getPort(portName, AnonymousComplexType.class);
-        
+
         try {
             SplitName name = new SplitName();
             name.setName("Tom Li");
@@ -101,6 +79,27 @@
             assertNotNull("no response received from service", reply);
             assertEquals("Tom", reply.getNames().getFirst());
             assertEquals("Li", reply.getNames().getSecond());
+        } catch (UndeclaredThrowableException ex) {
+            throw (Exception) ex.getCause();
+        }
+    }
+
+    public void testMinOccursAndNillableJAXBElement() throws Exception {
+
+        JaxbElementTest_Service service = new JaxbElementTest_Service();
+        assertNotNull(service);
+        JaxbElementTest port = service.getPort(JaxbElementTest.class);
+
+        try {
+
+            String response = port.newOperation("hello");
+            assertNotNull(response);
+            assertEquals("in=hello", response);
+
+            response = port.newOperation(null);
+            assertNotNull(response);
+            assertEquals("in=null", response);
+
         } catch (UndeclaredThrowableException ex) {
             throw (Exception) ex.getCause();
         }

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ServerMisc.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ServerMisc.java?view=auto&rev=487472
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ServerMisc.java (added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ServerMisc.java Thu Dec 14 23:24:17 2006
@@ -0,0 +1,52 @@
+/**
+ * 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.systest.jaxws;
+
+import javax.xml.ws.Endpoint;
+
+import org.apache.cxf.anonymous_complex_type.AnonymousComplexTypeImpl;
+import org.apache.cxf.jaxb_element_test.JaxbElementTestImpl;
+import org.apache.cxf.systest.common.TestServerBase;
+
+
+public class ServerMisc extends TestServerBase {
+
+    protected void run() {
+        Object implementor1 = new AnonymousComplexTypeImpl();
+        String address = "http://localhost:9000/anonymous_complex_typeSOAP";
+        Endpoint.publish(address, implementor1);
+
+        Object implementor2 = new JaxbElementTestImpl();
+        address = "http://localhost:9001/jaxb_element_test";
+        Endpoint.publish(address, implementor2);
+    }
+
+    public static void main(String[] args) {
+        try {
+            ServerMisc s = new ServerMisc();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+}

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ServerMisc.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ServerMisc.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/cxf/trunk/testutils/pom.xml
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/testutils/pom.xml?view=diff&rev=487472&r1=487471&r2=487472
==============================================================================
--- incubator/cxf/trunk/testutils/pom.xml (original)
+++ incubator/cxf/trunk/testutils/pom.xml Thu Dec 14 23:24:17 2006
@@ -289,6 +289,9 @@
                                 <wsdlOption>
                                     <wsdl>${basedir}/src/main/resources/wsdl/calculator.wsdl</wsdl>
                                 </wsdlOption>
+                                <wsdlOption>
+                                    <wsdl>${basedir}/src/main/resources/wsdl/jaxb_element_test.wsdl</wsdl>
+                                </wsdlOption>
 
                             </wsdlOptions>
                         </configuration>

Added: incubator/cxf/trunk/testutils/src/main/java/org/apache/cxf/jaxb_element_test/JaxbElementTestImpl.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/testutils/src/main/java/org/apache/cxf/jaxb_element_test/JaxbElementTestImpl.java?view=auto&rev=487472
==============================================================================
--- incubator/cxf/trunk/testutils/src/main/java/org/apache/cxf/jaxb_element_test/JaxbElementTestImpl.java (added)
+++ incubator/cxf/trunk/testutils/src/main/java/org/apache/cxf/jaxb_element_test/JaxbElementTestImpl.java Thu Dec 14 23:24:17 2006
@@ -0,0 +1,39 @@
+/**
+ * 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.jaxb_element_test;
+
+import javax.jws.WebService;
+
+@WebService(serviceName = "jaxb_element_test", 
+        portName = "jaxb_element_testSOAP", 
+        targetNamespace = "http://cxf.apache.org/jaxb_element_test/", 
+        endpointInterface = "org.apache.cxf.jaxb_element_test.JaxbElementTest")
+public class JaxbElementTestImpl implements JaxbElementTest {
+
+    public String newOperation(String in) {
+        // TODO Auto-generated method stub
+
+        if (in == null) {
+            return "in=null";
+        } else {
+            return "in=" + in;
+        }
+    }
+}

Propchange: incubator/cxf/trunk/testutils/src/main/java/org/apache/cxf/jaxb_element_test/JaxbElementTestImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/testutils/src/main/java/org/apache/cxf/jaxb_element_test/JaxbElementTestImpl.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/cxf/trunk/testutils/src/main/resources/wsdl/jaxb_element_test.wsdl
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/testutils/src/main/resources/wsdl/jaxb_element_test.wsdl?view=auto&rev=487472
==============================================================================
--- incubator/cxf/trunk/testutils/src/main/resources/wsdl/jaxb_element_test.wsdl (added)
+++ incubator/cxf/trunk/testutils/src/main/resources/wsdl/jaxb_element_test.wsdl Thu Dec 14 23:24:17 2006
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
+    xmlns:tns="http://cxf.apache.org/jaxb_element_test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
+    xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="jaxb_element_test"
+    targetNamespace="http://cxf.apache.org/jaxb_element_test/">
+    <wsdl:types>
+        <xsd:schema targetNamespace="http://cxf.apache.org/jaxb_element_test/">
+            <xsd:element name="NewOperationResponse" type="xsd:string">
+                <xsd:complexType>
+                    <xsd:sequence>
+                        <xsd:element name="out" minOccurs="0" nillable="true" type="xsd:string" />
+                    </xsd:sequence>
+                </xsd:complexType>
+            </xsd:element>
+            <xsd:element name="NewOperation" type="xsd:string">
+                <xsd:complexType>
+                    <xsd:sequence>
+                        <xsd:element name="in" minOccurs="0" nillable="true" type="xsd:string" />
+                    </xsd:sequence>
+                </xsd:complexType>
+            </xsd:element>
+        </xsd:schema>
+    </wsdl:types>
+    <wsdl:message name="NewOperationResponse">
+        <wsdl:part element="tns:NewOperationResponse" name="NewOperationResponse" />
+    </wsdl:message>
+    <wsdl:message name="NewOperation">
+        <wsdl:part element="tns:NewOperation" name="NewOperation" />
+    </wsdl:message>
+    <wsdl:portType name="jaxb_element_test">
+        <wsdl:operation name="NewOperation">
+            <wsdl:input message="tns:NewOperation" />
+            <wsdl:output message="tns:NewOperationResponse" />
+        </wsdl:operation>
+    </wsdl:portType>
+    <wsdl:binding name="jaxb_element_testSOAP" type="tns:jaxb_element_test">
+        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
+        <wsdl:operation name="NewOperation">
+            <soap:operation soapAction="http://cxf.apache.org/jaxb_element_test/NewOperation" />
+            <wsdl:input>
+                <soap:body use="literal" />
+            </wsdl:input>
+            <wsdl:output>
+                <soap:body use="literal" />
+            </wsdl:output>
+        </wsdl:operation>
+    </wsdl:binding>
+    <wsdl:service name="jaxb_element_test">
+        <wsdl:port binding="tns:jaxb_element_testSOAP" name="jaxb_element_testSOAP">
+            <soap:address location="http://localhost:9001/jaxb_element_test" />
+        </wsdl:port>
+    </wsdl:service>
+</wsdl:definitions>

Propchange: incubator/cxf/trunk/testutils/src/main/resources/wsdl/jaxb_element_test.wsdl
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/testutils/src/main/resources/wsdl/jaxb_element_test.wsdl
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: incubator/cxf/trunk/testutils/src/main/resources/wsdl/jaxb_element_test.wsdl
------------------------------------------------------------------------------
    svn:mime-type = text/xml