/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.test.jbossnet;

import org.jboss.axis.client.AxisClient;
import org.jboss.axis.client.ServiceFactory;
import org.jboss.axis.configuration.FileProvider;
import org.jboss.test.JBossTestCase;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import java.net.URL;

/**
 * Junit Test class with some JBoss.Net support
 * <br>
 * @since 12. Oktober 2001, 11:20
 * @author Thomas.Diesler@jboss.org
 */
public abstract class JBossNetTestBase extends JBossTestCase
{
   /** the protocol we use */
   protected String PROTOCOL = "http://";

   /** the address to which we forward the request */
   protected String ADDRESS = "" + getServerHost() + ":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 SERVICES_LOCATION = PROTOCOL + SERVICE_PORT;

   /** Creates new JBossNetTestBase */
   public JBossNetTestBase(String name)
   {
      super(name);
   }

   /** Create a Service that is preconfigured from WSDL
    */
   public Service createService(URL wsdlURL, QName serviceQName) throws Exception
   {
      // Note, we cannot use javax.xml.rpc.ServiceFactory.newFactory()
      ServiceFactory factory = new org.jboss.axis.client.ServiceFactory();
      Service service = factory.createService(wsdlURL, serviceQName);

      // configure the client engine
      String config = getAxisConfiguration();
      AxisClient clientEngine = new AxisClient(new FileProvider(config));
      ((org.jboss.axis.client.Service)service).setEngine(clientEngine);

      return service;
   }

   /** Create a Service that is preconfigured from WSDL
    */
   public Service createService(URL wsdlURL, QName serviceQName, boolean maintainSession) throws Exception
   {
      Service service = createService(wsdlURL, serviceQName);
      ((org.jboss.axis.client.Service)service).setMaintainSession(maintainSession);
      return service;
   }

   /** Overwrite to provide the path to the custom client config */
   protected String getAxisConfiguration()
   {
      return "client-config.wsdd";
   }
}
