001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.synapse.mediators;
020:
021: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
022: import org.apache.axiom.om.OMElement;
023: import org.apache.synapse.config.xml.MediatorFactory;
024: import org.apache.synapse.config.xml.MediatorSerializer;
025: import org.apache.synapse.config.xml.MediatorFactoryFinder;
026: import org.apache.synapse.Mediator;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.xml.sax.SAXException;
030: import org.custommonkey.xmlunit.XMLTestCase;
031:
032: import javax.xml.stream.XMLStreamReader;
033: import javax.xml.stream.XMLInputFactory;
034: import javax.xml.stream.XMLStreamException;
035: import javax.xml.parsers.ParserConfigurationException;
036: import java.io.StringReader;
037: import java.io.IOException;
038:
039: /**
040: *
041: *
042: */
043:
044: public abstract class AbstractTestCase extends XMLTestCase {
045:
046: private static final Log log = LogFactory
047: .getLog(AbstractTestCase.class);
048:
049: public AbstractTestCase(String name) {
050: super (name);
051: }
052:
053: public AbstractTestCase() {
054: }
055:
056: protected void setUp() throws Exception {
057: super .setUp();
058: }
059:
060: protected OMElement createOMElement(String xml) {
061: try {
062:
063: XMLStreamReader reader = XMLInputFactory.newInstance()
064: .createXMLStreamReader(new StringReader(xml));
065: StAXOMBuilder builder = new StAXOMBuilder(reader);
066: OMElement omElement = builder.getDocumentElement();
067: return omElement;
068:
069: } catch (XMLStreamException e) {
070: throw new RuntimeException(e);
071: }
072: }
073:
074: protected boolean serialization(String inputXml,
075: MediatorFactory mediatorFactory,
076: MediatorSerializer mediatorSerializer) {
077:
078: OMElement inputOM = createOMElement(inputXml);
079: Mediator mediator = mediatorFactory.createMediator(inputOM);
080: OMElement resultOM = mediatorSerializer.serializeMediator(null,
081: mediator);
082: try {
083: assertXMLEqual(resultOM.toString(), inputXml);
084: return true;
085: } catch (SAXException e) {
086: log.error(e);
087: } catch (IOException e) {
088: log.error(e);
089: }
090: return false;
091: }
092:
093: protected boolean serialization(String inputXml,
094: MediatorSerializer mediatorSerializer) {
095: OMElement inputOM = createOMElement(inputXml);
096: Mediator mediator = MediatorFactoryFinder.getInstance()
097: .getMediator(inputOM);
098: OMElement resultOM = mediatorSerializer.serializeMediator(null,
099: mediator);
100: try {
101: assertXMLEqual(resultOM.toString(), inputXml);
102: return true;
103: } catch (SAXException e) {
104: log.error(e);
105: } catch (IOException e) {
106: log.error(e);
107: }
108: return false;
109: }
110:
111: protected OMElement getParent() {
112: String parentXML = "<synapse xmlns=\"http://ws.apache.org/ns/synapse\"><definitions></definitions></synapse>";
113: return createOMElement(parentXML);
114: }
115:
116: protected boolean compare(OMElement inputElement,
117: OMElement serializedElement) {
118: try {
119: assertXMLEqual(inputElement.toString(), serializedElement
120: .toString());
121: return true;
122: } catch (SAXException e) {
123: log.error(e);
124: } catch (IOException e) {
125: log.error(e);
126: }
127: return false;
128: }
129: }
|