[jboss-svn-commits] JBL Code SVN: r12160 - in labs/jbossesb/workspace/dbevenius/product: core/listeners/src/org/jboss/soa/esb/actions/converters and 4 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat May 26 11:56:48 EDT 2007


Author: beve
Date: 2007-05-26 11:56:48 -0400 (Sat, 26 May 2007)
New Revision: 12160

Added:
   labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/util/
   labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/util/XPathUtil.java
   labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/util/XPathUtilTest.java
Modified:
   labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/AbstractObjectXStream.java
   labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java
   labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java
   labs/jbossesb/workspace/dbevenius/product/docs/MessageActionGuide.odt
Log:
Added the ability to specify an XPath expression identifying the root node to be used with XStream.
Updated the MessageActionGuide.odt to reflect this change


Modified: labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/AbstractObjectXStream.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/AbstractObjectXStream.java	2007-05-26 15:47:56 UTC (rev 12159)
+++ labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/AbstractObjectXStream.java	2007-05-26 15:56:48 UTC (rev 12160)
@@ -17,37 +17,38 @@
 public abstract class AbstractObjectXStream extends AbstractActionPipelineProcessor {
 	
 	
-	 protected String classAlias;
-	    protected boolean excludePackage = true;
-	    protected String classMethod;
-	    protected Class processorClass;
-		protected String classProcessor;
-		 private String name;
+	protected String classAlias;
+	protected String rootNodeName;
+	protected boolean excludePackage = true;
+	protected String classMethod;
+    protected Class processorClass;
+	protected String classProcessor;
+	private String name;
 		 
 	    
-	    /**
-	     * Public constructor.
-	     * @param properties Action Properties.
-	     * @throws ConfigurationException Action not properly configured.
-	     */
-	    public AbstractObjectXStream(ConfigTree properties) {
-	    	this( properties.getName(), properties.attributesAsList());
-	    }
+    /**
+     * Public constructor.
+     * @param properties Action Properties.
+     * @throws ConfigurationException Action not properly configured.
+     */
+    public AbstractObjectXStream(ConfigTree properties) {
+    	this( properties.getName(), properties.attributesAsList());
+    }
 	    
-	    /**
-	     * Public constructor.
-	     * @param actionName Action name.
-	     * @param properties Action Properties.
-	     * @throws ConfigurationException Action not properly configured.
-	     */
-	    public AbstractObjectXStream(String actionName, List<KeyValuePair> properties) {
-	    	name = actionName;
-	    	classAlias = KeyValuePair.getValue("class-alias", properties, null);
-	        excludePackage = KeyValuePair.getBooleanValue("exclude-package", properties, true);
-	    }
+    /**
+     * Public constructor.
+     * @param actionName Action name.
+     * @param properties Action Properties.
+     * @throws ConfigurationException Action not properly configured.
+     */
+    public AbstractObjectXStream(String actionName, List<KeyValuePair> properties) {
+    	name = actionName;
+    	classAlias = KeyValuePair.getValue("class-alias", properties, null);
+        excludePackage = KeyValuePair.getBooleanValue("exclude-package", properties, true);
+        rootNodeName = KeyValuePair.getValue("root-node", properties, null );
+    }
 	
 	
-	
 	public String getName() {
 		return name;
 	}
@@ -74,4 +75,9 @@
 		return classAlias;
 	}
 
+	public String getRootNodeName()
+	{
+		return rootNodeName;
+	}
+
 }

Modified: labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java	2007-05-26 15:47:56 UTC (rev 12159)
+++ labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java	2007-05-26 15:56:48 UTC (rev 12160)
@@ -1,10 +1,22 @@
 package org.jboss.soa.esb.actions.converters;
 
+import java.io.ByteArrayInputStream;
+import java.io.Closeable;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
 import org.apache.log4j.Logger;
 import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.actions.ActionProcessingException;
@@ -13,9 +25,15 @@
 import org.jboss.soa.esb.helpers.KeyValuePair;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.util.ClassUtil;
+import org.jboss.soa.esb.util.XPathUtil;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
 
 import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 import com.thoughtworks.xstream.io.xml.DomDriver;
