001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.security.xwss;
018:
019: import junit.framework.TestCase;
020: import org.springframework.core.io.ClassPathResource;
021: import org.springframework.core.io.Resource;
022: import org.springframework.ws.soap.saaj.SaajSoapMessage;
023: import org.springframework.xml.xpath.XPathExpression;
024: import org.springframework.xml.xpath.XPathExpressionFactory;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Node;
027:
028: import javax.xml.soap.MessageFactory;
029: import javax.xml.soap.MimeHeaders;
030: import javax.xml.soap.SOAPException;
031: import javax.xml.soap.SOAPMessage;
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: public abstract class XwssMessageInterceptorTestCase extends TestCase {
038:
039: protected XwsSecurityInterceptor interceptor;
040:
041: private MessageFactory messageFactory;
042:
043: private Map namespaces;
044:
045: protected final void setUp() throws Exception {
046: interceptor = new XwsSecurityInterceptor();
047: messageFactory = MessageFactory.newInstance();
048: namespaces = new HashMap();
049: namespaces.put("SOAP-ENV",
050: "http://schemas.xmlsoap.org/soap/envelope/");
051: namespaces
052: .put(
053: "wsse",
054: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
055: namespaces.put("ds", "http://www.w3.org/2000/09/xmldsig#");
056: namespaces.put("xenc", "http://www.w3.org/2001/04/xmlenc#");
057: onSetup();
058: }
059:
060: protected void assertXpathEvaluatesTo(String message,
061: String expectedValue, String xpathExpression,
062: SOAPMessage soapMessage) {
063: XPathExpression expression = XPathExpressionFactory
064: .createXPathExpression(xpathExpression, namespaces);
065: Document document = soapMessage.getSOAPPart();
066: String actualValue = expression.evaluateAsString(document);
067: assertEquals(message, expectedValue, actualValue);
068: }
069:
070: protected void assertXpathExists(String message,
071: String xpathExpression, SOAPMessage soapMessage) {
072: XPathExpression expression = XPathExpressionFactory
073: .createXPathExpression(xpathExpression, namespaces);
074: Document document = soapMessage.getSOAPPart();
075: Node node = expression.evaluateAsNode(document);
076: assertNotNull(message, node);
077: }
078:
079: protected void assertXpathNotExists(String message,
080: String xpathExpression, SOAPMessage soapMessage) {
081: XPathExpression expression = XPathExpressionFactory
082: .createXPathExpression(xpathExpression, namespaces);
083: Document document = soapMessage.getSOAPPart();
084: Node node = expression.evaluateAsNode(document);
085: assertNull(message, node);
086: }
087:
088: protected SaajSoapMessage loadSaajMessage(String fileName)
089: throws SOAPException, IOException {
090: MimeHeaders mimeHeaders = new MimeHeaders();
091: mimeHeaders.addHeader("Content-Type", "text/xml");
092: Resource resource = new ClassPathResource(fileName, getClass());
093: InputStream is = resource.getInputStream();
094: try {
095: assertTrue(
096: "Could not load SAAJ message [" + resource + "]",
097: resource.exists());
098: is = resource.getInputStream();
099: return new SaajSoapMessage(messageFactory.createMessage(
100: mimeHeaders, is));
101: } finally {
102: is.close();
103: }
104: }
105:
106: protected void onSetup() throws Exception {
107: }
108: }
|