/*=============================================================================*
* 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.resource.properties;
import junit.framework.TestCase;
import org.apache.commons.io.CopyUtils;
import org.apache.ws.resource.WsrfRuntime;
import org.apache.ws.resource.properties.impl.XmlBeansResourcePropertySet;
import org.apache.ws.resource.properties.impl.XmlBeansResourcePropertySetMetaData;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.common.XmlErrorPrinter;
import org.apache.xmlbeans.impl.tool.SchemaCompiler;
import org.oasisOpen.docs.wsrf.x2004.x10.wsrfWSResourceMetadataDescriptor10Draft01.DefinitionsDocument;
import org.oasisOpen.docs.wsrf.x2004.x10.wsrfWSResourceMetadataDescriptor10Draft01.MetadataDescriptorType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;
/**
* Base class for test cases for WSRP portType impls.
*
* @author Ian Springer
*/
public abstract class AbstractResourcePropertiesTestCase
extends TestCase
{
/** DOCUMENT_ME */
public static final String NSURI_SUSHI = "http://ws.apache.org/resource/properties/test/sushi";
/** DOCUMENT_ME */
protected static final String RESOURCE_PATH_SUSHI_PROPS_XSD =
"org/apache/ws/resource/properties/SushiProperties.xsd";
/** DOCUMENT_ME */
protected static final String RESOURCE_PATH_SUSHI_PROPS_XML =
"org/apache/ws/resource/properties/SushiProperties.xml";
/** DOCUMENT_ME */
protected static final String RESOURCE_PATH_SUSHI_METADATA_XML =
"org/apache/ws/resource/properties/SushiMetadata.wsrmd";
/** DOCUMENT_ME */
protected static final File BASE_TMP_DIR =
new File( System.getProperty( "java.io.tmpdir" ), "ws.apache.org/wsrf" );
/** DOCUMENT_ME */
protected static final File SRC_DIR = new File( BASE_TMP_DIR, "generated_src" );
/** DOCUMENT_ME */
protected static final File CLASSES_DIR = new File( BASE_TMP_DIR, "generated_classes" );
static
{
try
{
WsrfRuntime.getRuntime( ).init( new Properties( ) );
compileSushiPropsXsd( );
}
catch ( Exception e )
{
throw new RuntimeException( "Failed to either initialize JNDI or compile SushiProperties.xsd.", e );
}
enableSushiPropsTypeClassLoader( );
}
/** DOCUMENT_ME */
protected XmlBeansResourcePropertySet m_resourcePropSet;
/** DOCUMENT_ME */
protected SushiResourceContext m_resourceContext;
/**
* DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
* @throws RuntimeException DOCUMENT_ME
*/
protected static void compileSushiPropsXsd( )
throws Exception
{
InputStream xsdInStream = ClassLoader.getSystemResourceAsStream( RESOURCE_PATH_SUSHI_PROPS_XSD );
BASE_TMP_DIR.mkdirs( );
File xsdFile = new File( BASE_TMP_DIR, "SushiProperties.xsd" );
CopyUtils.copy( xsdInStream,
new FileOutputStream( xsdFile ) );
if ( !compileSchema( xsdFile ) )
{
throw new RuntimeException( );
}
}
/**
* DOCUMENT_ME
*/
protected static void enableSushiPropsTypeClassLoader( )
{
ClassLoader fileSystemTypeSystemClassLoader = null;
try
{
fileSystemTypeSystemClassLoader = new URLClassLoader( new URL[]
{
CLASSES_DIR.toURL( )
} );
}
catch ( MalformedURLException mue )
{
throw new RuntimeException( mue );
}
Thread.currentThread( ).setContextClassLoader( fileSystemTypeSystemClassLoader );
}
/**
* DOCUMENT_ME
*
* @throws XmlException DOCUMENT_ME
* @throws IOException DOCUMENT_ME
* @throws MetaDataViolationException DOCUMENT_ME
*/
protected void initResourcePropsDoc( )
throws XmlException,
IOException,
MetaDataViolationException
{
InputStream in =
Thread.currentThread( ).getContextClassLoader( ).getResourceAsStream( RESOURCE_PATH_SUSHI_PROPS_XML );
XmlObject propsDocXBean = XmlObject.Factory.parse( in );
in.close( );
in = Thread.currentThread( ).getContextClassLoader( ).getResourceAsStream( RESOURCE_PATH_SUSHI_METADATA_XML );
DefinitionsDocument metadataDocXBean = (DefinitionsDocument) XmlObject.Factory.parse( in );
MetadataDescriptorType metadataDesc = metadataDocXBean.getDefinitions( ).getMetadataDescriptorArray( 0 );
in.close( );
ResourcePropertySetMetaData propSetMetaData =
new XmlBeansResourcePropertySetMetaData( propsDocXBean.schemaType( ), metadataDesc );
m_resourcePropSet = new XmlBeansResourcePropertySet( propsDocXBean, propSetMetaData );
}
private static boolean compileSchema( File xsdFile )
{
SchemaCompiler.Parameters scompParams = new SchemaCompiler.Parameters( );
scompParams.setSrcDir( SRC_DIR );
scompParams.setClassesDir( CLASSES_DIR );
scompParams.setXsdFiles( new File[]
{
xsdFile
} );
scompParams.setDownload( true );
scompParams.setQuiet( true );
XmlErrorPrinter errorPrinter = new XmlErrorPrinter( true, null );
scompParams.setErrorListener( errorPrinter );
SRC_DIR.mkdirs( ); // necessary?
CLASSES_DIR.mkdirs( ); // necessary?
return SchemaCompiler.compile( scompParams );
}
}