View Javadoc

1   /*
2    * Copyright 2005-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.test.support.matcher;
18  
19  import java.io.IOException;
20  import java.util.Iterator;
21  import javax.xml.namespace.QName;
22  
23  import org.springframework.util.Assert;
24  import org.springframework.ws.soap.SoapHeader;
25  import org.springframework.ws.soap.SoapHeaderElement;
26  import org.springframework.ws.soap.SoapMessage;
27  
28  import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
29  
30  /**
31   * Matches SOAP headers.
32   *
33   * @author Arjen Poutsma
34   * @since 2.0
35   */
36  public class SoapHeaderMatcher extends AbstractSoapMessageMatcher {
37  
38      private final QName soapHeaderName;
39  
40      /**
41       * Creates a new instance of the {@code SoapHeaderMatcher} that checks for the presence of the given SOAP header
42       * name.
43       *
44       * @param soapHeaderName the header name to check for
45       */
46      public SoapHeaderMatcher(QName soapHeaderName) {
47          Assert.notNull(soapHeaderName, "'soapHeaderName' must not be null");
48          this.soapHeaderName = soapHeaderName;
49      }
50  
51      @Override
52      protected void match(SoapMessage soapMessage) throws IOException, AssertionError {
53          SoapHeader soapHeader = soapMessage.getSoapHeader();
54          assertTrue("SOAP message [" + soapMessage + "] does not contain SOAP header", soapHeader != null, "Envelope",
55                  soapMessage.getEnvelope().getSource());
56  
57          Iterator<SoapHeaderElement> soapHeaderElementIterator = soapHeader.examineAllHeaderElements();
58          boolean found = false;
59          while (soapHeaderElementIterator.hasNext()) {
60              SoapHeaderElement soapHeaderElement = soapHeaderElementIterator.next();
61              if (soapHeaderName.equals(soapHeaderElement.getName())) {
62                  found = true;
63                  break;
64              }
65          }
66          assertTrue("SOAP header [" + soapHeaderName + "] not found", found, "Envelope",
67                  soapMessage.getEnvelope().getSource());
68      }
69  }