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