Author: ningjiang
Date: Mon Oct 19 02:50:12 2009
New Revision: 826581

URL: http://svn.apache.org/viewvc?rev=826581&view=rev
Log:
MR-189 added more unit tests on camel-cxf

Added:
    
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfUtilsTest.java
   (with props)
Removed:
    
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/UriUtils.java
Modified:
    
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
    
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java

Modified: 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java?rev=826581&r1=826580&r2=826581&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
 (original)
+++ 
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsInvoker.java
 Mon Oct 19 02:50:12 2009
@@ -53,7 +53,11 @@
         org.apache.camel.Exchange camelExchange = endpoint.createExchange(ep);
         CxfRsBinding binding = endpoint.getBinding();
         binding.populateExchangeFromCxfRsRequest(cxfExchange, camelExchange, 
method, paramArray);
-        processor.process(camelExchange);
+        try {
+            processor.process(camelExchange);
+        } catch (Exception exception) {
+            camelExchange.setException(exception);
+        }
         if (camelExchange.getException() != null) {
             Throwable exception = camelExchange.getException();
             Object result = null;

Modified: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java?rev=826581&r1=826580&r2=826581&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
 (original)
+++ 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
 Mon Oct 19 02:50:12 2009
@@ -16,12 +16,17 @@
  */
 package org.apache.camel.component.cxf.jaxrs;
 
+import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.net.URL;
 
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
+import org.apache.camel.builder.NoErrorHandlerBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.cxf.CxfConstants;
 import org.apache.camel.component.cxf.jaxrs.testbean.Customer;
@@ -37,6 +42,7 @@
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() {
+                errorHandler(new NoErrorHandlerBuilder());
                 from(CXF_RS_ENDPOINT_URI).process(new Processor() {
 
                     public void process(Exchange exchange) throws Exception {
@@ -48,13 +54,17 @@
                         if ("getCustomer".equals(operationName)) {
                             String httpMethod = 
inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
                             assertEquals("Get a wrong http method", "GET", 
httpMethod);
-                            String uri = 
inMessage.getHeader(Exchange.HTTP_URI, String.class);                           
 
-                            assertEquals("Get a wrong http uri", 
"/customerservice/customers/126", uri);
-                            Customer customer = new Customer();
-                            customer.setId(Long.parseLong(id));
-                            customer.setName("Willem");
-                            // We just put the response Object into the out 
message body
-                            exchange.getOut().setBody(customer);
+                            String uri = 
inMessage.getHeader(Exchange.HTTP_URI, String.class);
+                            if ("/customerservice/customers/126".equals(uri)) 
{                            
+                                Customer customer = new Customer();
+                                customer.setId(Long.parseLong(id));
+                                customer.setName("Willem");
+                                // We just put the response Object into the 
out message body
+                                exchange.getOut().setBody(customer);
+                            } else {
+                                Response r = 
Response.status(404).entity("Can't found the customer with uri " + uri).build();
+                                throw new WebApplicationException(r);
+                            }
                         }
                     }
                     
@@ -72,6 +82,19 @@
         assertEquals("{\"Customer\":{\"id\":126,\"name\":\"Willem\"}}", 
CxfUtils.getStringFromInputStream(in));
        
     }
+    
+    @Test
+    public void testGetWrongCustomer() throws Exception {
+        URL url = new 
URL("http://localhost:9000/customerservice/customers/456";);
+        try {
+            url.openStream();
+            fail("Expect to get exception here");
+        } catch (FileNotFoundException exception) {
+            // do nothing here
+        }
+        
+        
+    }
         
 
 }

Added: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfUtilsTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfUtilsTest.java?rev=826581&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfUtilsTest.java
 (added)
+++ 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfUtilsTest.java
 Mon Oct 19 02:50:12 2009
@@ -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.camel.component.cxf.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
+import org.w3c.dom.Element;
+
+import org.apache.camel.converter.jaxp.XmlConverter;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CxfUtilsTest extends Assert {
+    private static final String TEST_XML1 = 
+        "<root><test1 id=\"1\"><test2 id=\"3\" 
xmlns=\"http://camel.apache.org/schema/one\";>hello</test2></test1></root>";
+    private static final String EXPECTED_STRING1 = "<test1 id=\"1\"><test2 
xmlns=\"http://camel.apache.org/schema/one\"; id=\"3\">hello</test2></test1>";   
     
+    private static final String TEST_XML2 = 
+        "<root xmlns=\"http://camel.apache.org/schema/one\"; 
xmlns:two=\"http://camel.apache.org/schema/two\";><test1 id=\"1\"><two:test2 
id=\"3\">hello</two:test2></test1></root>";
+    private static final String EXPECTED_STRING2 = 
+        "<test1 xmlns=\"http://camel.apache.org/schema/one\"; 
xmlns:two=\"http://camel.apache.org/schema/two\"; id=\"1\"><two:test2 
id=\"3\">hello</two:test2></test1>";
+    @Test
+    public void testXmlToString() throws Exception {
+        assertEquals("Get unexpected String", EXPECTED_STRING1, 
getSubElementString(TEST_XML1));
+        assertEquals("Get unexpected String", EXPECTED_STRING2, 
getSubElementString(TEST_XML2));
+    }
+    
+    private String getSubElementString(String string) throws Exception {
+        InputStream is = new ByteArrayInputStream(string.getBytes("UTF-8"));
+        XmlConverter converter = new XmlConverter();
+        Element element = converter.toDOMElement(converter.toDOMSource(is));
+        Element subElement = (Element)element.getFirstChild();
+        return CxfUtils.elementToString(subElement);
+        
+    }
+
+}

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

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


Reply via email to