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.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.xml.sax.InputSource;
33  
34  
35  
36  public class TestPolyList extends AbstractTestCase {
37      public static Log log = LogFactory.getLog(TestPolyList.class);
38  
39  	public TestPolyList(String testName) {
40  		super(testName);
41          
42          log.info("Mapping:\n" + MAPPING);
43  	}
44  
45  	private static final String XML = "<AlphaList><AlphaOneImpl><one>1</one></AlphaOneImpl><AlphaTwoImpl><two>2</two></AlphaTwoImpl></AlphaList>";
46  
47  	private static final String MAPPING = "<?xml version='1.0'?>"
48  			+ "<betwixt-config>" 
49  			+ "  <class name='org.apache.commons.betwixt.poly.AlphaOneImpl'>"
50  			+ "    <element name='AlphaOneImpl'>" 
51              + "      <element name='one' property='one'/>" 
52              + "    </element>" 
53  			+ "  </class>"
54  			+ "  <class name='org.apache.commons.betwixt.poly.AlphaTwoImpl'>"
55  			+ "    <element name='AlphaTwoImpl'>" 
56              + "      <element name='two' property='two'/>" 
57              + "    </element>" 
58  			+ "  </class>"
59  			+ "</betwixt-config>";
60  	
61  	public void testWrite() throws Exception {
62  		AlphaList bean = new AlphaList();
63  		AlphaOneImpl one = new AlphaOneImpl("1");
64  		bean.add(one);
65  		AlphaTwoImpl two = new AlphaTwoImpl("2");
66  		bean.add(two);
67  
68  		StringWriter out = new StringWriter();
69  		BeanWriter writer = new BeanWriter(out);
70  		StringReader mapping = new StringReader(MAPPING);
71  		writer.getXMLIntrospector().register(new InputSource(mapping));
72  		configure(writer.getBindingConfiguration());
73  		writer.write(bean);
74  
75          String written = out.getBuffer().toString();
76          log.info("Written:\n" + written);
77          
78          xmlAssertIsomorphicContent(
79                  parseString(XML),
80                  parseString(written),
81                  true);
82  	}
83  
84  	public void testRead() throws Exception {
85  		StringReader in = new StringReader(XML);
86  		BeanReader reader = new BeanReader();
87  		StringReader mapping = new StringReader(MAPPING);
88  		reader.registerMultiMapping(new InputSource(mapping));
89          reader.registerBeanClass(AlphaList.class);
90  		configure(reader.getBindingConfiguration());
91  		Object bean = reader.parse(in);
92  		assertTrue(bean instanceof AlphaList);
93  		AlphaList list = (AlphaList) bean;
94  		assertEquals(2, list.size());
95          
96          assertTrue(list.get(0) instanceof AlphaOneImpl);
97          AlphaOneImpl one = (AlphaOneImpl)list.get(0);
98          assertEquals("1", one.alpha());
99  
100         assertTrue(list.get(1) instanceof AlphaTwoImpl);
101         AlphaTwoImpl two = (AlphaTwoImpl)list.get(1);
102         assertEquals("2", two.alpha());
103     }
104 
105 	public void testIntrospection() throws Exception	 {
106 		XMLIntrospector introspector = new XMLIntrospector();
107 		XMLBeanInfo beanInfo = introspector.introspect(AlphaList.class);
108 		ElementDescriptor[] descriptors = beanInfo.getElementDescriptor().getElementDescriptors();
109 		assertEquals("One descriptor", 1, descriptors.length);
110         assertTrue(descriptors[0].isHollow());
111         assertNotNull(descriptors[0].getContextExpression());
112         assertNotNull(descriptors[0].getUpdater());
113         assertEquals("A list can contain any object", Object.class, descriptors[0].getSingularPropertyType());
114 	}
115 
116 	private void configure(BindingConfiguration configuration) {
117 		configuration.setMapIDs(false);
118 	}
119 }