You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2005/02/17 21:53:53 UTC
svn commit: r154190 - in jakarta/commons/proper/betwixt/trunk/src:
java/org/apache/commons/betwixt/ java/org/apache/commons/betwixt/expression/
java/org/apache/commons/betwixt/io/ java/org/apache/commons/betwixt/io/read/
java/org/apache/commons/betwixt/strategy/
test/org/apache/commons/betwixt/recursion/
test/org/apache/commons/betwixt/strategy/
Author: rdonkin
Date: Thu Feb 17 12:53:49 2005
New Revision: 154190
URL: http://svn.apache.org/viewcvs?view=rev&rev=154190
Log:
Added strategy for id storage. Contributed by Christian Aust.
Added:
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/IdStoringStrategy.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/ElementsList.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java
Modified:
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/BindingConfiguration.java
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ActionMappingStrategy.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/recursion/TestRecursion.java
Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/BindingConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/BindingConfiguration.java?view=diff&r1=154189&r2=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/BindingConfiguration.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/BindingConfiguration.java Thu Feb 17 12:53:49 2005
@@ -18,6 +18,7 @@
import java.io.Serializable;
import org.apache.commons.betwixt.strategy.DefaultObjectStringConverter;
+import org.apache.commons.betwixt.strategy.IdStoringStrategy;
import org.apache.commons.betwixt.strategy.ObjectStringConverter;
import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
@@ -48,6 +49,8 @@
private String classNameAttribute = "className";
/** Strategy for suppressing attributes with certain values when writing */
private ValueSuppressionStrategy valueSuppressionStrategy = ValueSuppressionStrategy.DEFAULT;
+ /** Strategy for storing and accessing ID values */
+ private IdStoringStrategy idStoringStrategy = IdStoringStrategy.DEFAULT;
/**
* Constructs a BindingConfiguration with default properties.
@@ -147,5 +150,24 @@
public void setValueSuppressionStrategy(
ValueSuppressionStrategy valueSuppressionStrategy) {
this.valueSuppressionStrategy = valueSuppressionStrategy;
+ }
+
+ /**
+ * Gets the strategy used to manage storage and retrieval of id's.
+ *
+ * @return Returns the idStoringStrategy, not null
+ */
+ public IdStoringStrategy getIdMappingStrategy() {
+ return idStoringStrategy;
+ }
+
+ /**
+ * Sets the strategy used to manage storage and retrieval of id's.
+ *
+ * @param idStoringStrategy
+ * The idStoringStrategy to set, not null
+ */
+ public void setIdMappingStrategy(IdStoringStrategy idMappingStrategy) {
+ this.idStoringStrategy = idMappingStrategy;
}
}
Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java?view=diff&r1=154189&r2=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/Context.java Thu Feb 17 12:53:49 2005
@@ -19,6 +19,7 @@
import java.util.Map;
import org.apache.commons.betwixt.BindingConfiguration;
+import org.apache.commons.betwixt.strategy.IdStoringStrategy;
import org.apache.commons.betwixt.strategy.ObjectStringConverter;
import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
import org.apache.commons.logging.Log;
@@ -269,4 +270,14 @@
ValueSuppressionStrategy valueSuppressionStrategy) {
bindingConfiguration.setValueSuppressionStrategy(valueSuppressionStrategy);
}
+
+ /**
+ * Gets the strategy used to manage storage and retrieval of id's.
+ *
+ * @return Returns the idStoringStrategy, not null
+ */
+ public IdStoringStrategy getIdMappingStrategy() {
+ return bindingConfiguration.getIdMappingStrategy();
+ }
+
}
Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java?view=diff&r1=154189&r2=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java Thu Feb 17 12:53:49 2005
@@ -19,7 +19,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.betwixt.AttributeDescriptor;
@@ -92,8 +91,6 @@
/** Log used for logging (Doh!) */
private Log log = LogFactory.getLog( AbstractBeanWriter.class );
- /** Map containing ID attribute values for beans */
- private HashMap idMap = new HashMap();
/** Stack containing beans - used to detect cycles */
private ArrayStack beanStack = new ArrayStack();
/** Used to generate ID attribute values*/
@@ -345,7 +342,7 @@
} else {
pushBean ( context.getBean() );
if ( getBindingConfiguration().getMapIDs() ) {
- ref = (String) idMap.get( context.getBean() );
+ ref = getBindingConfiguration().getIdMappingStrategy().getReferenceFor(context, context.getBean());
}
if ( ref == null ) {
// this is the first time that this bean has be written
@@ -353,7 +350,7 @@
if (idAttribute == null) {
// use a generated id
id = idGenerator.nextId();
- idMap.put( bean, id );
+ getBindingConfiguration().getIdMappingStrategy().setReference(context, bean, id);
if ( getBindingConfiguration().getMapIDs() ) {
// write element with id
@@ -396,7 +393,7 @@
// convert to string
id = exp.toString();
}
- idMap.put( bean, id);
+ getBindingConfiguration().getIdMappingStrategy().setReference(context, bean, id);
// the ID attribute should be written automatically
writeElement(
Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java?view=diff&r1=154189&r2=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/read/ReadContext.java Thu Feb 17 12:53:49 2005
@@ -16,7 +16,6 @@
package org.apache.commons.betwixt.io.read;
import java.beans.IntrospectionException;
-import java.util.HashMap;
import org.apache.commons.betwixt.AttributeDescriptor;
import org.apache.commons.betwixt.BindingConfiguration;
@@ -49,9 +48,7 @@
* @since 0.5
*/
public class ReadContext extends Context {
-
- /** Beans indexed by ID strings */
- private HashMap beansById = new HashMap();
+;
/** Classloader to be used to load beans during reading */
private ClassLoader classLoader;
/** The read specific configuration */
@@ -117,7 +114,6 @@
*/
public ReadContext(ReadContext readContext) {
super(readContext);
- beansById = readContext.beansById;
classLoader = readContext.classLoader;
readConfiguration = readContext.readConfiguration;
}
@@ -129,7 +125,7 @@
* @param bean the Object to store, not null
*/
public void putBean(String id, Object bean) {
- beansById.put(id, bean);
+ getIdMappingStrategy().setReference(this, bean, id);
}
/**
@@ -139,14 +135,14 @@
* @return the Object that the ID references, otherwise null
*/
public Object getBean(String id) {
- return beansById.get(id);
+ return getIdMappingStrategy().getReferenced(this, id);
}
/**
* Clears the beans indexed by id.
*/
public void clearBeans() {
- beansById.clear();
+ getIdMappingStrategy().reset();
}
/**
Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ActionMappingStrategy.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ActionMappingStrategy.java?view=diff&r1=154189&r2=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ActionMappingStrategy.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ActionMappingStrategy.java Thu Feb 17 12:53:49 2005
@@ -19,6 +19,7 @@
import org.apache.commons.betwixt.io.read.MappingAction;
import org.apache.commons.betwixt.io.read.ReadContext;
+import org.apache.commons.betwixt.strategy.DefaultActionMappingStrategy;
import org.xml.sax.Attributes;
/**
Added: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java?view=auto&rev=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/DefaultIdStoringStrategy.java Thu Feb 17 12:53:49 2005
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * 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 org.apache.commons.betwixt.strategy;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.betwixt.expression.Context;
+
+/**
+ * Stores every ID that given to it into an internal <code>HashMap</code> and
+ * returns it on request.
+ *
+ * @author <a href="mailto:christian@wilde-welt.de">Christian Aust </a>
+ * @since 0.6.1
+ */
+public class DefaultIdStoringStrategy extends IdStoringStrategy {
+ private Map idByBeanMap;
+ private Map beanByIdMap;
+
+ /**
+ * Constructs a {@link IdStoringStrategy}using a <code>HashMap</code> for
+ * storage.
+ */
+ public DefaultIdStoringStrategy() {
+ idByBeanMap = new HashMap();
+ beanByIdMap = new HashMap();
+ }
+
+ /**
+ * Returns a String id for the given bean if it has been stored previously.
+ * Otherwise returns null.
+ *
+ * @param context
+ * current context, not null
+ * @param bean
+ * the instance, not null
+ * @return id as String, or null if not found
+ * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#getReferenceFor(org.apache.commons.betwixt.expression.Context,
+ * java.lang.Object)
+ */
+ public String getReferenceFor(Context context, Object bean) {
+ return (String) idByBeanMap.get(bean);
+ }
+
+ /**
+ * Stores an ID for the given instance and context. It will check first if
+ * this ID has been previously stored and will do nothing in that case.
+ *
+ * @param context
+ * current context, not null
+ * @param bean
+ * current instance, not null
+ * @param id
+ * the ID to store
+ * @see org.apache.commons.betwixt.strategy.IdStoringStrategy#setReference(org.apache.commons.betwixt.expression.Context,
+ * java.lang.Object, java.lang.String)
+ */
+ public void setReference(Context context, Object bean, String id) {
+ if (!idByBeanMap.containsKey(bean)) {
+ idByBeanMap.put(bean, id);
+ beanByIdMap.put(id, bean);
+ }
+ }
+
+ /**
+ * Gets an object matching the given reference.
+ * @param context <code>Context</code>, not null
+ * @param id the reference id
+ * @return an bean matching the given reference,
+ * or null if there is no bean matching the given reference
+ */
+ public Object getReferenced(Context context, String id) {
+ return beanByIdMap.get(id);
+ }
+
+ /**
+ * Clears all beans.
+ */
+ public void reset() {
+ idByBeanMap.clear();
+ beanByIdMap.clear();
+ }
+
+
+}
Added: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/IdStoringStrategy.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/IdStoringStrategy.java?view=auto&rev=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/IdStoringStrategy.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/IdStoringStrategy.java Thu Feb 17 12:53:49 2005
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * 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 org.apache.commons.betwixt.strategy;
+
+import org.apache.commons.betwixt.expression.Context;
+
+/**
+ * Pluggable strategy for id storage management.
+ * It is possible to use this strategy for innovative
+ * active storage storage strategies as well as passive ones.
+ * For example, it is possible to have some beans map to
+ * references without ever being fully mapped.
+ *
+ * @author <a href="mailto:christian@wilde-welt.de">Christian Aust </a>
+ * @since 0.6.1
+ */
+public abstract class IdStoringStrategy {
+
+ /**
+ * Default storage strategy
+ *
+ * @see DefaultIdStoringStrategy
+ */
+ public static IdStoringStrategy DEFAULT = new DefaultIdStoringStrategy();
+
+ /**
+ * Retrieves a reference for the given instance.
+ * If a not null value is returned from this method,
+ * then the bean content will not be written.
+ * Use {@link IDGenerator} strategy to vary the values
+ * written for a bean.
+ *
+ * @param context
+ * current context, not null
+ * @param bean
+ * the instance, not null
+ * @return id as String when this bean has already been reference,
+ * or null to indicate that this bean is not yet reference
+ */
+ public abstract String getReferenceFor(Context context, Object bean);
+
+ /**
+ * Stores an instance reference for later retrieval.
+ * This method is shared by writing and reading.
+ *
+ * @param context
+ * current context, not null
+ * @param bean
+ * the instance, not null
+ * @param id
+ * the id to use
+ */
+ public abstract void setReference(Context context, Object bean, String id);
+
+ /**
+ * Gets an object matching the given reference.
+ * @param context <code>Context</code>, not null
+ * @param id the reference id
+ * @return an bean matching the given reference,
+ * or null if there is no bean matching the given reference
+ */
+ public abstract Object getReferenced(Context context, String id);
+
+ /**
+ * Reset to the initial state.
+ *
+ */
+ public abstract void reset();
+
+}
Modified: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/recursion/TestRecursion.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/recursion/TestRecursion.java?view=diff&r1=154189&r2=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/recursion/TestRecursion.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/recursion/TestRecursion.java Thu Feb 17 12:53:49 2005
@@ -12,8 +12,8 @@
* 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 org.apache.commons.betwixt.recursion;
import java.io.StringReader;
@@ -30,57 +30,52 @@
import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.betwixt.io.CyclicReferenceException;
-
/**
* This will test the recursive behaviour of betwixt.
- *
- * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
- * @version $Id: TestRecursion.java,v 1.15 2004/06/13 21:32:48 rdonkin Exp $
+ *
+ * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt </a>
+ * @version $Id$
*/
-public class TestRecursion extends AbstractTestCase
-{
-
-
-
- public TestRecursion(String testName)
- {
+public class TestRecursion extends AbstractTestCase {
+
+ public TestRecursion(String testName) {
super(testName);
}
-
- public static Test suite()
- {
+
+ public static Test suite() {
return new TestSuite(TestRecursion.class);
}
-
+
/**
* This will test reading a simple recursive xml file
- *
+ *
*/
- public void testReadwithCollectionsInElementRoundTrip()
- throws Exception
- {
- //SimpleLog log = new SimpleLog("[testReadwithCollectionsInElementRoundTrip:XMLIntrospectorHelper]");
+ public void testReadwithCollectionsInElementRoundTrip() throws Exception {
+ //SimpleLog log = new
+ // SimpleLog("[testReadwithCollectionsInElementRoundTrip:XMLIntrospectorHelper]");
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
//XMLIntrospectorHelper.setLog(log);
-
- //log = new SimpleLog("[testReadwithCollectionsInElementRoundTrip:XMLIntrospector]");
+
+ //log = new
+ // SimpleLog("[testReadwithCollectionsInElementRoundTrip:XMLIntrospector]");
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
-
+
XMLIntrospector intro = createXMLIntrospector();
//intro.setLog(log);
intro.getConfiguration().setWrapCollectionsInElement(true);
-
- //log = new SimpleLog("[testReadwithCollectionsInElementRoundTrip:BeanReader]");
+
+ //log = new
+ // SimpleLog("[testReadwithCollectionsInElementRoundTrip:BeanReader]");
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
-
+
BeanReader reader = new BeanReader();
reader.setXMLIntrospector(intro);
//reader.setLog(log);
reader.registerBeanClass(ElementBean.class);
-
- ElementBean bean = (ElementBean) reader.parse(
- getTestFileURL("src/test/org/apache/commons/betwixt/recursion/recursion.xml"));
-
+
+ ElementBean bean = (ElementBean) reader
+ .parse(getTestFileURL("src/test/org/apache/commons/betwixt/recursion/recursion.xml"));
+
List elements = bean.getElements();
assertEquals("Root elements size", 2, elements.size());
Element elementOne = (Element) elements.get(0);
@@ -91,114 +86,128 @@
elements = elementOne.getElements();
assertEquals("Element one children", 2, elements.size());
Element elementOneOne = (Element) elements.get(0);
- assertEquals("Element one one name", "element11", elementOneOne.getName());
+ assertEquals("Element one one name", "element11", elementOneOne
+ .getName());
Element elementOneTwo = (Element) elements.get(1);
- assertEquals("Element one two name", "element12", elementOneTwo.getName());
- assertEquals("Element one two children", 0, elementOneTwo.getElements().size());
+ assertEquals("Element one two name", "element12", elementOneTwo
+ .getName());
+ assertEquals("Element one two children", 0, elementOneTwo.getElements()
+ .size());
elements = elementOneOne.getElements();
assertEquals("Element one one children", 2, elements.size());
Element elementOneOneOne = (Element) elements.get(0);
- assertEquals("Element one one one name", "element111", elementOneOneOne.getName());
+ assertEquals("Element one one one name", "element111", elementOneOneOne
+ .getName());
Element elementOneOneTwo = (Element) elements.get(1);
- assertEquals("Element one one two name", "element112", elementOneOneTwo.getName());
-
+ assertEquals("Element one one two name", "element112", elementOneOneTwo
+ .getName());
+
StringWriter buffer = new StringWriter();
- write (bean, buffer, true);
-
+ write(bean, buffer, true);
+
String xml = "<?xml version='1.0'?><ElementBean><elements><element name='element1'>"
- + "<elements><element name='element11'><elements><element name='element111'>"
- + "<elements/></element><element name='element112'><elements/></element>"
- + "</elements></element><element name='element12'><elements/></element>"
- + "</elements></element><element name='element2'><elements/>"
- + "</element></elements></ElementBean>";
-
- xmlAssertIsomorphic(
- parseString(xml),
- parseString(buffer.getBuffer().toString()),
- true);
+ + "<elements><element name='element11'><elements><element name='element111'>"
+ + "<elements/></element><element name='element112'><elements/></element>"
+ + "</elements></element><element name='element12'><elements/></element>"
+ + "</elements></element><element name='element2'><elements/>"
+ + "</element></elements></ElementBean>";
+
+ xmlAssertIsomorphic(parseString(xml), parseString(buffer.getBuffer()
+ .toString()), true);
}
+
/**
* This will test reading a simple recursive xml file
*/
- public void testReadWithoutCollectionsInElementRoundTrip()
- throws Exception
- {
-// SimpleLog log = new SimpleLog("[testReadWithoutCollectionsInElementRoundTrip:BeanRuleSet]");
-// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
-// BeanRuleSet.setLog(log);
-
-// log = new SimpleLog("[testReadWithoutCollectionsInElementRoundTrip:XMLIntrospector]");
-// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
-
+ public void testReadWithoutCollectionsInElementRoundTrip() throws Exception {
+ // SimpleLog log = new
+ // SimpleLog("[testReadWithoutCollectionsInElementRoundTrip:BeanRuleSet]");
+ // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
+ // BeanRuleSet.setLog(log);
+
+ // log = new
+ // SimpleLog("[testReadWithoutCollectionsInElementRoundTrip:XMLIntrospector]");
+ // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
+
XMLIntrospector intro = createXMLIntrospector();
intro.getConfiguration().setWrapCollectionsInElement(false);
-// intro.setLog(log);
-// log = new SimpleLog("[testReadWithoutCollectionsInElementRoundTrip:XMLIntrospectorHelper]");
-// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
-// XMLIntrospectorHelper.setLog(log);
+ // intro.setLog(log);
+ // log = new
+ // SimpleLog("[testReadWithoutCollectionsInElementRoundTrip:XMLIntrospectorHelper]");
+ // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
+ // XMLIntrospectorHelper.setLog(log);
BeanReader reader = new BeanReader();
-// log = new SimpleLog("[testReadWithoutCollectionsInElementRoundTrip:BeanReader]");
-// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
-// reader.setLog(log);
+ // log = new
+ // SimpleLog("[testReadWithoutCollectionsInElementRoundTrip:BeanReader]");
+ // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
+ // reader.setLog(log);
reader.setXMLIntrospector(intro);
reader.registerBeanClass(ElementBean.class);
- ElementBean bean = (ElementBean) reader.
- parse(getTestFileURL("src/test/org/apache/commons/betwixt/recursion/recursion2.xml"));
+ ElementBean bean = (ElementBean) reader
+ .parse(getTestFileURL("src/test/org/apache/commons/betwixt/recursion/recursion2.xml"));
List elements = bean.getElements();
assertEquals("Number of elements in root bean", 2, elements.size());
Element elementOne = (Element) bean.elements.get(0);
assertEquals("First element name", "element1", elementOne.getName());
Element elementTwo = (Element) bean.elements.get(1);
assertEquals("Second element name", "element2", elementTwo.getName());
-
+
elements = elementOne.getElements();
- assertEquals("Number of child elements in first element", 2, elements.size());
+ assertEquals("Number of child elements in first element", 2, elements
+ .size());
Element elementOneOne = (Element) elements.get(0);
assertEquals("11 element name", "element11", elementOneOne.getName());
Element elementOneTwo = (Element) elements.get(1);
assertEquals("12 element name", "element12", elementOneTwo.getName());
-
+
elements = elementOneOne.getElements();
- assertEquals("Number of child elements in element 11", 2, elements.size());
+ assertEquals("Number of child elements in element 11", 2, elements
+ .size());
Element elementOneOneOne = (Element) elements.get(0);
- assertEquals("111 element name", "element111", elementOneOneOne.getName());
+ assertEquals("111 element name", "element111", elementOneOneOne
+ .getName());
+
+ assertEquals("111 child elements ", 0, elementOneOneOne.getElements()
+ .size());
- assertEquals("111 child elements ", 0, elementOneOneOne.getElements().size());
-
Element elementOneOneTwo = (Element) elements.get(1);
- assertEquals("112 element name", "element112", elementOneOneTwo.getName());
- assertEquals("112 child elements ", 0, elementOneOneTwo.getElements().size());
-
+ assertEquals("112 element name", "element112", elementOneOneTwo
+ .getName());
+ assertEquals("112 child elements ", 0, elementOneOneTwo.getElements()
+ .size());
+
elements = elementOneTwo.getElements();
- assertEquals("Number of child elements in element 12", 0, elements.size());
-
+ assertEquals("Number of child elements in element 12", 0, elements
+ .size());
+
elements = elementTwo.getElements();
- assertEquals("Number of child elements in element 2", 0, elements.size());
-
+ assertEquals("Number of child elements in element 2", 0, elements
+ .size());
+
StringWriter buffer = new StringWriter();
buffer.write("<?xml version='1.0'?>");
- write (bean, buffer, false);
-
+ write(bean, buffer, false);
+
String xml = "<ElementBean><element name='element1'><element name='element11'><element name='element111' />"
+ "<element name='element112' /> </element><element name='element12' /> </element>"
+ "<element name='element2' /> </ElementBean>";
-
- xmlAssertIsomorphic(parseString(xml), parseString(buffer.getBuffer().toString()), true);
-
+
+ xmlAssertIsomorphic(parseString(xml), parseString(buffer.getBuffer()
+ .toString()), true);
+
}
-
+
/**
- * Opens a writer and writes an object model according to the
- * retrieved bean
+ * Opens a writer and writes an object model according to the retrieved bean
*/
private void write(Object bean, Writer out, boolean wrapIt)
- throws Exception
- {
+ throws Exception {
BeanWriter writer = new BeanWriter(out);
- writer.setWriteEmptyElements( true );
+ writer.setWriteEmptyElements(true);
writer.setXMLIntrospector(createXMLIntrospector());
// specifies weather to use collection elements or not.
- writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(wrapIt);
+ writer.getXMLIntrospector().getConfiguration()
+ .setWrapCollectionsInElement(wrapIt);
// we don't want to write Id attributes to every element
// we just want our opbject model written nothing more..
writer.getBindingConfiguration().setMapIDs(false);
@@ -207,6 +216,7 @@
writer.setEndOfLine("\n");
writer.write(bean);
}
+
/**
* Set up the XMLIntroSpector
*/
@@ -216,160 +226,153 @@
// set elements for attributes to true
introspector.getConfiguration().setAttributesForPrimitives(true);
introspector.getConfiguration().setWrapCollectionsInElement(false);
-
+
return introspector;
}
-
/**
*/
- public void testBeanWithIdProperty() throws Exception
- {
+ public void testBeanWithIdProperty() throws Exception {
IdBean bean = new IdBean("Hello, World");
bean.setNotId("Not ID");
StringWriter out = new StringWriter();
out.write("<?xml version='1.0'?>");
BeanWriter writer = new BeanWriter(out);
- writer.setWriteEmptyElements( true );
- writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
+ writer.setWriteEmptyElements(true);
+ writer.getXMLIntrospector().getConfiguration()
+ .setAttributesForPrimitives(true);
writer.getBindingConfiguration().setMapIDs(true);
writer.write(bean);
-
+
String xml = "<?xml version='1.0'?><IdBean notId='Not ID' id='Hello, World'/>";
-
- xmlAssertIsomorphic(parseString(xml), parseString(out.getBuffer().toString()), true);
- }
-
+
+ xmlAssertIsomorphic(parseString(xml), parseString(out.getBuffer()
+ .toString()), true);
+ }
+
/**
- * Check that a cyclic reference exception is not thrown in this case
+ * Check that a cyclic reference exception is not thrown in this case
*/
- public void testCyclicReferenceStack1() throws Exception
- {
+ public void testCyclicReferenceStack1() throws Exception {
Element alpha = new Element("Alpha");
Element beta = new Element("Beta");
Element gamma = new Element("Gamma");
Element epsilon = new Element("Epsilon");
-
+
alpha.addElement(beta);
beta.addElement(gamma);
gamma.addElement(epsilon);
alpha.addElement(epsilon);
-
+
StringWriter stringWriter = new StringWriter();
BeanWriter writer = new BeanWriter(stringWriter);
- writer.setWriteEmptyElements( true );
+ writer.setWriteEmptyElements(true);
writer.getBindingConfiguration().setMapIDs(false);
writer.write(alpha);
String xml = "<?xml version='1.0'?><Element><name>Alpha</name><elements><element>"
- + "<name>Beta</name><elements><element><name>Gamma</name><elements>"
- + "<element><name>Epsilon</name><elements/></element></elements>"
- + "</element></elements></element><element><name>Epsilon</name>"
- + "<elements/></element></elements></Element>";
-
- xmlAssertIsomorphic(parseString(xml), parseString(stringWriter.getBuffer().toString()), true);
- }
+ + "<name>Beta</name><elements><element><name>Gamma</name><elements>"
+ + "<element><name>Epsilon</name><elements/></element></elements>"
+ + "</element></elements></element><element><name>Epsilon</name>"
+ + "<elements/></element></elements></Element>";
+
+ xmlAssertIsomorphic(parseString(xml), parseString(stringWriter
+ .getBuffer().toString()), true);
+ }
/**
* This should throw a cyclic reference
*/
- public void testCyclicReferenceStack2() throws Exception
- {
+ public void testCyclicReferenceStack2() throws Exception {
Element alpha = new Element("Alpha");
Element beta = new Element("Beta");
Element gamma = new Element("Gamma");
Element epsilon = new Element("Epsilon");
-
+
alpha.addElement(beta);
beta.addElement(gamma);
gamma.addElement(epsilon);
epsilon.addElement(beta);
-
+
StringWriter stringWriter = new StringWriter();
BeanWriter writer = new BeanWriter(stringWriter);
- writer.setWriteEmptyElements( true );
+ writer.setWriteEmptyElements(true);
writer.getBindingConfiguration().setMapIDs(false);
-
- //SimpleLog log = new SimpleLog("[testCyclicReferenceStack2:BeanWriter]");
+
+ //SimpleLog log = new
+ // SimpleLog("[testCyclicReferenceStack2:BeanWriter]");
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
//writer.setLog(log);
-
+
//log = new SimpleLog("[testCyclicReferenceStack2:BeanWriter]");
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
//writer.setAbstractBeanWriterLog(log);
-
+
try {
writer.write(alpha);
fail("Cycle was not detected!");
-
+
} catch (CyclicReferenceException e) {
// that's what we expected!
}
- }
-
-
- /** Tests for a stack overflow bug */
- public void testRegisterOverflow() throws Exception {
- BeanReader reader = new BeanReader();
- try
- {
- reader.registerBeanClass(NorthWind.class);
- }
- catch (StackOverflowError e)
- {
- e.printStackTrace();
- fail("Expected registration to succeed");
- }
- }
-
- public void testRegisterOverflow2() throws Exception {
- BeanReader beanReader = new BeanReader();
- try
- {
- beanReader.registerBeanClass(PersonTest.class);
- }
- catch (StackOverflowError e)
- {
- e.printStackTrace();
- fail("Expected registration to succeed");
- }
- }
-
- public void testCycleReferences() throws Exception {
- PersonTest person = new PersonTest();
- person.setName("John Doe");
- AddressTest address = new AddressTest();
- address.setStreetAddress("1221 Washington Street");
- person.setAddress(address);
- ReferenceTest reference = new ReferenceTest();
- reference.setPerson(person);
- address.setReference(reference);
-
- StringWriter outputWriter = new StringWriter();
-
- outputWriter.write("<?xml version='1.0' ?>\n");
- BeanWriter beanWriter = new BeanWriter(outputWriter);
- beanWriter.enablePrettyPrint();
- beanWriter.getBindingConfiguration().setMapIDs(true);
- beanWriter.write(person);
-
- BeanReader beanReader = new BeanReader();
- beanReader.getBindingConfiguration().setMapIDs(true);
-
- // Configure the reader
- beanReader.registerBeanClass(PersonTest.class);
- beanReader.registerBeanClass(AddressTest.class);
- beanReader.registerBeanClass(ReferenceTest.class);
-
- String out = outputWriter.toString();
- StringReader xmlReader = new StringReader(out);
-
- //Parse the xml
- PersonTest result = (PersonTest)beanReader.parse(xmlReader);
- assertSame("Cycle did not result in the same reference", result, result.getAddress().getReference().getPerson());
-
- }
-
+ }
+
+ /** Tests for a stack overflow bug */
+ public void testRegisterOverflow() throws Exception {
+ BeanReader reader = new BeanReader();
+ try {
+ reader.registerBeanClass(NorthWind.class);
+ } catch (StackOverflowError e) {
+ e.printStackTrace();
+ fail("Expected registration to succeed");
+ }
+ }
+
+ public void testRegisterOverflow2() throws Exception {
+ BeanReader beanReader = new BeanReader();
+ try {
+ beanReader.registerBeanClass(PersonTest.class);
+ } catch (StackOverflowError e) {
+ e.printStackTrace();
+ fail("Expected registration to succeed");
+ }
+ }
+
+ public void testCycleReferences() throws Exception {
+ PersonTest person = new PersonTest();
+ person.setName("John Doe");
+ AddressTest address = new AddressTest();
+ address.setStreetAddress("1221 Washington Street");
+ person.setAddress(address);
+ ReferenceTest reference = new ReferenceTest();
+ reference.setPerson(person);
+ address.setReference(reference);
+
+ StringWriter outputWriter = new StringWriter();
+
+ outputWriter.write("<?xml version='1.0' ?>\n");
+ BeanWriter beanWriter = new BeanWriter(outputWriter);
+ beanWriter.enablePrettyPrint();
+ beanWriter.getBindingConfiguration().setMapIDs(true);
+ beanWriter.write(person);
+
+ BeanReader beanReader = new BeanReader();
+ beanReader.getBindingConfiguration().setMapIDs(true);
+
+ // Configure the reader
+ beanReader.registerBeanClass(PersonTest.class);
+ beanReader.registerBeanClass(AddressTest.class);
+ beanReader.registerBeanClass(ReferenceTest.class);
+
+ String out = outputWriter.toString();
+ StringReader xmlReader = new StringReader(out);
+
+ //Parse the xml
+ PersonTest result = (PersonTest) beanReader.parse(xmlReader);
+ assertSame("Cycle did not result in the same reference", result, result
+ .getAddress().getReference().getPerson());
+
+ }
}
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/ElementsList.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/ElementsList.java?view=auto&rev=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/ElementsList.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/ElementsList.java Thu Feb 17 12:53:49 2005
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * 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 org.apache.commons.betwixt.strategy;
+
+import java.util.*;
+
+/**
+ */
+public class ElementsList {
+ private List elements = new ArrayList();
+
+ public Iterator getElements() {
+ return elements.iterator();
+ }
+
+ public void addElement(Element element) {
+ elements.add(element);
+ }
+
+ public Element get(int index)
+ {
+ return (Element) elements.get(index);
+ }
+}
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java?view=auto&rev=154190
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java Thu Feb 17 12:53:49 2005
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * 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 org.apache.commons.betwixt.strategy;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Iterator;
+
+import org.apache.commons.betwixt.AbstractTestCase;
+import org.apache.commons.betwixt.expression.Context;
+import org.apache.commons.betwixt.io.BeanReader;
+import org.apache.commons.betwixt.io.BeanWriter;
+
+/**
+ */
+public class TestIdStorageStrategy extends AbstractTestCase {
+
+ public TestIdStorageStrategy(String testName) {
+ super(testName);
+ }
+
+ public void testWrite() throws Exception {
+
+ final Element alpha = new Element("ONE");
+ Element beta = new Element("TWO");
+ ElementsList elements = new ElementsList();
+ elements.addElement(alpha);
+ elements.addElement(beta);
+
+ IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
+
+ public String getReferenceFor(Context context, Object bean) {
+ String result = null;
+ if (bean == alpha) {
+ result = "ALPHA";
+ }
+ else
+ {
+ result = super.getReferenceFor(context, bean);
+ }
+ return result;
+ }
+
+ public void setReference(Context context, Object bean, String id) {
+ if (bean != alpha) {
+ super.setReference(context, bean, id);
+ }
+ }
+ };
+
+ StringWriter out = new StringWriter();
+ out.write("<?xml version='1.0'?>");
+ BeanWriter writer = new BeanWriter(out);
+ writer.getBindingConfiguration().setIdMappingStrategy(storingStrategy);
+ writer.write(elements);
+
+ String expected = "<?xml version='1.0'?>" +
+ "<ElementsList id='1'>" +
+ " <elements>" +
+ " <element idref='ALPHA'/>" +
+ " <element id='2'>" +
+ " <value>TWO</value>" +
+ " </element>" +
+ " </elements>" +
+ "</ElementsList>";
+
+ xmlAssertIsomorphicContent(parseString(expected), parseString(out));
+ }
+
+ public void testRead() throws Exception {
+
+ String xml = "<?xml version='1.0'?>" +
+ "<ElementsList id='1'>" +
+ " <elements>" +
+ " <element idref='ALPHA'/>" +
+ " <element id='2'>" +
+ " <value>TWO</value>" +
+ " </element>" +
+ " </elements>" +
+ "</ElementsList>";
+
+ final Element alpha = new Element("ONE");
+
+ IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() {
+
+ public void setReference(Context context, Object bean, String id) {
+ if (bean != alpha) {
+ super.setReference(context, bean, id);
+ }
+ }
+
+ public Object getReferenced(Context context, String id) {
+ if ("ALPHA".equals(id)) {
+ return alpha;
+ }
+ return getReferenced(context, id);
+ }
+
+ };
+
+ BeanReader reader = new BeanReader();
+ reader.getBindingConfiguration().setIdMappingStrategy(storingStrategy);
+ reader.registerBeanClass(ElementsList.class);
+ ElementsList elements = (ElementsList) reader.parse(new StringReader(xml));
+ assertNotNull(elements);
+ Element one = elements.get(0);
+ assertTrue(one == alpha);
+ Element two = elements.get(1);
+ assertNotNull(two);
+ }
+
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org