/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

// $Id: HelloUnitTestCase.java,v 1.1.1.1.4.1 2004/08/09 09:54:32 tdiesler Exp $

package org.jboss.test.jbossnet.ejbsimple;

import org.jboss.test.jbossnet.WSTests;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

import com.ibm.wsdl.factory.WSDLFactoryImpl;

import junit.framework.Test;

import java.io.InputStream;
import java.net.URL;

import javax.wsdl.Definition;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * Tests remote accessibility of stateless ejb bean
 * @created 5. Oktober 2001, 12:11
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @author Adrian Brock
 * @version $Revision: 1.1.1.1.4.1 $
 */

public class HelloUnitTestCase extends WSTests
{
   protected String HELLO_SOAP_ACTION = "HelloRemote";
   protected String HELLO_LOCAL_SOAP_ACTION = "HelloLocal";
   protected String HELLO_BOTH_SOAP_ACTION = "HelloBoth";

   // Constructors --------------------------------------------------
   public HelloUnitTestCase(String name)
   {
      super(name);
   }

   /** the session beans with which we interact */
   HelloRemote hello;
   HelloRemote helloLocal;
   HelloRemote helloBoth;

   /** setup the bean */
   public void setUp() throws Exception
   {
      super.setUp();
      hello =
         (HelloRemote) createAxisService(HelloRemote.class, HELLO_SOAP_ACTION);
      helloLocal =
         (HelloRemote) createAxisService(HelloRemote.class,
            HELLO_LOCAL_SOAP_ACTION);
      helloBoth =
         (HelloRemote) createAxisService(HelloRemote.class,
            HELLO_BOTH_SOAP_ACTION);
   }

   /** where the config is stored */
   protected String getAxisConfiguration()
   {
      return "jbossnet/ejbsimple/client/" + super.getAxisConfiguration();
   }

   /** Make sure we have the xerces parser, otherwise element serialization might fail */
   public void testClientParser() throws Exception
   {
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      assertEquals("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl", docFactory.getClass().getName());
   }

   /** test a simple hello world */
   public void testHello() throws Exception
   {
      assertEquals(
         "Return value of remote ejb service must be hello.",
         hello.hello("World"),
         "Hello World!");
      assertEquals(
         "Return value of local ejb service must be hello.",
         helloLocal.hello("World"),
         "Hello World!");
      assertEquals(
         "Return value of both ejb service must be hello.",
         helloBoth.hello("World"),
         "Hello World!");
   }

   /** test some structural parameters */
   public void testHowdy() throws Exception
   {
      HelloData data = new HelloData();
      data.setName("CGJ");
      assertEquals(
         "Return value of remote ejb service must be howdy.",
         hello.howdy(data),
         "Howdy CGJ!");
      assertEquals(
         "Return value of local ejb service must be howdy.",
         helloLocal.howdy(data),
         "Howdy CGJ!");
      assertEquals(
         "Return value of both ejb service must be howdy.",
         helloBoth.howdy(data),
         "Howdy CGJ!");
   }

   /** arrays should be tested too */
   public void testTypedArrays() throws Exception
   {
      HelloData[] values =
         new HelloData[] { new HelloData(), new HelloData()};
      assertTrue(
         "Return value of remote ejb service must be same.",
         java.util.Arrays.equals(hello.typedArrays(values), values));
      assertTrue(
         "Return value of local ejb service must be same.",
         java.util.Arrays.equals(helloLocal.typedArrays(values), values));
      assertTrue(
         "Return value of both ejb service must be same.",
         java.util.Arrays.equals(helloBoth.typedArrays(values), values));
   }
   
   /** arrays should be tested too */
   public void testArrays() throws Exception
   {
      Object[] values =
         new Object[] { new String("Test"), new Integer(1), new HelloData()};
      assertTrue(
         "Return value of remote ejb service must be same.",
         java.util.Arrays.equals(hello.arrays(values), values));
      assertTrue(
         "Return value of local ejb service must be same.",
         java.util.Arrays.equals(helloLocal.arrays(values), values));
      assertTrue(
         "Return value of both ejb service must be same.",
         java.util.Arrays.equals(helloBoth.arrays(values), values));
   }

   /** order preservation ok ? */
   public void testReverse() throws Exception
   {
      Object[] values =
         new Object[] { new String("Test"), new Integer(1), new HelloData()};
      Object[] expected =
         new Object[] { new HelloData(), new Integer(1), new String("Test")};
      assertTrue(
         "Return value of remote ejb service must be reversed.",
         java.util.Arrays.equals(hello.reverse(values), expected));
      assertTrue(
         "Return value of local ejb service must be reversed.",
         java.util.Arrays.equals(helloLocal.reverse(values), expected));
      assertTrue(
         "Return value of both ejb service must be reversed.",
         java.util.Arrays.equals(helloBoth.reverse(values), expected));
   }

   /** order preservation ok ? */
   public void testElement() throws Exception
   {
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      System.out.println("Using: " + docFactory);
      Document doc = docFactory.newDocumentBuilder().newDocument();
      Element testElement=doc.createElement("TestElement");
      assertEquals(
         "Return element of remote ejb service must fit.",
         hello.element(testElement).getNodeName(),testElement.getNodeName());
      assertEquals(
         "Return element of local ejb service must fit.",
         helloLocal.element(testElement).getNodeName(),testElement.getNodeName());
      assertEquals(
         "Return element of both ejb service must fit.",
         helloBoth.element(testElement).getNodeName(),testElement.getNodeName());
   }

   /** tests wsdl access */
   public void testWsdl() throws Exception
   {
      // check if the WSDL is available
      URL urlWSDL = new URL(END_POINT + "/" + HELLO_SOAP_ACTION + "?wsdl");
      InputStream isWSDL = urlWSDL.openStream();

      // check if it is a valid WSDL
      WSDLFactory wsdlFactory = new WSDLFactoryImpl();
      WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
      Definition wsdlDefinition =
         wsdlReader.readWSDL(null, new InputSource(isWSDL));
      assertNotNull("cannot obtain the remote wsdl", wsdlDefinition);

      urlWSDL = new URL(END_POINT + "/" + HELLO_LOCAL_SOAP_ACTION + "?wsdl");
      isWSDL = urlWSDL.openStream();

      // check if it is a valid WSDL
      wsdlFactory = new WSDLFactoryImpl();
      wsdlReader = wsdlFactory.newWSDLReader();
      wsdlDefinition = wsdlReader.readWSDL(null, new InputSource(isWSDL));
      assertNotNull("cannot obtain the local wsdl", wsdlDefinition);

      urlWSDL = new URL(END_POINT + "/" + HELLO_BOTH_SOAP_ACTION + "?wsdl");
      isWSDL = urlWSDL.openStream();

      // check if it is a valid WSDL
      wsdlFactory = new WSDLFactoryImpl();
      wsdlReader = wsdlFactory.newWSDLReader();
      wsdlDefinition = wsdlReader.readWSDL(null, new InputSource(isWSDL));
      assertNotNull("cannot obtain the both wsdl", wsdlDefinition);
   }

   /** this is to deploy the whole ear */
   public static Test suite() throws Exception
   {
      return getDeploySetup(HelloUnitTestCase.class, "wsejbsimple.ear");
   }

   /** standalone */
   public static void main(String[] args)
   {
      junit.textui.TestRunner.run(HelloUnitTestCase.class);
   }

}