+import com.thoughtworks.xstream.io.xml.DomReader;
 
 
 /**
@@ -29,11 +47,14 @@
  *     &lt;property name="class-alias" value="Customer" /&gt; &lt;!-- Optional. Class alias used in call to <a href="http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/XStream.html">XStream.alias(String, Class)</a> prior to deserialisation. --&gt;
  *     &lt;property name="incoming-type" value="CustomerProcessor" /&gt; &lt;!-- Required. Class for incoming type used to process the message after  deserialisation. --&gt;
  *     &lt;property name="exclude-package" value="false" /&gt; &lt;!-- Optional. Default "true".  Not applicable if a "class-alias" is specified. --&gt;
- *     &lt;aliases&gt; &lt;!-- Optional list of extra aliases to add to XStream  --&gt;
+ *     &lt;property name="root-node" value="/root/Customer" /&gt; 
+ *     &lt;!-- Optional. Specify an XPath expression be used to determine the root node used with XStream. 
+ *     Useful when the object to convert is not the root node of the document --&gt;
+ *     &lt;property name="aliases"&gt; &lt;!-- Optional list of extra aliases to add to XStream  --&gt;
  * 		&lt;alias name="aliasName" class="className" /&gt; 
  * 		&lt;alias name="aliasName" class="className" /&gt; 
  * 		...
- *     &lt;/aliases&gt;
+ *     &lt;/property&gt;
  * &lt;/Action&gt;
  * </pre>
  * <p/>
@@ -89,23 +110,15 @@
 	public Message process(Message message) throws ActionProcessingException {
 		Object object = ActionUtils.getTaskObject(message);
 		
-		String xml = object.toString();
-
-		XStream xstream = new XStream(new DomDriver());
-		
-        xstream.alias(getAlias(incomingType), incomingType);
-        addAliases( aliases, xstream );
-        
-        Object arg;
 		try {
-			arg = incomingType.newInstance();
-			arg = xstream.fromXML(xml, arg);
-   		 	ActionUtils.setTaskObject(message,arg);
+			Object toObject = incomingType.newInstance();
+			fromXmlToObject( object.toString(), toObject );
+   		 	ActionUtils.setTaskObject(message,toObject);
 		} catch (InstantiationException e) {
-			e.printStackTrace();
+			logger.error( e );
 			throw new ActionProcessingException("Could not invoke for Arg: " + getName(),e );
 		} catch (IllegalAccessException e) {
-			e.printStackTrace();
+			logger.error( e );
 			throw new ActionProcessingException("Could not access for Arg: " + getName(),e );
 		} 
 		
@@ -159,4 +172,68 @@
 		}
 	}
 	
+	/**
+	 * 
+	 * @param xml		the xml String
+	 * @param root		an instance of the type of the root element
+	 * @throws ActionProcessingException
+	 * @throws ParserConfigurationException 
+	 * @throws IOException 
+	 * @throws SAXException 
+	 */
+	protected void fromXmlToObject(String xml, Object root ) throws ActionProcessingException
+	{
+		HierarchicalStreamReader reader = null;
+		try
+		{
+			XStream xstream = new XStream( new DomDriver() );
+			
+			reader = new DomReader( getRootElement( xml, rootNodeName ) );
+				
+	        xstream.alias(getAlias(incomingType), incomingType);
+	        addAliases( aliases, xstream );
+		        
+			xstream.unmarshal( reader, root );
+			
+		}
+		finally 
+		{
+			if ( reader != null)  reader.close();
+		}
+	}
+
+	/*
+	 * Simply delegates to XPathUtil and catches exceptions specific
+	 * to that class and rethrows an ActionProcessingException
+	 */
+	private Element getRootElement( String xml, String xPathExpression ) throws ActionProcessingException
+	{
+		logger.debug( "rootNodeName : " + xPathExpression );
+		
+		try
+		{
+			return XPathUtil.getNodeFromXPathExpression( xml, xPathExpression );
+		} 
+		catch (ParserConfigurationException e)
+		{
+			logger.error( e );
+			throw new ActionProcessingException( e );
+		} 
+		catch (SAXException e)
+		{
+			logger.error( e );
+			throw new ActionProcessingException( e );
+		} 
+		catch (IOException e)
+		{
+			logger.error( e );
+			throw new ActionProcessingException( e );
+		}
+		catch (XPathExpressionException e)
+		{
+			logger.error( e );
+			throw new ActionProcessingException( e );
+		}
+	}
+	
 }

