1   /*
2    * Copyright 2006 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.soap.security.xwss;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.HashMap;
22  import java.util.Map;
23  import javax.xml.soap.MessageFactory;
24  import javax.xml.soap.MimeHeaders;
25  import javax.xml.soap.SOAPException;
26  import javax.xml.soap.SOAPMessage;
27  
28  import org.custommonkey.xmlunit.XMLTestCase;
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Node;
31  
32  import org.springframework.core.io.ClassPathResource;
33  import org.springframework.core.io.Resource;
34  import org.springframework.ws.soap.saaj.SaajSoapMessage;
35  import org.springframework.xml.xpath.XPathExpression;
36  import org.springframework.xml.xpath.XPathExpressionFactory;
37  
38  public abstract class AbstractXwssMessageInterceptorTestCase extends XMLTestCase {
39  
40      protected XwsSecurityInterceptor interceptor;
41  
42      private MessageFactory messageFactory;
43  
44      private Map namespaces;
45  
46      protected final void setUp() throws Exception {
47          interceptor = new XwsSecurityInterceptor();
48          messageFactory = MessageFactory.newInstance();
49          namespaces = new HashMap();
50          namespaces.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
51          namespaces.put("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
52          namespaces.put("ds", "http://www.w3.org/2000/09/xmldsig#");
53          namespaces.put("xenc", "http://www.w3.org/2001/04/xmlenc#");
54          onSetup();
55      }
56  
57      protected void assertXpathEvaluatesTo(String message,
58                                            String expectedValue,
59                                            String xpathExpression,
60                                            SOAPMessage soapMessage) {
61          XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
62          Document document = soapMessage.getSOAPPart();
63          String actualValue = expression.evaluateAsString(document);
64          assertEquals(message, expectedValue, actualValue);
65      }
66  
67      protected void assertXpathExists(String message, String xpathExpression, SOAPMessage soapMessage) {
68          XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
69          Document document = soapMessage.getSOAPPart();
70          Node node = expression.evaluateAsNode(document);
71          assertNotNull(message, node);
72      }
73  
74      protected void assertXpathNotExists(String message, String xpathExpression, SOAPMessage soapMessage) {
75          XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
76          Document document = soapMessage.getSOAPPart();
77          Node node = expression.evaluateAsNode(document);
78          assertNull(message, node);
79      }
80  
81      protected SaajSoapMessage loadSaajMessage(String fileName) throws SOAPException, IOException {
82          MimeHeaders mimeHeaders = new MimeHeaders();
83          mimeHeaders.addHeader("Content-Type", "text/xml");
84          Resource resource = new ClassPathResource(fileName, getClass());
85          InputStream is = resource.getInputStream();
86          try {
87              assertTrue("Could not load SAAJ message [" + resource + "]", resource.exists());
88              is = resource.getInputStream();
89              return new SaajSoapMessage(messageFactory.createMessage(mimeHeaders, is));
90          }
91          finally {
92              is.close();
93          }
94      }
95  
96      protected void onSetup() throws Exception {
97      }
98  }