/**
*
* 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import junit.framework.TestCase;
import org.apache.tuscany.sdo.api.SDOHelper;
import org.apache.tuscany.sdo.api.SDOUtil;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
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.XMLDocument;
import commonj.sdo.helper.XMLHelper;
import commonj.sdo.helper.XSDHelper;
import commonj.sdo.impl.HelperProvider;
public class XMLHelperTestCase extends TestCase {
HelperContext hc;
private XSDHelper xsdHelper;
XMLHelper xmlh;
static final String INDENT = " ", MARGIN = " ", LINE_BREAK = "\n\n";
void define(String model) throws Exception {
// Populate the meta data for the test model
URL url = getClass().getResource(model);
xsdHelper.define(url.openStream(), url.toString());
}
protected void setUp() throws Exception {
super.setUp();
hc = SDOUtil.createHelperContext();
xsdHelper = hc.getXSDHelper();
xmlh = hc.getXMLHelper();
// Populate the meta data for the test (Stock Quote) model
define("/simpleWithChangeSummary.xsd");
define("/SequenceChangeSummary.xsd");
define("/simple.xsd");
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testLoadInputStreamStringObject() throws IOException {
}
protected final void format(String xml,String formatted) throws IOException {
XMLDocument doc = xmlh.load(getClass().getResource(xml).openStream());
Map options = new HashMap();
options.put(SDOHelper.XMLOptions.XML_SAVE_INDENT, INDENT);
options.put(SDOHelper.XMLOptions.XML_SAVE_MARGIN, MARGIN);
options.put(SDOHelper.XMLOptions.XML_SAVE_LINE_BREAK, LINE_BREAK);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// doc declares NameSpaces at root
xmlh.save(xmlh.createDocument(doc.getRootObject(), doc.getRootElementURI(), doc.getRootElementName()), baos, options);
//xmlh.save(xmlh.createDocument(doc.getRootObject(), doc.getRootElementURI(), doc.getRootElementName()), System.out, options);
assertEquals(formatted, baos.toString());
}
public void notestSaveXMLDocumentOutputStreamObject() throws IOException {
format("/simpleWithChangeSummary.xml",
MARGIN+ "" +LINE_BREAK+
MARGIN+ "" +LINE_BREAK+
MARGIN+INDENT+ "FBNT" +LINE_BREAK+
MARGIN+INDENT+ "FlyByNightTechnology" +LINE_BREAK+
MARGIN+INDENT+ "999.0" +LINE_BREAK+
MARGIN+INDENT+ "1000.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "1500.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "2500.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "3000.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "4000.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "fbnt" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "1000.0" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "2000.02000.99" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+ "");
}
public void notestSaveMixedOutputStreamObject() throws IOException {
format("/mixedChangeSummary.xml",
MARGIN+ "" +LINE_BREAK+
MARGIN+ "" +LINE_BREAK+
INDENT+ "" +LINE_BREAK+
INDENT+INDENT+ "fbnt" +LINE_BREAK+
INDENT+INDENT+ "FlyByNightTechnology" +LINE_BREAK+
INDENT+INDENT+ "1000.0" +LINE_BREAK+
INDENT+INDENT+ "" +LINE_BREAK+
INDENT+INDENT+ "2000.02000.99" +LINE_BREAK+
INDENT+INDENT+ "" +LINE_BREAK+
INDENT+ "" +LINE_BREAK+
"FBNTFlyByNightTechnology999.01500.02500.01000.03000.04000.0");
}
public void notestOpenMixedOutputStreamObject() throws IOException {
format("/openChangeSummary.xml",
MARGIN+ "" +LINE_BREAK+
MARGIN+ "" +LINE_BREAK+
MARGIN+INDENT+ "FBNT" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "1500.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "2500.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "3000.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "4000.0" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "fbnt" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "2000.02000.99" +LINE_BREAK+
MARGIN+INDENT+INDENT+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+INDENT+ "" +LINE_BREAK+
MARGIN+INDENT+ "" +LINE_BREAK+
MARGIN+ "");
}
private String quoteXML =
"" +
"" +
"fbnt" +
"FlyByNightTechnology" +
"1000.0" +
"1000.0" +
"1000.0" +
"1000.0" +
"1000.0" +
"1000.0" +
"" +
"2000.0" +
"" +
"";
public void testLoadDOMSource() throws IOException, ParserConfigurationException, SAXException
{
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
dFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
Document document = dBuilder.parse(new ByteArrayInputStream(quoteXML.getBytes()));
DOMSource domSource = new DOMSource(document);
XMLDocument xmlDocument = hc.getXMLHelper().load(domSource, null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Map options = new HashMap();
options.put(SDOHelper.XMLOptions.XML_SAVE_INDENT, "");
options.put(SDOHelper.XMLOptions.XML_SAVE_MARGIN, "");
options.put(SDOHelper.XMLOptions.XML_SAVE_LINE_BREAK, "");
hc.getXMLHelper().save(xmlDocument, baos, options);
boolean isEqual = TestUtil.equalXmlFiles(new ByteArrayInputStream(quoteXML.getBytes()), new ByteArrayInputStream(baos.toByteArray()));
assertTrue(isEqual);
}
private String xsdStr =
"" +
"" +
// "" +
"" +
"" +
"" +
"" +
"" +
"";
/**
* Test the scenario of serializing and deserializing an SDO with an undefined global SDO property
* In this scenario, the target XSD namespace doesn't have any global element defined
*
*/
public void testDemandCreateRootObject() {
HelperContext hc = SDOUtil.createHelperContext();
hc.getXSDHelper().define(xsdStr);
DataObject quote = hc.getDataFactory().create("http://www.example.com/simple", "Quote");
quote.set("symbol", "abc");
String xmlStr = hc.getXMLHelper().save(quote, quote.getType().getURI(), "demandcreate");
XMLDocument doc = hc.getXMLHelper().load(xmlStr);
try {
doc.getRootObject();
}
catch (ClassCastException e) {
fail(e.toString());
}
}
/**
* This test case is similar to the testDemandCreateRootObject above. The only difference is
* the data model was created using SDO APIs instead of XSD.
*
*/
public void testDemandCreateRootObject2() {
HelperContext hc1 = SDOUtil.createHelperContext();
Type stringType = hc1.getTypeHelper().getType("commonj.sdo", "String");
DataObject quoteTypeDef = hc1.getDataFactory().create("commonj.sdo", "Type");
quoteTypeDef.set("uri", "http://www.example.com/simple");
quoteTypeDef.set("name", "Quote");
DataObject symbolPropDef = quoteTypeDef.createDataObject("property");
symbolPropDef.set("name", "symbol");
symbolPropDef.set("type", stringType);
hc1.getTypeHelper().define(quoteTypeDef);
DataObject quote = hc1.getDataFactory().create("http://www.example.com/simple", "Quote");
quote.set("symbol", "abc");
String xmlStr = hc1.getXMLHelper().save(quote, quote.getType().getURI(), "demandcreate");
HelperContext hc2 = SDOUtil.createHelperContext();
hc2.getXSDHelper().define(xsdStr);
XMLDocument doc = hc2.getXMLHelper().load(xmlStr);
try {
doc.getRootObject();
}
catch (ClassCastException e) {
fail(e.toString());
}
}
public void testEncoding() throws IOException
{
TypeHelper types = hc.getTypeHelper();
Type stringType = types.getType("commonj.sdo", "String");
DataObject customerType = hc.getDataFactory().create("commonj.sdo", "Type");
customerType.set("uri", "http://example.com/simple");
customerType.set("name", "Simple");
DataObject multiProperty = customerType.createDataObject("property");
multiProperty.set("name", "name");
multiProperty.set("type", stringType);
types.define(customerType);
DataObject obj = hc.getDataFactory().create("http://example.com/simple",
"Simple");
obj.set("name", "John Smith");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
hc.getXMLHelper().save(obj, "http://www.example.com/company" , "company", baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toString().getBytes());
XMLDocument xmlDoc = hc.getXMLHelper().load(bais);
if( !"UTF-8".equals(xmlDoc.getEncoding()) )
{
fail("Encoding ('" + xmlDoc.getEncoding() +"' is not correct. UTF-8 is the expected encoding.");
}
}
}