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;
018:
019: import java.beans.IntrospectionException;
020: import java.io.IOException;
021: import java.io.StringReader;
022: import java.io.StringWriter;
023:
024: import org.apache.commons.betwixt.io.BeanReader;
025: import org.apache.commons.betwixt.io.BeanWriter;
026: import org.xml.sax.InputSource;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * Tests the multi-mapping of polymorphic references.
031: *
032: * @author Thomas Dudziak (tomdz at apache.org)
033: */
034: public class TestReferenceMapping extends AbstractTestCase {
035: public static class Container {
036: private Element _element1;
037: private Element _element2;
038:
039: public Element getElement1() {
040: return _element1;
041: }
042:
043: public void setElement1(Element element) {
044: _element1 = element;
045: }
046:
047: public Element getElement2() {
048: return _element2;
049: }
050:
051: public void setElement2(Element element) {
052: _element2 = element;
053: }
054: }
055:
056: public static interface Element {
057: }
058:
059: public static class ElementA implements Element {
060: }
061:
062: public static class ElementB implements Element {
063: }
064:
065: private static final String MAPPING = "<?xml version=\"1.0\"?>\n"
066: + "<betwixt-config>\n"
067: + " <class name=\"org.apache.commons.betwixt.TestReferenceMapping$Container\">\n"
068: + " <element name=\"container\">\n"
069: + " <element property=\"element1\"/>\n"
070: + " <element name=\"element2\" property=\"element2\"/>\n"
071: + " </element>\n"
072: + " </class>\n"
073: + " <class name=\"org.apache.commons.betwixt.TestReferenceMapping$ElementA\">\n"
074: + " <element name=\"elementA\"/>\n"
075: + " </class>\n"
076: + " <class name=\"org.apache.commons.betwixt.TestReferenceMapping$ElementB\">\n"
077: + " <element name=\"elementB\"/>\n" + " </class>\n"
078: + "</betwixt-config>";
079: private static final String EXPECTED = "<?xml version=\"1.0\" ?>\n"
080: + " <container>\n" + " <elementB/>\n"
081: + " <element2/>\n" + " </container>\n";
082:
083: public TestReferenceMapping(String testName) {
084: super (testName);
085: }
086:
087: public void testRoundTripWithSingleMappingFile()
088: throws IOException, SAXException, IntrospectionException {
089: Container container = new Container();
090:
091: container.setElement1(new ElementB());
092: container.setElement2(new ElementA());
093:
094: StringWriter outputWriter = new StringWriter();
095:
096: outputWriter.write("<?xml version=\"1.0\" ?>\n");
097:
098: BeanWriter beanWriter = new BeanWriter(outputWriter);
099: beanWriter.setEndOfLine("\n");
100: beanWriter.enablePrettyPrint();
101: beanWriter.setWriteEmptyElements(true);
102: beanWriter.getBindingConfiguration().setMapIDs(false);
103: beanWriter.getXMLIntrospector().register(
104: new InputSource(new StringReader(MAPPING)));
105: beanWriter.setEndOfLine("\n"); //force to ensure matches on expected
106: beanWriter.write(container);
107:
108: String output = outputWriter.toString();
109:
110: assertEquals(EXPECTED, output);
111:
112: BeanReader beanReader = new BeanReader();
113:
114: beanReader.registerMultiMapping(new InputSource(
115: new StringReader(MAPPING)));
116:
117: StringReader xmlReader = new StringReader(output);
118:
119: container = (Container) beanReader.parse(xmlReader);
120:
121: assertTrue(container.getElement1() instanceof ElementB);
122: // betwixt cannot know which type the element has
123: assertNull(container.getElement2());
124: }
125: }
|