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.saaj;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.mail.util.ByteArrayDataSource;
027: import javax.xml.stream.XMLStreamReader;
028:
029: import org.w3c.dom.Element;
030:
031: import org.apache.cxf.BusFactory;
032: import org.apache.cxf.binding.soap.Soap12;
033: import org.apache.cxf.binding.soap.TestBase;
034: import org.apache.cxf.binding.soap.TestUtil;
035: import org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor;
036: import org.apache.cxf.headers.Header;
037: import org.apache.cxf.interceptor.StaxInInterceptor;
038: import org.junit.Before;
039: import org.junit.Test;
040:
041: public class SAAJInInterceptorTest extends TestBase {
042:
043: private ReadHeadersInterceptor rhi;
044: private StaxInInterceptor staxIntc = new StaxInInterceptor();
045: private SAAJInInterceptor saajIntc;
046:
047: @Before
048: public void setUp() throws Exception {
049: super .setUp();
050:
051: rhi = new ReadHeadersInterceptor(BusFactory.getDefaultBus(),
052: "phase1");
053: chain.add(rhi);
054:
055: saajIntc = new SAAJInInterceptor("phase2");
056: chain.add(saajIntc);
057:
058: }
059:
060: @Test
061: public void testHandleHeader() {
062: try {
063: prepareSoapMessage("../test-soap-header.xml");
064: } catch (IOException ioe) {
065: fail("Failed in creating soap message");
066: }
067:
068: staxIntc.handleMessage(soapMessage);
069: rhi.handleMessage(soapMessage);
070: saajIntc.handleMessage(soapMessage);
071:
072: // check the xmlReader should be placed on the first entry of the body
073: // element
074: XMLStreamReader xmlReader = soapMessage
075: .getContent(XMLStreamReader.class);
076: assertEquals("check the first entry of body", "itinerary",
077: xmlReader.getLocalName());
078:
079: List<Header> eleHeaders = soapMessage.getHeaders();
080: List<Element> headerChilds = new ArrayList<Element>();
081: Iterator<Header> iter = eleHeaders.iterator();
082:
083: while (iter.hasNext()) {
084: Header hdr = iter.next();
085:
086: if (hdr.getObject() instanceof Element) {
087: headerChilds.add((Element) hdr.getObject());
088: }
089: }
090: // for (int i = 0; i < eleHeaders.getChildNodes().getLength(); i++) {
091: // if (eleHeaders.getChildNodes().item(i) instanceof Element) {
092: // Element element = (Element)eleHeaders.getChildNodes().item(i);
093: // headerChilds.add(element);
094: // }
095: // }
096:
097: assertEquals(2, headerChilds.size());
098: }
099:
100: private void prepareSoapMessage(String message) throws IOException {
101:
102: soapMessage = TestUtil.createEmptySoapMessage(Soap12
103: .getInstance(), chain);
104: ByteArrayDataSource bads = new ByteArrayDataSource(this
105: .getClass().getResourceAsStream(message), "text/xml");
106: soapMessage
107: .setContent(InputStream.class, bads.getInputStream());
108:
109: }
110: }
|