dblevins    2005/03/09 01:19:56

  Added:       modules/core/src/test/org/openejb/server/axis
                        WSContainerTest.java
  Log:

  First run of axis integration.  more to sew up yet.
  
  Revision  Changes    Path
  1.1                  
openejb/modules/core/src/test/org/openejb/server/axis/WSContainerTest.java
  
  Index: WSContainerTest.java
  ===================================================================
  /**
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "OpenEJB" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of The OpenEJB Group.  For written permission,
   *    please contact [EMAIL PROTECTED]
   *
   * 4. Products derived from this Software may not be called "OpenEJB"
   *    nor may "OpenEJB" appear in their names without prior written
   *    permission of The OpenEJB Group. OpenEJB is a registered
   *    trademark of The OpenEJB Group.
   *
   * 5. Due credit should be given to the OpenEJB Project
   *    (http://openejb.org/).
   *
   * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
   *
   * $Id: WSContainerTest.java,v 1.1 2005/03/09 06:19:56 dblevins Exp $
   */
  package org.openejb.server.axis;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintStream;
  import java.net.HttpURLConnection;
  import java.net.InetAddress;
  import java.net.URI;
  import java.net.URL;
  import java.util.jar.JarFile;
  import javax.management.ObjectName;
  import javax.wsdl.Definition;
  import javax.wsdl.factory.WSDLFactory;
  import javax.wsdl.xml.WSDLReader;
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  
  import junit.framework.TestCase;
  import org.apache.geronimo.kernel.Kernel;
  import org.apache.geronimo.kernel.management.State;
  import org.apache.geronimo.axis.builder.AxisServiceBuilder;
  import org.apache.axis.description.JavaServiceDesc;
  import org.openejb.server.StandardServiceStackGBean;
  import org.openejb.server.soap.SoapHttpListenerGBean;
  import org.openejb.server.httpd.HttpServerGBean;
  import org.openejb.slsb.MockEJBContainer;
  import org.openejb.slsb.MockEJBContainerGBean;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.DefaultHandler;
  
  public class WSContainerTest extends TestCase {
  
  //    static {
  //        org.apache.log4j.BasicConfigurator.configure();
  //    }
  
  
      private Definition getDefinition(URL wsdlURL) throws Exception {
          WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader();
          return wsdlReader.readWSDL(wsdlURL.toExternalForm());
      }
  
      // TODO  Get these running again
      public void xtestGetWSDL() throws Exception {
          Kernel kernel = new Kernel("wstest");
          kernel.boot();
  
          URL wsdlURL = new 
File("target/test-ejb-jar/META-INF/wsdl/test-ejb.wsdl").toURL();
          JarFile jarFile = new JarFile("target/test-ejb-jar.jar");
          String ejbName = "SimpleStatelessSession";
          ClassLoader classLoader = this.getClass().getClassLoader();
  
          JavaServiceDesc serviceDesc = 
AxisServiceBuilder.createEJBServiceDesc(jarFile, ejbName, classLoader);
  
          ObjectName ejbContainer = MockEJBContainer.addGBean(kernel, 
"MockEJB");
          ObjectName listener = SoapHttpListenerGBean.addGBean(kernel, 
"HTTPSOAP");
          ObjectName wsContainer = WSContainerGBean.addGBean(kernel, 
"HTTPSOAP", ejbContainer, listener, new URI("/services/Simple"), wsdlURL, 
serviceDesc);
          ObjectName server = HttpServerGBean.addGBean(kernel, "HTTPSOAP", 
listener);
          ObjectName stack = StandardServiceStackGBean.addGBean(kernel, 
"HTTPSOAP", 0, InetAddress.getByName("localhost"), null, 1, 5, null, null, 
server);
  
          assertRunning(kernel, ejbContainer);
          assertRunning(kernel, wsContainer);
          assertRunning(kernel, listener);
          assertRunning(kernel, server);
          assertRunning(kernel, stack);
  
          InputStream in = null;
          try {
              kernel.setAttribute(stack, "soTimeout", new Integer(1000));
              int port = ((Integer) kernel.getAttribute(stack, 
"port")).intValue();
              URL url = new URL("http://localhost:"; + port + 
"/test/service?wsdl");
              in = url.openStream();
  
              WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader();
              Definition definition = wsdlReader.readWSDL(null, new 
InputSource(in));
  
              assertNotNull(definition);
  
          } catch (Exception e) {
              fail(e.getMessage());
          } finally {
              in.close();
              kernel.stopGBean(stack);
              kernel.shutdown();
          }
      }
  
      // TODO  Get these running again
      public void xtestAxisStyleMessage() throws Exception {
          Kernel kernel = new Kernel("wstest");
          kernel.boot();
  
          URL wsdlURL = new 
File("target/test-ejb-jar/META-INF/wsdl/test-ejb.wsdl").toURL();
          JarFile jarFile = new JarFile("target/test-ejb-jar.jar");
          String ejbName = "SimpleStatelessSession";
          ClassLoader classLoader = this.getClass().getClassLoader();
  
          JavaServiceDesc serviceDesc = 
AxisServiceBuilder.createEJBServiceDesc(jarFile, ejbName, classLoader);
  
          ObjectName ejbContainer = MockEJBContainerGBean.addGBean(kernel, new 
File("target/test-ejb-jar").toURL(), "SimpleEJB", 
"org.openejb.test.simple.slsb.SimpleStatelessSessionEJB", 
"org.openejb.test.simple.slsb.SimpleStatelessSessionHome", 
"org.openejb.test.simple.slsb.SimpleStatelessSession", 
"org.openejb.test.simple.slsb.SimpleStatelessSessionLocalHome", 
"org.openejb.test.simple.slsb.SimpleStatelessSessionLocal", 
"org.openejb.test.simple.slsb.SimpleStatelessSessionEndpoint");
          ObjectName listener = SoapHttpListenerGBean.addGBean(kernel, 
"HTTPSOAP");
          ObjectName wsContainer = WSContainerGBean.addGBean(kernel, 
"HTTPSOAP", ejbContainer, listener, new URI("/services/Simple"), wsdlURL, 
serviceDesc);
          ObjectName server = HttpServerGBean.addGBean(kernel, "HTTPSOAP", 
listener);
          ObjectName stack = StandardServiceStackGBean.addGBean(kernel, 
"HTTPSOAP", 0, InetAddress.getByName("localhost"), null, 1, 5, null, null, 
server);
  
          assertRunning(kernel, ejbContainer);
          assertRunning(kernel, wsContainer);
          assertRunning(kernel, listener);
          assertRunning(kernel, server);
          assertRunning(kernel, stack);
  
          HttpURLConnection connection = null;
  
          try {
              kernel.setAttribute(stack, "soTimeout", new Integer(1000));
              int port = ((Integer) kernel.getAttribute(stack, 
"port")).intValue();
              URL url = new URL("http://localhost:"; + port + 
"/services/Simple");
  
              connection = (HttpURLConnection) url.openConnection();
              connection.setDoOutput(true);
              connection.setRequestProperty("Content-Type", "text/xml");
              PrintStream out = new PrintStream(connection.getOutputStream());
              out.print("<soapenv:Envelope\n" +
                      "    
xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"; +
                      "    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"; +
                      "    
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>\n" +
                      "<soapenv:Body>\n" +
                      "<ns1:echo 
soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\n"; +
                      "          
xmlns:ns1=\"http://openejb.org/test-ejb-jar\";>\n" +
                      "<String_1 xsi:type=\"xsd:string\">hello</String_1>\n" +
                      "</ns1:echo>\n" +
                      "</soapenv:Body>\n" +
                      "</soapenv:Envelope>");
              out.flush();
              out.close();
  
              String result = getResult(connection.getInputStream());
  
              assertEquals("hello", result);
  
          } catch (Exception e) {
              fail(e.getMessage());
          } finally {
              connection.disconnect();
              kernel.stopGBean(stack);
              kernel.shutdown();
          }
      }
  
      public void testFixme(){
          
      }
  
      private void assertRunning(Kernel kernel, ObjectName objectName) throws 
Exception {
          int state = ((Integer) kernel.getAttribute(objectName, 
"state")).intValue();
          assertEquals("should be running: " + objectName, State.RUNNING_INDEX, 
state);
      }
  
      private String getResult(InputStream responseStream) throws 
ParserConfigurationException, SAXException, IOException {
          SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
          SAXParser saxParser = saxParserFactory.newSAXParser();
          TestHandler handler = new TestHandler();
          saxParser.parse(responseStream, handler);
          return handler.result;
      }
  
      private static class TestHandler extends DefaultHandler {
          String result;
          boolean found;
          public void endElement(String uri, String localName, String qName) 
throws SAXException {
              if (qName.equals("result")){
                  found = true;
              }
          }
  
          public void characters(char ch[], int start, int length) throws 
SAXException {
              if (!found){
                  result = new String(ch, start, length);
              }
          }
      }
  }
  
  
  

Reply via email to