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 javax.xml.transform.dom.DOMResult;
20  
21  import org.springframework.ws.soap.SoapMessage;
22  import org.springframework.xml.transform.StringSource;
23  import org.springframework.xml.transform.TransformerHelper;
24  
25  import org.junit.Test;
26  import org.w3c.dom.Document;
27  
28  import static org.easymock.EasyMock.*;
29  
30  public class SoapEnvelopeDiffMatcherTest {
31  	
32  	@Test
33      public void match() throws Exception {
34  		StringBuilder xmlBuilder = new StringBuilder();
35  		xmlBuilder.append("<?xml version='1.0'?>");
36  		xmlBuilder.append("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>");
37  		xmlBuilder.append("<soap:Header><header xmlns='http://example.com'/></soap:Header>");
38  		xmlBuilder.append("<soap:Body><payload xmlns='http://example.com'/></soap:Body>");
39  		xmlBuilder.append("</soap:Envelope>");
40  		String xml = xmlBuilder.toString();
41  		DOMResult result = new DOMResult();
42  		TransformerHelper transformerHelper = new TransformerHelper();
43  		transformerHelper.transform(new StringSource(xml), result);
44          SoapMessage message = createMock(SoapMessage.class);
45          expect(message.getDocument()).andReturn((Document)result.getNode()).once();
46          replay(message);
47  
48          SoapEnvelopeDiffMatcher matcher = new SoapEnvelopeDiffMatcher(new StringSource(xml));
49          matcher.match(message);
50  
51          verify(message);
52      }
53  	
54  	@Test(expected = AssertionError.class)
55      public void nonMatch() throws Exception {
56  		StringBuilder xmlBuilder = new StringBuilder();
57  		xmlBuilder.append("<?xml version='1.0'?>");
58  		xmlBuilder.append("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>");
59  		xmlBuilder.append("<soap:Header><header xmlns='http://example.com'/></soap:Header>");
60  		xmlBuilder.append("<soap:Body><payload%s xmlns='http://example.com'/></soap:Body>");
61  		xmlBuilder.append("</soap:Envelope>");
62  		String xml = xmlBuilder.toString();
63  		String actual = String.format(xml, "1");
64  		DOMResult result = new DOMResult();
65  		TransformerHelper transformerHelper = new TransformerHelper();
66  		transformerHelper.transform(new StringSource(actual), result);
67          SoapMessage message = createMock(SoapMessage.class);
68          expect(message.getDocument()).andReturn((Document)result.getNode()).once();
69          replay(message);
70  
71          String expected = String.format(xml, "2");
72          SoapEnvelopeDiffMatcher matcher = new SoapEnvelopeDiffMatcher(new StringSource(expected));
73          matcher.match(message);
74      }
75  	
76  }