You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2010/05/13 16:07:59 UTC

svn commit: r943892 - in /camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf: CxfConsumerPayloadTest.java converter/MyCxfCustomerConverter.java

Author: ningjiang
Date: Thu May 13 14:07:59 2010
New Revision: 943892

URL: http://svn.apache.org/viewvc?rev=943892&view=rev
Log:
CAMEL-2714 Added a unit test to show how to use the customer converter to turn a CxfPayLoad object into String

Added:
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java   (with props)
Modified:
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java?rev=943892&r1=943891&r2=943892&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java (original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadTest.java Thu May 13 14:07:59 2010
@@ -36,6 +36,10 @@ public class CxfConsumerPayloadTest exte
     private static final String ECHO_BOOLEAN_RESPONSE = "<ns1:echoBooleanResponse xmlns:ns1=\"http://cxf.component.camel.apache.org/\">"
             + "<return xmlns=\"http://cxf.component.camel.apache.org/\">true</return>"
             + "</ns1:echoBooleanResponse>";
+    private static final String ECHO_REQUEST = "<ns1:echo xmlns:ns1=\"http://cxf.component.camel.apache.org/\">"
+            + "<arg0 xmlns=\"http://cxf.component.camel.apache.org/\">Hello World!</arg0></ns1:echo>";
+    private static final String ECHO_BOOLEAN_REQUEST = "<ns1:echoBoolean xmlns:ns1=\"http://cxf.component.camel.apache.org/\">"
+            + "<arg0 xmlns=\"http://cxf.component.camel.apache.org/\">true</arg0></ns1:echoBoolean>";
 
     // START SNIPPET: payload
     protected RouteBuilder createRouteBuilder() {
@@ -43,14 +47,19 @@ public class CxfConsumerPayloadTest exte
             public void configure() {
                 from(SIMPLE_ENDPOINT_URI + "&dataFormat=PAYLOAD").to("log:info").process(new Processor() {
                     @SuppressWarnings("unchecked")
-                    public void process(final Exchange exchange) throws Exception {                        
+                    public void process(final Exchange exchange) throws Exception {
                         CxfPayload<SoapHeader> requestPayload = exchange.getIn().getBody(CxfPayload.class);
                         List<Element> inElements = requestPayload.getBody();
                         List<Element> outElements = new ArrayList<Element>();
+                        // You can use a customer toStringConverter to turn a CxfPayLoad message into String as you want                        
+                        String request = exchange.getIn().getBody(String.class);
                         XmlConverter converter = new XmlConverter();
                         String documentString = ECHO_RESPONSE;
                         if (inElements.get(0).getLocalName().equals("echoBoolean")) {
                             documentString = ECHO_BOOLEAN_RESPONSE;
+                            assertEquals("Get a wrong request", ECHO_BOOLEAN_REQUEST, request);
+                        } else {
+                            assertEquals("Get a wrong request", ECHO_REQUEST, request);
                         }
                         Document outDocument = converter.toDOMDocument(documentString);
                         outElements.add(outDocument.getDocumentElement());

Added: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java?rev=943892&view=auto
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java (added)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java Thu May 13 14:07:59 2010
@@ -0,0 +1,51 @@
+/**
+ * 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.camel.component.cxf.converter;
+
+import javax.xml.transform.TransformerException;
+
+import org.w3c.dom.Element;
+
+import org.apache.camel.Converter;
+import org.apache.camel.component.cxf.CxfPayload;
+import org.apache.camel.converter.jaxp.XmlConverter;
+
+// This converter is used to show how to override the CxfPayload default toString converter
+@Converter 
+public final class MyCxfCustomerConverter {
+    
+    private MyCxfCustomerConverter() {
+        //Helper class
+    }
+    
+    @Converter 
+    public static String cxfPayloadToString(final CxfPayload payload) {
+        XmlConverter converter = new XmlConverter();
+        StringBuilder buf = new StringBuilder();
+        for (Object element : payload.getBody()) {
+            String elementString = "";
+            try {
+                elementString = converter.toString((Element)element);
+            } catch (TransformerException e) {
+                elementString = element.toString();
+            }
+            buf.append(elementString);
+        }
+        return buf.toString();
+    }
+
+}

Propchange: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date