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:
020: package org.apache.synapse.mediators;
021:
022: import org.apache.axiom.om.OMAbstractFactory;
023: import org.apache.axiom.om.OMDocument;
024: import org.apache.axiom.om.OMElement;
025: import org.apache.axiom.om.OMFactory;
026: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
027: import org.apache.axiom.soap.SOAPEnvelope;
028: import org.apache.synapse.MessageContext;
029: import org.apache.synapse.TestMessageContext;
030: import org.apache.synapse.config.Entry;
031: import org.apache.synapse.config.SynapseConfiguration;
032: import org.apache.synapse.core.SynapseEnvironment;
033: import org.apache.synapse.core.axis2.Axis2MessageContext;
034: import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
035: import org.apache.synapse.registry.url.SimpleURLRegistry;
036: import org.apache.axis2.context.ConfigurationContext;
037: import org.apache.axis2.engine.AxisConfiguration;
038:
039: import javax.xml.stream.XMLInputFactory;
040: import javax.xml.stream.XMLStreamException;
041: import javax.xml.stream.XMLStreamReader;
042: import java.io.StringReader;
043: import java.util.Iterator;
044: import java.util.Map;
045:
046: public class TestUtils {
047:
048: public static TestMessageContext getTestContext(String bodyText,
049: Map props) throws Exception {
050:
051: // create a test synapse context
052: TestMessageContext synCtx = new TestMessageContext();
053: SynapseConfiguration testConfig = new SynapseConfiguration();
054: testConfig.setRegistry(new SimpleURLRegistry());
055:
056: if (props != null) {
057: Iterator iter = props.keySet().iterator();
058: while (iter.hasNext()) {
059: String key = (String) iter.next();
060: testConfig.addEntry(key, (Entry) props.get(key));
061: }
062: }
063: synCtx.setConfiguration(testConfig);
064: synCtx.setEnvironment(new Axis2SynapseEnvironment(
065: new ConfigurationContext(new AxisConfiguration()),
066: testConfig));
067:
068: SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory()
069: .getDefaultEnvelope();
070: OMDocument omDoc = OMAbstractFactory.getSOAP11Factory()
071: .createOMDocument();
072: omDoc.addChild(envelope);
073:
074: XMLStreamReader parser = XMLInputFactory.newInstance()
075: .createXMLStreamReader(new StringReader(bodyText));
076: StAXOMBuilder builder = new StAXOMBuilder(parser);
077:
078: // set a dummy static message
079: envelope.getBody().addChild(builder.getDocumentElement());
080:
081: synCtx.setEnvelope(envelope);
082: return synCtx;
083: }
084:
085: public static Axis2MessageContext getAxis2MessageContext(
086: String bodyText, Map props) throws Exception {
087: // create a test synapse context
088: SynapseConfiguration testConfig = new SynapseConfiguration();
089: org.apache.axis2.context.MessageContext inContext = new org.apache.axis2.context.MessageContext();
090:
091: Axis2MessageContext synCtx = new Axis2MessageContext(inContext,
092: testConfig, null);
093: testConfig.setRegistry(new SimpleURLRegistry());
094:
095: if (props != null) {
096: Iterator iter = props.keySet().iterator();
097: while (iter.hasNext()) {
098: String key = (String) iter.next();
099: testConfig.addEntry(key, (Entry) props.get(key));
100: }
101: }
102: synCtx.setConfiguration(testConfig);
103:
104: SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory()
105: .getDefaultEnvelope();
106: OMDocument omDoc = OMAbstractFactory.getSOAP11Factory()
107: .createOMDocument();
108: omDoc.addChild(envelope);
109:
110: XMLStreamReader parser = XMLInputFactory.newInstance()
111: .createXMLStreamReader(new StringReader(bodyText));
112: StAXOMBuilder builder = new StAXOMBuilder(parser);
113:
114: // set a dummy static message
115: envelope.getBody().addChild(builder.getDocumentElement());
116:
117: synCtx.setEnvelope(envelope);
118: return synCtx;
119: }
120:
121: public static TestMessageContext getTestContextForXSLTMediator(
122: String bodyText, Map props) throws Exception {
123:
124: // create a test synapse context
125: TestMessageContext synCtx = new TestMessageContext();
126: SynapseConfiguration testConfig = new SynapseConfiguration();
127: testConfig.setRegistry(new SimpleURLRegistry());
128:
129: if (props != null) {
130: Iterator iter = props.keySet().iterator();
131: while (iter.hasNext()) {
132: String key = (String) iter.next();
133: testConfig.addEntry(key, (Entry) props.get(key));
134: }
135: }
136: synCtx.setConfiguration(testConfig);
137:
138: SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory()
139: .getDefaultEnvelope();
140: OMDocument omDoc = OMAbstractFactory.getSOAP11Factory()
141: .createOMDocument();
142: omDoc.addChild(envelope);
143:
144: XMLStreamReader parser = XMLInputFactory.newInstance()
145: .createXMLStreamReader(new StringReader(bodyText));
146: StAXOMBuilder builder = new StAXOMBuilder(parser);
147:
148: // set a dummy static message
149: OMFactory fac = OMAbstractFactory.getOMFactory();
150: envelope.getBody().addChild(
151: fac.createOMText("first text child "));
152: envelope.getBody().addChild(builder.getDocumentElement());
153: envelope.getBody().addChild(
154: fac.createOMText("second text child "));
155:
156: synCtx.setEnvelope(envelope);
157: return synCtx;
158: }
159:
160: public static TestMessageContext getTestContextForXSLTMediatorUsingFile(
161: String path, Map props) throws Exception {
162:
163: // create a test synapse context
164: TestMessageContext synCtx = new TestMessageContext();
165: SynapseConfiguration testConfig = new SynapseConfiguration();
166: testConfig.setRegistry(new SimpleURLRegistry());
167:
168: if (props != null) {
169: Iterator iter = props.keySet().iterator();
170: while (iter.hasNext()) {
171: String key = (String) iter.next();
172: testConfig.addEntry(key, (Entry) props.get(key));
173: }
174: }
175: synCtx.setConfiguration(testConfig);
176:
177: SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory()
178: .getDefaultEnvelope();
179: OMDocument omDoc = OMAbstractFactory.getSOAP11Factory()
180: .createOMDocument();
181: omDoc.addChild(envelope);
182:
183: //XMLStreamReader parser = XMLInputFactory.newInstance().
184: // createXMLStreamReader(new FileReader(path));
185: StAXOMBuilder builder = new StAXOMBuilder(path);
186:
187: // set a dummy static message
188: OMFactory fac = OMAbstractFactory.getOMFactory();
189: envelope.getBody().addChild(
190: fac.createOMText("first text child "));
191: envelope.getBody().addChild(builder.getDocumentElement());
192: envelope.getBody().addChild(
193: fac.createOMText("second text child "));
194:
195: synCtx.setEnvelope(envelope);
196: return synCtx;
197: }
198:
199: public static TestMessageContext getTestContext(String bodyText)
200: throws Exception {
201: return getTestContext(bodyText, null);
202: }
203:
204: public static MessageContext createLightweightSynapseMessageContext(
205: String payload) throws Exception {
206: org.apache.axis2.context.MessageContext mc = new org.apache.axis2.context.MessageContext();
207: SynapseConfiguration config = new SynapseConfiguration();
208: SynapseEnvironment env = new Axis2SynapseEnvironment(config);
209: MessageContext synMc = new Axis2MessageContext(mc, config, env);
210: SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory()
211: .getDefaultEnvelope();
212: OMDocument omDoc = OMAbstractFactory.getSOAP11Factory()
213: .createOMDocument();
214: omDoc.addChild(envelope);
215:
216: envelope.getBody().addChild(createOMElement(payload));
217:
218: synMc.setEnvelope(envelope);
219: return synMc;
220: }
221:
222: public static OMElement createOMElement(String xml) {
223: try {
224:
225: XMLStreamReader reader = XMLInputFactory.newInstance()
226: .createXMLStreamReader(new StringReader(xml));
227: StAXOMBuilder builder = new StAXOMBuilder(reader);
228: OMElement omElement = builder.getDocumentElement();
229: return omElement;
230:
231: } catch (XMLStreamException e) {
232: throw new RuntimeException(e);
233: }
234: }
235:
236: }
|