/**
*
* 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.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.tuscany.sdo.util.SDOUtil;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.XMLDocument;
import commonj.sdo.helper.XMLHelper;
import commonj.sdo.helper.XSDHelper;
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");
}
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(SDOUtil.XML_SAVE_INDENT, INDENT);
options.put(SDOUtil.XML_SAVE_MARGIN, MARGIN);
options.put(SDOUtil.XML_SAVE_LineBreak, 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 testSaveXMLDocumentOutputStreamObject() 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 testSaveMixedOutputStreamObject() 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 testOpenMixedOutputStreamObject() 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+ "");
}
}