geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r574379 - in /geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src: main/java/org/apache/greeter_control/GreeterImpl.java test/java/org/apache/geronimo/testsuite/testset/JaxWSTest.java test/resources/request2.xml
Date Mon, 10 Sep 2007 21:33:13 GMT
Author: gawor
Date: Mon Sep 10 14:33:13 2007
New Revision: 574379

URL: http://svn.apache.org/viewvc?rev=574379&view=rev
Log:
improved tests and added a basic test case for exception handling

Added:
    geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/resources/request2.xml
  (with props)
Modified:
    geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/main/java/org/apache/greeter_control/GreeterImpl.java
    geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/java/org/apache/geronimo/testsuite/testset/JaxWSTest.java

Modified: geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/main/java/org/apache/greeter_control/GreeterImpl.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/main/java/org/apache/greeter_control/GreeterImpl.java?rev=574379&r1=574378&r2=574379&view=diff
==============================================================================
--- geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/main/java/org/apache/greeter_control/GreeterImpl.java
(original)
+++ geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/main/java/org/apache/greeter_control/GreeterImpl.java
Mon Sep 10 14:33:13 2007
@@ -30,8 +30,13 @@
 
 import javax.jws.WebService;
 import javax.jws.HandlerChain;
+import javax.xml.namespace.QName;
+import javax.xml.soap.SOAPException;
+import javax.xml.soap.SOAPFactory;
+import javax.xml.soap.SOAPFault;
 import javax.xml.ws.WebServiceContext;
 import javax.xml.ws.handler.MessageContext;
+import javax.xml.ws.soap.SOAPFaultException;
 
 /* serviceName, portName specified in webservices.xml */
 @WebService(serviceName = "SOAPService",
@@ -82,11 +87,6 @@
 			values.add("BAR");
 			responseHeaders.put("foo", values);
         }
-        /*
-        // make return code 201
-        ctx.put(MessageContext.HTTP_RESPONSE_CODE,
-                new Integer(201));
-        */
 
         return greeting + " " + me;
     }
