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.io;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import org.apache.commons.betwixt.AbstractTestCase;
025: import org.xml.sax.InputSource;
026:
027: public class TestMapUpdater extends AbstractTestCase {
028:
029: public TestMapUpdater(String testName) {
030: super (testName);
031: }
032:
033: public void testMapUpdater() throws Exception {
034: String config = "<?xml version='1.0'?>"
035: + "<betwixt-config primitiveTypes='attribute'>"
036: + " <class name='org.apache.commons.betwixt.io.TestMapUpdater$ParentBean'>"
037: + " <element name='parentBean'>"
038: + " <element name='pairs'>"
039: + " <element property='pairs' updater='addPair'/>"
040: + " </element>"
041: + " <addDefaults add-properties='true' add-adders='false'/>"
042: + " </element>" + " </class>"
043: + "</betwixt-config>";
044:
045: String result = "<?xml version=\"1.0\"?>\n"
046: + " <parentBean id=\"1\">\n" + " <pairs>\n"
047: + " <entry id=\"2\">\n"
048: + " <key>key</key>\n"
049: + " <value>value</value>\n" + " </entry>\n"
050: + " </pairs>\n" + " </parentBean>\n";
051:
052: ParentBean pb = new ParentBean();
053: pb.getPairs().put("key", "value");
054:
055: StringWriter writer = new StringWriter();
056: BeanWriter beanWriter = new BeanWriter(writer);
057: beanWriter.enablePrettyPrint();
058: beanWriter.getXMLIntrospector().register(
059: new InputSource(new StringReader(config)));
060: beanWriter.writeXmlDeclaration("<?xml version=\"1.0\"?>");
061: beanWriter.write(pb);
062:
063: xmlAssertIsomorphic(parseString(result), parseString(writer));
064:
065: BeanReader beanReader = new BeanReader();
066: beanReader.registerMultiMapping(new InputSource(
067: new StringReader(config)));
068:
069: ParentBean pbRead = (ParentBean) beanReader
070: .parse(new StringReader(writer.toString()));
071:
072: StringWriter writer2 = new StringWriter();
073: BeanWriter beanWriter2 = new BeanWriter(writer2);
074: beanWriter2.enablePrettyPrint();
075: beanWriter2.getXMLIntrospector().register(
076: new InputSource(new StringReader(config)));
077: beanWriter2.writeXmlDeclaration("<?xml version=\"1.0\"?>");
078: beanWriter2.write(pbRead);
079:
080: xmlAssertIsomorphic(parseString(result), parseString(writer2));
081:
082: }
083:
084: public static class ParentBean {
085: private Map pairs = new HashMap();
086:
087: public Map getPairs() {
088: return pairs;
089: }
090:
091: public void setPairs(Map pairs) {
092: this .pairs = pairs;
093: }
094:
095: public void addPair(String key, String value) {
096: pairs.put(key, value);
097: }
098:
099: }
100:
101: }
|