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