/** * * 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.SimpleFactory; import commonj.sdo.DataObject; import commonj.sdo.Type; import commonj.sdo.helper.DataFactory; import commonj.sdo.helper.HelperContext; import commonj.sdo.helper.TypeHelper; import commonj.sdo.helper.XSDHelper; /** * @version $Rev$ $Date$ */ public class XSDHelperTestCase extends TestCase { private static final String TEST_MODEL = "/simple.xsd"; private static final String TEST_MODEL2 = "/xsdCorners.xsd"; private URL modelURL; private URL xsdCornersURL; HelperContext hc; protected void setUp() throws Exception { super.setUp(); hc = SDOUtil.createHelperContext(); modelURL = getClass().getResource(TEST_MODEL); xsdCornersURL = getClass().getResource(TEST_MODEL2); } public void testDefineWithLocation() throws IOException { XSDHelper xsdHelper = hc.getXSDHelper(); List types = xsdHelper.define(modelURL.openStream(), modelURL.toString()); assertEquals(2, types.size()); } public void testDefineWithNoLocation() { XSDHelper xsdHelper = hc.getXSDHelper(); List types = xsdHelper.define(getClass().getResourceAsStream(TEST_MODEL), null); assertEquals(2, types.size()); } public void testDuplicateDefineWithLocation() throws IOException { XSDHelper xsdHelper = hc.getXSDHelper(); 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; SimpleFactory.INSTANCE.register(hc); XSDHelper xsdHelper = hc.getXSDHelper(); DataObject quoteSDO = (DataObject)SimpleFactory.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 = hc.getXSDHelper(); 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 = hc.getTypeHelper(); XSDHelper xsdHelper = hc.getXSDHelper(); SimpleFactory.INSTANCE.register(hc); DataObject quoteSDO = (DataObject)SimpleFactory.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); } public void testPrefixFromNSWithHyphenNumber() throws IOException { XSDHelper xsdHelper = hc.getXSDHelper(); xsdHelper.define(xsdCornersURL.openStream(), xsdCornersURL.toString()); DataFactory df = hc.getDataFactory(); DataObject root = df.create("http://www.example.com/simple-1", "A"); root.setString("a1", "a1s"); root.setString("a2", "a2s"); String doc = hc.getXMLHelper().save(root, "http://www.example.com/simple-1", "a"); assertTrue(doc.indexOf("xmlns:s1=\"http://www.example.com/simple-1\"") != -1); } public void testShortPrefix() throws IOException { XSDHelper xsdHelper = hc.getXSDHelper(); URL url = getClass().getResource("/prefix.xsd"); xsdHelper.define(url.openStream(), url.toString()); DataObject cmd = hc.getDataFactory().create("http://soaassureservice.soabench.ibm.com", "CreateClaim"); cmd.setString("requestInfo", "cost"); String doc = hc.getXMLHelper().save(cmd, "http://soaassureservice.soabench.ibm.com", "createClaim"); assertTrue(doc.indexOf("xmlns:as=\"http://soaassureservice.soabench.ibm.com\"") != -1); } }