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