View Javadoc

1   /*
2    * Copyright 2005-2012 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 javax.xml.transform.Source;
21  import javax.xml.transform.TransformerException;
22  import javax.xml.transform.dom.DOMResult;
23  
24  import org.springframework.util.Assert;
25  import org.springframework.ws.soap.SoapMessage;
26  import org.springframework.xml.transform.TransformerHelper;
27  
28  import org.custommonkey.xmlunit.Diff;
29  import org.custommonkey.xmlunit.XMLUnit;
30  import org.w3c.dom.Document;
31  
32  import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
33  import static org.springframework.ws.test.support.AssertionErrors.fail;
34  
35  /**
36   * Matches [email protected] Source} SOAP envelopes.
37   *
38   * @author Alexander Shutyaev
39   * @since 2.1.1
40   */
41  public class SoapEnvelopeDiffMatcher extends AbstractSoapMessageMatcher {
42  	
43  	private final Source expected;
44  
45      private final TransformerHelper transformerHelper = new TransformerHelper();
46  
47      static {
48          XMLUnit.setIgnoreWhitespace(true);
49      }
50  
51      public SoapEnvelopeDiffMatcher(Source expected) {
52          Assert.notNull(expected, "'expected' must not be null");
53          this.expected = expected;
54      }
55      
56      @Override
57      protected void match(SoapMessage soapMessage) throws IOException, AssertionError {
58      	Document actualDocument = soapMessage.getDocument();
59          Document expectedDocument = createDocumentFromSource(expected);
60      	Diff diff = new Diff(expectedDocument, actualDocument);
61          assertTrue("Envelopes are different, " + diff.toString(), diff.similar());
62      }
63  
64  	private Document createDocumentFromSource(Source source) {
65          try {
66              DOMResult result = new DOMResult();
67              transformerHelper.transform(source, result);
68              return (Document) result.getNode();
69          }
70          catch (TransformerException ex) {
71              fail("Could not transform source to DOMResult" + ex.getMessage());
72              return null;
73          }
74      }
75  
76  }