Added: labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/util/XPathUtil.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/util/XPathUtil.java	                        (rev 0)
+++ labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/util/XPathUtil.java	2007-05-26 15:56:48 UTC (rev 12160)
@@ -0,0 +1,156 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2006, JBoss Inc., and
+ * individual contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of individual
+ * contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.Closeable;
+import java.io.IOException;
+
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+/**
+ * Util methods for XPath related operations.
+ * 
+ * @author <a href="daniel.bevenius at redpill.se">Daniel Bevenius</a>				
+ * @since 4.2
+ *
+ */
+public class XPathUtil
+{
+	private static Logger log = Logger.getLogger( XPathUtil.class );
+	
+	/**
+	 * Parses the passed in xml and creates a Dom Document
+	 * 
+	 * @param xml
+	 * @return Document
+	 * @throws SAXException
+	 * @throws IOException
+	 * @throws ParserConfigurationException
+	 */
+	public static org.w3c.dom.Document getDocument( String xml ) throws SAXException, IOException, ParserConfigurationException
+	{
+		if (xml == null) 
+			throw new IllegalArgumentException( "xml must not be null" );
+		
+		ByteArrayInputStream inputStream = null;
+		try
+		{
+			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
+			inputStream = new ByteArrayInputStream( xml.getBytes() );
+			return documentBuilder.parse( inputStream );
+		} 
+		finally
+		{
+			close( inputStream );
+		}
+	}
+	
+	/**
+	 * Will compile the passed in XPath expression and evalutate it
+	 * against the passed in doument.
+	 * 
+	 * @param document			Document that the XPath expression should evaluate upon.
+	 * @param xPathExpression	XPath expression
+	 * @return Element			The Element found
+	 * @throws XPathExpressionException
+	 */
+	public static Element getNodeFromXPathExpression( Document document, String xPathExpression ) throws XPathExpressionException
+	{
+		if (document == null)
+			throw new IllegalArgumentException( "document must not be null" );
+		
+		if ( xPathExpression == null )
+			return document.getDocumentElement();
+		
+		Node rootNode = (Node) getQNameFromXPathExpression( document, xPathExpression, XPathConstants.NODE );
+		
+		return (Element) rootNode;
+	}
+	
+	/**
+	 * @param xml				XML string that will be the target of the XPath evaluation
+	 * @param document			Document that the XPath expression should evaluate upon.
+	 * @param xPathExpression	XPath expression
+	 * @return Element			The Element found
+	 * 
+	 * @throws XPathExpressionException
+	 * @throws SAXException
+	 * @throws IOException
+	 * @throws ParserConfigurationException
+	 */
+	public static Element getNodeFromXPathExpression( String xml, String xPathExpression ) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException
+	{
+		return getNodeFromXPathExpression( getDocument( xml ), xPathExpression );
+	}
+	
+	/**
+	 * All public methods should be able to delegate the actual XPath evaluation
+	 * to this method.
+	 * 
+	 * @param document			Document that the XPath expression should evaluate upon.
+	 * 							Must not be null. This caller is responsible for checking for null.
+	 * @param xPathExpression	XPath expression
+	 * 							Must not be null. This caller is responsible for checking for null.
+	 * @param type				The XPathConstant type to use. The affects the type returned from the evaluate method.
+	 * 
+	 * @return Object			of type specified in the passed in <code>type</code> argument
+	 * 
+	 * @throws XPathExpressionException
+	 */
+	private static Object getQNameFromXPathExpression( Document document, String xPathExpression, QName type ) throws XPathExpressionException
+	{
+		XPath xpath = XPathFactory.newInstance().newXPath();
+		XPathExpression expression = xpath.compile( xPathExpression );
+		
+		return expression.evaluate( document.getDocumentElement(), type );
+	}
+
+	private static void close (Closeable c )
+	{
+		try
+		{
+			if ( c != null )
+				c.close();
+		} 
+		catch (IOException e)
+		{
+			log.error(  "Error when closing IOStream", e );
+		}
+	}
+
+}

Modified: labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java	2007-05-26 15:47:56 UTC (rev 12159)
+++ labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java	2007-05-26 15:56:48 UTC (rev 12160)
@@ -22,11 +22,15 @@
 
 package org.jboss.soa.esb.actions.converters;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
