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   
18  package org.apache.commons.betwixt.poly;
19  
20  import java.io.StringReader;
21  import java.io.StringWriter;
22  
23  import org.apache.commons.betwixt.AbstractTestCase;
24  import org.apache.commons.betwixt.BindingConfiguration;
25  import org.apache.commons.betwixt.ElementDescriptor;
26  import org.apache.commons.betwixt.XMLBeanInfo;
27  import org.apache.commons.betwixt.XMLIntrospector;
28  import org.apache.commons.betwixt.io.BeanReader;
29  import org.apache.commons.betwixt.io.BeanWriter;
30  import org.xml.sax.InputSource;
31  
32  public class TestPolyListHolder extends AbstractTestCase {
33  
34      public TestPolyListHolder(String testName) {
35          super(testName);
36      }
37  
38      private static final String XML = "<AlphaListHolder>" +
39              "    <AlphaList>" +
40              "        <AlphaOneImpl>" +
41              "            <one>1</one>" +
42              "        </AlphaOneImpl>" +
43              "        <AlphaTwoImpl>" +
44              "            <two>2</two>" +
45              "        </AlphaTwoImpl>" +
46              "    </AlphaList>" +
47              "</AlphaListHolder>";
48  
49      private static final String MAPPING = "<?xml version='1.0'?>"
50              + "<betwixt-config>" 
51              + "  <class name='org.apache.commons.betwixt.poly.AlphaListHolder'>"
52              + "    <element name='AlphaListHolder'>"
53              + "       <element name='AlphaList' property='alphaList' updater='setAlphaList'/>"
54              + "    </element>"
55              + "  </class>"
56              + "  <class name='org.apache.commons.betwixt.poly.AlphaOneImpl'>"
57              + "    <element name='AlphaOneImpl'>" 
58              + "      <element name='one' property='one'/>" 
59              + "    </element>" 
60              + "  </class>"
61              + "  <class name='org.apache.commons.betwixt.poly.AlphaTwoImpl'>"
62              + "    <element name='AlphaTwoImpl'>" 
63              + "      <element name='two' property='two'/>" 
64              + "    </element>" 
65              + "  </class>"
66              + "</betwixt-config>";
67      
68      public void testWrite() throws Exception {
69          AlphaList list = new AlphaList();
70          AlphaOneImpl one = new AlphaOneImpl("1");
71          list.add(one);
72          AlphaTwoImpl two = new AlphaTwoImpl("2");
73          list.add(two);
74          
75          AlphaListHolder bean = new AlphaListHolder();
76          bean.setAlphaList(list);
77  
78          StringWriter out = new StringWriter();
79          BeanWriter writer = new BeanWriter(out);
80          StringReader mapping = new StringReader(MAPPING);
81          writer.getXMLIntrospector().register(new InputSource(mapping));
82          configure(writer.getBindingConfiguration());
83          writer.write(bean);
84  
85          String written = out.getBuffer().toString();
86          
87          xmlAssertIsomorphicContent(
88                  parseString(XML),
89                  parseString(written),
90                  true);
91      }
92  
93      public void testRead() throws Exception {
94  
95          StringReader in = new StringReader(XML);
96          BeanReader reader = new BeanReader();
97          
98          StringReader mapping = new StringReader(MAPPING);
99          reader.registerMultiMapping(new InputSource(mapping));
100         reader.registerBeanClass(AlphaList.class);
101         configure(reader.getBindingConfiguration());
102         Object bean = reader.parse(in);
103         
104         assertTrue(bean instanceof AlphaListHolder);
105         AlphaListHolder holder = (AlphaListHolder)bean;
106         
107         AlphaList list = holder.getAlphaList();
108         assertNotNull(list);
109         assertEquals(2, list.size());
110         
111         assertTrue(list.get(0) instanceof AlphaOneImpl);
112         AlphaOneImpl one = (AlphaOneImpl)list.get(0);
113         assertEquals("1", one.alpha());
114 
115         assertTrue(list.get(1) instanceof AlphaTwoImpl);
116         AlphaTwoImpl two = (AlphaTwoImpl)list.get(1);
117         assertEquals("2", two.alpha());
118     }
119 
120     private void configure(BindingConfiguration configuration) {
121         configuration.setMapIDs(false);
122     }
123     
124 
125     public void testIntrospection() throws Exception {
126         XMLIntrospector introspector = new XMLIntrospector();
127         
128         StringReader mapping = new StringReader(MAPPING);
129         introspector.register(new InputSource(mapping));
130        
131         XMLBeanInfo beanInfo = introspector.introspect(AlphaListHolder.class);
132         ElementDescriptor descriptor = beanInfo.getElementDescriptor();
133         assertNotNull(descriptor);
134         ElementDescriptor[] descriptors = descriptor.getElementDescriptors();
135         assertNotNull(descriptors);
136         assertEquals("Only one descriptor", 1, descriptors.length);
137         assertNotNull("Expected updater", descriptors[0].getUpdater());
138     }
139 }