/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.tuscany.sdo.test;
import java.io.IOException;
import java.net.URL;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import junit.framework.TestCase;
import org.apache.tuscany.sdo.util.SDOUtil;
import com.example.simple.impl.SimpleFactoryImpl;
import commonj.sdo.DataObject;
import commonj.sdo.Type;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XSDHelper;
/**
* @version $Rev: 448276 $ $Date: 2006-09-20 18:33:20 +0100 (Wed, 20 Sep 2006) $
*/
public class XSDHelperTestCase extends TestCase {
private static final String TEST_MODEL = "/simple.xsd";
private URL modelURL;
public void testDefineWithLocation() throws IOException {
XSDHelper xsdHelper = SDOUtil.createXSDHelper(SDOUtil.createTypeHelper());
List types = xsdHelper.define(modelURL.openStream(), modelURL.toString());
assertEquals(2, types.size());
}
public void testDefineWithNoLocation() {
XSDHelper xsdHelper = SDOUtil.createXSDHelper(SDOUtil.createTypeHelper());
List types = xsdHelper.define(getClass().getResourceAsStream(TEST_MODEL), null);
assertEquals(2, types.size());
}
public void testDuplicateDefineWithLocation() throws IOException {
XSDHelper xsdHelper = SDOUtil.createXSDHelper(SDOUtil.createTypeHelper());
List types = xsdHelper.define(modelURL.openStream(), modelURL.toString());
assertEquals(2, types.size());
List types2 = xsdHelper.define(modelURL.openStream(), modelURL.toString());
assertEquals(0, types2.size());
}
public void testXSDGeneration_staticSDOType() throws IOException
{
//test for static sdo type. The test succeeds if the IllegalArgumentException is thrown
//by XSDHelper.generate method in which case the string xsd must be null;
XSDHelper xsdHelper = SDOUtil.createXSDHelper(SDOUtil.createTypeHelper());
DataObject quoteSDO = (DataObject)SimpleFactoryImpl.INSTANCE.createQuote();
List typeList = new Vector();
typeList.add(quoteSDO.getType());
String xsd = null;
try
{
xsd = xsdHelper.generate(typeList);
xsd = "";
}
catch ( IllegalArgumentException e )
{
}
assertNull(xsd);
}
public void testXSDGeneration_DynamicSDOType() throws IOException
{
//test for dynamic SDOs that have no XSD model. Here the testcase succeeds only if the
//xsd is generated by XSDHelper in which case xsd must not be null
XSDHelper xsdHelper = SDOUtil.createXSDHelper(SDOUtil.createTypeHelper());
DataObject quoteType = DataFactory.INSTANCE.create("commonj.sdo", "Type");
quoteType.set("uri", "http://www.example.com/dynamic");
quoteType.set("name", "DynamicQuote");
DataObject aProperty = quoteType.createDataObject("property");
aProperty.set("name", "symbol");
aProperty.set("type", TypeHelper.INSTANCE.getType("commonj.sdo", "String"));
aProperty = quoteType.createDataObject("property");
aProperty.set("name", "price");
aProperty.set("type", TypeHelper.INSTANCE.getType("commonj.sdo", "Decimal"));
aProperty = quoteType.createDataObject("property");
aProperty.set("name", "volume");
aProperty.set("type", TypeHelper.INSTANCE.getType("commonj.sdo", "Double"));
TypeHelper.INSTANCE.define(quoteType);
Type dynamicQuoteType =
TypeHelper.INSTANCE.getType("http://www.example.com/dynamic", "DynamicQuote");
Vector types = new Vector();
types.add(dynamicQuoteType);
String xsd = null;
try
{
xsd = xsdHelper.generate(types);
//System.out.println(xsd);
}
catch ( IllegalArgumentException e )
{
}
assertNotNull(xsd);
}
public void testXSDGeneration_DynamicWithNestedStaticSDOType() throws IOException
{
//testing static SDO with XSD Model being contained in a Dynamic SDO not having an XSD Model.
//the schema must be generated with imports / includes for the XSD corresponding to the static
//sdo types.
TypeHelper typeHelper = SDOUtil.createTypeHelper();
XSDHelper xsdHelper = SDOUtil.createXSDHelper(typeHelper);
DataObject quoteSDO = (DataObject)SimpleFactoryImpl.INSTANCE.createQuote();
DataObject quoteType = DataFactory.INSTANCE.create("commonj.sdo", "Type");
quoteType.set("uri", "http://www.example.com/dynamic");
quoteType.set("name", "DynamicQuote");
DataObject aProperty = quoteType.createDataObject("property");
aProperty.set("name", "symbol");
aProperty.set("type", typeHelper.getType("commonj.sdo", "String"));
aProperty = quoteType.createDataObject("property");
aProperty.set("name", "price");
aProperty.set("type", typeHelper.getType("commonj.sdo", "Decimal"));
aProperty = quoteType.createDataObject("property");
aProperty.set("name", "volume");
aProperty.set("type", typeHelper.getType("commonj.sdo", "Double"));
aProperty = quoteType.createDataObject("property");
aProperty.set("name", "containedQuotes");
aProperty.set("type", typeHelper.getType(quoteSDO.getType().getURI(), quoteSDO.getType().getName()));
aProperty.set("containment", new Boolean(true));
aProperty = quoteType.createDataObject("property");
aProperty.set("name", "referredQuotes");
aProperty.set("type", typeHelper.getType(quoteSDO.getType().getURI(), quoteSDO.getType().getName()));
typeHelper.define(quoteType);
Type dynamicQuoteType =
typeHelper.getType("http://www.example.com/dynamic", "DynamicQuote");
Vector types = new Vector();
types.add(dynamicQuoteType);
String xsd = null;
try
{
Hashtable schemaLocationMap = new Hashtable();
schemaLocationMap.put("http://www.example.com/simple", "http://www.example.com/simple/xsd");
xsd = xsdHelper.generate(types, schemaLocationMap);
//System.out.println(xsd);
}
catch ( IllegalArgumentException e )
{
}
assertNotNull(xsd);
}
protected void setUp() throws Exception {
super.setUp();
modelURL = getClass().getResource(TEST_MODEL);
}
}