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.binding.soap;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025:
026: import javax.xml.stream.XMLStreamReader;
027: import javax.xml.stream.XMLStreamWriter;
028:
029: import org.apache.cxf.BusFactory;
030: import org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor;
031: import org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor;
032: import org.apache.cxf.interceptor.StaxInInterceptor;
033: import org.apache.cxf.staxutils.StaxUtils;
034: import org.junit.Before;
035: import org.junit.Test;
036:
037: public class SoapOutInterceptorTest extends TestBase {
038: private ReadHeadersInterceptor rhi;
039: private SoapOutInterceptor soi;
040:
041: @Before
042: public void setUp() throws Exception {
043: super .setUp();
044: StaxInInterceptor sii = new StaxInInterceptor("phase1");
045: chain.add(sii);
046:
047: rhi = new ReadHeadersInterceptor(BusFactory.getDefaultBus(),
048: "phase2");
049: chain.add(rhi);
050:
051: soi = new SoapOutInterceptor(BusFactory.getDefaultBus(),
052: "phase3");
053: chain.add(soi);
054: }
055:
056: @Test
057: public void testHandleMessage() throws Exception {
058: prepareSoapMessage();
059:
060: ByteArrayOutputStream out = new ByteArrayOutputStream();
061: soapMessage.setContent(OutputStream.class, out);
062: soapMessage.setContent(XMLStreamWriter.class, StaxUtils
063: .createXMLStreamWriter(out));
064:
065: soapMessage.getInterceptorChain().doIntercept(soapMessage);
066:
067: assertNotNull(soapMessage.getHeaders());
068:
069: Exception oe = (Exception) soapMessage
070: .getContent(Exception.class);
071: if (oe != null) {
072: throw oe;
073: }
074:
075: InputStream bis = new ByteArrayInputStream(out.toByteArray());
076: XMLStreamReader xmlReader = StaxUtils
077: .createXMLStreamReader(bis);
078: assertInputStream(xmlReader);
079: }
080:
081: private void assertInputStream(XMLStreamReader xmlReader)
082: throws Exception {
083: assertEquals(XMLStreamReader.START_ELEMENT, xmlReader.nextTag());
084: assertEquals(Soap12.getInstance().getEnvelope(), xmlReader
085: .getName());
086:
087: assertEquals(XMLStreamReader.START_ELEMENT, xmlReader.nextTag());
088: assertEquals(Soap12.getInstance().getHeader(), xmlReader
089: .getName());
090:
091: assertEquals(XMLStreamReader.START_ELEMENT, xmlReader.nextTag());
092: assertEquals("reservation", xmlReader.getLocalName());
093:
094: assertEquals(XMLStreamReader.START_ELEMENT, xmlReader.nextTag());
095: assertEquals("reference", xmlReader.getLocalName());
096: // I don't think we're writing the body yet...
097: //
098: // assertEquals(XMLStreamReader.START_ELEMENT, xmlReader.nextTag());
099: // assertEquals(Soap12.getInstance().getBody(), xmlReader.getName());
100: }
101:
102: private void prepareSoapMessage() throws IOException {
103: soapMessage = TestUtil.createEmptySoapMessage(Soap12
104: .getInstance(), chain);
105:
106: soapMessage.setContent(InputStream.class, getClass()
107: .getResourceAsStream("test-soap-header.xml"));
108: }
109:
110: }
|