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.dotbetwixt;
18  
19  import java.io.StringReader;
20  
21  import org.apache.commons.betwixt.AbstractTestCase;
22  import org.apache.commons.betwixt.AddressBean;
23  import org.apache.commons.betwixt.ElementDescriptor;
24  import org.apache.commons.betwixt.XMLBeanInfo;
25  import org.apache.commons.betwixt.XMLIntrospector;
26  import org.xml.sax.InputSource;
27  
28  /**
29   * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
30   */
31  public class TestMultiMap extends AbstractTestCase {
32  
33      public TestMultiMap(String testName) {
34          super(testName);
35      }
36  
37      private static final String MAPPING = "<?xml version='1.0'?>" +
38      		"     <betwixt-config>" +
39      		"            <class name='org.apache.commons.betwixt.PartyBean'>" +
40      		"    		    	<element name='party'>" +
41      		"                <element name='the-excuse' property='excuse'/>" +
42      		"                <element name='location' property='venue'/>      " +
43      		"                <element name='time' property='fromHour'/>" +
44      		"              </element>" +
45      		"            </class>" +
46      		"            <class name='org.apache.commons.betwixt.AddressBean'>" +
47      		"              <element name='not-address'>" +
48      		"                <element name='not-street' property='street'/>" +
49      		"                <element name='not-city' property='city'/>" +
50      		"                <element name='not-code' property='code'/>" +
51      		"                <element name='not-country' property='country'/>" +
52      		"              </element>" +
53      		"            </class>" +
54      		"            <class name='org.apache.commons.betwixt.dotbetwixt.SimpleTestBean'>" +
55          "                <element name='jelly'>" +
56          "                    <element name='wibble' property='alpha'/>" +
57          "                    <element name='wobble' property='beta'/>" +
58          "                </element>" +
59      		"            </class>" +
60      		"     </betwixt-config>";
61  
62      public void testRegisterMultiMapping() throws Exception {
63          XMLIntrospector xmlIntrospector = new XMLIntrospector();
64          Class[] mapped = xmlIntrospector.register(new InputSource(new StringReader(MAPPING)));
65          
66          assertEquals("Mapped classes", 3, mapped.length);
67          
68          XMLBeanInfo beanInfo = xmlIntrospector.introspect(AddressBean.class);
69          assertNotNull("Bean info mapping", beanInfo);
70          ElementDescriptor descriptor = beanInfo.getElementDescriptor();
71          assertEquals("Root element name", "not-address", descriptor.getLocalName());
72          ElementDescriptor[] childDescriptors = descriptor.getElementDescriptors();
73          assertEquals("4 child elements", 4, childDescriptors.length);
74          assertEquals("First element", "not-street", childDescriptors[0].getLocalName());
75          assertEquals("Second element", "not-city", childDescriptors[1].getLocalName());
76          assertEquals("Third element", "not-code", childDescriptors[2].getLocalName());
77          assertEquals("Forth element", "not-country", childDescriptors[3].getLocalName());
78                
79          beanInfo = xmlIntrospector.introspect(SimpleTestBean.class);
80          assertNotNull("Bean info mapping", beanInfo);
81          descriptor = beanInfo.getElementDescriptor();
82          assertEquals("Root element name", "jelly", descriptor.getLocalName());
83          childDescriptors = descriptor.getElementDescriptors();
84          assertEquals("Child elements", 2, childDescriptors.length);
85          assertEquals("First element", "wibble", childDescriptors[0].getLocalName());
86          assertEquals("Second element", "wobble", childDescriptors[1].getLocalName());
87               
88      }
89  }