You are viewing a plain text version of this content. The canonical link for it is here.
Posted to muse-commits@ws.apache.org by ct...@apache.org on 2009/09/28 14:06:01 UTC

svn commit: r819501 - in /webservices/muse/trunk/tests: ./ src/ src/org/ src/org/apache/ src/org/apache/muse/ src/org/apache/muse/ws/ src/org/apache/muse/ws/addressing/ src/org/apache/muse/ws/addressing/soap/ src/org/apache/muse/ws/dm/ src/org/apache/m...

Author: ctwiner
Date: Mon Sep 28 12:06:00 2009
New Revision: 819501

URL: http://svn.apache.org/viewvc?rev=819501&view=rev
Log:
temporary home for the tests, should be integrated in the Maven build....

Added:
    webservices/muse/trunk/tests/
    webservices/muse/trunk/tests/src/
    webservices/muse/trunk/tests/src/org/
    webservices/muse/trunk/tests/src/org/apache/
    webservices/muse/trunk/tests/src/org/apache/muse/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/addressing/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/addressing/soap/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/addressing/soap/FaultCodeNSTest.java
    webservices/muse/trunk/tests/src/org/apache/muse/ws/dm/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/dm/muws/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/dm/muws/events/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/dm/muws/events/ManagementEventComponentSerializingTest.java
    webservices/muse/trunk/tests/src/org/apache/muse/ws/notification/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/notification/impl/
    webservices/muse/trunk/tests/src/org/apache/muse/ws/notification/impl/XPathFilterNamespaceTest.java

Added: webservices/muse/trunk/tests/src/org/apache/muse/ws/addressing/soap/FaultCodeNSTest.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/tests/src/org/apache/muse/ws/addressing/soap/FaultCodeNSTest.java?rev=819501&view=auto
==============================================================================
--- webservices/muse/trunk/tests/src/org/apache/muse/ws/addressing/soap/FaultCodeNSTest.java (added)
+++ webservices/muse/trunk/tests/src/org/apache/muse/ws/addressing/soap/FaultCodeNSTest.java Mon Sep 28 12:06:00 2009
@@ -0,0 +1,45 @@
+package org.apache.muse.ws.addressing.soap;
+
+import javax.xml.namespace.QName;
+
+import org.apache.muse.util.xml.XmlUtils;
+import org.w3c.dom.Document;
+
+import junit.framework.TestCase;
+
+public class FaultCodeNSTest extends TestCase {
+
+	String soapfault = "<?xml version='1.0' ?>"+
+"  <env:Fault xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\""+
+"            xmlns:rpc='http://www.w3.org/2003/05/soap-rpc'>"+
+"     <env:Code>"+
+"       <env:Value>env:Sender</env:Value>"+
+"       <env:Subcode>"+
+"        <env:Value>rpc:BadArguments</env:Value>"+
+"       </env:Subcode>"+
+"     </env:Code>"+
+"     <env:Reason>"+
+"      <env:Text xml:lang=\"en-US\">Processing error</env:Text>"+
+"      <env:Text xml:lang=\"cs\">Chyba zpracování</env:Text>"+
+"     </env:Reason>"+
+"     <env:Detail>"+
+"      <e:myFaultDetails"+ 
+"        xmlns:e=\"http://travelcompany.example.org/faults\">"+
+"        <e:message>Name does not match card number</e:message>"+
+"        <e:errorcode>999</e:errorcode>"+
+"      </e:myFaultDetails>"+
+"     </env:Detail>"+
+"   </env:Fault>";
+	
+	public void testSoapFaultCodeNamespaces() throws Exception {
+		Document doc = XmlUtils.createDocument(soapfault);
+		
+		SoapFault fault = new SoapFault(doc.getDocumentElement());
+		QName faultCode = fault.getCode();
+        System.out.println("Fault code: " + faultCode.getPrefix() + ", " + faultCode.getLocalPart() + ", " + faultCode.getNamespaceURI());
+        assertEquals("http://www.w3.org/2003/05/soap-envelope",faultCode.getNamespaceURI());
+        QName sfaultCode = fault.getSubCode();
+        System.out.println("SubFault code: " + sfaultCode.getPrefix() + ", " + sfaultCode.getLocalPart() + ", " + sfaultCode.getNamespaceURI());
+        assertEquals("http://www.w3.org/2003/05/soap-rpc",sfaultCode.getNamespaceURI());        
+	}
+}

