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/24 23:29:30 UTC
svn commit: r155265 - in
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt:
expression/TestContext.java strategy/AlphaBean.betwixt
strategy/AlphaBean.java strategy/BetaBean.betwixt strategy/BetaBean.java
strategy/TestConversionFlavour.java
Author: rdonkin
Date: Thu Feb 24 14:29:28 2005
New Revision: 155265
URL: http://svn.apache.org/viewcvs?view=rev&rev=155265
Log:
Tests for change to ObjectToStringConverter to make context available and to make options available from context. This is an alternative way to address the needs of Issue 33331.
Added:
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestContext.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.betwixt
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.betwixt
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.java
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestConversionFlavour.java
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestContext.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestContext.java?view=auto&rev=155265
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestContext.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestContext.java Thu Feb 24 14:29:28 2005
@@ -0,0 +1,40 @@
+/*
+ * 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.expression;
+
+import org.apache.commons.betwixt.Options;
+
+import junit.framework.TestCase;
+
+/**
+ */
+public class TestContext extends TestCase {
+
+ public void testOptions() throws Exception {
+ Context context = new Context();
+ assertNull(context.getOptions());
+ Options firstOptions = new Options();
+ context.pushOptions(firstOptions);
+ assertEquals(firstOptions, context.getOptions());
+ Options secondOptions = new Options();
+ context.pushOptions(secondOptions);
+ assertEquals(secondOptions, context.getOptions());
+ context.popOptions();
+ assertEquals(firstOptions, context.getOptions());
+ context.popOptions();
+ assertNull(context.getOptions());
+ }
+}
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.betwixt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.betwixt?view=auto&rev=155265
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.betwixt (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.betwixt Thu Feb 24 14:29:28 2005
@@ -0,0 +1,27 @@
+<?xml version='1.0'?>
+<!--
+ Copyright 2001-2004 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.
+-->
+<info primitiveTypes="attribute">
+ <element name='alpha'>
+ <element name='name' property='name'>
+ <option>
+ <name>org.apache.commons.betwixt.flavour</name>
+ <value>Bananas</value>
+ </option>
+ </element>
+ <addDefaults/>
+ </element>
+</info>
\ No newline at end of file
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.java?view=auto&rev=155265
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/AlphaBean.java Thu Feb 24 14:29:28 2005
@@ -0,0 +1,59 @@
+/*
+ * 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 AlphaBean {
+
+ private Collection children = new ArrayList();
+ private Map mapped = new HashMap();
+ private BetaBean betaBean;
+ private String name;
+
+ public BetaBean getBetaBean() {
+ return betaBean;
+ }
+
+ public void setBetaBean(BetaBean betaBean) {
+ this.betaBean = betaBean;
+ }
+
+ public Collection getChildren() {
+ return children;
+ }
+
+ public void addChild(BetaBean bean) {
+ this.children.add(bean);
+ }
+
+ public Map getMapped() {
+ return mapped;
+ }
+
+ public void put(String key, BetaBean value) {
+ this.mapped.put(key, value);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.betwixt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.betwixt?view=auto&rev=155265
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.betwixt (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.betwixt Thu Feb 24 14:29:28 2005
@@ -0,0 +1,27 @@
+<?xml version='1.0'?>
+<!--
+ Copyright 2001-2004 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.
+-->
+<info primitiveTypes="attribute">
+ <element name='beta'>
+ <element name='name' property='name'>
+ <option>
+ <name>org.apache.commons.betwixt.flavour</name>
+ <value>Peach</value>
+ </option>
+ </element>
+ <addDefaults/>
+ </element>
+</info>
\ No newline at end of file
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.java?view=auto&rev=155265
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/BetaBean.java Thu Feb 24 14:29:28 2005
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+/**
+ */
+public class BetaBean {
+
+ private String name;
+
+ public BetaBean() {}
+ public BetaBean(String name) {
+ setName(name);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestConversionFlavour.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestConversionFlavour.java?view=auto&rev=155265
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestConversionFlavour.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestConversionFlavour.java Thu Feb 24 14:29:28 2005
@@ -0,0 +1,131 @@
+/*
+ * 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.Collection;
+
+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 TestConversionFlavour extends AbstractTestCase {
+
+ public TestConversionFlavour(String testName) {
+ super(testName);
+ }
+
+
+ public void testRead() throws Exception {
+ String xml = "<alpha>" +
+ " <name>BananasSIX</name>" +
+ " <betaBean>" +
+ " <name>PeachONE</name>" +
+ " </betaBean>" +
+ " <children>" +
+ " <child>" +
+ " <name>PeachTWO</name>" +
+ " </child>" +
+ " </children>" +
+ " <mapped>" +
+ " <entry>" +
+ " <key>Key</key>" +
+ " <value>" +
+ " <name>PeachTHREE</name>" +
+ " </value>" +
+ " </entry>" +
+ " </mapped>" +
+ " </alpha>";
+
+ StringReader in = new StringReader(xml);
+ BeanReader reader = new BeanReader();
+ reader.getBindingConfiguration().setMapIDs(false);
+ reader.getBindingConfiguration().setObjectStringConverter(new PrependingConverter());
+ reader.registerBeanClass(AlphaBean.class);
+ AlphaBean bean = (AlphaBean) reader.parse(in);
+ assertNotNull(bean);
+ assertEquals("SIX", bean.getName());
+ BetaBean betaBean = bean.getBetaBean();
+ assertNotNull(betaBean);
+ assertEquals("ONE", betaBean.getName());
+ Collection children = bean.getChildren();
+ assertEquals(1, children.size());
+ BetaBean child = (BetaBean) children.iterator().next();
+ assertEquals("TWO", child.getName());
+ }
+
+ public void testWrite() throws Exception {
+ AlphaBean alphaBean = new AlphaBean();
+ alphaBean.setName("SIX");
+ BetaBean betaBeanOne = new BetaBean("ONE");
+ alphaBean.setBetaBean(betaBeanOne);
+ BetaBean betaBeanTwo = new BetaBean("TWO");
+ alphaBean.addChild(betaBeanTwo);
+ BetaBean betaBeanThree = new BetaBean("THREE");
+ alphaBean.put("Key", betaBeanThree);
+
+ StringWriter out = new StringWriter();
+ BeanWriter writer = new BeanWriter(out);
+ writer.getBindingConfiguration().setMapIDs(false);
+ writer.getBindingConfiguration().setObjectStringConverter(new PrependingConverter());
+ writer.write(alphaBean);
+
+ String xml = "<alpha>" +
+ " <name>BananasSIX</name>" +
+ " <betaBean>" +
+ " <name>PeachONE</name>" +
+ " </betaBean>" +
+ " <children>" +
+ " <child>" +
+ " <name>PeachTWO</name>" +
+ " </child>" +
+ " </children>" +
+ " <mapped>" +
+ " <entry>" +
+ " <key>Key</key>" +
+ " <value>" +
+ " <name>PeachTHREE</name>" +
+ " </value>" +
+ " </entry>" +
+ " </mapped>" +
+ " </alpha>";
+
+ xmlAssertIsomorphicContent(parseString(xml), parseString(out));
+ }
+
+
+ public static final class PrependingConverter extends DefaultObjectStringConverter {
+
+ public String objectToString(Object object, Class type, String flavour,
+ Context context) {
+ String result = super.objectToString(object, type, flavour, context);
+ if (flavour != null) {
+ result = flavour + result;
+ }
+ return result;
+ }
+
+ public Object stringToObject(String value, Class type, String flavour,
+ Context context) {
+ if (flavour != null) {
+ value = value.substring(flavour.length());
+ }
+ return super.stringToObject(value, type, flavour, context);
+ }
+}
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org