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.IOException;
021: import java.io.InputStream;
022:
023: import javax.xml.bind.JAXBContext;
024: import javax.xml.namespace.QName;
025: import javax.xml.stream.XMLInputFactory;
026: import javax.xml.stream.XMLStreamReader;
027:
028: import org.apache.cxf.databinding.DataReader;
029: import org.apache.cxf.jaxb.JAXBDataBinding;
030: import org.apache.cxf.service.model.MessagePartInfo;
031: import org.apache.cxf.staxutils.StaxStreamFilter;
032: import org.apache.hello_world_doc_lit_bare.types.TradePriceData;
033: import org.apache.hello_world_rpclit.types.MyComplexStruct;
034: import org.apache.hello_world_soap_http.types.GreetMe;
035: import org.apache.hello_world_soap_http.types.GreetMeResponse;
036: import org.junit.After;
037: import org.junit.Assert;
038: import org.junit.Before;
039: import org.junit.Test;
040:
041: public class XMLStreamDataReaderTest extends Assert {
042:
043: private XMLInputFactory factory;
044: private XMLStreamReader reader;
045: private InputStream is;
046:
047: @Before
048: public void setUp() throws Exception {
049: factory = XMLInputFactory.newInstance();
050: }
051:
052: @After
053: public void tearDown() throws IOException {
054: is.close();
055: }
056:
057: @Test
058: public void testReadWrapper() throws Exception {
059: JAXBDataBinding db = getDataBinding(GreetMe.class);
060:
061: reader = getTestReader("../resources/GreetMeDocLiteralReq.xml");
062: assertNotNull(reader);
063:
064: DataReader<XMLStreamReader> dr = db
065: .createReader(XMLStreamReader.class);
066: assertNotNull(dr);
067: Object val = dr.read(reader);
068: assertNotNull(val);
069: assertTrue(val instanceof GreetMe);
070: assertEquals("TestSOAPInputPMessage", ((GreetMe) val)
071: .getRequestType());
072: }
073:
074: @Test
075: public void testReadWrapperReturn() throws Exception {
076: JAXBDataBinding db = getDataBinding(GreetMeResponse.class);
077:
078: reader = getTestReader("../resources/GreetMeDocLiteralResp.xml");
079: assertNotNull(reader);
080:
081: DataReader<XMLStreamReader> dr = db
082: .createReader(XMLStreamReader.class);
083: assertNotNull(dr);
084:
085: Object retValue = dr.read(reader);
086:
087: assertNotNull(retValue);
088: assertTrue(retValue instanceof GreetMeResponse);
089: assertEquals("TestSOAPOutputPMessage",
090: ((GreetMeResponse) retValue).getResponseType());
091: }
092:
093: @Test
094: public void testReadRPC() throws Exception {
095: JAXBDataBinding db = getDataBinding(MyComplexStruct.class);
096:
097: QName[] tags = { new QName(
098: "http://apache.org/hello_world_rpclit",
099: "sendReceiveData") };
100:
101: reader = getTestReader("../resources/greetMeRpcLitReq.xml");
102: assertNotNull(reader);
103:
104: XMLStreamReader localReader = getTestFilteredReader(reader,
105: tags);
106:
107: DataReader<XMLStreamReader> dr = db
108: .createReader(XMLStreamReader.class);
109: assertNotNull(dr);
110: Object val = dr.read(new QName(
111: "http://apache.org/hello_world_rpclit", "in"),
112: localReader, MyComplexStruct.class);
113: assertNotNull(val);
114:
115: assertTrue(val instanceof MyComplexStruct);
116: assertEquals("this is element 1", ((MyComplexStruct) val)
117: .getElem1());
118: assertEquals("this is element 2", ((MyComplexStruct) val)
119: .getElem2());
120: assertEquals(42, ((MyComplexStruct) val).getElem3());
121: }
122:
123: @Test
124: public void testReadBare() throws Exception {
125: JAXBDataBinding db = getDataBinding(TradePriceData.class);
126:
127: reader = getTestReader("../resources/sayHiDocLitBareReq.xml");
128: assertNotNull(reader);
129:
130: DataReader<XMLStreamReader> dr = db
131: .createReader(XMLStreamReader.class);
132: assertNotNull(dr);
133: QName elName = new QName(
134: "http://apache.org/hello_world_doc_lit_bare/types",
135: "inout");
136: MessagePartInfo part = new MessagePartInfo(elName, null);
137: part.setElement(true);
138: part.setElementQName(elName);
139: part.setTypeClass(TradePriceData.class);
140: Object val = dr.read(part, reader);
141:
142: assertNotNull(val);
143: assertTrue(val instanceof TradePriceData);
144: assertEquals("CXF", ((TradePriceData) val).getTickerSymbol());
145: assertEquals(1.0f, ((TradePriceData) val).getTickerPrice());
146: }
147:
148: private JAXBDataBinding getDataBinding(Class... clz)
149: throws Exception {
150: JAXBContext ctx = JAXBContext.newInstance(clz);
151: return new JAXBDataBinding(ctx);
152: }
153:
154: private XMLStreamReader getTestFilteredReader(XMLStreamReader r,
155: QName[] q) throws Exception {
156: StaxStreamFilter filter = new StaxStreamFilter(q);
157: return factory.createFilteredReader(r, filter);
158: }
159:
160: private XMLStreamReader getTestReader(String resource)
161: throws Exception {
162: is = getTestStream(resource);
163: assertNotNull(is);
164: return factory.createXMLStreamReader(is);
165: }
166:
167: private InputStream getTestStream(String resource) {
168: return getClass().getResourceAsStream(resource);
169: }
170: }
|