/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.ws.util.test.axis;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.axis.ConfigurationException;
import org.apache.axis.configuration.FileProvider;
import org.apache.ws.util.rpc.JaxRpcConstants;
import org.apache.ws.util.soap.SaajConstants;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.URL;
/**
* Provides some conviencence functionality for easier testing of web services.
* Methods provided by this JUnit test case superclass will allow an embedded
* Axis server to be started and stopped.
*
* @author mazz
*/
public abstract class AbstractAxisTestCase
extends TestCase
{
private static final int ANY_FREE_PORT = 0;
/*
* Make sure JAX-RPC and SAAJ factories all are set to Axis' impls.
*/
static
{
System.setProperty( JaxRpcConstants.SYSPROP_SERVICE_FACTORY, JaxRpcConstants.IMPL_SERVICE_FACTORY_AXIS );
System.setProperty( SaajConstants.SYSPROP_SOAP_FACTORY, SaajConstants.IMPL_SOAP_FACTORY_AXIS );
System.setProperty( SaajConstants.SYSPROP_MESSAGE_FACTORY, SaajConstants.IMPL_MESSAGE_FACTORY_AXIS );
System.setProperty( SaajConstants.SYSPROP_SOAP_CONNECTION_FACTORY,
SaajConstants.IMPL_SOAP_CONNECTION_FACTORY_AXIS );
System.setProperty( SaajConstants.SYSPROP_SOAP_ELEMENT_FACTORY, SaajConstants.IMPL_SOAP_ELEMENT_FACTORY_AXIS );
}
/**
* This constant is used for enabling/disabling the use of the embedded axis server.
*/
public boolean RUN_AXIS_SERVER = true;
/**
* the embedded Axis server
*/
private NotSoSimpleAxisServer m_simpleAxisServer;
/**
* @see TestCase#TestCase()
*/
public AbstractAxisTestCase( )
{
super( );
}
/**
* Enables/Disables the running of the Axis server. The DEFAULT is true (run Axis server)
*
* The reason for adding this is to allow derived test cases which can either use the underlying SimpleAxisServer
* or "turn-off" that functionality and hit a runnign instance.
*
* @param runServer
*/
public AbstractAxisTestCase( boolean runServer )
{
RUN_AXIS_SERVER = runServer;
}
/**
* @see TestCase#TestCase(String)
*/
public AbstractAxisTestCase( String name )
{
super( name );
}
/**
* Returns the base URL used to contact the Axis server. To access a web service hosted inside
* of the embedded Axis server, append the name of the web service to this base URL.
* <p/>
* Subclasses may override this method if the default is not acceptable. The
* default is <code>http://127.0.0.1:####/axis/services/</code> where <code>####</code>
* is the {@link #getAxisServerSocketPort() Axis port number} and <code>axis</code> is
* the {@link #getAxisContextName() Axis context name}.
*
* @return base URL for all web services hosted in the embedded Axis server
*/
protected URL getAxisBaseUrl( )
{
try
{
return new URL( "http",
"127.0.0.1",
m_simpleAxisServer.getServerSocket( ).getLocalPort( ),
"/" + getAxisContextName( ) + "/services/" );
}
catch ( MalformedURLException murle )
{ // should never occur - throw error so as not to force a throws clause in signature
throw new AssertionFailedError( murle.toString( ) );
}
}
/**
* Returns the full directory path containing the WSDD configuration file(s).
* This is the base path that is prepended to the
* {@link #getAxisConfigFileName() configuration file name}.
* <p/>
* Subclasses may override this method if the default is not acceptable. The
* default is the current directory as defined by the system property
* <code>user.dir</code>.
*
* @return filename of the WSDD configuration file
*/
protected String getAxisConfigBasePath( )
{
return System.getProperty( "user.dir" );
}
/**
* Returns the filename of the actual Axis WSDD configuration file, excluding
* all directory paths.
* <p/>
* Subclasses may override this method if the default is not acceptable. The
* default is <code>server-config.wsdd</code>.
*
* @return filename of the WSDD configuration file
*/
protected String getAxisConfigFileName( )
{
return "server-config.wsdd";
}
/**
* Returns the context name of the Axis servlet for use within an endpoint URL.
*
* @return context name of the Axis servlet
*/
protected String getAxisContextName( )
{
return "axis";
}
/**
* This setter is to allow subclasses to tell us if we should use an already existing Axis server.
*
* @param server the embedded Axis server to be used by the tests
*/
protected void setAxisServer( NotSoSimpleAxisServer server )
{
m_simpleAxisServer = server;
}
/**
* @return the embedded Axis server to be used by the tests
*/
protected NotSoSimpleAxisServer getAxisServer( )
{
return m_simpleAxisServer;
}
/**
* Returns the number of the port that the embedded Axis server will accept
* requests on.
* <p/>
* Subclasses may override this method if the default is not acceptable. The
* default is <code>8080</code>.
*
* @return port number that the Axis server will listen to
*/
protected int getAxisServerSocketPort( )
{
return ANY_FREE_PORT;
}
/**
* Returns a URL used to request a WSDL document for a web service with the given name.
* This method uses {@link #getAxisBaseUrl()} to determine the base URL.
*
* @param serviceName the name of the web service
*
* @return URL used to request a web service WSDL document
*
* @see #getAxisBaseUrl()
*/
protected URL getAxisWebServiceUrl( String serviceName )
{
try
{
return new URL( getAxisBaseUrl( ),
serviceName );
}
catch ( MalformedURLException murle )
{ // should never occur - throw error so as not to force a throws clause in signature
throw new AssertionFailedError( murle.toString( ) );
}
}
/**
* Returns a URL used to request a WSDL document for a web service with the given name.
* This method uses {@link #getAxisBaseUrl()} to determine the base URL.
*
* @param serviceName the name of the web service
*
* @return URL used to request a web service WSDL document
*
* @see #getAxisBaseUrl()
*/
protected URL getAxisWebServiceWsdlUrl( String serviceName )
{
try
{
return new URL( getAxisBaseUrl( ).toString( ) + serviceName + "?wsdl" );
}
catch ( MalformedURLException murle )
{ // should never occur - throw error so as not to force a throws clause in signature
throw new AssertionFailedError( murle.toString( ) );
}
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*
* @throws ConfigurationException DOCUMENT_ME
*/
protected FileProvider getFileProvider( )
throws ConfigurationException
{
FileProvider config = new FileProvider( getAxisConfigBasePath( ),
getAxisConfigFileName( ) );
return config;
}
/**
* Reads in the Axis configuration file, creates a server socket to accept
* requests and then starts the embedded Axis server.
*
* @throws Exception if failed to get the configuration file, failed to start
* the server socket or failed to start the server
*/
protected void startAxisServer( )
throws Exception
{
FileProvider config = getFileProvider( );
ServerSocket ss = new ServerSocket( getAxisServerSocketPort( ) );
m_simpleAxisServer = new NotSoSimpleAxisServer( );
m_simpleAxisServer.setServerSocket( ss );
m_simpleAxisServer.setMyConfig( config );
m_simpleAxisServer.start( );
}
/**
* Stops the embedded Axis server if it is running.
*/
protected void stopAxisServer( )
{
if ( m_simpleAxisServer != null )
{
m_simpleAxisServer.stop( );
m_simpleAxisServer = null;
}
}
}