[jboss-svn-commits] JBL Code SVN: r25312 - in labs/jbossesb/branches/JBESB_4_4_GA_FP: product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream and 4 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Feb 17 17:50:56 EST 2009
Author: beve
Date: 2009-02-17 17:50:56 -0500 (Tue, 17 Feb 2009)
New Revision: 25312
Added:
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/Comment.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/CommentConverter.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamConfigBuilder.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/testlib/xmlunit-1.2.jar
Removed:
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/
Modified:
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/ObjectToXStream.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/AttributeAliasConf.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/ClassAliasConf.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/ConverterConf.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/FieldAliasConf.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/ImplicitCollectionConf.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/XStreamConfigurator.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/TestBean.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/ObjectToXStreamUnitTest.java
labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java
Log:
Work for https://jira.jboss.org/jira/browse/JBESB-2419 "XStreamToObject. Add support for implicit collections and field alias"
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/ObjectToXStream.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/ObjectToXStream.java 2009-02-17 17:59:12 UTC (rev 25311)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/ObjectToXStream.java 2009-02-17 22:50:56 UTC (rev 25312)
@@ -23,16 +23,22 @@
package org.jboss.soa.esb.actions.converters;
import java.util.HashMap;
-import javax.xml.namespace.QName;
+import java.util.List;
import java.util.Map;
-import org.apache.log4j.Logger;
+import javax.xml.namespace.QName;
+
import org.jboss.soa.esb.ConfigurationException;
-import org.jboss.soa.esb.listeners.message.MessageDeliverException;
import org.jboss.soa.esb.actions.AbstractActionPipelineProcessor;
import org.jboss.soa.esb.actions.ActionProcessingException;
import org.jboss.soa.esb.actions.ActionUtils;
+import org.jboss.soa.esb.actions.converters.xstream.conf.ClassAliasConf;
+import org.jboss.soa.esb.actions.converters.xstream.conf.ConverterConf;
+import org.jboss.soa.esb.actions.converters.xstream.conf.FieldAliasConf;
+import org.jboss.soa.esb.actions.converters.xstream.conf.ImplicitCollectionConf;
+import org.jboss.soa.esb.actions.converters.xstream.conf.XStreamConfigurator;
import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.message.MessageDeliverException;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.MessagePayloadProxy;
import org.jboss.soa.esb.message.body.content.BytesBody;
@@ -47,24 +53,56 @@
* Uses the <a href="http://xstream.codehaus.org/">XStream</a> processor to generate an XML message String from the supplied object.
* <p/>
* Sample Action Configuration:
- * <pre>
- * <Action name="Customer-To-XML" processor="ObjectToXStream">
- * <property name="class-alias" value="Customer" /> <!-- 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 serialisation. -->
- * <property name="exclude-package" value="false" /> <!-- Default "true". Not applicable if a "class-alias" is specified. -->
- * <property name="namespace-uri" value="namespace goes here" />
- * <property name="aliases"> <!-- Optional list of extra aliases to add to XStream -->
- * <alias name="aliasName" class="className" />
- * <alias name="aliasName" class="className" />
+ * <pre>{@code
+ * <action name="Customer-To-XML" processor="ObjectToXStream">
+ * <property name="class-alias" value="Customer" />
+ * <property name="exclude-package" value="false" />
+ * <property name="namespace-uri" value="namespace goes here" />
+ * <property name="aliases">
+ * <alias name="aliasName" class="className" />
+ * <alias name="aliasName" class="className" />
* ...
- * </property>
- * <property name="namespaces"> <!-- Optional list of namespaces to register with XStream -->
- * <namespace namespace-uri="http://www.xyz.com" local-part="xyz" />
- * <namespace namespace-uri="http://www.xyz.com/x" local-part="x" />
- * </property>
+ * </property>
+ * <property name="namespaces">
+ * <namespace namespace-uri="http://www.xyz.com" local-part="xyz" />
+ * <namespace namespace-uri="http://www.xyz.com/x" local-part="x" />
+ * </property>
+ * <property name="fieldAliases">
+ * <field-alias alias="aliasName" class="className" fieldName="fieldName"/>
+ * <field-alias alias="aliasName" definedIn="className" fieldName="fieldName"/>
+ * ...
+ * </property>
+ * <property name="attributeAliases">
+ * <attribute-alias name="aliasName" class="className"/>
+ * <attribute-alias name="aliasName" class="className"/>
* ...
- * </Action>
- * </pre>
+ * </property>
+ * <property name="implicit-collections">
+ * <implicit-collection class="className" fieldName="fieldName" fieldType="java.lang.String" />
+ * <implicit-collection class="className" fieldName="fieldName" fieldType="java.lang.Integer"/>
+ * ...
+ * </property>
+ * <property name="converters">
+ * <converter class="className" />
+ * <converter class="className" />
+ * ...
+ * </property>
+ * </action>
+ * }</pre>
* <p/>
+ * * <p/>
+ * <lu>
+ * <li><i>class-alias</i> Optional. Class alias for the 'incoming-type'.</li>
+ * <li><i>exclude-package</i> Optional, defaults to true. Determines whether package name should be removed from the incoming type.</li>
+ * <li><i>aliases</i> Optional. Specifies extra class aliases.</li>
+ * <li><i>namespace-uri</i> Optional. List of namespaces that will be registerd with XStream.</li>
+ * <li><i>fieldAliases</i> Optional. Specifies field aliases.</li>
+ * <li><i>implicit-collections</i> Optional. Specifies implicit collections which are when you have an xml element that is a place holder for a collection of other elements.
+ * In this case you are telling XStream to not include the holder element but instead place its element into the the 'fieldName' in the target class.
+ * 'className' is the collection type.
+ * 'fieldType' is the type the elements in the collection.</li>
+ * <li><i>converters</i> Optional. Specifies converters that will be registered with XStream.</li>
+ * </lu>
* The XML root element is either set from the "class-alias" property or the classes full name. In the later case, the class package is
* excluded unless "exclude-package" is set to "false"/"no".
*
@@ -73,7 +111,6 @@
* @since Version 4.0
*/
public class ObjectToXStream extends AbstractActionPipelineProcessor {
- private Logger log = Logger.getLogger( ObjectToXStream.class );
public static final String ATTR_CLASS_ALIAS = "class-alias";
public static final String ATTR_EXCLUDE_PACKAGE = "exclude-package";
@@ -86,7 +123,12 @@
private MessagePayloadProxy payloadProxy;
private Map<String,String> namespaces;
private String mode;
+ private List<ClassAliasConf> classAliases;
+ private List<FieldAliasConf> fieldAliases;
+ private List<ImplicitCollectionConf> implicitCollections;
+ private List<ConverterConf> converters;
+
/**
* Public constructor.
* @param configTree Action Properties.
@@ -100,6 +142,11 @@
excludePackage = configTree.getAttribute(ATTR_EXCLUDE_PACKAGE, "true").equals("true");
namespaces = getNamespaces(configTree);
mode = configTree.getAttribute(ATTR_XSTREAM_MODE, "XPATH_RELATIVE_REFERENCES");
+ XStreamConfigurator xstreamConfig = new XStreamConfigurator(configTree);
+ classAliases = xstreamConfig.getClassAliases();
+ fieldAliases = xstreamConfig.getFieldAliases();
+ implicitCollections = xstreamConfig.getImplicitCollections();
+ converters = xstreamConfig.getConverters();
}
/* (non-Javadoc)
@@ -125,9 +172,14 @@
} else {
xstream.alias(classAlias, object.getClass());
}
+ XStreamConfigurator.addClassAliases(classAliases, xstream);
+ XStreamConfigurator.addFieldAliases(fieldAliases, xstream);
+ XStreamConfigurator.addImplicitCollections(implicitCollections, xstream);
+ XStreamConfigurator.addConverters(converters, xstream);
try {
- payloadProxy.setPayload(message, xstream.toXML(object));
+ String xml = xstream.toXML(object);
+ payloadProxy.setPayload(message, xml);
} catch (MessageDeliverException e) {
throw new ActionProcessingException(e);
}
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java 2009-02-17 17:59:12 UTC (rev 25311)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java 2009-02-17 22:50:56 UTC (rev 25312)
@@ -36,6 +36,9 @@
import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.actions.ActionProcessingException;
import org.jboss.soa.esb.actions.ActionUtils;
+import org.jboss.soa.esb.actions.converters.xstream.conf.FieldAliasConf;
+import org.jboss.soa.esb.actions.converters.xstream.conf.ImplicitCollectionConf;
+import org.jboss.soa.esb.actions.converters.xstream.conf.XStreamConfigurator;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.helpers.KeyValuePair;
import org.jboss.soa.esb.listeners.message.MessageDeliverException;
@@ -55,51 +58,76 @@
/**
- * Object to processor.
+ * XML to Object processor that uses Uses the <a href="http://xstream.codehaus.org/">XStream</a> .
* <p/>
- * Uses the <a href="http://xstream.codehaus.org/">XStream</a> processor to generate an XML message String from the supplied object.
- * <p/>
* Sample Action Configuration:
- * <pre>
- * <Action name="doCustomer" processor="XStreamObject">
- * <property name="class-alias" value="Customer" /> <!-- 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. -->
- * <property name="incoming-type" value="CustomerProcessor" /> <!-- Required. Class for incoming type used to process the message after deserialisation. -->
- * <property name="exclude-package" value="false" /> <!-- Optional. Default "true". Not applicable if a "class-alias" is specified. -->
- * <property name="root-node" value="/root/Customer" />
- * <!-- 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 -->
- * <property name="aliases"> <!-- Optional list of extra aliases to add to XStream -->
- * <alias name="aliasName" class="className" />
- * <alias name="aliasName" class="className" />
+ * <pre>{@code
+ * <action name="doCustomer" class="XStreamObject">
+ * <property name="class-alias" value="Customer"/>
+ * <property name="incoming-type" value="CustomerProcessor"/>
+ * <property name="exclude-package" value="false"/>
+ * <property name="root-node" value="/root/Customer"/>
+ * <property name="aliases">
+ * <alias name="aliasName" class="className" />
+ * <alias name="aliasName" class="className" />
* ...
- * </property>
- * <property name="converters"> <!-- Optional list of converter to register with XStream -->
- * <converter class="className" />
- * <converter class="className" />
+ * </property>
+ * <property name="fieldAliases">
+ * <field-alias alias="aliasName" class="className" fieldName="fieldName"/>
+ * <field-alias alias="aliasName" class="className" fieldName="fieldName"/>
* ...
- * </Action>
- * </pre>
+ * </property>
+ * <property name="attributeAliases">
+ * <attribute-alias name="aliasName" class="className"/>
+ * <attribute-alias name="aliasName" class="className"/>
+ * ...
+ * </property>
+ * <property name="implicit-collections">
+ * <implicit-collection class="className" fieldName="fieldName" fieldType="java.lang.String" />
+ * <implicit-collection class="className" fieldName="fieldName" fieldType="java.lang.Integer"/>
+ * ...
+ * </property>
+ * <property name="converters">
+ * <converter class="className" />
+ * <converter class="className" />
+ * ...
+ * </action>
+ * }</pre>
* <p/>
+ * <lu>
+ * <li><i>class-alias</i> Optional. Class alias for the 'incoming-type'.</li>
+ * <li><i>incoming-type</i> Required. Class of the incoming type.</li>
+ * <li><i>exclude-package-type</i> Optional, defaults to true. Determines whether package name should be removed from the incoming type.</li>
+ * <li><i>root-node</i> Optional. Specifies an XPath expression to be used to determine the root node that XStream will use.</li>
+ * <li><i>aliases</i> Optional. Specifies extra class aliases.</li>
+ * <li><i>fieldAliases</i> Optional. Specifies field aliases.</li>
+ * <li><i>implicit-collections</i> Optional. Specifies implicit collections which are when you have an xml element that is a place holder for a collection of other elements.
+ * In this case you are telling XStream to not include the holder element but instead place its element into the the 'fieldName' in the target class.
+ * 'className' is the collection type.
+ * 'fieldType' is the type the elements in the collection.</li>
+ * <li><i>converters</i> Optional. Specifies converters that will be registered with XStream.</li>
+ * </lu>
+ *
* The XML root element is either set from the "class-alias" property or the classes full name. In the later case, the class package is
* excluded unless "exclude-package" is set to "false"/"no".
*
- * This can be used with ObjectToXStream
- *
* @author danielmarchant
* @author Daniel Bevenius
* @since Version 4.0
*/
-public class XStreamToObject extends AbstractObjectXStream {
+public class XStreamToObject extends AbstractObjectXStream {
private static Logger logger = Logger.getLogger(XStreamToObject.class);
// class related variables
- private Class incomingType;
+ private Class<?> incomingType;
// action related variables
- private Map<String,String> aliases;
- private Map<String,String> attributeAliases;
+ private Map<String,String> aliases;
+ private Map<String,String> attributeAliases;
+ private List<FieldAliasConf> fieldAliases;
private List<String> converters;
+ private List<ImplicitCollectionConf> implicitCollections;
private MessagePayloadProxy payloadProxy;
@@ -110,9 +138,12 @@
*/
public XStreamToObject(ConfigTree properties) {
this(properties.getName(), properties.attributesAsList());
+ XStreamConfigurator xstreamConfig = new XStreamConfigurator(properties);
aliases = getAliases( properties, "alias" );
+ fieldAliases = xstreamConfig.getFieldAliases();
attributeAliases = getAliases( properties, "attribute-alias" );
converters = getConverters( properties, "converter" );
+ implicitCollections = xstreamConfig.getImplicitCollections();
payloadProxy = new MessagePayloadProxy(properties,
new String[] {BytesBody.BYTES_LOCATION, ActionUtils.POST_ACTION_DATA},
new String[] {ActionUtils.POST_ACTION_DATA});
@@ -189,6 +220,31 @@
}
/**
+ * Will extract the alias elements from the passed-in conifgTree
+ *
+ * @param configTree the configuration for this class
+ *
+ * @return Map<String,String> either an empty map or a map containing the alias name
+ * as its key and the corresponding value is the class to map
+ * it to.
+ */
+ protected List<FieldAliasConf> getFieldAliases( final ConfigTree configTree, final String childName)
+ {
+ List<FieldAliasConf> aliases = new ArrayList<FieldAliasConf>();
+
+ ConfigTree[] children = configTree.getChildren( childName );
+
+ if ( children != null )
+ {
+ for ( ConfigTree alias : children )
+ {
+ aliases.add(new FieldAliasConf(alias.getAttribute("alias"), alias.getAttribute("class"), alias.getAttribute("fieldName")));
+ }
+ }
+ return aliases;
+ }
+
+ /**
* Will extract the converter elements from the passed-in conifgTree
*
* @param configTree the configuration for this class
@@ -209,29 +265,29 @@
}
/**
- * Added the aliases contained in the passed-in map to the
- * passed-in XStream object
- *
- * @param aliases Map of aliases.
- * @throws ActionProcessingException
- */
- protected void addAliases( Map<String, String> aliases, XStream xstream) throws ActionProcessingException
- {
- if ( aliases == null )
- return;
-
- Set<Map.Entry<String,String>> set = aliases.entrySet();
- for (Map.Entry me : set ) {
- String className = (String) me.getValue();
- try {
- Class clazz = ClassUtil.forName( className, getClass() );
- xstream.alias((String)me.getKey(), clazz );
- } catch (ClassNotFoundException e) {
- logger.error("ClassNotFoundException: ", e);
- throw new ActionProcessingException("Could not add alias : " + (String)me.getKey() + ", class : " + className ,e );
- }
- }
- }
+ * Added the aliases contained in the passed-in map to the
+ * passed-in XStream object
+ *
+ * @param aliases Map of aliases.
+ * @throws ActionProcessingException
+ */
+ protected void addAliases( Map<String, String> aliases, XStream xstream) throws ActionProcessingException
+ {
+ if ( aliases == null )
+ return;
+
+ Set<Map.Entry<String,String>> set = aliases.entrySet();
+ for (Map.Entry me : set ) {
+ String className = (String) me.getValue();
+ try {
+ Class<?> clazz = ClassUtil.forName( className, getClass() );
+ xstream.alias((String)me.getKey(), clazz );
+ } catch (ClassNotFoundException e) {
+ logger.error("ClassNotFoundException: ", e);
+ throw new ActionProcessingException("Could not add alias : " + (String)me.getKey() + ", class : " + className ,e );
+ }
+ }
+ }
/**
* Registers the converters contained in the passed in list
@@ -250,7 +306,7 @@
if ( converterClass == null )
continue;
try {
- Class clazz = ClassUtil.forName( converterClass, getClass() );
+ Class<?> clazz = ClassUtil.forName( converterClass, getClass() );
xstream.registerConverter((Converter)clazz.newInstance());
} catch (ClassNotFoundException e) {
@@ -283,7 +339,7 @@
for (Map.Entry me : set ) {
String className = (String) me.getValue();
try {
- Class clazz = ClassUtil.forName( className, getClass() );
+ Class<?> clazz = ClassUtil.forName( className, getClass() );
xstream.useAttributeFor( (String)me.getKey(), clazz );
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException: ", e);
@@ -311,8 +367,10 @@
XStream xstream = new XStream( new DomDriver() );
xstream.alias(getAlias(incomingType), incomingType);
addAliases( aliases, xstream );
+ XStreamConfigurator.addFieldAliases(fieldAliases, xstream);
addAttributeAliases( attributeAliases, xstream );
addConverters( converters, xstream );
+ XStreamConfigurator.addImplicitCollections(implicitCollections, xstream);
return xstream.unmarshal( reader, root );
}
finally
@@ -352,5 +410,4 @@
throw new ActionProcessingException( e );
}
}
-
}
Copied: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream (from rev 25116, labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream)
Copied: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf (from rev 25116, labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf)
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/AttributeAliasConf.java
===================================================================
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/ClassAliasConf.java
===================================================================
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/ConverterConf.java
===================================================================
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/FieldAliasConf.java
===================================================================
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/ImplicitCollectionConf.java
===================================================================
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/src/org/jboss/soa/esb/actions/converters/xstream/conf/XStreamConfigurator.java
===================================================================
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/TestBean.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/TestBean.java 2009-02-17 17:59:12 UTC (rev 25311)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/TestBean.java 2009-02-17 22:50:56 UTC (rev 25312)
@@ -22,6 +22,8 @@
package org.jboss.soa.esb.actions;
+import java.util.List;
+
/**
* Just a simple test JavaBean class.
* @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
@@ -30,10 +32,11 @@
public class TestBean implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
+ public enum GENDER { MALE, FEMALE};
private String name;
private String initials;
private String phone;
- public enum GENDER { MALE, FEMALE};
+ private List<String> addresses;
private GENDER gender;
@@ -75,4 +78,19 @@
{
this.gender = gender;
}
+
+ public List<String> getAddresses()
+ {
+ return addresses;
+ }
+
+ public void setAddresses(List<String> addresses)
+ {
+ this.addresses = addresses;
+ }
+
+ public String toString()
+ {
+ return "TestBean [name=" + name + ", phone=" + phone + ", initials=" + initials + ", gender=" + gender + ", addresses=" + addresses + "]";
+ }
}
Copied: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/Comment.java (from rev 25116, labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/Comment.java)
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/Comment.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/Comment.java 2009-02-17 22:50:56 UTC (rev 25312)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors 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.actions.converters;
+
+import java.io.Serializable;
+
+/**
+ * Used in XStream Converter related tests.
+ *
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ *
+ */
+public class Comment implements Serializable
+{
+ private static final long serialVersionUID = 1L;
+
+ private String comment;
+
+ public Comment()
+ {
+ }
+
+ public Comment(final String comment)
+ {
+ this.comment = comment;
+ }
+
+ public String getComment()
+ {
+ return comment;
+ }
+
+ public void setComment(String comment)
+ {
+ this.comment = comment;
+ }
+
+ public String toString()
+ {
+ return "[Comment: " + comment + "]";
+ }
+
+}
Copied: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/CommentConverter.java (from rev 25116, labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/CommentConverter.java)
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/CommentConverter.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/CommentConverter.java 2009-02-17 22:50:56 UTC (rev 25312)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors 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.actions.converters;
+
+import com.thoughtworks.xstream.converters.Converter;
+import com.thoughtworks.xstream.converters.MarshallingContext;
+import com.thoughtworks.xstream.converters.UnmarshallingContext;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+
+/**
+ * XStream Converter used in tests.
+ *
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ *
+ */
+public class CommentConverter implements Converter
+{
+ public boolean canConvert(Class clazz)
+ {
+ return clazz.equals(Comment.class);
+ }
+
+ public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context)
+ {
+ Comment comment = (Comment) value;
+ writer.startNode("comment");
+ writer.setValue(comment.getComment());
+ writer.endNode();
+ }
+
+ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
+ {
+ return new Comment(reader.getValue());
+ }
+}
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/ObjectToXStreamUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/ObjectToXStreamUnitTest.java 2009-02-17 17:59:12 UTC (rev 25311)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/ObjectToXStreamUnitTest.java 2009-02-17 22:50:56 UTC (rev 25312)
@@ -22,18 +22,26 @@
package org.jboss.soa.esb.actions.converters;
+import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
import junit.framework.TestCase;
-import org.apache.log4j.Logger;
+import org.custommonkey.xmlunit.XMLUnit;
import org.jboss.soa.esb.actions.ActionProcessingException;
import org.jboss.soa.esb.actions.TestBean;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
+import org.xml.sax.SAXException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
+
/**
* ObjectToXStream unit tests.
* @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
@@ -42,8 +50,6 @@
*/
public class ObjectToXStreamUnitTest extends TestCase {
- private Logger log = Logger.getLogger( ObjectToXStreamUnitTest.class );
-
public void test_default() throws ActionProcessingException {
ConfigTree config = new ConfigTree("config");
@@ -65,10 +71,10 @@
}
public void test_with_package() throws ActionProcessingException {
- ConfigTree config = new ConfigTree("config");
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.excludePackage(false);
- config.setAttribute("exclude-package", "false");
- ObjectToXStream objectToXStream = new ObjectToXStream(config);
+ ObjectToXStream objectToXStream = new ObjectToXStream(builder.build());
Message oMsg = MessageFactory.getInstance().getMessage();
oMsg.getBody().add(new TestBean("Tom", "1234"));
@@ -85,12 +91,12 @@
assertEquals("1234", bean.getPhone());
}
- public void test_with_alias() throws ActionProcessingException {
- ConfigTree config = new ConfigTree("config");
-
- config.setAttribute("class-alias", "TomsClass");
- ObjectToXStream objectToXStream = new ObjectToXStream(config);
+ public void test_with_classAlias() throws ActionProcessingException {
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("TomsClass");
+ ObjectToXStream objectToXStream = new ObjectToXStream(builder.build());
+
Message oMsg = MessageFactory.getInstance().getMessage();
oMsg.getBody().add(new TestBean("Tom", "1234"));
objectToXStream.process(oMsg);
@@ -109,14 +115,11 @@
public void test_with_namespace() throws ActionProcessingException {
final String ns = "http://www.xyz.com.org/xyz";
- ConfigTree config = new ConfigTree("config");
-
- config.setAttribute("class-alias", "TomsClass");
- ConfigTree namespace = new ConfigTree("namespace", config);
- namespace.setAttribute( ObjectToXStream.ATTR_NAMESPACE_URI, ns);
- namespace.setAttribute( ObjectToXStream.ATTR_NAMESPACE_LOCALPART, "TomsClass");
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("TomsClass");
+ builder.namespace(ns, "TomsClass");
- ObjectToXStream objectToXStream = new ObjectToXStream(config);
+ ObjectToXStream objectToXStream = new ObjectToXStream(builder.build());
Message oMsg = MessageFactory.getInstance().getMessage();
oMsg.getBody().add(new TestBean("Tom", "1234"));
@@ -125,4 +128,77 @@
assertTrue( xml.contains( "TomsClass xmlns=\"" + ns ));
}
+ public void test_with_alias() throws ActionProcessingException, SAXException, IOException {
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.alias("TomsClass", TestBean.class.getName());
+ ObjectToXStream objectToXStream = new ObjectToXStream(builder.build());
+
+ Message message = MessageFactory.getInstance().getMessage();
+ message.getBody().add(new TestBean("Tom", "1234"));
+ objectToXStream.process(message);
+
+ String expectedXml = "<TomsClass><name>Tom</name><phone>1234</phone></TomsClass>";
+ String actualXml = (String) message.getBody().get();
+ assertXMLEqual(expectedXml, actualXml);
+ }
+
+ public void test_with_fieldAlias() throws ActionProcessingException, SAXException, IOException {
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.alias("TomsClass", TestBean.class.getName());
+ builder.fieldAlias("firstName", TestBean.class, "name");
+ ObjectToXStream objectToXStream = new ObjectToXStream(builder.build());
+
+ Message message = MessageFactory.getInstance().getMessage();
+ message.getBody().add(new TestBean("Tom", "1234"));
+ objectToXStream.process(message);
+
+ String expectedXml = "<TomsClass><firstName>Tom</firstName><phone>1234</phone></TomsClass>";
+ String actualXml = (String) message.getBody().get();
+ assertXMLEqual(expectedXml, actualXml);
+ }
+
+ public void test_with_implicitCollection() throws ActionProcessingException, SAXException, IOException {
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("TomsClass");
+ builder.implicitCollection(TestBean.class.getName(), "addresses", "address", String.class.getName());
+
+ ObjectToXStream objectToXStream = new ObjectToXStream(builder.build());
+
+ Message message = MessageFactory.getInstance().getMessage();
+ TestBean testBean = new TestBean("Tom", "1234");
+ List<String> addresses = new ArrayList<String>();
+ addresses.add("street1");
+ addresses.add("street2");
+ testBean.setAddresses(addresses);
+ message.getBody().add(testBean);
+ objectToXStream.process(message);
+
+ String expectedXml = "<TomsClass><name>Tom</name><phone>1234</phone><address>street1</address><address>street2</address></TomsClass>";
+ String actualXml = (String) message.getBody().get();
+ assertXMLEqual(expectedXml, actualXml);
+ }
+
+ public void test_with_converter() throws ActionProcessingException, SAXException, IOException {
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("comments");
+ builder.incomingType(Comment.class.getName());
+
+ ObjectToXStream objectToXStream = new ObjectToXStream(builder.build());
+
+ Message message = MessageFactory.getInstance().getMessage();
+ Comment comment = new Comment("some comment");
+
+ message.getBody().add(comment);
+ objectToXStream.process(message);
+
+ String expectedXml = "<comments><comment>some comment</comment></comments>";
+ String actualXml = (String) message.getBody().get();
+ assertXMLEqual(expectedXml, actualXml);
+ }
+
+ public void setUp()
+ {
+ XMLUnit.setIgnoreWhitespace(true);
+ }
+
}
Copied: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamConfigBuilder.java (from rev 25116, labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamConfigBuilder.java)
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamConfigBuilder.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamConfigBuilder.java 2009-02-17 22:50:56 UTC (rev 25312)
@@ -0,0 +1,144 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors 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.actions.converters;
+
+import org.jboss.soa.esb.helpers.ConfigTree;
+
+/**
+ * ConfigTreeBuilder for XStream unit tests.
+ * <p/>
+ *
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ *
+ */
+public class XStreamConfigBuilder
+{
+ private ConfigTree config;
+
+ public XStreamConfigBuilder()
+ {
+ config = new ConfigTree("config");
+ }
+
+ public XStreamConfigBuilder classAlias(final String classAlias)
+ {
+ config.setAttribute("class-alias", classAlias);
+ return this;
+ }
+
+ public XStreamConfigBuilder excludePackage(final boolean excludePackage)
+ {
+ config.setAttribute("exclude-package", String.valueOf(excludePackage));
+ return this;
+ }
+
+ public XStreamConfigBuilder incomingType(final String type)
+ {
+ config.setAttribute("incoming-type", type);
+ return this;
+ }
+
+ public XStreamConfigBuilder rootNodeName(final String rootNodeName)
+ {
+ config.setAttribute("root-node", rootNodeName);
+ return this;
+ }
+
+ public XStreamConfigBuilder alias(final String aliasName, final String className)
+ {
+ ConfigTree alias1 = new ConfigTree("alias", config);
+ alias1.setAttribute("name", aliasName);
+ alias1.setAttribute("class", className);
+ return this;
+ }
+
+ public XStreamConfigBuilder attributeAlias(final String aliasName, final String className)
+ {
+ ConfigTree attrAlias = new ConfigTree("attribute-alias", config);
+ attrAlias.setAttribute("name", aliasName);
+ attrAlias.setAttribute("class", className);
+ return this;
+ }
+
+ public XStreamConfigBuilder fieldAlias(final String aliasName, final Class<?> definedIn, final String fieldName)
+ {
+ ConfigTree attrAlias = new ConfigTree("field-alias", config);
+ attrAlias.setAttribute("alias", aliasName);
+ attrAlias.setAttribute("definedIn", definedIn.getName());
+ attrAlias.setAttribute("fieldName", fieldName);
+ return this;
+ }
+
+ public XStreamConfigBuilder implicitCollection(final String ownerType, final String fieldName)
+ {
+ ConfigTree attrAlias = new ConfigTree("implicit-collection", config);
+ attrAlias.setAttribute("ownerType", ownerType);
+ attrAlias.setAttribute("fieldName", fieldName);
+ return this;
+ }
+
+ public XStreamConfigBuilder implicitCollection(final String ownerType, final String fieldName, final String itemType)
+ {
+ ConfigTree attrAlias = new ConfigTree("implicit-collection", config);
+ attrAlias.setAttribute("ownerType", ownerType);
+ attrAlias.setAttribute("fieldName", fieldName);
+ attrAlias.setAttribute("itemType", itemType);
+ return this;
+ }
+
+ public XStreamConfigBuilder implicitCollection(final String ownerType, final String fieldName, final String itemFieldType, final String itemType)
+ {
+ ConfigTree attrAlias = new ConfigTree("implicit-collection", config);
+ attrAlias.setAttribute("ownerType", ownerType);
+ attrAlias.setAttribute("fieldName", fieldName);
+ attrAlias.setAttribute("itemFieldName", itemFieldType);
+ attrAlias.setAttribute("itemType", itemType);
+ return this;
+ }
+
+ public XStreamConfigBuilder namespace(final String ns, final String localPart)
+ {
+ ConfigTree namespace = new ConfigTree("namespace", config);
+ namespace.setAttribute(ObjectToXStream.ATTR_NAMESPACE_URI, ns);
+ namespace.setAttribute(ObjectToXStream.ATTR_NAMESPACE_LOCALPART, localPart);
+ return this;
+ }
+
+ public XStreamConfigBuilder converter(final String className)
+ {
+ ConfigTree converter = new ConfigTree("converter", config);
+ converter.setAttribute("class", className);
+ return this;
+ }
+
+ public XStreamConfigBuilder converter(final String className, int priority)
+ {
+ ConfigTree converter = new ConfigTree("converter", config);
+ converter.setAttribute("class", className);
+ converter.setAttribute("priority", String.valueOf(priority));
+ return this;
+ }
+
+ public ConfigTree build()
+ {
+ return config;
+ }
+}
\ No newline at end of file
Modified: labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java 2009-02-17 17:59:12 UTC (rev 25311)
+++ labs/jbossesb/branches/JBESB_4_4_GA_FP/product/rosetta/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java 2009-02-17 22:50:56 UTC (rev 25312)
@@ -26,8 +26,8 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import java.io.Serializable;
import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import junit.framework.JUnit4TestAdapter;
@@ -40,13 +40,6 @@
import org.jboss.soa.esb.message.format.MessageFactory;
import org.junit.Test;
-import com.thoughtworks.xstream.XStream;
-import com.thoughtworks.xstream.converters.Converter;
-import com.thoughtworks.xstream.converters.MarshallingContext;
-import com.thoughtworks.xstream.converters.UnmarshallingContext;
-import com.thoughtworks.xstream.io.HierarchicalStreamReader;
-import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
-
/**
* XStreamToObject unit tests.
* @author daniel Marchant
@@ -60,11 +53,10 @@
@Test
public void test_default() throws ActionProcessingException {
- ConfigTree config = new ConfigTree("config");
-
- config.setAttribute("incoming-type",TestBean.class.getName());
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.incomingType(TestBean.class.getName());
- XStreamToObject xstreamToObject = new XStreamToObject(config);
+ XStreamToObject xstreamToObject = new XStreamToObject(builder.build());
Message oMsg = MessageFactory.getInstance().getMessage();
@@ -82,12 +74,11 @@
@Test
public void test_with_package() throws ActionProcessingException {
- ConfigTree config = new ConfigTree("config");
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.excludePackage(false);
+ builder.incomingType(TestBean.class.getName());
+ XStreamToObject xstreamToObject = new XStreamToObject(builder.build());
- config.setAttribute("exclude-package", "false");
- config.setAttribute("incoming-type", TestBean.class.getName());
- XStreamToObject xstreamToObject = new XStreamToObject(config);
-
Message oMsg = MessageFactory.getInstance().getMessage();
String msg = "<" + TestBean.class.getName() + "> <name>Tom</name><phone>1234</phone></"+ TestBean.class.getName() +">";
@@ -104,13 +95,12 @@
@Test
public void test_with_alias() throws ActionProcessingException {
- ConfigTree config = new ConfigTree("config");
-
- config.setAttribute("class-alias", "TomsClass");
- config.setAttribute("exclude-package", "false");
- config.setAttribute("incoming-type", TestBean.class.getName());
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("TomsClass");
+ builder.excludePackage(false);
+ builder.incomingType(TestBean.class.getName());
- XStreamToObject xstreamToObject = new XStreamToObject(config);
+ XStreamToObject xstreamToObject = new XStreamToObject(builder.build());
Message oMsg = MessageFactory.getInstance().getMessage();
@@ -126,38 +116,69 @@
}
@Test
- public void getAliases()
- {
- ConfigTree configTree = getConfigTreeWithAliases("TomsClass", false, TestBean.class, null );
- XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+ public void fieldAliases() throws ActionProcessingException {
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("TomsClass");
+ builder.excludePackage(false);
+ builder.incomingType(TestBean.class.getName());
+ builder.fieldAlias("firstName", TestBean.class, "name");
+ builder.fieldAlias("phoneNumber", TestBean.class, "phone");
- Map<String, String> aliases = xstreamToObject.getAliases( configTree, "alias" );
+ XStreamToObject xstreamToObject = new XStreamToObject(builder.build());
+
+ Message oMsg = MessageFactory.getInstance().getMessage();
+ String msg = "<TomsClass><firstName>Tom</firstName><phoneNumber>1234</phoneNumber></TomsClass>";
- assertAliases( aliases );
+ oMsg.getBody().add(msg);
+ oMsg = xstreamToObject.process(oMsg);
+
+ TestBean bean = (TestBean) oMsg.getBody().get();
+
+ assertEquals("Tom", bean.getName());
+ assertEquals("1234", bean.getPhone());
}
-
+
@Test
- public void addAliases() throws ActionProcessingException
- {
- ConfigTree configTree = getConfigTreeWithAliases("TomsClass", false, TestBean.class, null );
- XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+ public void implicitCollections() throws ActionProcessingException
+ {
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("TomsClass");
+ builder.incomingType(TestBean.class.getName());
+ builder.alias("address", String.class.getName());
+ builder.implicitCollection(List.class.getName(), "addresses", "address", String.class.getName());
- Map<String, String> aliases = xstreamToObject.getAliases( configTree, "alias" );
- xstreamToObject.addAliases( aliases, new XStream() );
+ XStreamToObject xstreamToObject = new XStreamToObject(builder.build());
- assertAliases( aliases );
- }
-
+ Message msg = MessageFactory.getInstance().getMessage();
+ String xml = "<TomsClass><name>Tom</name><phone>1234</phone><addresses><address>street1</address><address>street2</address></addresses></TomsClass>";
+ msg.getBody().add(xml);
+
+ msg = xstreamToObject.process(msg);
+
+ TestBean bean = (TestBean) msg.getBody().get();
+ System.out.println(bean);
+ List<String> addresses = bean.getAddresses();
+ assertEquals(2, addresses.size());
+ assertEquals("street1", addresses.get(0));
+ assertEquals("street2", addresses.get(1));
+ }
+
+
@Test
- public void addAliasesNegative() throws ActionProcessingException
+ public void getAliases()
{
- ConfigTree configTree = getConfigTree( "TomsClass", false, TestBean.class, null );
- XStreamToObject xstreamToObject = createXStreamObject( "TomsClass", false, TestBean.class, null );
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("TomsClass");
+ builder.excludePackage(false);
+ builder.incomingType(TestBean.class.getName());
+ builder.alias("aliasName1", TestBean.class.getName());
+ builder.alias("aliasName2", TestBean.class.getName());
- Map<String, String> aliases = xstreamToObject.getAliases( configTree, "alias" );
- xstreamToObject.addAliases( aliases, new XStream() );
+ ConfigTree configTree = builder.build();
+ XStreamToObject xstreamToObject = new XStreamToObject( configTree );
- assertEquals( "Aliases map should have been empty!", 0, aliases.size() );
+ Map<String, String> aliases = xstreamToObject.getAliases( configTree, "alias" );
+ assertAliases( aliases );
}
@Test
@@ -194,7 +215,7 @@
String xml = "<someNode><TomsClass><name>Tom</name><phone>1234</phone></TomsClass></someNode>";
Object obj = new TestBean();
- xstreamToObject.fromXmlToObject( xml, obj );
+ obj = xstreamToObject.fromXmlToObject( xml, obj );
assertTrue( obj instanceof TestBean );
assertEquals("1234", ((TestBean)obj).getPhone());
@@ -209,7 +230,7 @@
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 );
+ obj = xstreamToObject.fromXmlToObject( xml, obj );
assertTrue( obj instanceof TestBean2 );
assertEquals("Daniel", ((TestBean2)obj).getName());
@@ -224,7 +245,7 @@
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 );
+ obj = xstreamToObject.fromXmlToObject( xml, obj );
assertTrue( obj instanceof TestBean2 );
assertEquals("Bevenius", ((TestBean2)obj).getName());
@@ -234,11 +255,15 @@
public void fromXmlToObject_with_collection() throws ActionProcessingException
{
String rootNodeName = "/someNode/list";
- ConfigTree configTree = getConfigTree( "list", true, ArrayList.class, rootNodeName);
- ConfigTree alias1 = new ConfigTree( "alias", configTree);
- alias1.setAttribute( "name", "TomsClass");
- alias1.setAttribute( "class", TestBean.class.getName());
- XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("list");
+ builder.excludePackage(true);
+ builder.incomingType(ArrayList.class.getName());
+ builder.rootNodeName(rootNodeName);
+ builder.alias("TomsClass", TestBean.class.getName());
+
+ XStreamToObject xstreamToObject = new XStreamToObject( builder.build() );
String xml = "<someNode><list><TomsClass><name>Tom</name></TomsClass><TomsClass><name>Daniel</name></TomsClass></list></someNode>";
@@ -255,18 +280,17 @@
public void fromXmlToObject_with_attributeAliases() throws ActionProcessingException
{
String rootNodeName = "/someNode/list";
- ConfigTree configTree = getConfigTree( "list", true, ArrayList.class, rootNodeName);
- ConfigTree alias1 = new ConfigTree( "alias", configTree);
- alias1.setAttribute( "name", "TomsClass");
- alias1.setAttribute( "class", TestBean.class.getName());
-
- ConfigTree attrAlias = new ConfigTree( "attribute-alias", configTree);
- attrAlias.setAttribute( "name", "initials");
- attrAlias.setAttribute( "class", String.class.getName() );
-
- XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("list");
+ builder.excludePackage(true);
+ builder.incomingType(ArrayList.class.getName());
+ builder.rootNodeName(rootNodeName);
+ builder.alias("TomsClass", TestBean.class.getName());
+ builder.attributeAlias("initials", String.class.getName());
+ XStreamToObject xstreamToObject = new XStreamToObject( builder.build() );
+
String xml = "<someNode><list><TomsClass initials='TF'><name>Tom</name></TomsClass><TomsClass initials='DB'><name>Daniel</name></TomsClass></list></someNode>";
Object obj = new ArrayList<TestBean>();
@@ -284,17 +308,17 @@
public void fromXmlToObject_with_enum() throws ActionProcessingException
{
String rootNodeName = "/someNode/list";
- ConfigTree configTree = getConfigTree( "list", true, ArrayList.class, rootNodeName);
- ConfigTree alias1 = new ConfigTree( "alias", configTree);
- alias1.setAttribute( "name", "TomsClass");
- alias1.setAttribute( "class", TestBean.class.getName());
- ConfigTree gender = new ConfigTree( "alias", configTree);
- gender.setAttribute( "name", "gender");
- gender.setAttribute( "class", TestBean.GENDER.class.getName());
-
- XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("list");
+ builder.excludePackage(true);
+ builder.incomingType(ArrayList.class.getName());
+ builder.rootNodeName(rootNodeName);
+ builder.alias("TomsClass", TestBean.class.getName());
+ builder.alias("gender", TestBean.GENDER.class.getName());
+ XStreamToObject xstreamToObject = new XStreamToObject( builder.build() );
+
String xml = "<someNode><list><TomsClass><gender>MALE</gender></TomsClass><TomsClass><gender>FEMALE</gender></TomsClass></list></someNode>";
Object obj = new ArrayList<TestBean>();
@@ -315,16 +339,15 @@
public void fromXmlToObject_with_collections() throws ActionProcessingException
{
String rootNodeName = "/someNode/list";
- ConfigTree configTree = getConfigTree( "list", true, ArrayList.class, rootNodeName);
- ConfigTree alias1 = new ConfigTree( "alias", configTree);
- alias1.setAttribute( "name", "Persons");
- alias1.setAttribute( "class", ArrayList.class.getName());
+ XStreamConfigBuilder builder = new XStreamConfigBuilder();
+ builder.classAlias("list");
+ builder.excludePackage(true);
+ builder.incomingType(ArrayList.class.getName());
+ builder.rootNodeName(rootNodeName);
+ builder.alias("Persons", ArrayList.class.getName());
+ builder.alias("TomsClass", TestBean.class.getName());
- ConfigTree alias2 = new ConfigTree( "alias", configTree);
- alias2.setAttribute( "name", "TomsClass");
- alias2.setAttribute( "class", TestBean.class.getName());
-
- XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+ XStreamToObject xstreamToObject = new XStreamToObject( builder.build() );
String xml = "<someNode><list><Persons><TomsClass><list></list></TomsClass><TomsClass></TomsClass></Persons></list></someNode>";
@@ -340,7 +363,8 @@
@Test
public void fromXmlToObject_with_converter() throws ActionProcessingException
{
- ConfigTree configTree = getConfigTree( "comment", true, Comment.class, null);
+ XStreamConfigBuilder builder = new XStreamConfigBuilder().classAlias("comment").excludePackage(true).incomingType(Comment.class.getName());
+ ConfigTree configTree = builder.build();
ConfigTree converter = new ConfigTree( "converter", configTree);
converter.setAttribute( "class", CommentConverter.class.getName());
@@ -356,9 +380,10 @@
assertEquals( expectedComment, ((Comment)obj).getComment());
}
- private XStreamToObject createXStreamObject( String classAlias, boolean exludePackage, Class incomingType, String rootNodeName)
+ private XStreamToObject createXStreamObject( String classAlias, boolean exludePackage, Class<?> incomingType, String rootNodeName)
{
- ConfigTree configTree = getConfigTree( classAlias, exludePackage, incomingType, rootNodeName);
+ XStreamConfigBuilder builder = new XStreamConfigBuilder().classAlias(classAlias).excludePackage(exludePackage).incomingType(incomingType.getName()).rootNodeName(rootNodeName);
+ ConfigTree configTree = builder.build();
return new XStreamToObject( configTree );
}
@@ -369,31 +394,6 @@
assertEquals( TestBean.class.getName(), aliases.get( "aliasName2" ) );
}
- private ConfigTree getConfigTree(String classAlias, boolean excludePackage, Class incomingType, String rootNodeName)
- {
- 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(String classAlias, boolean excludePackage, Class incomingType, String rootNodeName)
- {
- ConfigTree configTree = getConfigTree( classAlias,false, incomingType, rootNodeName );
-
- ConfigTree alias1 = new ConfigTree( "alias", configTree);
- alias1.setAttribute( "name", "aliasName1");
- alias1.setAttribute( "class", TestBean.class.getName());
-
- ConfigTree alias2 = new ConfigTree( "alias", configTree);
- alias2.setAttribute( "name", "aliasName2");
- alias2.setAttribute( "class", TestBean.class.getName());
-
- return configTree;
- }
-
/**
* An abstract class for testing
*/
@@ -414,56 +414,8 @@
{
this.name = name;
}
-
}
- private static class Comment implements Serializable
- {
- private String comment;
-
- public Comment() {}
-
- public Comment(final String comment)
- {
- this.comment = comment;
- }
-
- public String getComment()
- {
- return comment;
- }
-
- public void setComment( String comment )
- {
- this.comment = comment;
- }
-
- public String toString()
- {
- return "[Comment: " + comment + "]";
- }
-
- }
- static class CommentConverter implements Converter {
- private Logger log = Logger.getLogger(CommentConverter.class);
-
- public boolean canConvert(Class clazz) {
- return clazz.equals(Comment.class);
- }
-
- public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
- log.debug( value );
- Comment comment = (Comment) value;
- writer.startNode("comment");
- writer.setValue(comment.getComment());
- writer.endNode();
- }
-
- public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
- return new Comment(reader.getValue());
- }
- }
-
/**
* Just here to get Ant to find annotated test.
*/
Added: labs/jbossesb/branches/JBESB_4_4_GA_FP/testlib/xmlunit-1.2.jar
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/branches/JBESB_4_4_GA_FP/testlib/xmlunit-1.2.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
More information about the jboss-svn-commits
mailing list