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: */package org.apache.cxf.jaxb.io;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022:
023: import javax.xml.bind.JAXBContext;
024: import javax.xml.namespace.QName;
025: import javax.xml.stream.XMLInputFactory;
026: import javax.xml.stream.XMLOutputFactory;
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.stream.XMLStreamWriter;
029:
030: import org.apache.cxf.databinding.DataWriter;
031: import org.apache.cxf.jaxb.JAXBDataBinding;
032: import org.apache.cxf.service.model.MessagePartInfo;
033: import org.apache.cxf.staxutils.DepthXMLStreamReader;
034: import org.apache.cxf.staxutils.StaxUtils;
035: import org.apache.hello_world_doc_lit_bare.types.TradePriceData;
036: import org.apache.hello_world_rpclit.types.MyComplexStruct;
037: import org.apache.hello_world_soap_http.types.GreetMe;
038: import org.apache.hello_world_soap_http.types.GreetMeResponse;
039: import org.junit.After;
040: import org.junit.Assert;
041: import org.junit.Before;
042: import org.junit.Test;
043:
044: public class XMLStreamDataWriterTest extends Assert {
045:
046: private ByteArrayOutputStream baos;
047: private XMLStreamWriter streamWriter;
048: private XMLInputFactory inFactory;
049:
050: @Before
051: public void setUp() throws Exception {
052: baos = new ByteArrayOutputStream();
053: XMLOutputFactory factory = XMLOutputFactory.newInstance();
054: streamWriter = factory.createXMLStreamWriter(baos);
055: assertNotNull(streamWriter);
056: inFactory = XMLInputFactory.newInstance();
057: }
058:
059: @After
060: public void tearDown() throws Exception {
061: baos.close();
062: }
063:
064: @Test
065: public void testWriteRPCLit1() throws Exception {
066: JAXBDataBinding db = getTestWriterFactory();
067:
068: DataWriter<XMLStreamWriter> dw = db
069: .createWriter(XMLStreamWriter.class);
070: assertNotNull(dw);
071:
072: String val = new String("TESTOUTPUTMESSAGE");
073: QName elName = new QName(
074: "http://apache.org/hello_world_rpclit/types", "in");
075: MessagePartInfo part = new MessagePartInfo(elName, null);
076: part.setElement(true);
077: part.setElementQName(elName);
078: dw.write(val, part, streamWriter);
079: streamWriter.flush();
080:
081: ByteArrayInputStream bais = new ByteArrayInputStream(baos
082: .toByteArray());
083: XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
084: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
085: StaxUtils.toNextElement(reader);
086: assertEquals(new QName(
087: "http://apache.org/hello_world_rpclit/types", "in"),
088: reader.getName());
089:
090: StaxUtils.nextEvent(reader);
091: StaxUtils.toNextText(reader);
092: assertEquals("TESTOUTPUTMESSAGE", reader.getText());
093: }
094:
095: @Test
096: public void testWriteRPCLit2() throws Exception {
097: JAXBDataBinding db = getTestWriterFactory(MyComplexStruct.class);
098:
099: DataWriter<XMLStreamWriter> dw = db
100: .createWriter(XMLStreamWriter.class);
101: assertNotNull(dw);
102:
103: MyComplexStruct val = new MyComplexStruct();
104: val.setElem1("This is element 1");
105: val.setElem2("This is element 2");
106: val.setElem3(1);
107:
108: QName elName = new QName(
109: "http://apache.org/hello_world_rpclit/types", "in");
110: MessagePartInfo part = new MessagePartInfo(elName, null);
111: part.setElement(true);
112: part.setElementQName(elName);
113:
114: dw.write(val, part, streamWriter);
115: streamWriter.flush();
116:
117: ByteArrayInputStream bais = new ByteArrayInputStream(baos
118: .toByteArray());
119: XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
120: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
121: StaxUtils.toNextElement(reader);
122: assertEquals(new QName(
123: "http://apache.org/hello_world_rpclit/types", "in"),
124: reader.getName());
125:
126: StaxUtils.nextEvent(reader);
127: StaxUtils.toNextElement(reader);
128: assertEquals(new QName(
129: "http://apache.org/hello_world_rpclit/types", "elem1"),
130: reader.getName());
131:
132: StaxUtils.nextEvent(reader);
133: StaxUtils.toNextText(reader);
134: assertEquals("This is element 1", reader.getText());
135: }
136:
137: @Test
138: public void testWriteBare() throws Exception {
139: JAXBDataBinding db = getTestWriterFactory(TradePriceData.class);
140:
141: DataWriter<XMLStreamWriter> dw = db
142: .createWriter(XMLStreamWriter.class);
143: assertNotNull(dw);
144:
145: TradePriceData val = new TradePriceData();
146: val.setTickerSymbol("This is a symbol");
147: val.setTickerPrice(1.0f);
148:
149: QName elName = new QName(
150: "http://apache.org/hello_world_doc_lit_bare/types",
151: "inout");
152: MessagePartInfo part = new MessagePartInfo(elName, null);
153: part.setElement(true);
154: part.setElementQName(elName);
155: dw.write(val, part, streamWriter);
156: streamWriter.flush();
157:
158: ByteArrayInputStream bais = new ByteArrayInputStream(baos
159: .toByteArray());
160: XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
161: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
162: StaxUtils.toNextElement(reader);
163: assertEquals(new QName(
164: "http://apache.org/hello_world_doc_lit_bare/types",
165: "inout"), reader.getName());
166:
167: StaxUtils.nextEvent(reader);
168: StaxUtils.toNextElement(reader);
169: assertEquals(new QName(
170: "http://apache.org/hello_world_doc_lit_bare/types",
171: "tickerSymbol"), reader.getName());
172:
173: StaxUtils.nextEvent(reader);
174: StaxUtils.toNextText(reader);
175: assertEquals("This is a symbol", reader.getText());
176: }
177:
178: @Test
179: public void testWriteWrapper() throws Exception {
180: JAXBDataBinding db = getTestWriterFactory(GreetMe.class);
181:
182: DataWriter<XMLStreamWriter> dw = db
183: .createWriter(XMLStreamWriter.class);
184: assertNotNull(dw);
185:
186: GreetMe val = new GreetMe();
187: val.setRequestType("Hello");
188:
189: dw.write(val, streamWriter);
190: streamWriter.flush();
191:
192: ByteArrayInputStream bais = new ByteArrayInputStream(baos
193: .toByteArray());
194: XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
195: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
196: StaxUtils.toNextElement(reader);
197: assertEquals(new QName(
198: "http://apache.org/hello_world_soap_http/types",
199: "greetMe"), reader.getName());
200:
201: StaxUtils.nextEvent(reader);
202: StaxUtils.toNextElement(reader);
203: assertEquals(new QName(
204: "http://apache.org/hello_world_soap_http/types",
205: "requestType"), reader.getName());
206:
207: StaxUtils.nextEvent(reader);
208: StaxUtils.toNextText(reader);
209: assertEquals("Hello", reader.getText());
210: }
211:
212: @Test
213: public void testWriteWrapperReturn() throws Exception {
214: JAXBDataBinding db = getTestWriterFactory(GreetMeResponse.class);
215:
216: DataWriter<XMLStreamWriter> dw = db
217: .createWriter(XMLStreamWriter.class);
218: assertNotNull(dw);
219:
220: GreetMeResponse retVal = new GreetMeResponse();
221: retVal.setResponseType("TESTOUTPUTMESSAGE");
222:
223: dw.write(retVal, streamWriter);
224: streamWriter.flush();
225:
226: ByteArrayInputStream bais = new ByteArrayInputStream(baos
227: .toByteArray());
228: XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
229: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
230: StaxUtils.toNextElement(reader);
231: assertEquals(new QName(
232: "http://apache.org/hello_world_soap_http/types",
233: "greetMeResponse"), reader.getName());
234:
235: StaxUtils.nextEvent(reader);
236: StaxUtils.toNextElement(reader);
237: assertEquals(new QName(
238: "http://apache.org/hello_world_soap_http/types",
239: "responseType"), reader.getName());
240:
241: StaxUtils.nextEvent(reader);
242: StaxUtils.toNextText(reader);
243: assertEquals("TESTOUTPUTMESSAGE", reader.getText());
244: }
245:
246: private JAXBDataBinding getTestWriterFactory(Class... clz)
247: throws Exception {
248: JAXBContext ctx = JAXBContext.newInstance(clz);
249: return new JAXBDataBinding(ctx);
250: }
251: }
|