Added: webservices/muse/trunk/tests/src/org/apache/muse/ws/dm/muws/events/ManagementEventComponentSerializingTest.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/tests/src/org/apache/muse/ws/dm/muws/events/ManagementEventComponentSerializingTest.java?rev=819501&view=auto
==============================================================================
--- webservices/muse/trunk/tests/src/org/apache/muse/ws/dm/muws/events/ManagementEventComponentSerializingTest.java (added)
+++ webservices/muse/trunk/tests/src/org/apache/muse/ws/dm/muws/events/ManagementEventComponentSerializingTest.java Mon Sep 28 12:06:00 2009
@@ -0,0 +1,54 @@
+package org.apache.muse.ws.dm.muws.events;
+
+import java.net.URI;
+
+import org.apache.muse.util.xml.XmlUtils;
+import org.apache.muse.ws.addressing.EndpointReference;
+import org.apache.muse.ws.dm.muws.events.impl.SimpleWefFactory;
+import org.w3c.dom.Element;
+
+import junit.framework.TestCase;
+
+public class ManagementEventComponentSerializingTest extends TestCase {
+
+	public void testManagementEventComponentSerializingTest() throws Exception {
+		WefFactory factory = new SimpleWefFactory();
+		ManagementEvent event = factory.createEvent();
+		 
+		//Create source component
+		Component source = factory.createComponent();
+		source.setName(WefConstants.SOURCE_COMP_QNAME);
+		EndpointReference epr = new EndpointReference(new URI("http://some.address"));
+		Element eprXml = epr.toXML();
+		ComponentAddress sourceAddress = factory.createComponentAddress();
+		sourceAddress.addExtendedElement(eprXml);
+		source.setAddress(sourceAddress);
+		source.setResourceID("resource");
+		event.setSource(source); 
+		
+		String strrep = event.toString();
+		System.out.println("event " + strrep);
+		
+		// now read it back in
+		ManagementEvent copyEvent = factory.createEvent(XmlUtils.createDocument(strrep).getDocumentElement());
+		
+		String reserialized = copyEvent.getSource().toString();
+		System.out.println("event copy addr " + reserialized);
+		
+		// reparse then check...
+		Element sc = XmlUtils.createDocument(reserialized).getDocumentElement();
+		// get first child..
+		Element child[] = XmlUtils.getAllElements(sc);
+		
+		assertEquals(1, child.length);
+		assertEquals(WefConstants.COMP_ADDRESS_QNAME, XmlUtils.getElementQName( child[0] ));
+		
+		// get its child.
+		child = XmlUtils.getAllElements(child[0]);
+		
+		// should not be component addresss
+		assertEquals(1, child.length);
+		assertFalse(WefConstants.COMP_ADDRESS_QNAME.equals( XmlUtils.getElementQName( child[0] ) ));
+		
+	}
+}

