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;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.util.Arrays;
023: import java.util.List;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.stream.XMLStreamReader;
027: import javax.xml.stream.XMLStreamWriter;
028:
029: import org.apache.cxf.interceptor.BareOutInterceptor;
030: import org.apache.cxf.interceptor.InterceptorChain;
031: import org.apache.cxf.message.Message;
032: import org.apache.cxf.service.model.BindingOperationInfo;
033: import org.apache.cxf.staxutils.DepthXMLStreamReader;
034: import org.apache.cxf.staxutils.StaxUtils;
035: import org.apache.hello_world_soap_http.types.GreetMe;
036: import org.apache.hello_world_soap_http.types.GreetMeResponse;
037: import org.easymock.IMocksControl;
038: import org.easymock.classextension.EasyMock;
039: import org.junit.After;
040: import org.junit.Before;
041: import org.junit.Test;
042:
043: public class BareOutInterceptorTest extends TestBase {
044:
045: BareOutInterceptor interceptor;
046:
047: private ByteArrayOutputStream baos;
048: private XMLStreamWriter writer;
049:
050: @Before
051: public void setUp() throws Exception {
052: super .setUp();
053:
054: interceptor = new BareOutInterceptor();
055: baos = new ByteArrayOutputStream();
056: writer = getXMLStreamWriter(baos);
057: message.setContent(XMLStreamWriter.class, writer);
058: message.getExchange()
059: .put(BindingOperationInfo.class, operation);
060: IMocksControl control = EasyMock.createNiceControl();
061: InterceptorChain ic = control
062: .createMock(InterceptorChain.class);
063: message.setInterceptorChain(ic);
064: control.replay();
065: }
066:
067: @After
068: public void tearDown() throws Exception {
069: baos.close();
070: }
071:
072: @Test
073: public void testWriteOutbound() throws Exception {
074: GreetMeResponse greetMe = new GreetMeResponse();
075: greetMe.setResponseType("responseType");
076:
077: message.setContent(List.class, Arrays.asList(greetMe));
078:
079: interceptor.handleMessage(message);
080:
081: writer.close();
082:
083: assertNull(message.getContent(Exception.class));
084:
085: ByteArrayInputStream bais = new ByteArrayInputStream(baos
086: .toByteArray());
087: //System.err.println(baos.toString());
088: XMLStreamReader xr = StaxUtils.createXMLStreamReader(bais);
089: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
090: StaxUtils.toNextElement(reader);
091: assertEquals(new QName(
092: "http://apache.org/hello_world_soap_http/types",
093: "greetMeResponse"), reader.getName());
094:
095: StaxUtils.nextEvent(reader);
096: StaxUtils.toNextElement(reader);
097: assertEquals(new QName(
098: "http://apache.org/hello_world_soap_http/types",
099: "responseType"), reader.getName());
100: }
101:
102: @Test
103: public void testWriteInbound() throws Exception {
104: GreetMe greetMe = new GreetMe();
105: greetMe.setRequestType("requestType");
106:
107: message.setContent(List.class, Arrays.asList(greetMe));
108: message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
109:
110: interceptor.handleMessage(message);
111:
112: writer.close();
113:
114: assertNull(message.getContent(Exception.class));
115:
116: ByteArrayInputStream bais = new ByteArrayInputStream(baos
117: .toByteArray());
118: XMLStreamReader xr = StaxUtils.createXMLStreamReader(bais);
119: DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
120: StaxUtils.toNextElement(reader);
121: assertEquals(new QName(
122: "http://apache.org/hello_world_soap_http/types",
123: "greetMe"), reader.getName());
124:
125: StaxUtils.nextEvent(reader);
126: StaxUtils.toNextElement(reader);
127: assertEquals(new QName(
128: "http://apache.org/hello_world_soap_http/types",
129: "requestType"), reader.getName());
130: }
131: }
|