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 2006/05/30 23:40:28 UTC
svn commit: r410383 - in
/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly:
AlphaListHolder.java AlphaOneImpl.java AlphaTwoImpl.java IAlpha.java
TestPolyList.java TestPolyListHolder.java
Author: rdonkin
Date: Tue May 30 14:40:27 2006
New Revision: 410383
URL: http://svn.apache.org/viewvc?rev=410383&view=rev
Log:
Unit tests for polymorphic list property. Contributed by Holger Haag. JIRA BETWIXT-52.
Added:
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaListHolder.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyListHolder.java
Modified:
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaListHolder.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaListHolder.java?rev=410383&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaListHolder.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaListHolder.java Tue May 30 14:40:27 2006
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2006 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.poly;
+
+public class AlphaListHolder {
+ private AlphaList alphaList;
+
+ public AlphaList getAlphaList() {
+ return alphaList;
+ }
+
+ public void setAlphaList(AlphaList alphaList) {
+ this.alphaList = (AlphaList) alphaList;
+ }
+
+
+}
+
Modified: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java?rev=410383&r1=410382&r2=410383&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java Tue May 30 14:40:27 2006
@@ -17,8 +17,26 @@
package org.apache.commons.betwixt.poly;
public class AlphaOneImpl implements IAlpha {
-
- public void alpha() {
+ public String alpha() {
+ return getOne();
}
+ private String one;
+
+ public String getOne() {
+ return one;
+ }
+
+ public void setOne(String one) {
+ this.one = one;
+ }
+
+
+ public AlphaOneImpl(final String one) {
+ setOne(one);
+ }
+
+ public AlphaOneImpl() {
+ super();
+ }
}
Modified: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java?rev=410383&r1=410382&r2=410383&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java Tue May 30 14:40:27 2006
@@ -17,7 +17,29 @@
public class AlphaTwoImpl implements IAlpha {
- public void alpha() {
+ public String alpha() {
+ return getTwo();
}
+ private String two;
+
+ public String getTwo() {
+ return two;
+ }
+
+ public void setTwo(String two) {
+ this.two = two;
+ }
+
+
+ public AlphaTwoImpl(String two) {
+ super();
+
+ setTwo(two);
+ }
+
+ public AlphaTwoImpl() {
+ super();
+ }
+
}
Modified: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java?rev=410383&r1=410382&r2=410383&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java Tue May 30 14:40:27 2006
@@ -17,5 +17,5 @@
public interface IAlpha {
- public void alpha();
+ public String alpha();
}
Modified: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java?rev=410383&r1=410382&r2=410383&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java Tue May 30 14:40:27 2006
@@ -26,32 +26,42 @@
import org.apache.commons.betwixt.XMLIntrospector;
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.xml.sax.InputSource;
+
+
public class TestPolyList extends AbstractTestCase {
+ public static Log log = LogFactory.getLog(TestPolyList.class);
public TestPolyList(String testName) {
super(testName);
+
+ log.info("Mapping:\n" + MAPPING);
}
- private static final String XML = "<AlphaList>" + "<AlphaOneImpl/>"
- + "<AlphaTwoImpl/>" + "</AlphaList>";
+ private static final String XML = "<AlphaList><AlphaOneImpl><one>1</one></AlphaOneImpl><AlphaTwoImpl><two>2</two></AlphaTwoImpl></AlphaList>";
private static final String MAPPING = "<?xml version='1.0'?>"
+ "<betwixt-config>"
+ " <class name='org.apache.commons.betwixt.poly.AlphaOneImpl'>"
- + " <element name='AlphaOneImpl'/>"
+ + " <element name='AlphaOneImpl'>"
+ + " <element name='one' property='one'/>"
+ + " </element>"
+ " </class>"
+ " <class name='org.apache.commons.betwixt.poly.AlphaTwoImpl'>"
- + " <element name='AlphaTwoImpl'/>"
+ + " <element name='AlphaTwoImpl'>"
+ + " <element name='two' property='two'/>"
+ + " </element>"
+ " </class>"
+ "</betwixt-config>";
public void testWrite() throws Exception {
AlphaList bean = new AlphaList();
- AlphaOneImpl one = new AlphaOneImpl();
+ AlphaOneImpl one = new AlphaOneImpl("1");
bean.add(one);
- AlphaTwoImpl two = new AlphaTwoImpl();
+ AlphaTwoImpl two = new AlphaTwoImpl("2");
bean.add(two);
StringWriter out = new StringWriter();
@@ -60,10 +70,13 @@
writer.getXMLIntrospector().register(new InputSource(mapping));
configure(writer.getBindingConfiguration());
writer.write(bean);
-
+
+ String written = out.getBuffer().toString();
+ log.info("Written:\n" + written);
+
xmlAssertIsomorphicContent(
parseString(XML),
- parseString(out.getBuffer().toString()),
+ parseString(written),
true);
}
@@ -78,7 +91,15 @@
assertTrue(bean instanceof AlphaList);
AlphaList list = (AlphaList) bean;
assertEquals(2, list.size());
- }
+
+ assertTrue(list.get(0) instanceof AlphaOneImpl);
+ AlphaOneImpl one = (AlphaOneImpl)list.get(0);
+ assertEquals("1", one.alpha());
+
+ assertTrue(list.get(1) instanceof AlphaTwoImpl);
+ AlphaTwoImpl two = (AlphaTwoImpl)list.get(1);
+ assertEquals("2", two.alpha());
+ }
public void testIntrospection() throws Exception {
XMLIntrospector introspector = new XMLIntrospector();
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyListHolder.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyListHolder.java?rev=410383&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyListHolder.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyListHolder.java Tue May 30 14:40:27 2006
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2006 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.poly;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import org.apache.commons.betwixt.AbstractTestCase;
+import org.apache.commons.betwixt.BindingConfiguration;
+import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.XMLBeanInfo;
+import org.apache.commons.betwixt.XMLIntrospector;
+import org.apache.commons.betwixt.io.BeanReader;
+import org.apache.commons.betwixt.io.BeanWriter;
+import org.xml.sax.InputSource;
+
+public class TestPolyListHolder extends AbstractTestCase {
+
+ public TestPolyListHolder(String testName) {
+ super(testName);
+ }
+
+ private static final String XML = "<AlphaListHolder>" +
+ " <AlphaList>" +
+ " <AlphaOneImpl>" +
+ " <one>1</one>" +
+ " </AlphaOneImpl>" +
+ " <AlphaTwoImpl>" +
+ " <two>2</two>" +
+ " </AlphaTwoImpl>" +
+ " </AlphaList>" +
+ "</AlphaListHolder>";
+
+ private static final String MAPPING = "<?xml version='1.0'?>"
+ + "<betwixt-config>"
+ + " <class name='org.apache.commons.betwixt.poly.AlphaListHolder'>"
+ + " <element name='AlphaListHolder'>"
+ + " <element name='AlphaList' property='alphaList' updater='setAlphaList'/>"
+ + " </element>"
+ + " </class>"
+ + " <class name='org.apache.commons.betwixt.poly.AlphaOneImpl'>"
+ + " <element name='AlphaOneImpl'>"
+ + " <element name='one' property='one'/>"
+ + " </element>"
+ + " </class>"
+ + " <class name='org.apache.commons.betwixt.poly.AlphaTwoImpl'>"
+ + " <element name='AlphaTwoImpl'>"
+ + " <element name='two' property='two'/>"
+ + " </element>"
+ + " </class>"
+ + "</betwixt-config>";
+
+ public void testWrite() throws Exception {
+ AlphaList list = new AlphaList();
+ AlphaOneImpl one = new AlphaOneImpl("1");
+ list.add(one);
+ AlphaTwoImpl two = new AlphaTwoImpl("2");
+ list.add(two);
+
+ AlphaListHolder bean = new AlphaListHolder();
+ bean.setAlphaList(list);
+
+ StringWriter out = new StringWriter();
+ BeanWriter writer = new BeanWriter(out);
+ StringReader mapping = new StringReader(MAPPING);
+ writer.getXMLIntrospector().register(new InputSource(mapping));
+ configure(writer.getBindingConfiguration());
+ writer.write(bean);
+
+ String written = out.getBuffer().toString();
+
+ xmlAssertIsomorphicContent(
+ parseString(XML),
+ parseString(written),
+ true);
+ }
+
+ public void testRead() throws Exception {
+
+ StringReader in = new StringReader(XML);
+ BeanReader reader = new BeanReader();
+
+ StringReader mapping = new StringReader(MAPPING);
+ reader.registerMultiMapping(new InputSource(mapping));
+ reader.registerBeanClass(AlphaList.class);
+ configure(reader.getBindingConfiguration());
+ Object bean = reader.parse(in);
+
+ assertTrue(bean instanceof AlphaListHolder);
+ AlphaListHolder holder = (AlphaListHolder)bean;
+
+ AlphaList list = holder.getAlphaList();
+ assertNotNull(list);
+ assertEquals(2, list.size());
+
+ assertTrue(list.get(0) instanceof AlphaOneImpl);
+ AlphaOneImpl one = (AlphaOneImpl)list.get(0);
+ assertEquals("1", one.alpha());
+
+ assertTrue(list.get(1) instanceof AlphaTwoImpl);
+ AlphaTwoImpl two = (AlphaTwoImpl)list.get(1);
+ assertEquals("2", two.alpha());
+ }
+
+ private void configure(BindingConfiguration configuration) {
+ configuration.setMapIDs(false);
+ }
+
+
+ public void testIntrospection() throws Exception {
+ XMLIntrospector introspector = new XMLIntrospector();
+
+ StringReader mapping = new StringReader(MAPPING);
+ introspector.register(new InputSource(mapping));
+
+ XMLBeanInfo beanInfo = introspector.introspect(AlphaListHolder.class);
+ ElementDescriptor descriptor = beanInfo.getElementDescriptor();
+ assertNotNull(descriptor);
+ ElementDescriptor[] descriptors = descriptor.getElementDescriptors();
+ assertNotNull(descriptors);
+ assertEquals("Only one descriptor", 1, descriptors.length);
+ assertNotNull("Expected updater", descriptors[0].getUpdater());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org