/*=============================================================================*
* 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.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;
/**
* TODO
*
* @author Ian P. Springer (Hewlett-Packard Company)
*/
public class AbstractResourcePropertiesTestCase extends TestCase
{
public static final String NSURI_SUSHI = "http://ws.apache.org/resource/properties/test/sushi";
protected static final String RESOURCE_PATH_SUSHI_PROPS_XSD = "org/apache/ws/resource/properties/SushiProperties.xsd";
protected static final String RESOURCE_PATH_SUSHI_PROPS_XML = "org/apache/ws/resource/properties/SushiProperties.xml";
protected static final String RESOURCE_PATH_SUSHI_METADATA_XML = "org/apache/ws/resource/properties/SushiMetadata.wsrmd";
protected static final File BASE_TMP_DIR = new File( System.getProperty( "java.io.tmpdir" ), "ws.apache.org/apollo" );;
protected static final File SRC_DIR = new File( BASE_TMP_DIR, "generated_src" );
protected static final File CLASSES_DIR = new File( BASE_TMP_DIR, "generated_classes" );
protected XmlBeansResourcePropertySet m_resourcePropSet;
protected SushiResourceContext m_resourceContext;
static
{
try
{
compileSushiPropsXsd();
}
catch ( Exception e )
{
throw new RuntimeException( "Failed to compile SushiProperties.xsd.", e );
}
enableSushiPropsTypeClassLoader();
}
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();
}
}
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 );
}
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 );
}
}