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

// $Id: WSTests.java,v 1.1.1.1.4.1 2004/08/09 09:54:31 tdiesler Exp $

package org.jboss.test.jbossnet;

import org.jboss.test.JBossTestCase;

import org.jboss.net.axis.AxisInvocationHandler;
import org.jboss.net.axis.XMLResourceProvider;
import org.jboss.net.jmx.MBeanInvocationHandler;
import org.jboss.net.jmx.adaptor.RemoteAdaptor;
import org.jboss.net.jmx.adaptor.RemoteAdaptorInvocationHandler;

import org.apache.axis.client.Service;
import java.net.URL;
import java.util.Map;

/**
 * Junit Test class with some Axis support
 * <br>
 * @since 12. Oktober 2001, 11:20
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1.1.1.4.1 $
 */
public abstract class WSTests extends JBossTestCase {
   /** the protocol we use */
   protected String PROTOCOL = "http://";

   /** the address to which we forward the request */
   protected String ADDRESS = "localhost:8080/";

   /** where the axis servlet context is installed */
   protected String AXIS_CONTEXT = ADDRESS + "jboss-net/";

   /** where the service port is located under */
   protected String SERVICE_PORT = AXIS_CONTEXT + "services";

   /** has an associated end point that may be configured once */
   protected String END_POINT = PROTOCOL + SERVICE_PORT;

   /** the map of methods to interface names */
   protected Map interfaceMap = new AxisInvocationHandler.DefaultInterfaceMap();

   /** the map of methods to method names */
   protected Map methodMap = new AxisInvocationHandler.DefaultMethodMap();

   /** we have an already instantied configuration for each service */
   protected XMLResourceProvider provider;
   
   /** Creates new AxisTestCase */
   public WSTests(String name) {
      super(name);
   }

   public void setUp() throws Exception {
      String axisConfig = getAxisConfiguration();
      URL configURL = getClass().getClassLoader().getResource(axisConfig);
      if (configURL == null)
         throw new IllegalStateException(
            "Failed to find resource: " + axisConfig);
      log.info("Configuring axis with provider resource " + configURL);
      provider=new XMLResourceProvider(configURL,getClass().getClassLoader());
   }

   /** creates a new service */
   protected Service createService() {
      Service result=new Service(provider);
      result.setMaintainSession(true);
      return result;
   }
   
   /** searches for the right configuration provider */
   protected String getAxisConfiguration() {
      return "client-config.wsdd";
   }

   /** creates a new Axis service using the test engine*/
   protected AxisInvocationHandler createAxisInvocationHandler(String endpoint, String soapAction)
      throws Exception {
      return new AxisInvocationHandler(
         new URL(endpoint),
         soapAction,
         createService(),
         methodMap,
         interfaceMap);
   }
   
   /** creates a new Axis service using the test engine*/
   protected AxisInvocationHandler createAxisInvocationHandler(String soapAction)
      throws Exception {
      return createAxisInvocationHandler(
         END_POINT,
         soapAction);
   }

   /** creates a new Axis service using the test engine*/
   protected MBeanInvocationHandler createMBeanInvocationHandler(String soapAction)
      throws Exception {
      return new MBeanInvocationHandler(
         new URL(END_POINT),
         soapAction,
         createService(),
         methodMap,
         interfaceMap);
   }
   
   /** creates a new Axis service using the test engine*/
   protected Object createAxisService(Class _class, String soapAction)
      throws Exception {
      return AxisInvocationHandler.createAxisService(
         _class,
         createAxisInvocationHandler(soapAction));
   }

   /** creates a new Axis service using the test engine*/
   protected Object createAxisService(Class _class, String endpoint, String soapAction)
      throws Exception {
      return AxisInvocationHandler.createAxisService(
         _class,
         createAxisInvocationHandler(endpoint, soapAction));
   }
   
   /** creates a new Axis service using the test engine*/
   protected Object createMBeanService(Class _class, String soapAction)
      throws Exception {
      return MBeanInvocationHandler.createMBeanService(
         _class,
         createMBeanInvocationHandler(soapAction));
   }

   /** creates a new remote adaptor using the test engine*/
   protected RemoteAdaptor createRemoteAdaptor(String soapAction)
      throws Exception {
      return RemoteAdaptorInvocationHandler.createRemoteAdaptor(
         createMBeanInvocationHandler(soapAction));
   }

}