-import junit.framework.TestCase;
+import junit.framework.JUnit4TestAdapter;
 
 import org.jboss.soa.esb.actions.ActionProcessingException;
 import org.jboss.soa.esb.actions.ActionUtils;
@@ -35,6 +39,7 @@
 import org.jboss.soa.esb.helpers.KeyValuePair;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.Test;
 
 import com.thoughtworks.xstream.XStream;
 
@@ -44,19 +49,17 @@
  * @author Daniel Bevenius
  * @since Version 4.0
  */
-public class XStreamToObjectUnitTest extends TestCase {
-
+public class XStreamToObjectUnitTest {
+	
+	@Test
     public void test_default() throws ActionProcessingException {
         List<KeyValuePair> properties = new ArrayList<KeyValuePair>();
         properties.add(new KeyValuePair("incoming-type",TestBean.class.getName() ));
         
-        
         XStreamToObject xstreamToObject = new XStreamToObject("TestBean-Serialiser", properties);
 
-        
         Message oMsg = MessageFactory.getInstance().getMessage();
 
-        
         String msg = "<" + TestBean.class.getSimpleName() + "> <name>Tom</name><phone>1234</phone></" + TestBean.class.getSimpleName() + ">";
         
         ActionUtils.setTaskObject(oMsg,msg);
@@ -69,6 +72,7 @@
          
     }
 
+	@Test
     public void test_with_package() throws ActionProcessingException {
         List<KeyValuePair> properties = new ArrayList<KeyValuePair>();
         properties.add(new KeyValuePair("exclude-package", "false"));
@@ -76,7 +80,6 @@
         
         XStreamToObject xstreamToObject = new XStreamToObject("TestBean-Serialiser", properties);
 
-        
         Message oMsg = MessageFactory.getInstance().getMessage();
         
         String msg = "<" + TestBean.class.getName() + "> <name>Tom</name><phone>1234</phone></"+ TestBean.class.getName() +">";
@@ -91,6 +94,7 @@
         
     }
 
+	@Test
     public void test_with_alias() throws ActionProcessingException {
         List<KeyValuePair> properties = new ArrayList<KeyValuePair>();
         
@@ -100,7 +104,6 @@
         
         XStreamToObject xstreamToObject = new XStreamToObject("TestBean-Serialiser", properties);
 
-        
         Message oMsg = MessageFactory.getInstance().getMessage();
         
         String msg = "<TomsClass><name>Tom</name><phone>1234</phone></TomsClass>";
@@ -114,33 +117,34 @@
         assertEquals("1234", bean.getPhone());
     }
     
-    public void test_getAliases()
+	@Test
+    public void getAliases()
     {
-    	ConfigTree configTree = getConfigTreeWithAliases();
+    	ConfigTree configTree = getConfigTreeWithAliases("TomsClass", false, TestBean.class, null );
         XStreamToObject xstreamToObject = new XStreamToObject( configTree );
         
         Map<String, String> aliases = xstreamToObject.getAliases( configTree );
         
         assertAliases( aliases );
-    	
     }
     
-    public void test_addAliases() throws ActionProcessingException
+	@Test
+    public void addAliases() throws ActionProcessingException
     {
-    	ConfigTree configTree = getConfigTreeWithAliases();
+    	ConfigTree configTree = getConfigTreeWithAliases("TomsClass", false, TestBean.class, null );
         XStreamToObject xstreamToObject = new XStreamToObject( configTree );
         
         Map<String, String> aliases = xstreamToObject.getAliases( configTree );
         xstreamToObject.addAliases( aliases, new XStream() );
         
         assertAliases( aliases );
-    	
     }
     
-    public void test_addAliasesNegative() throws ActionProcessingException
+	@Test
+    public void addAliasesNegative() throws ActionProcessingException
     {
-    	ConfigTree configTree = getConfigTree();
-        XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+    	ConfigTree configTree = getConfigTree( "TomsClass", false, TestBean.class, null );
+        XStreamToObject xstreamToObject = createXStreamObject( "TomsClass", false, TestBean.class, null );
         
         Map<String, String> aliases = xstreamToObject.getAliases( configTree );
         xstreamToObject.addAliases( aliases, new XStream() );
@@ -149,11 +153,10 @@
     	
     }
     
+	@Test
     public void test_with_multiple_aliases() throws ActionProcessingException {
     	
-    	ConfigTree configTree = getConfigTreeWithAliases();
-    	
-        XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+        XStreamToObject xstreamToObject = createXStreamObject( "TomsClass", false, TestBean.class, null );
 
         Message oMsg = MessageFactory.getInstance().getMessage();
         
@@ -168,6 +171,64 @@
         assertEquals("1234", bean.getPhone());
     }
     
+	@Test
+    public void test_setRootNodeNegative() throws ActionProcessingException
+    {
+        XStreamToObject xstreamToObject = createXStreamObject( "TomsClass", false, TestBean.class, null );
+        assertEquals ( null , xstreamToObject.getRootNodeName() );
+    }
+    
+	@Test
+    public void fromXmlToObject_with_rootNode() throws ActionProcessingException
+    {
+    	String rootNodeName = "/someNode/TomsClass";
+        XStreamToObject xstreamToObject = createXStreamObject( "TomsClass", false, TestBean.class, rootNodeName );
+        
+        String xml = "<someNode><TomsClass><name>Tom</name><phone>1234</phone></TomsClass></someNode>";
+        Object obj = new TestBean();
+        
+        xstreamToObject.fromXmlToObject( xml, obj );
+        
+        assertTrue( obj instanceof TestBean );
+        assertEquals("1234", ((TestBean)obj).getPhone());
+    }
+    
+	@Test
+    public void fromXmlToObject_with_rootNode_as_second_child_node() throws ActionProcessingException
+    {
+    	String rootNodeName = "/someNode/TomsClass2";
+        XStreamToObject xstreamToObject = createXStreamObject( "TomsClass2", false,TestBean2.class, rootNodeName );
+        
+        String xml = "<someNode><TomsClass><name>Tom</name><phone>1234</phone></TomsClass><TomsClass2><name>Daniel</name></TomsClass2></someNode>";
+        Object obj = new TestBean2();
+        
+        xstreamToObject.fromXmlToObject( xml, obj );
+        
+        assertTrue( obj instanceof TestBean2 );
+        assertEquals("Daniel", ((TestBean2)obj).getName());
+    }
+	
+	@Test
+    public void fromXmlToObject_with_rootNode_as_second_child_node_and_sibling() throws ActionProcessingException
+    {
+    	String rootNodeName = "/someNode/TomsClass2[2]";
+        XStreamToObject xstreamToObject = createXStreamObject( "TomsClass2", false,TestBean2.class, rootNodeName );
+        
+        String xml = "<someNode><TomsClass><name>Tom</name><phone>1234</phone></TomsClass><TomsClass2><name>Daniel</name></TomsClass2><TomsClass2><name>Bevenius</name></TomsClass2></someNode>";
+        Object obj = new TestBean2();
+        
+        xstreamToObject.fromXmlToObject( xml, obj );
+        
+        assertTrue( obj instanceof TestBean2 );
+        assertEquals("Bevenius", ((TestBean2)obj).getName());
+    }
+    
+    private XStreamToObject createXStreamObject( String classAlias, boolean exludePackage, Class incomingType, String rootNodeName)
+    {
+    	ConfigTree configTree = getConfigTree( classAlias, exludePackage, incomingType, rootNodeName);
+        return new XStreamToObject( configTree );
+    }
+    
     private void assertAliases( Map<String, String> aliases )
     {
     	assertNotNull( "Aliases map should not have been null!",  aliases );
@@ -175,24 +236,20 @@
         assertEquals( TestBean.class.getName(), aliases.get( "aliasName2" ) );
     }
     
-    private ConfigTree getConfigTree()
+    private ConfigTree getConfigTree(String classAlias, boolean excludePackage, Class incomingType, String rootNodeName)
 	{
-		ConfigTree configTree = new ConfigTree ( "test");
-    	configTree.setAttribute( "class-alias", "TomsClass");
-    	configTree.setAttribute( "exclude-package", "false");
-    	configTree.setAttribute( "incoming-type", TestBean.class.getName());
+		ConfigTree configTree = new ConfigTree ("test");
+    	configTree.setAttribute( "class-alias", classAlias);
+    	configTree.setAttribute( "exclude-package", String.valueOf( excludePackage ));
+    	configTree.setAttribute( "incoming-type", incomingType.getName());
+    	configTree.setAttribute( "root-node", rootNodeName );
 		return configTree;
 	}
 
-	private ConfigTree getConfigTreeWithAliases()
+    private ConfigTree getConfigTreeWithAliases(String classAlias, boolean excludePackage, Class incomingType, String rootNodeName)
 	{
-		ConfigTree configTree = new ConfigTree ( "test");
-    	configTree.setAttribute( "class-alias", "TomsClass");
-    	configTree.setAttribute( "exclude-package", "false");
-    	configTree.setAttribute( "incoming-type", TestBean.class.getName());
+		ConfigTree configTree = getConfigTree( classAlias,false, incomingType, rootNodeName );
 		
-//    	ConfigTree aliases = new ConfigTree( "aliases", configTree);
-//    	ConfigTree alias1 = new ConfigTree( "alias", aliases);
     	ConfigTree alias1 = new ConfigTree( "alias", configTree);
     	alias1.setAttribute( "name", "aliasName1");
     	alias1.setAttribute( "class", TestBean.class.getName());
@@ -203,7 +260,32 @@
     	
 		return configTree;
 	}
+
+    /**
+     * Just a bean for testing
+     */
+	private static class TestBean2 {
+		private String name;
+
+		public String getName()
+		{
+			return name;
+		}
+
+		public void setName( String name )
+		{
+			this.name = name;
+		}
+		
+	}
 	
+	/**
+	 * Just here to get Ant to find annotated test.
+	 */
+	public static junit.framework.Test suite()
+	{
+		return new JUnit4TestAdapter( XStreamToObjectUnitTest.class);
+	}
 	
 }	
 	
\ No newline at end of file

Added: labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/util/XPathUtilTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/util/XPathUtilTest.java	                        (rev 0)
+++ labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/util/XPathUtilTest.java	2007-05-26 15:56:48 UTC (rev 12160)
@@ -0,0 +1,93 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2006, JBoss Inc., and
+ * individual contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of individual
+ * contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.util;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPathExpressionException;
+
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+/**
+ * Tests the XPathUtil class
+ * 
+ * @author <a href="daniel.bevenius at redpill.se">Daniel Bevenius</a>				
+ * @since 4.2
+ *
+ */
+public class XPathUtilTest
+{
+	private String xml = "<someNode><childnode><name>Daniel</name></childnode></someNode>";
+	
+	@Test( expected= IllegalArgumentException.class )
+	public void getDocumentNegative() throws SAXException, IOException, ParserConfigurationException
+	{
+		XPathUtil.getDocument( null );
+	}
+	
+	@Test
+	public void getDocument() throws SAXException, IOException, ParserConfigurationException
+	{
+		Document document = XPathUtil.getDocument( xml );
+		assertNotNull( document );
+	}
+	
+	@Test( expected= IllegalArgumentException.class )
+	public void getNodeFromXPathExpressionNegative_null_document( ) throws XPathExpressionException
+	{
+		Document document = null;
+		XPathUtil.getNodeFromXPathExpression( document, "/some/path[0]" );
+	}
+	
+	@Test
+	public void getNodeFromXPathExpression( ) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException
+	{
+		Element node = XPathUtil.getNodeFromXPathExpression( XPathUtil.getDocument( xml ), "/someNode/childnode" );
+		assertEquals( "childnode", node.getNodeName() );
+	}
+	
+	@Test
+	public void getNodeFromXPathExpression_sibling( ) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException
+	{
+		String xml = "<someNode><childnode><name>Daniel</name></childnode><childnode><name>Bevenius</name></childnode></someNode>";
+		Element node = XPathUtil.getNodeFromXPathExpression( XPathUtil.getDocument( xml ), "/someNode/childnode[2]" );
+		assertEquals( "childnode", node.getNodeName() );
+		Node nameNode = node.getElementsByTagName( "name" ).item( 0 );
+		assertNotNull( nameNode );
+		assertEquals( "Bevenius", nameNode.getTextContent() );
+	}
+	
+	@Test
+	public void getNodeFromXPathExpression_StringXml_and_XPathExpression() throws XPathExpressionException, SAXException, IOException, ParserConfigurationException
+	{
+		Element node = XPathUtil.getNodeFromXPathExpression( xml, "/someNode/childnode" );
+		assertEquals( "childnode", node.getNodeName() );
+	}
+
+}

Modified: labs/jbossesb/workspace/dbevenius/product/docs/MessageActionGuide.odt
===================================================================
(Binary files differ)




More information about the jboss-svn-commits mailing list