Added: webservices/muse/trunk/tests/src/org/apache/muse/ws/notification/impl/XPathFilterNamespaceTest.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/tests/src/org/apache/muse/ws/notification/impl/XPathFilterNamespaceTest.java?rev=819501&view=auto
==============================================================================
--- webservices/muse/trunk/tests/src/org/apache/muse/ws/notification/impl/XPathFilterNamespaceTest.java (added)
+++ webservices/muse/trunk/tests/src/org/apache/muse/ws/notification/impl/XPathFilterNamespaceTest.java Mon Sep 28 12:06:00 2009
@@ -0,0 +1,378 @@
+package org.apache.muse.ws.notification.impl;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.transform.TransformerException;
+
+import junit.framework.TestCase;
+
+import org.apache.muse.util.xml.XPathUtils;
+import org.apache.muse.util.xml.XmlUtils;
+import org.apache.muse.ws.notification.Filter;
+import org.apache.muse.ws.notification.WsnConstants;
+import org.apache.muse.ws.resource.basefaults.BaseFault;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+/**
+ * Issue 293 
+ * 
+ * A series of tests to cover proper namespace matching on XPath filters.
+ * 
+ * The first test is against namespace prefix delcarations which match those
+ * of the message.  The second against non matching ones.
+ * 
+ * The tests are run for both simple XML and for the XPathFilter
+ * 
+ * @author Chris Twiner
+ *
+ */
+public class XPathFilterNamespaceTest extends TestCase {
+
+	String base = "<payload xmlns='top' xmlns:ref='urn:one' xmlns:one='urn:another'><ref:body1/><one:body2/></payload>";
+	
+	String matchingPrefixFilter="<wsnt:Filter xmlns:wsnt='http://docs.oasis-open.org/wsn/b-2'>"+
+    "<wsnt:MessageContent Dialect='http://www.w3.org/TR/1999/REC-xpath-19991116' " +
+        "xmlns:ref='urn:one'>"+
+        "//ref:body1"+
+        "</wsnt:MessageContent>"+
+	"</wsnt:Filter>";
+
+	String nonMatchingPrefixFilter="<wsnt:Filter xmlns:wsnt='http://docs.oasis-open.org/wsn/b-2'>"+
+    "<wsnt:MessageContent Dialect='http://www.w3.org/TR/1999/REC-xpath-19991116' " +
+        "xmlns:prefix='urn:one'>"+
+        "//prefix:body1"+
+        "</wsnt:MessageContent>"+
+	"</wsnt:Filter>";
+	
+	public void testXMLOnlyNamespace() throws IOException, SAXException, TransformerException{
+		SimpleNotificationMessage msg = new SimpleNotificationMessage();
+		
+		msg.addMessageContent(XmlUtils.createDocument(base).getDocumentElement());
+		
+		// expected behavior as the source and the test prefixes are the same
+		assertTrue( XPathUtils.isMatch(msg.toXML(), "//ref:body1") );
+		// expected to fail as the one prefix does not match either prefix or the local-name
+		assertFalse( XPathUtils.isMatch(msg.toXML(), "//one:body1") );
+	}
+	
+	public void testDirectXPathNamespaceMatches() throws IOException, SAXException, TransformerException{
+		SimpleNotificationMessage msg = new SimpleNotificationMessage();
+		
+		msg.addMessageContent(XmlUtils.createDocument(base).getDocumentElement());
+		
+		Map resolver = new HashMap();
+		resolver.put("ref", "urn:one");
+		resolver.put("one", "urn:one");
+	    
+		// should both work
+		assertTrue( XPathUtils.isMatch(msg.toXML(), "//ref:body1", resolver) );
+		assertTrue( XPathUtils.isMatch(msg.toXML(), "//one:body1", resolver) );
+	}
+	
+	public void testMapCreationMatches() throws IOException, SAXException, TransformerException{
+		SimpleNotificationMessage msg = new SimpleNotificationMessage();
+		
+		msg.addMessageContent(XmlUtils.createDocument(base).getDocumentElement());
+	
+		Element mf = XmlUtils.getElement(XmlUtils.createDocument(matchingPrefixFilter)
+				.getDocumentElement(), WsnConstants.MESSAGE_CONTENT_QNAME);
+		Element nmf = XmlUtils.getElement(XmlUtils.createDocument(nonMatchingPrefixFilter)
+				.getDocumentElement(), WsnConstants.MESSAGE_CONTENT_QNAME);
+		
+		// should both work
+		assertTrue( XPathUtils.isMatch(msg.toXML(), XmlUtils.extractText(mf), XmlUtils.getNamespaceDeclarations(mf)) );
+		assertTrue( XPathUtils.isMatch(msg.toXML(), XmlUtils.extractText(nmf), XmlUtils.getNamespaceDeclarations(nmf)) );
+	}
+	
+	/**
+	 * Use the actual filter
+	 * @throws IOException
+	 * @throws SAXException
+	 * @throws TransformerException
+	 * @throws BaseFault 
+	 */
+	public void testFilterMatches() throws IOException, SAXException, TransformerException, BaseFault{
+		SimpleNotificationMessage msg = new SimpleNotificationMessage();
+		msg.addMessageContent(XmlUtils.createDocument(base).getDocumentElement());
+	
+		Element mf = XmlUtils.getElement(XmlUtils.createDocument(matchingPrefixFilter)
+				.getDocumentElement(), WsnConstants.MESSAGE_CONTENT_QNAME);
+		Element nmf = XmlUtils.getElement(XmlUtils.createDocument(nonMatchingPrefixFilter)
+				.getDocumentElement(), WsnConstants.MESSAGE_CONTENT_QNAME);
+
+		FilterFactoryHandler handler = new MessagePatternFilterHandler();
+		Filter mfFilter = handler.newInstance(mf);
+		Filter nmfFilter = handler.newInstance(nmf);
+		
+		// should both work
+		assertTrue( mfFilter.accepts(msg) );
+		assertTrue( nmfFilter.accepts(msg) );
+	}
+	
+	/**
+	 * Does the filter keep the namespaces upon serialization?
+	 *
+	 * @throws IOException
+	 * @throws SAXException
+	 * @throws TransformerException
+	 * @throws BaseFault
+	 */
+	public void testSubscriptionSeriallization() throws IOException, SAXException, TransformerException, BaseFault{
+		Element mfilter = XmlUtils.getElement(XmlUtils.createDocument(matchingPrefixFilter)
+				.getDocumentElement(), WsnConstants.MESSAGE_CONTENT_QNAME);
+		Map origNSmfilter = XmlUtils.getNamespaceDeclarations(mfilter);
+
+		FilterFactoryHandler handler = new MessagePatternFilterHandler();
+		Filter mfFilter = handler.newInstance(mfilter);
+		
+		Element outmfilter = XmlUtils.getElement(mfFilter.toXML()
+				, WsnConstants.MESSAGE_CONTENT_QNAME);
+		Map outNSmfilter = XmlUtils.getNamespaceDeclarations(outmfilter);
+
+		assertTrue( origNSmfilter.containsKey("ref"));
+		assertTrue( outNSmfilter.containsKey("ref"));
+	}
+	
+//	public boolean isMatch(Node context, String xpath, Map namespaces) 
+//    throws TransformerException
+//	{
+//		MapNSResolver resolver = new MapNSResolver();
+//		resolver.addMap(namespaces);
+//		XObject result = XPathAPI.eval(context, xpath, resolver);
+//	    return result.bool();
+//	}
+//	
+//	/**
+//	 * 293 - NOTE as the xml nodes are not thread safe we work out the namespaces at the time of declaration of
+//	 * a given xpath and cash the map.
+//	 * 
+//	 * The access to the underlying HashMap should be thread safe as it is only lookup no changes possible and 
+//	 * no iterator usage.
+//	 * 
+//	 * @author Chris Twiner
+//	 *
+//	 */
+//	class MapNSResolver implements PrefixResolver{
+//
+//		private Map map = new HashMap();
+//		
+//		public void addMap(Map prefixmappings){
+//			map.putAll(prefixmappings);
+//		}
+//		
+//		public void addMapping(String prefix, String namespace){
+//			map.put(prefix, namespace);
+//		}
+//		
+//		public String getBaseIdentifier() {
+//			return null;
+//		}
+//
+//		public String getNamespaceForPrefix(String prefix) {
+//			return (String) map.get(prefix);
+//		}
+//
+//		public String getNamespaceForPrefix(String prefix, Node context) {
+//			return null;
+//		}
+//
+//		public boolean handlesNullPrefixes() {
+//			return false;
+//		}
+//    	
+//    }
+//
+//    /**
+//     * 
+//     * Searches the given sub-tree and returns all of the namespace URIs that 
+//     * are used to declare the root Element and its child Elements. Unlike getAllNamespaces
+//     * it only retrieves the namespace declarations i.e. xmlns:prefix="namespace".
+//     * 
+//     * Also unlike getAllNamespaces this works up the xml nodes to the root.  Namespace
+//     * prefixes are therefore correct for that node, reassigned prefixes higher up the tree are ignored.
+//     * 
+//     * 
+//     * @param xml 
+//     *        The root of the sub-tree to perform the search on.
+//     * 
+//     * @return A Map containing all namespaces used in Element QNames. The 
+//     *         Map is keyed by the namespace prefixes. The Map does not 
+//     *         include namespaces outside of the Element QNames.
+//     *         
+//     *
+//     */
+//    public static Map getNamespaceDeclarations(Element xml)
+//    {
+//        return getNamespaceDeclarations(xml, new HashMap());
+//    }
+//    
+//    /**
+//     * 
+//     * This is an auxiliary method used to recursively search an Element 
+//     * and its parent nodes for namespace/prefix definitions. It is used to implement 
+//     * getNamespaceDeclarations(Element).
+//     *
+//     * @param xml
+//     *        The current Element in the recursive search. All namespace prefixes will be searched.
+//     *        , and then its children will be searched.
+//     * 
+//     * @param namespacesByPrefix
+//     *        The result Map so far. This is a Map[prefix, namespace].
+//     * 
+//     * @return The same Map as the second parameter (namespacesByPrefix), 
+//     *         but with more entries.
+//     * 
+//     * @see #getAllNamespaces(Element)
+//     *
+//     */
+//    private static Map getNamespaceDeclarations(Element xml, Map namespacesByPrefix)
+//    {
+//        // get the prefixes declared here and head up the tree
+//    	// code borrowed from Xalan XPathUtils, as it works.  
+//    	
+//    	// Its important to really check for the NS but this usually only works on
+//    	// a fresh parse. Developers may manually add declarations that are not in the
+//    	// correct namespace, then we guess.  As the xmlns prefix is reserved its safe just
+//    	// to use this code.
+//    	
+//        Node parent = xml;
+//
+//          int type;
+//
+//          while ((null != parent)
+//                 && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
+//                     || (type == Node.ENTITY_REFERENCE_NODE)))
+//          {
+//            if (type == Node.ELEMENT_NODE)
+//            {
+//              NamedNodeMap nnm = parent.getAttributes();
+//
+//              for (int i = 0; i < nnm.getLength(); i++)
+//              {
+//                Node attr = nnm.item(i);
+//                String aname = attr.getNodeName();
+//                boolean isPrefix = aname.startsWith("xmlns:");
+//
+//                if (isPrefix || aname.equals("xmlns"))
+//                {
+//                  int index = aname.indexOf(':');
+//                  String pre = isPrefix ? aname.substring(index + 1) : "";
+//                  String namespace = attr.getNodeValue();
+//                  
+//                  namespacesByPrefix.put(pre, namespace);
+//                }
+//              }
+//            }
+//
+//            parent = parent.getParentNode();
+//          }
+//        
+//
+//        return namespacesByPrefix;
+//    }
+//
+//    public class MessagePatternFilter implements Filter
+//    {
+//        //
+//        // Used to look up all exception messages
+//        // TODO turn back into static when working
+//        private Messages _MESSAGES = MessagesFactory.get(MessagePatternFilter.class);
+//        
+//        //
+//        // the xpath that will be evaluated against outgoing messages
+//        //
+//        private final String _pattern;
+//        
+//        /**
+//         * The map of prefixes that makes _pattern work
+//         */
+//        private final Map _namespaces;
+//        
+//        public MessagePatternFilter(String pattern, Map namespaces, String dialect) 
+//            throws InvalidMessageContentExpressionFault
+//        {
+//            //
+//            // we only support xpath
+//            //
+//            if (!dialect.equals(XPathUtils.NAMESPACE_URI))
+//            {
+//                Object[] filler = { dialect, XPathUtils.NAMESPACE_URI };
+//                throw new InvalidMessageContentExpressionFault(_MESSAGES.get("InvalidDialect", filler));
+//            }
+//            
+//            _pattern = pattern;
+//            _namespaces = namespaces;
+//        }
+//        
+//        public boolean accepts(NotificationMessage message)
+//        {
+//            //
+//            // we must append the message XML to the document so we can 
+//            // can resolve /absolute XPaths in addition to relative ones
+//            //
+//            Document doc = XmlUtils.createDocument();
+//            Element messageXML = message.toXML(doc);
+//            doc.appendChild(messageXML);
+//            
+//            try
+//            {
+//            	// TODO migrate to XPathUtils
+//            	return isMatch(messageXML, _pattern, _namespaces);
+//            }
+//            
+//            catch (TransformerException error)
+//            {
+//                return false;
+//            }
+//        }
+//        
+//        public String toString()
+//        {
+//            return XmlUtils.toString(toXML(), false);
+//        }
+//
+//        public Element toXML()
+//        {
+//            return toXML(XmlUtils.createDocument());
+//        }
+//
+//        public Element toXML(Document doc)
+//        {
+//            Element filter = XmlUtils.createElement(doc, WsnConstants.FILTER_QNAME);
+//            
+//            Element message = XmlUtils.createElement(doc, WsnConstants.MESSAGE_CONTENT_QNAME, _pattern);
+//            message.setAttribute(WsnConstants.DIALECT, XPathUtils.NAMESPACE_URI);
+//            
+//            // TODO
+//            // add back the namespaces
+//            
+//            filter.appendChild(message);
+//            
+//            return filter;
+//        }
+//    }
+//    
+//    public class MessagePatternFilterHandler implements FilterFactoryHandler
+//    {
+//        public boolean accepts(QName filterName, String filterDialect)
+//        {
+//            boolean rightName = filterName.equals(WsnConstants.MESSAGE_CONTENT_QNAME);
+//            boolean rightDialect = filterDialect.equals(XPathUtils.NAMESPACE_URI);
+//            return rightName && rightDialect;
+//        }
+//        
+//        public Filter newInstance(Element filterXML)
+//            throws BaseFault
+//        {
+//            String dialect = filterXML.getAttribute(WsnConstants.DIALECT);
+//            String pattern = XmlUtils.extractText(filterXML);
+//            return new MessagePatternFilter(pattern, 
+//            		getNamespaceDeclarations(filterXML)
+//            				, dialect);
+//        }
+//    }
+
+}