@@ -103,7 +103,18 @@
 
     public String sayHi() {
         LOG.info("Invoking sayHi ");
-		return "Hi";
+        
+        SOAPFault fault = null;
+        try {
+            fault = SOAPFactory.newInstance().createFault();
+            fault.setFaultCode(new QName("http://foo", "MyFaultCode"));
+            fault.setFaultString("my error");
+            fault.setFaultActor("my actor");
+        } catch (SOAPException ex) {
+            throw new RuntimeException(ex);
+        }
+
+        throw new SOAPFaultException(fault);
     }
 
     public void greetMeOneWay(String me){

Modified: geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/java/org/apache/geronimo/testsuite/testset/JaxWSTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/java/org/apache/geronimo/testsuite/testset/JaxWSTest.java?rev=574379&r1=574378&r2=574379&view=diff
==============================================================================
--- geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/java/org/apache/geronimo/testsuite/testset/JaxWSTest.java
(original)
+++ geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/java/org/apache/geronimo/testsuite/testset/JaxWSTest.java
Mon Sep 10 14:33:13 2007
@@ -21,34 +21,30 @@
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.StringReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.util.Iterator;
 import java.util.Map;
 
-import javax.wsdl.factory.WSDLFactory;
 import javax.wsdl.Definition;
-import javax.wsdl.Operation;
 import javax.wsdl.PortType;
+import javax.wsdl.factory.WSDLFactory;
 import javax.wsdl.xml.WSDLReader;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.namespace.QName;
 
+import org.apache.geronimo.testsupport.TestSupport;
+import org.testng.annotations.Test;
 import org.w3c.dom.Document;
-import org.w3c.dom.NodeList;
 import org.w3c.dom.Element;
-
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
 import org.xml.sax.InputSource;
 
-import org.apache.geronimo.testsupport.TestSupport;
-import org.testng.annotations.Test;
-
-/**
- * FIXME: update test code as CXF/G integration gets fixed.
- */
 public class JaxWSTest extends TestSupport {
 
     private String baseURL = "http://localhost:8080/";
@@ -64,7 +60,6 @@
         testInvocation("/servlet2");
     }
 
-    @Test
     private void testInvocation(String servlet) throws Exception {
         String warName = System.getProperty("webAppName");
         assertNotNull("Web application name not specified", warName);
@@ -75,76 +70,149 @@
         URL url = new URL(baseURL + warName + servlet);
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         try {
-            conn.setDoOutput(true);
-            conn.setDoInput(true);
-            conn.setUseCaches(false);
-            conn.setRequestMethod("POST");
-            conn.setRequestProperty("Content-Type", "text/xml");
-
-            OutputStream out = conn.getOutputStream();
-
-            byte[] data = new byte[1024];
-            int read = 0;
-            while ((read = requestInput.read(data, 0, data.length)) != -1) {
-                out.write(data, 0, read);
-            }
+            String reply = call(requestInput, conn);
+            
+            assertEquals("responseCode", 200, conn.getResponseCode());
+            String contentType = conn.getHeaderField("Content-Type");
+            assertTrue("contentType", contentType.indexOf("text/xml") != -1);
+                        
+            InputSource is = new InputSource(new StringReader(reply));
+            Document doc = parseMessage(is);
+            
+            Text replyMsg = findText(doc.getDocumentElement(), "Hello foo bar");
+            assertTrue("reploy message", replyMsg != null);
+            
+        } finally {
+            conn.disconnect();
+        }
+    }
+    
+    @Test
+    public void testInvocationFault1() throws Exception {
+        testInvocationFault("/servlet1");
+    }
 
-            requestInput.close();
+    @Test
+    public void testInvocationFault2() throws Exception {
+        testInvocationFault("/servlet2");
+    }
+    
+    private void testInvocationFault(String servlet) throws Exception {
+        String warName = System.getProperty("webAppName");
+        assertNotNull("Web application name not specified", warName);
+        
+        InputStream requestInput = JaxWSTest.class.getResourceAsStream("/request2.xml");
+        assertNotNull("SOAP request not specified", requestInput);
+                
+        URL url = new URL(baseURL + warName + servlet);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        try {
+            String reply = call(requestInput, conn);
 
-            out.flush();
-            out.close();
+            assertEquals("responseCode", 500, conn.getResponseCode());
+            String contentType = conn.getHeaderField("Content-Type");
+            assertTrue("contentType", contentType.indexOf("text/xml") != -1);
+                        
+            InputSource is = new InputSource(new StringReader(reply));
+            Document doc = parseMessage(is);
+            
+            Element faultString = findElement(doc.getDocumentElement(), "faultstring");
+            assertTrue("faultString", faultString != null);
+            assertEquals("faultString value", "my error", faultString.getFirstChild().getNodeValue());
+            
+        } finally {
+            conn.disconnect();
+        }
+    }
+    
+    private Document parseMessage(InputSource is) throws Exception {
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        dbf.setNamespaceAware(true);
+        DocumentBuilder db = dbf.newDocumentBuilder();
 
-            boolean found = false;
-            boolean found2 = false;
+        Document doc = db.parse(is);
+        return doc;
+    }
 
-            // FIXME: Simple test is required for now, as CXF has problems
-            BufferedReader in = 
-                new BufferedReader(new InputStreamReader(conn.getInputStream()));
-            String inputLine;
-            while ((inputLine = in.readLine()) != null) {
-                System.out.println(inputLine);
-
-                if (found == false &&
-                    inputLine.indexOf("foo bar") != -1) {
-                    found = true;
-                }
-                if (found2 == false &&
-                    inputLine.indexOf("Hello") != -1) {
-                    found2 = true;
+    private Element findElement(Element element, String name) {
+        NodeList list = element.getChildNodes();
+        for (int i = 0; i < list.getLength(); i++) {
+            Node child = (Node)list.item(i);
+            if (child instanceof Element) {
+                Element childEl = (Element)child;
+                if (name.equals(childEl.getLocalName())) {
+                    return childEl;
+                } else {
+                    childEl = findElement(childEl, name);
+                    if (childEl != null) {
+                        return childEl;
+                    }
                 }
             }
-            in.close();
-
-            assertTrue("Reply", found);
-
-            if(!found2) {
-                System.out.println("Did not find \"Hello\" in the Response");
+        }
+        return null;
+    }
+    
+    private Text findText(Element element, String value) {
+        NodeList list = element.getChildNodes();
+        for (int i = 0; i < list.getLength(); i++) {
+            Node child = (Node)list.item(i);
+            if (child instanceof Text) {
+                Text text = (Text)child;
+                if (text.getData().indexOf(value) != -1) {
+                    return text;
+                }
+            } else if (child instanceof Element) {
+                Element childEl = (Element)child;
+                Text text = findText(childEl, value);
+                if (text != null) {
+                    return text;
+                }
             }
+        }   
+        return null;
+    }
+    
+    private String call(InputStream requestInput, HttpURLConnection conn) throws IOException
{        
+        conn.setDoOutput(true);
+        conn.setDoInput(true);
+        conn.setUseCaches(false);
+        conn.setRequestMethod("POST");
+        conn.setRequestProperty("Content-Type", "text/xml");
+
+        OutputStream out = conn.getOutputStream();
+
+        byte[] data = new byte[1024];
+        int read = 0;
+        while ((read = requestInput.read(data, 0, data.length)) != -1) {
+            out.write(data, 0, read);
+        }
 
-            /* Better test, disabled for now
-            InputSource is = new InputSource(conn.getInputStream());
-
-            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-            dbf.setNamespaceAware(true);
-            DocumentBuilder db = dbf.newDocumentBuilder();
-
-            Document doc = db.parse(is);
-            
-            NodeList nodes = doc.getElementsByTagNameNS("http://apache.org/hello_world_soap_http/types",
-                                                        "responseType");
-            assertEquals(1, nodes.getLength());
-
-            Element element = (Element)nodes.item(0);
-
-            assertTrue(element.getFirstChild() != null);
+        requestInput.close();
 
-            assertEquals("Hello foo bar", element.getFirstChild().getNodeValue());
-            */
+        out.flush();
+        out.close();
 
-        } finally {
-            conn.disconnect();
+        InputStream is = null;
+        
+        try {
+            is = conn.getInputStream();
+        } catch (IOException e) {
+            is = conn.getErrorStream();
         }
+        
+        StringBuffer buf = new StringBuffer();
+        BufferedReader in = new BufferedReader(new InputStreamReader(is));
+        String inputLine;
+        while ((inputLine = in.readLine()) != null) {
+            System.out.println(inputLine);
+            buf.append(inputLine);
+        }
+        in.close();
+        
+        return buf.toString();
     }
+
 
     @Test
     public void testWSDL1() throws Exception {

Added: geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/resources/request2.xml
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/resources/request2.xml?rev=574379&view=auto
==============================================================================
--- geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/resources/request2.xml
(added)
+++ geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/resources/request2.xml
Mon Sep 10 14:33:13 2007
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
+   <soap:Body>
+     <sayHi xmlns="http://apache.org/greeter_control/types"/>
+   </soap:Body>
+</soap:Envelope>

Propchange: geronimo/server/trunk/testsuite/webservices-testsuite/jaxws-tests/jaxws-war/src/test/resources/request2.xml
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message