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;
19  
20  /**
21   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
22   * @version $Revision: 561314 $
23   */
24  public class TestArrayMaps extends AbstractTestCase {
25      
26      private static final AddressBean[] EMPTY_ADDRESS_ARRAY = {};
27      private static final Class ADDRESS_ARRAY_CLASS 
28          = EMPTY_ADDRESS_ARRAY.getClass();
29  
30      public TestArrayMaps(String testName) {
31          super(testName);
32      }
33      
34      public void testIntrospection() throws Exception {
35          XMLIntrospector introspector = new XMLIntrospector();
36          introspector.getConfiguration().setAttributesForPrimitives(true);
37          
38          XMLBeanInfo xmlBeanInfo 
39              = introspector.introspect(AddressBookWithMapArrayAdder.class);
40          
41          ElementDescriptor beanDescriptor = xmlBeanInfo.getElementDescriptor();
42          ElementDescriptor[] childDescriptors = beanDescriptor.getElementDescriptors();
43          assertEquals("Only one child element", 1, childDescriptors.length);
44          ElementDescriptor wrappingDescriptor = childDescriptors[0];
45          ElementDescriptor[] wrappingChildDescriptors = wrappingDescriptor.getElementDescriptors();
46          assertEquals("One child descriptor", 1, wrappingChildDescriptors.length);
47          ElementDescriptor entryDescriptor = wrappingChildDescriptors[0];
48          ElementDescriptor[] entryChildDescriptors = entryDescriptor.getElementDescriptors();
49          assertEquals("Two child descriptors", 2, entryChildDescriptors.length);
50          ElementDescriptor keyDescriptor = null;
51          ElementDescriptor valueDescriptor = null;
52          if ("key".equals(entryChildDescriptors[0].getQualifiedName())) {
53              keyDescriptor = entryChildDescriptors[0];
54          }
55          if ("value".equals(entryChildDescriptors[0].getQualifiedName())) {
56              valueDescriptor = entryChildDescriptors[0];
57          }        
58          if ("key".equals(entryChildDescriptors[1].getQualifiedName())) {
59              keyDescriptor = entryChildDescriptors[1];
60          }
61          if ("value".equals(entryChildDescriptors[1].getQualifiedName())) {
62              valueDescriptor = entryChildDescriptors[1];
63          }
64          
65          assertNotNull("Expected key descriptor", keyDescriptor);
66          assertNotNull("Expected value descriptor", valueDescriptor);
67          assertNotNull("Expected key property type", keyDescriptor.getPropertyType());
68          assertNotNull("Expected value property type", valueDescriptor.getPropertyType());
69          
70          ElementDescriptor[] childValueDescriptors = valueDescriptor.getElementDescriptors();
71          assertEquals("One hollow child descriptor for array", 1, childValueDescriptors.length);
72          ElementDescriptor hollowValueDescriptor = childValueDescriptors[0];
73          assertEquals("Child descriptor is hollow", true, hollowValueDescriptor.isHollow());
74          assertEquals(
75              "Child descriptor has AddressBean[] property type", 
76              ADDRESS_ARRAY_CLASS, 
77              hollowValueDescriptor.getPropertyType());
78          assertEquals(
79              "Child descriptor has AddressBean[] singular property type", 
80              ADDRESS_ARRAY_CLASS, 
81              hollowValueDescriptor.getSingularPropertyType());
82      }
83  
84  }