[Practicalxml-commits] SF.net SVN: practicalxml:[90] branches/dev-1.1/src
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2009-07-16 15:15:32
|
Revision: 90
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=90&view=rev
Author: kdgregory
Date: 2009-07-16 15:15:28 +0000 (Thu, 16 Jul 2009)
Log Message:
-----------
re-introduce OutputHandler interface
Modified Paths:
--------------
branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanConverter.java
branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandler.java
branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputOptions.java
branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanConverter.java
Added Paths:
-----------
branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandlerImpl.java
branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandlerImpl.java
Removed Paths:
-------------
branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandler.java
Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanConverter.java
===================================================================
--- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanConverter.java 2009-07-15 19:31:56 UTC (rev 89)
+++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanConverter.java 2009-07-16 15:15:28 UTC (rev 90)
@@ -62,7 +62,7 @@
BeanOutputOptions... options)
{
Element root = DomUtil.newDocument(nsUri, "temp");
- BeanOutputDriver.dispatch(rootName, bean, new BeanOutputHandler(root, options));
+ BeanOutputDriver.dispatch(rootName, bean, new BeanOutputHandlerImpl(root, options));
return replaceRoot(root);
}
@@ -81,7 +81,7 @@
BeanOutputOptions... options)
{
Element root = DomUtil.newDocument("temp");
- BeanOutputDriver.dispatch(rootName, bean, new BeanOutputHandler(root, options));
+ BeanOutputDriver.dispatch(rootName, bean, new BeanOutputHandlerImpl(root, options));
return replaceRoot(root);
}
Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandler.java
===================================================================
--- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandler.java 2009-07-15 19:31:56 UTC (rev 89)
+++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandler.java 2009-07-16 15:15:28 UTC (rev 90)
@@ -14,335 +14,35 @@
package net.sf.practicalxml.converter;
-import net.sf.practicalxml.DomUtil;
-import net.sf.practicalxml.XmlUtil;
-
-import java.util.Arrays;
import java.util.Date;
-import java.util.EnumSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import javax.xml.XMLConstants;
-import org.w3c.dom.Element;
-
/**
- * {@link OutputHandler} that builds a DOM tree. The structure of this
- * tree is customized by passing values from {@link BeanOutputOptions}.
- * <p>
- * Instances of this class are constructed around an <code>Element</code>,
- * which represents the object to be introspected by {@link BeanOutputDriver};
- * this simplifies recursive calls.
- * <p>
- * This class is not threadsafe (but then, neither are the JDK's DOM classes).
+ * {@link BeanOutputDriver} interacts with instances of this interface.
+ * There's only one such instance within the Practical XML library:
+ * {@link BeanOutputHandlerImpl}, but the existence of the interface
+ * lets you reuse the driver for other purposes.
*/
-public class BeanOutputHandler
+public interface BeanOutputHandler
{
- private EnumSet<BeanOutputOptions> _options;
- private Appender _appender;
-
-
- /**
- * Public constructor, which uses default appender and allows individual
- * options specifiers.
- */
- public BeanOutputHandler(Element appendTo, BeanOutputOptions... options)
- {
- _appender = new Appender(appendTo);
- _options = EnumSet.noneOf(BeanOutputOptions.class);
- for (BeanOutputOptions option : options)
- _options.add(option);
- }
-
-
- /**
- * Internal constructor, used by container classes for recursive calls.
- */
- protected BeanOutputHandler(BeanOutputHandler parent, Appender appender)
- {
- _appender = appender;
- _options = parent._options;
- }
-
-
-//----------------------------------------------------------------------------
-// OutputHandler implementation
-//----------------------------------------------------------------------------
-
- public void convert(String name, Boolean value)
- {
- Object formatted = value;
- if (shouldFormatAsXsd())
- formatted = value.booleanValue() ? "true" : "false";
-
- _appender.append(name, "xsd:boolean", formatted);
- }
-
-
- public void convert(String name, Byte value)
- {
- _appender.append(name, "xsd:byte", value);
- }
-
-
- public void convert(String name, Character value)
- {
- _appender.append(name, "xsd:string", value);
- }
-
-
- public void convert(String name, Date value)
- {
- Object formatted = value;
- if (shouldFormatAsXsd())
- formatted = XmlUtil.formatXsdDatetime(value);
-
- _appender.append(name, "xsd:dateTime", formatted);
- }
-
-
- public void convert(String name, Double value)
- {
- Object formatted = value;
- if (shouldFormatAsXsd())
- formatted = XmlUtil.formatXsdDecimal(value);
-
- _appender.append(name, "xsd:decimal", formatted);
- }
-
-
- public void convert(String name, Float value)
- {
- Object formatted = value;
- if (shouldFormatAsXsd())
- formatted = XmlUtil.formatXsdDecimal(value);
-
- _appender.append(name, "xsd:decimal", formatted);
- }
-
-
- public void convert(String name, Integer value)
- {
- _appender.append(name, "xsd:int", value);
- }
-
-
- public void convert(String name, Long value)
- {
- _appender.append(name, "xsd:long", value);
- }
-
-
- public void convert(String name, Number value)
- {
- _appender.append(name, "xsd:decimal", value);
- }
-
-
- public void convert(String name, Short value)
- {
- _appender.append(name, "xsd:short", value);
- }
-
-
- public void convert(String name, String value)
- {
- _appender.append(name, "xsd:string", value);
- }
-
-
- public void convert(String name, List<?> value)
- {
- Element container = _appender.append(name, javaXsiType(value), null);
- appendSequence(container, value.iterator(), true);
- }
-
-
- public void convert(String name, Set<?> value)
- {
- Element container = _appender.append(name, javaXsiType(value), null);
- appendSequence(container, value.iterator(), false);
- }
-
-
- public void convert(String name, Map<?,?> map)
- {
- Element container = _appender.append(name, javaXsiType(map), null);
- BeanOutputHandler childHandler = new BeanOutputHandler(this, new MapAppender(container));
- BeanOutputDriver.introspect(map, childHandler);
- }
-
-
- public void convert(String name, Object value)
- {
- Element container = _appender.append(name, javaXsiType(value), null);
- Appender childAppender = value.getClass().isArray()
- ? new IndexedAppender(container)
- : new Appender(container);
- BeanOutputHandler childHandler = new BeanOutputHandler(this, childAppender);
- BeanOutputDriver.introspect(value, childHandler);
- }
-
-
- public void convert(String name, Object[] array)
- {
- Element container = _appender.append(name, javaXsiType(array), null);
- appendSequence(container, Arrays.asList(array).iterator(), true);
- }
-
-
- public void convertNull(String name)
- {
- if (!_options.contains(BeanOutputOptions.XSI_NIL))
- return;
-
- Element child = _appender.append(name, "", null);
- child.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:nil", "true");
- }
-
-
-//----------------------------------------------------------------------------
-// Internals
-//----------------------------------------------------------------------------
-
- protected boolean shouldFormatAsXsd()
- {
- return _options.contains(BeanOutputOptions.XSD_FORMAT)
- || _options.contains(BeanOutputOptions.XSI_TYPE);
- }
-
-
- /**
- * Returns the <code>xsi:type</code> value for a Java object (should not
- * be called for primitive wrappers, as these are covered by "xsd" types).
- */
- private String javaXsiType(Object obj)
- {
- return "java:" + obj.getClass().getName();
- }
-
-
- /**
- * Common code for sequenced objects -- arrays, lists, and sets.
- */
- private void appendSequence(Element parent, Iterator<?> itx, boolean isIndexed)
- {
- Appender childAppender = isIndexed ? new IndexedAppender(parent)
- : new Appender(parent);
- BeanOutputHandler childHandler = new BeanOutputHandler(this, childAppender);
- while (itx.hasNext())
- BeanOutputDriver.dispatch("data", itx.next(), childHandler);
- }
-
-
- /**
- * This class is responsible for adding nodes to the DOM tree. Each
- * instance of <code>XmlOutputHandler</code> is constructed around one
- * of these (or a subclass). Subclasses may override the {@link #append}
- * method to add additional information to the appended nodes.
- * <p>
- * A little bit of weirdness: as an inner class, this is constructed in
- * the context of an <code>XmlOutputHandler</code>, and has access to
- * members defined by that handler. However, in a recursive call, it is
- * actually held and used by a different handler. Since the second handler
- * copies members of the first, I don't see this as a problem ... until
- * some member doesn't get copied.
- */
- private class Appender
- {
- private Element _appendTo;
-
- public Appender(Element appendTo)
- {
- _appendTo = appendTo;
- }
-
- /**
- * Appends a child to the element we manage.
- *
- * @param name Local-name for the new element. As long as you're
- * dealing with Java, property names and element names
- * are interchangeable. If handling a <code>Map</code>,
- * you'll need to ensure that keys are appropriate XML
- * element names.
- * @param xsiType The value to insert in an <code>xsi:type</code>
- * attribute. This is required, even if the output-type
- * option isn't turned on.
- * @param value If not-null, will be stringified and attached to the
- * element as a text node. Apply any fancy formatting
- * before coming here. If null, will be ignored.
- */
- public Element append(String name, String xsiType, Object value)
- {
- if ((name == null) || (name.length() == 0))
- name = "data";
-
- Element child = DomUtil.appendChildInheritNamespace(_appendTo, name);
-
- if (_options.contains(BeanOutputOptions.XSI_TYPE))
- child.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:type", xsiType);
-
- if (value != null)
- child.setTextContent(String.valueOf(value));
-
- return child;
- }
- }
-
-
- /**
- * An appender for array/list processing, that attaches an index attribute
- * to the appended node.
- */
- private class IndexedAppender
- extends Appender
- {
- int _index = 0;
-
- public IndexedAppender(Element appendTo)
- {
- super(appendTo);
- }
-
- @Override
- public Element append(String name, String xsiType, Object value)
- {
- Element child = super.append(name, xsiType, value);
- child.setAttribute("index", String.valueOf(_index++));
- return child;
- }
- }
-
-
- /**
- * An appender for maps, which either uses the passed name as the
- * element name, or as the value of a "key" attribute.
- */
- private class MapAppender
- extends Appender
- {
- public MapAppender(Element appendTo)
- {
- super(appendTo);
- }
-
- @Override
- public Element append(String name, String xsiType, Object value)
- {
- Element child = null;
- if (_options.contains(BeanOutputOptions.INTROSPECT_MAPS))
- {
- child = super.append(name, xsiType, value);
- }
- else
- {
- child = super.append("data", xsiType, value);
- child.setAttribute("key", name);
- }
- return child;
- }
- }
+ public void convert(String name, Boolean value);
+ public void convert(String name, Byte value);
+ public void convert(String name, Character value);
+ public void convert(String name, Date value);
+ public void convert(String name, Double value);
+ public void convert(String name, Float value);
+ public void convert(String name, Integer value);
+ public void convert(String name, Long value);
+ public void convert(String name, Number value);
+ public void convert(String name, Short value);
+ public void convert(String name, String value);
+ public void convert(String name, List<?> value);
+ public void convert(String name, Set<?> value);
+ public void convert(String name, Map<?,?> map);
+ public void convert(String name, Object value);
+ public void convert(String name, Object[] array);
+ public void convertNull(String name);
}
Copied: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandlerImpl.java (from rev 89, branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandler.java)
===================================================================
--- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandlerImpl.java (rev 0)
+++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputHandlerImpl.java 2009-07-16 15:15:28 UTC (rev 90)
@@ -0,0 +1,351 @@
+// Copyright 2008-2009 severally by the contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package net.sf.practicalxml.converter;
+
+import net.sf.practicalxml.DomUtil;
+import net.sf.practicalxml.XmlUtil;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.EnumSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.xml.XMLConstants;
+
+import org.w3c.dom.Element;
+
+
+/**
+ * Invoked by {@link BeanOutputDriver} to build a DOM tree from introspected
+ * Java objects.
+ * <p>
+ * Instances of this class are constructed around an <code>Element</code>,
+ * which represents the object to be introspected by {@link BeanOutputDriver}.
+ * This simplifies recursive calls, at the expense of not being able to attach
+ * type information at the top level. See {@link BeanConverter} for the rather
+ * ugly hack to work around this.
+ * <p>
+ * This class is not threadsafe (but then, neither are the JDK's DOM classes).
+ */
+public class BeanOutputHandlerImpl
+implements BeanOutputHandler
+{
+ private EnumSet<BeanOutputOptions> _options;
+ private Appender _appender;
+
+
+ /**
+ * Public constructor, which uses default appender and allows individual
+ * options specifiers.
+ */
+ public BeanOutputHandlerImpl(Element appendTo, BeanOutputOptions... options)
+ {
+ _appender = new Appender(appendTo);
+ _options = EnumSet.noneOf(BeanOutputOptions.class);
+ for (BeanOutputOptions option : options)
+ _options.add(option);
+ }
+
+
+ /**
+ * Internal constructor, used by container classes for recursive calls.
+ */
+ protected BeanOutputHandlerImpl(BeanOutputHandlerImpl parent, Appender appender)
+ {
+ _appender = appender;
+ _options = parent._options;
+ }
+
+
+//----------------------------------------------------------------------------
+// OutputHandler implementation
+//----------------------------------------------------------------------------
+
+ public void convert(String name, Boolean value)
+ {
+ Object formatted = value;
+ if (shouldFormatAsXsd())
+ formatted = value.booleanValue() ? "true" : "false";
+
+ _appender.append(name, "xsd:boolean", formatted);
+ }
+
+
+ public void convert(String name, Byte value)
+ {
+ _appender.append(name, "xsd:byte", value);
+ }
+
+
+ public void convert(String name, Character value)
+ {
+ _appender.append(name, "xsd:string", value);
+ }
+
+
+ public void convert(String name, Date value)
+ {
+ Object formatted = value;
+ if (shouldFormatAsXsd())
+ formatted = XmlUtil.formatXsdDatetime(value);
+
+ _appender.append(name, "xsd:dateTime", formatted);
+ }
+
+
+ public void convert(String name, Double value)
+ {
+ Object formatted = value;
+ if (shouldFormatAsXsd())
+ formatted = XmlUtil.formatXsdDecimal(value);
+
+ _appender.append(name, "xsd:decimal", formatted);
+ }
+
+
+ public void convert(String name, Float value)
+ {
+ Object formatted = value;
+ if (shouldFormatAsXsd())
+ formatted = XmlUtil.formatXsdDecimal(value);
+
+ _appender.append(name, "xsd:decimal", formatted);
+ }
+
+
+ public void convert(String name, Integer value)
+ {
+ _appender.append(name, "xsd:int", value);
+ }
+
+
+ public void convert(String name, Long value)
+ {
+ _appender.append(name, "xsd:long", value);
+ }
+
+
+ public void convert(String name, Number value)
+ {
+ _appender.append(name, "xsd:decimal", value);
+ }
+
+
+ public void convert(String name, Short value)
+ {
+ _appender.append(name, "xsd:short", value);
+ }
+
+
+ public void convert(String name, String value)
+ {
+ _appender.append(name, "xsd:string", value);
+ }
+
+
+ public void convert(String name, List<?> value)
+ {
+ Element container = _appender.append(name, javaXsiType(value), null);
+ appendSequence(container, value.iterator(), true);
+ }
+
+
+ public void convert(String name, Set<?> value)
+ {
+ Element container = _appender.append(name, javaXsiType(value), null);
+ appendSequence(container, value.iterator(), false);
+ }
+
+
+ public void convert(String name, Map<?,?> map)
+ {
+ Element container = _appender.append(name, javaXsiType(map), null);
+ BeanOutputHandlerImpl childHandler = new BeanOutputHandlerImpl(this, new MapAppender(container));
+ BeanOutputDriver.introspect(map, childHandler);
+ }
+
+
+ public void convert(String name, Object value)
+ {
+ Element container = _appender.append(name, javaXsiType(value), null);
+ Appender childAppender = value.getClass().isArray()
+ ? new IndexedAppender(container)
+ : new Appender(container);
+ BeanOutputHandlerImpl childHandler = new BeanOutputHandlerImpl(this, childAppender);
+ BeanOutputDriver.introspect(value, childHandler);
+ }
+
+
+ public void convert(String name, Object[] array)
+ {
+ Element container = _appender.append(name, javaXsiType(array), null);
+ appendSequence(container, Arrays.asList(array).iterator(), true);
+ }
+
+
+ public void convertNull(String name)
+ {
+ if (!_options.contains(BeanOutputOptions.XSI_NIL))
+ return;
+
+ Element child = _appender.append(name, "", null);
+ child.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:nil", "true");
+ }
+
+
+//----------------------------------------------------------------------------
+// Internals
+//----------------------------------------------------------------------------
+
+ protected boolean shouldFormatAsXsd()
+ {
+ return _options.contains(BeanOutputOptions.XSD_FORMAT)
+ || _options.contains(BeanOutputOptions.XSI_TYPE);
+ }
+
+
+ /**
+ * Returns the <code>xsi:type</code> value for a Java object (should not
+ * be called for primitive wrappers, as these are covered by "xsd" types).
+ */
+ private String javaXsiType(Object obj)
+ {
+ return "java:" + obj.getClass().getName();
+ }
+
+
+ /**
+ * Common code for sequenced objects -- arrays, lists, and sets.
+ */
+ private void appendSequence(Element parent, Iterator<?> itx, boolean isIndexed)
+ {
+ Appender childAppender = isIndexed ? new IndexedAppender(parent)
+ : new Appender(parent);
+ BeanOutputHandlerImpl childHandler = new BeanOutputHandlerImpl(this, childAppender);
+ while (itx.hasNext())
+ BeanOutputDriver.dispatch("data", itx.next(), childHandler);
+ }
+
+
+ /**
+ * This class is responsible for adding nodes to the DOM tree. Each
+ * instance of <code>XmlOutputHandler</code> is constructed around one
+ * of these (or a subclass). Subclasses may override the {@link #append}
+ * method to add additional information to the appended nodes.
+ * <p>
+ * A little bit of weirdness: as an inner class, this is constructed in
+ * the context of an <code>XmlOutputHandler</code>, and has access to
+ * members defined by that handler. However, in a recursive call, it is
+ * actually held and used by a different handler. Since the second handler
+ * copies members of the first, I don't see this as a problem ... until
+ * some member doesn't get copied.
+ */
+ private class Appender
+ {
+ private Element _appendTo;
+
+ public Appender(Element appendTo)
+ {
+ _appendTo = appendTo;
+ }
+
+ /**
+ * Appends a child to the element we manage.
+ *
+ * @param name Local-name for the new element. As long as you're
+ * dealing with Java, property names and element names
+ * are interchangeable. If handling a <code>Map</code>,
+ * you'll need to ensure that keys are appropriate XML
+ * element names.
+ * @param xsiType The value to insert in an <code>xsi:type</code>
+ * attribute. This is required, even if the output-type
+ * option isn't turned on.
+ * @param value If not-null, will be stringified and attached to the
+ * element as a text node. Apply any fancy formatting
+ * before coming here. If null, will be ignored.
+ */
+ public Element append(String name, String xsiType, Object value)
+ {
+ if ((name == null) || (name.length() == 0))
+ name = "data";
+
+ Element child = DomUtil.appendChildInheritNamespace(_appendTo, name);
+
+ if (_options.contains(BeanOutputOptions.XSI_TYPE))
+ child.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:type", xsiType);
+
+ if (value != null)
+ child.setTextContent(String.valueOf(value));
+
+ return child;
+ }
+ }
+
+
+ /**
+ * An appender for array/list processing, that attaches an index attribute
+ * to the appended node.
+ */
+ private class IndexedAppender
+ extends Appender
+ {
+ int _index = 0;
+
+ public IndexedAppender(Element appendTo)
+ {
+ super(appendTo);
+ }
+
+ @Override
+ public Element append(String name, String xsiType, Object value)
+ {
+ Element child = super.append(name, xsiType, value);
+ child.setAttribute("index", String.valueOf(_index++));
+ return child;
+ }
+ }
+
+
+ /**
+ * An appender for maps, which either uses the passed name as the
+ * element name, or as the value of a "key" attribute.
+ */
+ private class MapAppender
+ extends Appender
+ {
+ public MapAppender(Element appendTo)
+ {
+ super(appendTo);
+ }
+
+ @Override
+ public Element append(String name, String xsiType, Object value)
+ {
+ Element child = null;
+ if (_options.contains(BeanOutputOptions.INTROSPECT_MAPS))
+ {
+ child = super.append(name, xsiType, value);
+ }
+ else
+ {
+ child = super.append("data", xsiType, value);
+ child.setAttribute("key", name);
+ }
+ return child;
+ }
+ }
+}
Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputOptions.java
===================================================================
--- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputOptions.java 2009-07-15 19:31:56 UTC (rev 89)
+++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/BeanOutputOptions.java 2009-07-16 15:15:28 UTC (rev 90)
@@ -2,7 +2,7 @@
package net.sf.practicalxml.converter;
/**
- * Options used by {@link BeanOutputHandler} to control the structure of the
+ * Options used by {@link BeanOutputHandlerImpl} to control the structure of the
* DOM tree.
*/
public enum BeanOutputOptions
Modified: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanConverter.java
===================================================================
--- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanConverter.java 2009-07-15 19:31:56 UTC (rev 89)
+++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanConverter.java 2009-07-16 15:15:28 UTC (rev 90)
@@ -25,7 +25,7 @@
/**
* Tests for the top-level <code>BeanConverter</code> methods. These tend to
- * be minimal; the detailed testing happens in {@link TestBeanOutputHandler}
+ * be minimal; the detailed testing happens in {@link TestBeanOutputHandlerImpl}
* and {@link TestBeanInputHandler}.
*/
public class TestBeanConverter
Deleted: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandler.java
===================================================================
--- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandler.java 2009-07-15 19:31:56 UTC (rev 89)
+++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandler.java 2009-07-16 15:15:28 UTC (rev 90)
@@ -1,574 +0,0 @@
-// Copyright 2008-2009 severally by the contributors
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package net.sf.practicalxml.converter;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
-import java.util.TreeSet;
-import javax.xml.XMLConstants;
-
-import org.w3c.dom.Element;
-
-import junit.framework.TestCase;
-
-import net.sf.practicalxml.DomUtil;
-import net.sf.practicalxml.OutputUtil;
-import net.sf.practicalxml.junit.DomAsserts;
-import net.sf.practicalxml.xpath.XPathWrapper;
-
-
-public class TestBeanOutputHandler
-extends TestCase
-{
-//----------------------------------------------------------------------------
-// Test Data / Classes
-//----------------------------------------------------------------------------
-
- /**
- * An array of simple value types, along with their XSD type name and
- * XSD-formatted value.
- */
- private final static Object[][] SIMPLE_VALUES = new Object[][]
- {
- new Object[] { Byte.valueOf((byte)123), "xsd:byte", "123" },
- new Object[] { Short.valueOf((short)4567), "xsd:short", "4567" },
- new Object[] { Integer.valueOf(12345678), "xsd:int", "12345678" },
- new Object[] { Long.valueOf(12345678901234L), "xsd:long", "12345678901234" },
- new Object[] { Float.valueOf((float)1234), "xsd:decimal", "1234.0" },
- new Object[] { Double.valueOf(1234567890.5), "xsd:decimal", "1234567890.5" },
- new Object[] { Boolean.TRUE, "xsd:boolean", "true" },
- new Object[] { Boolean.FALSE, "xsd:boolean", "false" },
- new Object[] { Character.valueOf('A'), "xsd:string", "A" },
- new Object[] { "this is a test", "xsd:string", "this is a test" },
- new Object[] { new Date(1247551703704L), "xsd:dateTime", "2009-07-14T06:08:23" },
- new Object[] { new BigInteger("123456789012345"), "xsd:decimal", "123456789012345" },
- new Object[] { new BigDecimal("123456789012345.123456789012345"), "xsd:decimal", "123456789012345.123456789012345" }
- };
-
-
- // has to be public so that we can introspect it
- public static class SimpleBean
- {
- private String _sval;
- private int _ival;
- private BigDecimal _dval;
- private boolean _bval;
-
- public SimpleBean()
- {
- // nothign to see here
- }
-
- public SimpleBean(String sval, int ival, BigDecimal dval, boolean bval)
- {
- _sval = sval;
- _ival = ival;
- _dval = dval;
- _bval = bval;
- }
-
- public String getSval() { return _sval; }
- public void setSval(String sval) { _sval = sval; }
-
- public int getIval() { return _ival; }
- public void setIval(int ival) { _ival = ival; }
-
- public BigDecimal getDval() { return _dval; }
- public void setDval(BigDecimal dval) { _dval = dval; }
-
- public boolean isBval() { return _bval; }
- public void setBval(boolean bval) { _bval = bval; }
- }
-
-
- public static class CompoundBean
- {
- private SimpleBean _simple;
- private int[] _primArray;
- private List<String> _stringList;
-
- public CompoundBean()
- {
- // nothing here
- }
-
- public CompoundBean(SimpleBean simple, int[] primArray, List<String> stringList)
- {
- super();
- _simple = simple;
- _primArray = primArray;
- _stringList = stringList;
- }
-
- public SimpleBean getSimple() { return _simple; }
- public void setSimple(SimpleBean simple) { _simple = simple; }
-
- public int[] getPrimArray() { return _primArray; }
- public void setPrimArray(int[] primArray) { _primArray = primArray; }
-
- public List<String> getStringList() { return _stringList; }
- public void setStringList(List<String> list) { _stringList = list; }
- }
-
-
-//----------------------------------------------------------------------------
-// Support Code
-//----------------------------------------------------------------------------
-
- /**
- * Asserts that an element has the expected number of children (verifies
- * that we didn't append to the wrong element).
- */
- private void assertChildCount(String message, Element parent, int expected)
- {
- List<Element> children = DomUtil.getChildren(parent);
- assertEquals(message + " child count:", expected, children.size());
- }
-
-
- /**
- * Asserts the name, type, and value of an element. If any of the expected
- * values are null, the associated test isn't performed.
- */
- private void assertNameTypeValue(
- String message, Element elem,
- String expectedName, String expectedType, String expectedValue)
- {
- if (expectedName != null)
- assertEquals(message + " name:", expectedName,
- elem.getNodeName());
- if (expectedType != null)
- assertEquals(message + " type:", expectedType,
- elem.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"));
- if (expectedValue != null)
- assertEquals(message + " value:", expectedValue,
- DomUtil.getText(elem));
- }
-
-
- /**
- * Asserts the name and type of a compound element, where the type is a Java classname.
- */
- private void assertContainerNameAndType(Element elem, String expectedName, Class<?> expectedType)
- {
- assertNameTypeValue("container", elem, expectedName, "java:" + expectedType.getName(), null);
- }
-
-
- /**
- * A variety of assertions about added children. Assumes that we'll add
- * a single child node per parent, and that we aren't using namespaces.
- */
- private void assertAppend(String message, Element parent,
- String expectedName,
- String expectedType,
- String expectedValue)
- {
- assertChildCount(message, parent, 1);
- assertNameTypeValue(message, DomUtil.getChildren(parent).get(0),
- expectedName, expectedType, expectedValue);
- }
-
-
-//----------------------------------------------------------------------------
-// Test Cases
-//----------------------------------------------------------------------------
-
- // this test is essentially repeated in the DispatchSimpleValues tests
- // it exists here as an initial verification of appended element structure
- public void testDispatch() throws Exception
- {
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", "bar", new BeanOutputHandler(root));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- assertAppend("", root, "foo", "", "bar");
- }
-
-
- public void testDispachWithNullName() throws Exception
- {
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch(null, "bar", new BeanOutputHandler(root));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- assertAppend("", root, "data", "", "bar");
- }
-
-
- public void testNamespaceInheritance() throws Exception
- {
- Element root = DomUtil.newDocument("urn:zippy", "argle:bargle");
- BeanOutputDriver.dispatch("foo", "bar", new BeanOutputHandler(root));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- List<Element> children = DomUtil.getChildren(root);
- assertEquals("child count", 1, children.size());
-
- Element child = children.get(0);
- assertEquals("urn:zippy", child.getNamespaceURI());
- assertEquals("argle", child.getPrefix());
- assertEquals("foo", child.getLocalName());
- }
-
-
- public void testDispatchNullValue() throws Exception
- {
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", null, new BeanOutputHandler(root));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- List<Element> children1 = DomUtil.getChildren(root);
- assertEquals("null element added", 0, children1.size());
-
- BeanOutputDriver.dispatch("foo", null, new BeanOutputHandler(root, BeanOutputOptions.XSI_NIL));
- List<Element> children2 = DomUtil.getChildren(root);
- assertEquals("null element added", 1, children2.size());
- assertEquals("nil flag", "true",
- children2.get(0).getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil"));
- }
-
-
-
- public void testDispatchSimpleValuesWithDefaults() throws Exception
- {
- Element root = DomUtil.newDocument("test");
- for (int ii = 0 ; ii < SIMPLE_VALUES.length ; ii++)
- {
- Object value = SIMPLE_VALUES[ii][0];
- Element parent = DomUtil.appendChild(root, "value");
- BeanOutputDriver.dispatch("foo", value, new BeanOutputHandler(parent));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
- assertAppend("value " + ii, parent, "foo", "", String.valueOf(value));
- }
- }
-
-
- public void testDispatchSimpleValuesWithXSDFormatting() throws Exception
- {
- Element root = DomUtil.newDocument("test");
- for (int ii = 0 ; ii < SIMPLE_VALUES.length ; ii++)
- {
- Element parent = DomUtil.appendChild(root, "value");
- BeanOutputDriver.dispatch("foo", SIMPLE_VALUES[ii][0],
- new BeanOutputHandler(parent, BeanOutputOptions.XSD_FORMAT));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
- assertAppend("value " + ii, parent, "foo", "", (String)SIMPLE_VALUES[ii][2]);
- }
- }
-
-
- public void testDispatchSimpleValuesWithXSIType() throws Exception
- {
- Element root = DomUtil.newDocument("test");
- for (int ii = 0 ; ii < SIMPLE_VALUES.length ; ii++)
- {
- Element parent = DomUtil.appendChild(root, "value");
- BeanOutputDriver.dispatch("foo", SIMPLE_VALUES[ii][0],
- new BeanOutputHandler(parent, BeanOutputOptions.XSI_TYPE));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
- assertAppend("value " + ii, parent, "foo", (String)SIMPLE_VALUES[ii][1], (String)SIMPLE_VALUES[ii][2]);
- }
- }
-
-
- public void testDispatchObjectArray() throws Exception
- {
- Object[] data = new String[] { "argle", "bargle" };
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", data, new BeanOutputHandler(root));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- assertAppend("container", root, "foo", "", null);
-
- Element container = DomUtil.getChild(root, "foo");
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 2, children.size());
- assertNameTypeValue("child 0", children.get(0), "data", "", "argle");
- assertNameTypeValue("child 1", children.get(1), "data", "", "bargle");
-
- DomAsserts.assertEquals("child 1 index", "0", root, "/test/foo/data[1]/@index");
- DomAsserts.assertEquals("child 2 index", "1", root, "/test/foo/data[2]/@index");
- }
-
-
- public void testDispatchObjectArrayWithType() throws Exception
- {
- Object[] data = new String[] { "argle", "bargle" };
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", data,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", data.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 2, children.size());
- assertNameTypeValue("child 0", children.get(0), "data", "xsd:string", "argle");
- assertNameTypeValue("child 1", children.get(1), "data", "xsd:string", "bargle");
-
- DomAsserts.assertEquals("child 1 index", "0", root, "/test/foo/data[1]/@index");
- DomAsserts.assertEquals("child 2 index", "1", root, "/test/foo/data[2]/@index");
- }
-
-
- public void testDispatchPrimitiveArrayWithType() throws Exception
- {
- int[] data = new int[] { 1, 2, 3 };
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", data,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", data.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 3, children.size());
- assertNameTypeValue("child 1", children.get(0), "data", "xsd:int", "1");
- assertNameTypeValue("child 2", children.get(1), "data", "xsd:int", "2");
- assertNameTypeValue("child 3", children.get(2), "data", "xsd:int", "3");
-
- DomAsserts.assertEquals("child 1 index", "0", root, "/test/foo/data[1]/@index");
- DomAsserts.assertEquals("child 2 index", "1", root, "/test/foo/data[2]/@index");
- DomAsserts.assertEquals("child 3 index", "2", root, "/test/foo/data[3]/@index");
- }
-
-
- public void testDispatchListWithType() throws Exception
- {
- List<String> data = new ArrayList<String>();
- data.add("argle");
- data.add("bargle");
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", data,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", data.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 2, children.size());
- assertNameTypeValue("child 0", children.get(0), "data", "xsd:string", "argle");
- assertNameTypeValue("child 1", children.get(1), "data", "xsd:string", "bargle");
-
- DomAsserts.assertEquals("child 1 index", "0", root, "/test/foo/data[1]/@index");
- DomAsserts.assertEquals("child 2 index", "1", root, "/test/foo/data[2]/@index");
- }
-
-
- public void testDispatchSetWithType() throws Exception
- {
- // use TreeSet so that order is guaranteed
- Set<String> data = new TreeSet<String>();
- data.add("bargle");
- data.add("argle");
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", data,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", data.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 2, children.size());
- assertNameTypeValue("child 0", children.get(0), "data", "xsd:string", "argle");
- assertNameTypeValue("child 1", children.get(1), "data", "xsd:string", "bargle");
-
- DomAsserts.assertEquals("child 1 index", "", root, "/test/foo/data[1]/@index");
- DomAsserts.assertEquals("child 2 index", "", root, "/test/foo/data[2]/@index");
- }
-
-
- public void testDispatchMapWithDefaultFormat() throws Exception
- {
- // use TreeMap so that order is guaranteed
- Map<String,Object> data = new TreeMap<String,Object>();
- data.put("foo", Integer.valueOf(123));
- data.put("argle", "bargle");
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", data,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", data.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 2, children.size());
- assertNameTypeValue("child 0", children.get(0), "data", "xsd:string", "bargle");
- assertNameTypeValue("child 1", children.get(1), "data", "xsd:int", "123");
-
- DomAsserts.assertEquals("child 1 key", "argle", root, "/test/foo/data[1]/@key");
- DomAsserts.assertEquals("child 2 key", "foo", root, "/test/foo/data[2]/@key");
- }
-
-
- public void testDispatchMapWithIntrospectFormat() throws Exception
- {
- // use TreeMap so that order is guaranteed
- Map<String,Object> data = new TreeMap<String,Object>();
- data.put("foo", Integer.valueOf(123));
- data.put("argle", "bargle");
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", data,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE,
- BeanOutputOptions.INTROSPECT_MAPS));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", data.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 2, children.size());
- assertNameTypeValue("child 0", children.get(0), "argle", "xsd:string", "bargle");
- assertNameTypeValue("child 1", children.get(1), "foo", "xsd:int", "123");
-
- DomAsserts.assertEquals("child 1 key", "", root, "/test/foo/argle/@key");
- DomAsserts.assertEquals("child 2 key", "", root, "/test/foo/foo/@key");
- }
-
-
- public void testDispatchSimpleBean() throws Exception
- {
- SimpleBean bean = new SimpleBean("zippy", 123, new BigDecimal("456.78"), true);
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", bean,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", bean.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 4, children.size());
-
- Element child1 = (Element)(new XPathWrapper("//sval").evaluate(root).get(0));
- assertNameTypeValue("sval", child1, "sval", "xsd:string", "zippy");
-
- Element child2 = (Element)(new XPathWrapper("//ival").evaluate(root).get(0));
- assertNameTypeValue("ival", child2, "ival", "xsd:int", "123");
-
- Element child3 = (Element)(new XPathWrapper("//dval").evaluate(root).get(0));
- assertNameTypeValue("dval", child3, "dval", "xsd:decimal", "456.78");
-
- Element child4 = (Element)(new XPathWrapper("//bval").evaluate(root).get(0));
- assertNameTypeValue("bval", child4, "bval", "xsd:boolean", "true");
- }
-
-
- public void testDispatchSimpleBeanIncludingNulls() throws Exception
- {
- SimpleBean bean = new SimpleBean();
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", bean,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE,
- BeanOutputOptions.XSI_NIL));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", bean.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 4, children.size());
-
- Element child1 = (Element)(new XPathWrapper("//sval").evaluate(root).get(0));
- assertEquals("sval nil", "true", child1.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil"));
-
- Element child2 = (Element)(new XPathWrapper("//ival").evaluate(root).get(0));
- assertNameTypeValue("ival", child2, "ival", "xsd:int", "0");
-
- Element child3 = (Element)(new XPathWrapper("//dval").evaluate(root).get(0));
- assertEquals("dval nil", "true", child3.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil"));
-
- Element child4 = (Element)(new XPathWrapper("//bval").evaluate(root).get(0));
- assertNameTypeValue("bval", child4, "bval", "xsd:boolean", "false");
- }
-
-
- public void testDispatchSimpleBeanIgnoringNulls() throws Exception
- {
- SimpleBean bean = new SimpleBean();
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", bean,
- new BeanOutputHandler(root, BeanOutputOptions.XSI_TYPE));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- Element container = DomUtil.getChild(root, "foo");
- assertContainerNameAndType(container, "foo", bean.getClass());
-
- List<Element> children = DomUtil.getChildren(container);
- assertEquals("child count", 2, children.size());
-
- Element child1 = (Element)(new XPathWrapper("//ival").evaluate(root).get(0));
- assertNameTypeValue("ival", child1, "ival", "xsd:int", "0");
-
- Element child2 = (Element)(new XPathWrapper("//bval").evaluate(root).get(0));
- assertNameTypeValue("bval", child2, "bval", "xsd:boolean", "false");
- }
-
-
- public void testDispatchCompoundBean() throws Exception
- {
- CompoundBean bean = new CompoundBean(
- new SimpleBean("zippy", 123, new BigDecimal("456.78"), true),
- new int[] { 1, 2, 3 },
- Arrays.asList("foo", "bar", "baz"));
-
- // at this point, I'm convinced the type output works, so we'll do default
- // output and then use XPath for all assertions
-
- Element root = DomUtil.newDocument("test");
- BeanOutputDriver.dispatch("foo", bean, new BeanOutputHandler(root));
-// System.out.println(OutputUtil.compactString(root.getOwnerDocument()));
-
- DomAsserts.assertEquals("zippy", root, "/test/foo/simple/sval");
- DomAsserts.assertEquals("123", root, "/test/foo/simple/ival");
- DomAsserts.assertEquals("456.78", root, "/test/foo/simple/dval");
- DomAsserts.assertEquals("true", root, "/test/foo/simple/bval");
-
- DomAsserts.assertEquals("1", root, "/test/foo/primArray/data[1]");
- DomAsserts.assertEquals("2", root, "/test/foo/primArray/data[2]");
- DomAsserts.assertEquals("3", root, "/test/foo/primArray/data[3]");
-
- DomAsserts.assertEquals("0", root, "/test/foo/primArray/data[1]/@index");
- DomAsserts.assertEquals("1", root, "/test/foo/primArray/data[2]/@index");
- DomAsserts.assertEquals("2", root, "/test/foo/primArray/data[3]/@index");
-
- DomAsserts.assertEquals("foo", root, "/test/foo/stringList/data[1]");
- DomAsserts.assertEquals("bar", root, "/test/foo/stringList/data[2]");
- DomAsserts.assertEquals("baz", root, "/test/foo/stringList/data[3]");
- }
-}
Copied: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandlerImpl.java (from rev 89, branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandler.java)
===================================================================
--- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandlerImpl.java (rev 0)
+++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestBeanOutputHandlerImpl.java 2009-07-16 15:15:28 UTC (rev 90)
@@ -0,0 +1,574 @@
+// Copyright 2008-2009 severally by the contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package net.sf.practicalxml.converter;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import javax.xml.XMLConstants;
+
+import org.w3c.dom.Element;
+
+import junit.framework.TestCase;
+
+import net.sf.practicalxml.DomUtil;
+import net.sf.practicalxml.OutputUtil;
+import net.sf.practicalxml.junit.DomAsserts;
+import net.sf.practicalxml.xpath.XPathWrapper;
+
+
+public class TestBeanOutputHandlerImpl
+extends TestCase
+{
+//----------------------------------------------------------------------------
+// Test Data / Classes
+//----------------------------------------------------------------------------
+
+ /**
+ * An array of simple value types, along with their XSD type name and
+ * XSD-formatted value.
+ */
+ private final static Object[][] SIMPLE_VALUES = new Object[][]
+ {
+ new Object[] { Byte.valueOf((byte)123), "xsd:byte", "123" },
+ new Object[] { Short.valueOf((short)4567), "xsd:short", "4567" },
+ new Object[] { Integer.valueOf(12345678), "xsd:int", "12345678" },
+ new Object[] { Long.valueOf(12345678901234L), "xsd:long", "12345678901234" },
+ new Object[] { Float.valueOf((float)1234), "xsd:decimal", "1234.0" },
+ new Object[] { Double.valueOf(1234567890.5), "xsd:decimal", "1234567890.5" },
+ new Object[] { Boolean.TRUE, "xsd:boolean", "true" },
+ new Object[] { Boolean.FALSE, "xsd:boolean", "false" },
+ new Object[] { Character.valueOf('A'), "xsd:string", "A" },
+ new Object[] { "this is a test", "xsd:string", "this is a test" },
+ new Object[] { new Date(1247551703704L), "xsd:dateTime", "2009-07-14T06:08:23" },
+ new Object[] { new BigInteger("123456789012345"), "xsd:decimal", "123456789012345" },
+ new Object[] { new BigDecimal("123456789012345.123456789012345"), "xsd:decimal", "123456789012345.123456789012345" }
+ };
+
+
+ // has to be public so that we can introspect it
+ public static class SimpleBean
+ {
+ private String _sval;
+ private int _ival;
+ private BigDecimal _dval;
+ private boolean _bval;
+
+ public SimpleBean()
+ {
+ // nothign to see here
+ }
+
+ public SimpleBean(String sval, int ival, BigDecimal dval, boolean bval)
+ {
+ _sval = sval;
+ _ival = ival;
+ _dval = dval;
+ _bval = bval;
+ }
+
+ public String getSval() { return _sval; }
+ public void setSval(String sval) { _sval = sval; }
+
+ public int getIval() { return _ival; }
+ public void setIval(int ival) { _ival = ival; }
+
+ public BigDecimal getDval() { return _dval; }
+ public void setDval(BigDecimal dval) { _dval = dval; }
+
+ public boolean isBval() { return _bval; }
+ public void setBval(boolean bval) { _bval = bval; }
+ }
+
+
+ public static class CompoundBean
+ {
+ private SimpleBean _simple;
+ private int[] _primArray;
+ private List<String> _stringList;
+
+ public CompoundBean()
+ {
+ // nothing here
+ }
+
+ public CompoundBean(SimpleBean simple, int[] primArray, List<String> stringList)
+ {
+ super();
+ _simple = simple;
+ _primArray = primArray;
+ _stringList = stringList;
+ }
+
+ public SimpleBean getSimple() { return _simple; }
+ public void setSimple(SimpleBean simple) { _simple = simple; }
+
+ public int[] getPrimArray() { return _primArray; }
+ public void setPrimArray(int[] primArray) { _primArray = primArray; }
+
+ public List<String> getStringList() { return _stringList; }
+ public void setStringList(List<String> list) { _stringList = list; }
+ }
+
+
+//----------------------------------------------------------------------------
+// Support Code
+//----------------------------------------------------------------------------
+
+ /**
+ * Asserts that an element has the expected number of children (verifies
+ * that we didn't append to the wrong element).
+ */
+ private void assertChildCount(String message, Element parent, int expected)
+ {
+ List<Element> children = DomUtil.getChildren(parent);
+ assertEquals(message + " child count:", expected, children.size());
+ }
+
+
+ /**
+ * Asserts the name, type, and value of an element. If any of the expected
+ * values are null, the associated test isn't performed.
+ */
+ private void assertNameTypeValue(
+ String message, Element elem,
+ String expectedName, String expectedType, String expectedValue)
+ {
+ if (expectedName != null)
+ assertEquals(message + " name:", expectedName,
+ elem.getNodeName());
+ if (expectedType != null)
+ assertEquals(message + " type:", expectedType,
+ elem.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"));
+ if (expectedValue != null)
+ assertEquals(message + " value:", expectedValue,
+ DomUtil.getText(elem));
+ }
+
+
+ /**
+ * Asserts the name and type of a compound element, where the type is a Java classname.
+ */
+ private void assertContainerNameAndType(Element elem, String expectedName, Class<?> expectedType)
+ {
+ assertNameTypeValue("container", elem, expectedName, "java:" + expectedType.getName(), null);
+ }
+
+
+ /**
+ * A variety of assertions about added children. Assumes that we'll add
+ * a single child node per parent, and that we aren't using namespaces.
+ */
+ private void assertAppend(String message, Element parent,
+ String expectedName,
+ String expectedType,
+ String expectedValue)
+ {
+ assertChildCount(message, parent, 1);
+ assertNameTypeValue(message, Do...
[truncated message content] |