1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt;
18  
19  
20  import java.beans.IntrospectionException;
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  
25  import org.apache.commons.betwixt.io.BeanReader;
26  import org.apache.commons.betwixt.io.BeanWriter;
27  import org.xml.sax.InputSource;
28  import org.xml.sax.SAXException;
29  
30  /**
31   * Tests the multi-mapping of polymorphic references.
32   * 
33   * @author Thomas Dudziak (tomdz at apache.org)
34   */
35  public class TestReferenceMapping extends AbstractTestCase
36  {
37      public static class Container
38      {
39          private Element _element1;
40          private Element _element2;
41  
42          public Element getElement1()
43          {
44              return _element1;
45          }
46  
47          public void setElement1(Element element)
48          {
49              _element1 = element;
50          }
51  
52          public Element getElement2()
53          {
54              return _element2;
55          }
56  
57          public void setElement2(Element element)
58          {
59              _element2 = element;
60          }
61      }
62  
63      public static interface Element
64      {}
65  
66      public static class ElementA implements Element
67      {}
68  
69      public static class ElementB implements Element
70      {}
71  
72      private static final String MAPPING =
73          "<?xml version=\"1.0\"?>\n"+
74          "<betwixt-config>\n"+
75          "  <class name=\"org.apache.commons.betwixt.TestReferenceMapping$Container\">\n"+
76          "    <element name=\"container\">\n"+
77          "      <element property=\"element1\"/>\n"+
78          "      <element name=\"element2\" property=\"element2\"/>\n"+
79          "    </element>\n"+
80          "  </class>\n"+
81          "  <class name=\"org.apache.commons.betwixt.TestReferenceMapping$ElementA\">\n"+
82          "    <element name=\"elementA\"/>\n"+
83          "  </class>\n"+
84          "  <class name=\"org.apache.commons.betwixt.TestReferenceMapping$ElementB\">\n"+
85          "    <element name=\"elementB\"/>\n"+
86          "  </class>\n"+
87          "</betwixt-config>";
88      private static final String EXPECTED =
89          "<?xml version=\"1.0\" ?>\n"+
90          "  <container>\n"+
91          "    <elementB/>\n"+
92          "    <element2/>\n"+
93          "  </container>\n";
94      
95      public TestReferenceMapping(String testName)
96      {
97          super(testName);
98      }
99  
100     public void testRoundTripWithSingleMappingFile() throws IOException, SAXException, IntrospectionException
101     {
102         Container container = new Container();
103 
104         container.setElement1(new ElementB());
105         container.setElement2(new ElementA());
106 
107         StringWriter outputWriter = new StringWriter();
108 
109         outputWriter.write("<?xml version=\"1.0\" ?>\n");
110 
111         BeanWriter beanWriter = new BeanWriter(outputWriter);
112         beanWriter.setEndOfLine("\n");
113         beanWriter.enablePrettyPrint();
114         beanWriter.setWriteEmptyElements(true);
115         beanWriter.getBindingConfiguration().setMapIDs(false);
116         beanWriter.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING)));
117         beanWriter.setEndOfLine("\n"); //force to ensure matches on expected
118         beanWriter.write(container);
119 
120         String output = outputWriter.toString();
121 
122         assertEquals(EXPECTED, output);
123             
124         BeanReader beanReader = new BeanReader();
125 
126         beanReader.registerMultiMapping(new InputSource(new StringReader(MAPPING)));
127 
128         StringReader xmlReader = new StringReader(output);
129 
130         container = (Container)beanReader.parse(xmlReader);
131 
132         assertTrue(container.getElement1() instanceof ElementB);
133         // betwixt cannot know which type the element has
134         assertNull(container.getElement2());
135     }
136 }