001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.strategy;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021: import java.util.Collection;
022:
023: import org.apache.commons.betwixt.AbstractTestCase;
024: import org.apache.commons.betwixt.expression.Context;
025: import org.apache.commons.betwixt.io.BeanReader;
026: import org.apache.commons.betwixt.io.BeanWriter;
027:
028: public class TestConversionFlavour extends AbstractTestCase {
029:
030: public TestConversionFlavour(String testName) {
031: super (testName);
032: }
033:
034: public void testRead() throws Exception {
035: String xml = "<alpha>" + " <name>BananasSIX</name>"
036: + " <betaBean>"
037: + " <name>PeachONE</name>"
038: + " </betaBean>" + " <children>"
039: + " <child>"
040: + " <name>PeachTWO</name>"
041: + " </child>" + " </children>"
042: + " <mapped>" + " <entry>"
043: + " <key>Key</key>" + " <value>"
044: + " <name>PeachTHREE</name>"
045: + " </value>" + " </entry>"
046: + " </mapped>" + " </alpha>";
047:
048: StringReader in = new StringReader(xml);
049: BeanReader reader = new BeanReader();
050: reader.getBindingConfiguration().setMapIDs(false);
051: reader.getBindingConfiguration().setObjectStringConverter(
052: new PrependingConverter());
053: reader.registerBeanClass(AlphaBean.class);
054: AlphaBean bean = (AlphaBean) reader.parse(in);
055: assertNotNull(bean);
056: assertEquals("SIX", bean.getName());
057: BetaBean betaBean = bean.getBetaBean();
058: assertNotNull(betaBean);
059: assertEquals("ONE", betaBean.getName());
060: Collection children = bean.getChildren();
061: assertEquals(1, children.size());
062: BetaBean child = (BetaBean) children.iterator().next();
063: assertEquals("TWO", child.getName());
064: }
065:
066: public void testWrite() throws Exception {
067: AlphaBean alphaBean = new AlphaBean();
068: alphaBean.setName("SIX");
069: BetaBean betaBeanOne = new BetaBean("ONE");
070: alphaBean.setBetaBean(betaBeanOne);
071: BetaBean betaBeanTwo = new BetaBean("TWO");
072: alphaBean.addChild(betaBeanTwo);
073: BetaBean betaBeanThree = new BetaBean("THREE");
074: alphaBean.put("Key", betaBeanThree);
075:
076: StringWriter out = new StringWriter();
077: BeanWriter writer = new BeanWriter(out);
078: writer.getBindingConfiguration().setMapIDs(false);
079: writer.getBindingConfiguration().setObjectStringConverter(
080: new PrependingConverter());
081: writer.write(alphaBean);
082:
083: String xml = "<alpha>" + " <name>BananasSIX</name>"
084: + " <betaBean>"
085: + " <name>PeachONE</name>"
086: + " </betaBean>" + " <children>"
087: + " <child>"
088: + " <name>PeachTWO</name>"
089: + " </child>" + " </children>"
090: + " <mapped>" + " <entry>"
091: + " <key>Key</key>" + " <value>"
092: + " <name>PeachTHREE</name>"
093: + " </value>" + " </entry>"
094: + " </mapped>" + " </alpha>";
095:
096: xmlAssertIsomorphicContent(parseString(xml), parseString(out),
097: true);
098: }
099:
100: public static final class PrependingConverter extends
101: DefaultObjectStringConverter {
102:
103: public String objectToString(Object object, Class type,
104: String flavour, Context context) {
105: String result = super .objectToString(object, type, flavour,
106: context);
107: if (flavour != null) {
108: result = flavour + result;
109: }
110: return result;
111: }
112:
113: public Object stringToObject(String value, Class type,
114: String flavour, Context context) {
115: if (flavour != null) {
116: value = value.substring(flavour.length());
117: }
118: return super.stringToObject(value, type, flavour, context);
119: }
120: }
121: }
|