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.axis2.jaxws.message;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.StringReader;
023:
024: import javax.jws.soap.SOAPBinding.Style;
025: import javax.xml.bind.JAXBElement;
026: import javax.xml.namespace.QName;
027: import javax.xml.stream.XMLInputFactory;
028: import javax.xml.stream.XMLStreamReader;
029:
030: import junit.framework.TestCase;
031: import org.apache.axiom.om.OMElement;
032: import org.apache.axiom.om.OMOutputFormat;
033: import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
034: import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
035: import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
036: import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
037: import org.apache.axis2.jaxws.message.factory.MessageFactory;
038: import org.apache.axis2.jaxws.registry.FactoryRegistry;
039: import org.apache.axis2.jaxws.TestLogger;
040: import org.test.stock1.ObjectFactory;
041: import org.test.stock1.StockPrice;
042:
043: /**
044: * MessageTests
045: * Tests to create and validate Message processing
046: * These are not client/server tests. Instead the tests simulate the processing of a Message during
047: * client/server processing.
048: */
049: public class MessageRPCTests extends TestCase {
050:
051: // String test variables
052: private static final String soap11env = "http://schemas.xmlsoap.org/soap/envelope/";
053: private static final String soap12env = "http://www.w3.org/2003/05/soap-envelope";
054: private static final String sampleEnvelopeHead11 = "<soapenv:Envelope xmlns:soapenv=\""
055: + soap11env + "\">" + "<soapenv:Header /><soapenv:Body>";
056:
057: private static final String sampleEnvelopeHead12 = "<soapenv:Envelope xmlns:soapenv=\""
058: + soap12env + "\">" + "<soapenv:Header /><soapenv:Body>";
059:
060: private static final String sampleEnvelopeTail = "</soapenv:Body></soapenv:Envelope>";
061:
062: private static final String sampleText = "<m:op xmlns:m='urn://sample'>"
063: + "<m:param xmlns:pre='urn://stock1.test.org' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:type='pre:StockPrice'>"
064: + "<price>100</price>" + "</m:param>" + "</m:op>";
065:
066: private static final String sampleEnvelope11 = sampleEnvelopeHead11
067: + sampleText + sampleEnvelopeTail;
068:
069: private static final String sampleEnvelope12 = sampleEnvelopeHead12
070: + sampleText + sampleEnvelopeTail;
071:
072: private static XMLInputFactory inputFactory = XMLInputFactory
073: .newInstance();
074:
075: public MessageRPCTests() {
076: super ();
077: }
078:
079: public MessageRPCTests(String arg0) {
080: super (arg0);
081: }
082:
083: /**
084: * Create a JAXBBlock containing a JAX-B business object
085: * and simulate a normal Proxy output flow
086: * @throws Exception
087: */
088: public void testJAXBOutflow() throws Exception {
089: // Create a SOAP 1.1 Message
090: MessageFactory mf = (MessageFactory) FactoryRegistry
091: .getFactory(MessageFactory.class);
092: Message m = mf.create(Protocol.soap11);
093:
094: // Indicate that the style is RPC, Indicate the Operation QName
095: m.setStyle(Style.RPC);
096: QName opQName = new QName("urn://sample", "op", "m");
097: m.setOperationElement(opQName);
098:
099: // Get the BlockFactory
100: JAXBBlockFactory bf = (JAXBBlockFactory) FactoryRegistry
101: .getFactory(JAXBBlockFactory.class);
102:
103: // Create the JAXBContext
104: JAXBBlockContext context = new JAXBBlockContext(
105: StockPrice.class.getPackage().getName());
106:
107: // Create the JAX-B object
108: ObjectFactory of = new ObjectFactory();
109: StockPrice obj = of.createStockPrice();
110: obj.setPrice("100");
111:
112: // Create the JAX-B Element
113: QName paramQName = new QName("urn://sample", "param", "m");
114: JAXBElement e = new JAXBElement(paramQName, StockPrice.class,
115: obj);
116:
117: // Create a JAXBBlock using the param object as the content. This simulates
118: // what occurs on the outbound JAX-WS Proxy client
119: Block block = bf.createFrom(e, context, null);
120:
121: // Add the block to the message as normal body content.
122: m.setBodyBlock(block);
123:
124: // Check to see if the message is a fault. The client/server will always call this method.
125: // The Message must respond appropriately without doing a conversion.
126: boolean isFault = m.isFault();
127: assertTrue(!isFault);
128: assertTrue("XMLPart Representation is "
129: + m.getXMLPartContentType(), "SPINE".equals(m
130: .getXMLPartContentType()));
131:
132: // On an outbound flow, we need to convert the Message
133: // to an OMElement, specifically an OM SOAPEnvelope,
134: // so we can set it on the Axis2 MessageContext
135: org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope) m
136: .getAsOMElement();
137:
138: // Check to see if the message is a fault. The client/server will always call this method.
139: // The Message must respond appropriately without doing a conversion.
140: isFault = m.isFault();
141: assertTrue(!isFault);
142: assertTrue("XMLPart Representation is "
143: + m.getXMLPartContentType(), "OM".equals(m
144: .getXMLPartContentType()));
145:
146: // Serialize the Envelope using the same mechanism as the
147: // HTTP client.
148: ByteArrayOutputStream baos = new ByteArrayOutputStream();
149: env.serializeAndConsume(baos, new OMOutputFormat());
150:
151: // To check that the output is correct, get the String contents of the
152: // reader
153: String newText = baos.toString();
154: TestLogger.logger.debug(newText);
155: assertTrue(newText.contains("m:op"));
156: assertTrue(newText.contains("m:param"));
157: assertTrue(newText.contains("100"));
158: assertTrue(newText.contains("Envelope"));
159: assertTrue(newText.contains("Body"));
160: }
161:
162: /**
163: * Create a JAXBBlock containing a JAX-B business object
164: * and simulate a normal Proxy output flow
165: * @throws Exception
166: */
167: public void testJAXBOutflowPerf() throws Exception {
168: // Create a SOAP 1.1 Message
169: MessageFactory mf = (MessageFactory) FactoryRegistry
170: .getFactory(MessageFactory.class);
171: Message m = mf.create(Protocol.soap11);
172:
173: // Indicate that the style is RPC, Indicate the Operation QName
174: m.setStyle(Style.RPC);
175: QName opQName = new QName("urn://sample", "op", "m");
176: m.setOperationElement(opQName);
177:
178: // Get the BlockFactory
179: JAXBBlockFactory bf = (JAXBBlockFactory) FactoryRegistry
180: .getFactory(JAXBBlockFactory.class);
181:
182: // Create the JAXBContext
183: JAXBBlockContext context = new JAXBBlockContext(
184: StockPrice.class.getPackage().getName());
185:
186: // Create the JAX-B object
187: ObjectFactory of = new ObjectFactory();
188: StockPrice obj = of.createStockPrice();
189: obj.setPrice("100");
190:
191: // Create the JAX-B Element
192: QName paramQName = new QName("urn://sample", "param", "m");
193: JAXBElement e = new JAXBElement(paramQName, StockPrice.class,
194: obj);
195:
196: // Create a JAXBBlock using the param object as the content. This simulates
197: // what occurs on the outbound JAX-WS Proxy client
198: Block block = bf.createFrom(e, context, null);
199:
200: // Add the block to the message as normal body content.
201: m.setBodyBlock(block);
202:
203: // Check to see if the message is a fault. The client/server will always call this method.
204: // The Message must respond appropriately without doing a conversion.
205: boolean isFault = m.isFault();
206: assertTrue(!isFault);
207: assertTrue("XMLPart Representation is "
208: + m.getXMLPartContentType(), "SPINE".equals(m
209: .getXMLPartContentType()));
210:
211: // On an outbound flow, we need to convert the Message
212: // to an OMElement, specifically an OM SOAPEnvelope,
213: // so we can set it on the Axis2 MessageContext
214: org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope) m
215: .getAsOMElement();
216:
217: // Check to see if the message is a fault. The client/server will always call this method.
218: // The Message must respond appropriately without doing a conversion.
219: isFault = m.isFault();
220: assertTrue(!isFault);
221: assertTrue("XMLPart Representation is "
222: + m.getXMLPartContentType(), "OM".equals(m
223: .getXMLPartContentType()));
224:
225: // PERFORMANCE CHECK:
226: // The param in the body should be an OMSourcedElement
227: OMElement o = env.getBody().getFirstElement().getFirstElement();
228: assertTrue(o instanceof OMSourcedElementImpl);
229: assertTrue(((OMSourcedElementImpl) o).isExpanded() == false);
230:
231: // Serialize the Envelope using the same mechanism as the
232: // HTTP client.
233: ByteArrayOutputStream baos = new ByteArrayOutputStream();
234: env.serializeAndConsume(baos, new OMOutputFormat());
235:
236: // To check that the output is correct, get the String contents of the
237: // reader
238: String newText = baos.toString();
239: TestLogger.logger.debug(newText);
240: assertTrue(newText.contains("m:op"));
241: assertTrue(newText.contains("m:param"));
242: assertTrue(newText.contains("100"));
243: assertTrue(newText.contains("Envelope"));
244: assertTrue(newText.contains("Body"));
245: }
246:
247: public void testJAXBInflow_soap11() throws Exception {
248: _testJAXBInflow(sampleEnvelope11);
249: }
250:
251: public void testJAXBInflow_soap12() throws Exception {
252: _testJAXBInflow(sampleEnvelope12);
253: }
254:
255: public void _testJAXBInflow(String sampleJAXBEnvelope)
256: throws Exception {
257: // Create a SOAP OM out of the sample incoming XML. This
258: // simulates what Axis2 will be doing with the inbound message.
259: StringReader sr = new StringReader(sampleJAXBEnvelope);
260: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
261: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
262: null);
263: OMElement omElement = builder.getSOAPEnvelope();
264:
265: // Create a SOAP 1.1 Message from the sample incoming XML
266: MessageFactory mf = (MessageFactory) FactoryRegistry
267: .getFactory(MessageFactory.class);
268: Message m = mf.createFrom(omElement, null);
269:
270: // Check to see if the message is a fault. The client/server will always call this method.
271: // The Message must respond appropriately without doing a conversion.
272: boolean isFault = m.isFault();
273: assertTrue(!isFault);
274: assertTrue("XMLPart Representation is "
275: + m.getXMLPartContentType(), "OM".equals(m
276: .getXMLPartContentType()));
277:
278: // Indicate that the message should be accessed as RPC
279: m.setStyle(Style.RPC);
280:
281: // Get the BlockFactory
282: JAXBBlockFactory bf = (JAXBBlockFactory) FactoryRegistry
283: .getFactory(JAXBBlockFactory.class);
284:
285: // Create the JAXBContext instance that will be used
286: // to deserialize the JAX-B object content in the message.
287: JAXBBlockContext context = new JAXBBlockContext(
288: StockPrice.class.getPackage().getName());
289:
290: // Get the JAXBBlock that wraps the content
291: Block b = m.getBodyBlock(context, bf);
292:
293: // Check to see if the message is a fault. The client/server will always call this method.
294: // The Message must respond appropriately without doing a conversion.
295: isFault = m.isFault();
296: assertTrue(!isFault);
297: assertTrue("XMLPart Representation is "
298: + m.getXMLPartContentType(), "SPINE".equals(m
299: .getXMLPartContentType()));
300:
301: // Get the business object from the block, which should be a
302: // JAX-B object
303: Object bo = b.getBusinessObject(true);
304:
305: // Check to make sure the right object was returned
306: assertNotNull(bo);
307: if (bo instanceof JAXBElement) {
308: bo = ((JAXBElement) bo).getValue();
309: }
310: assertTrue(bo instanceof StockPrice);
311:
312: // Check to make sure the content of that object is correct
313: StockPrice obj = (StockPrice) bo;
314: assertNotNull(obj);
315: assertTrue(obj.getPrice().equals("100"));
316: }
317:
318: }
|