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

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

package org.jboss.test.jbossnet.basic;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.jboss.test.jbossnet.WSTests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Tests remote accessibility of the main axis service servlet
 * @since 11. Oktober 2001
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @author <a href="mailto:thomas.diesler@arcor.de">Thomas Diesler</a>
 * @version $Revision: 1.1.1.1.4.1 $
 */

public class AxisUnitTestCase extends WSTests
{

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

   /** tests a successful http call to the given target */
   protected void doCall(String message, URL target) throws Exception
   {
      BufferedReader reader = new BufferedReader(new InputStreamReader(target.openStream()));
      try
      {
         String line = reader.readLine();
         assertNotNull(message + " proper response", line);
      }
      finally
      {
         reader.close();
      }
   }

   /** tests an illegal access to the given target */
   protected void doIllegalAccess(String message, URL target) throws Exception
   {
      try
      {
         target.openStream();
         fail(message + " access should be illegal");
      }
      catch (IOException e)
      {
         assertTrue("expected response code 403", e.getMessage().indexOf("403") > 0);
      }
   }

   /** tests availability of the servlet under various contexts */
   public void testServlet() throws Exception
   {
      doCall("Servlet availability", new URL(PROTOCOL + AXIS_CONTEXT + "servlet/AxisServlet"));
      doCall("service availability", new URL(END_POINT));
      // init parameter 'axis.enableListQuery' should default to false
      doIllegalAccess("list command", new URL(PROTOCOL + AXIS_CONTEXT + "servlet/AxisServlet?list"));
      doIllegalAccess("service list ", new URL(END_POINT + "?list"));
   }

   /** tests access to a jws page
    * JWSHandler accesses the axis compiler, which i snot part of axis-ws4ee  
    */
   public void _testJWS() throws Exception
   {
      doCall("JWS access",new URL(PROTOCOL+AXIS_CONTEXT+"EchoHeaders.jws?method=list"));
      doCall("JWS wsdl access",new URL(PROTOCOL+AXIS_CONTEXT+"EchoHeaders.jws?wsdl"));
   }
   
   public static Test suite() throws Exception
   {
      // nothing to deploy really
      return new TestSuite(AxisUnitTestCase.class);
   }
}
