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.digester;
18  
19  import java.io.StringReader;
20  import java.util.Map;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.commons.betwixt.ElementDescriptor;
25  import org.apache.commons.betwixt.XMLBeanInfo;
26  import org.apache.commons.betwixt.XMLIntrospector;
27  import org.apache.commons.betwixt.dotbetwixt.ExampleBean;
28  
29  /**
30   * Tests for reading dot betwist files.
31   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
32   * @version $Revision: 155402 $
33   */
34  public class TestDigestMultiMapping extends TestCase {
35      
36      
37      public void testDigestWithOptions() throws Exception {
38          String xml = "<?xml version='1.0'?>" +
39          "<betwixt-config>" +
40          "  <class name=\"" + TestDigestMultiMapping.class.getName() + "\">" +
41          "    <element name=\"test-multi-mapping\">" +
42          "      <option>" +
43          "        <name>test-key-a</name>" +
44          "        <value>test-value-a</value>" +
45          "      </option>" +
46          "      <option>" +
47          "        <name>test-key-b</name>" +
48          "        <value>test-value-b</value>" +
49          "      </option>" +
50          "      <element name=\"maps\" property=\"maps\">" +
51          "        <option>" +
52          "          <name>test-key-c</name>" +
53          "          <value>test-value-c</value>" +
54          "        </option>" +
55          "      </element>" +
56          "    </element>" +
57          "  </class>" +
58          "</betwixt-config>";
59          
60          MultiMappingBeanInfoDigester digester = new MultiMappingBeanInfoDigester();
61          digester.setXMLIntrospector(new XMLIntrospector());
62          digester.setBeanClass(ExampleBean.class);
63          
64          digester.parse(new StringReader(xml));
65          Map beanInfoMap = digester.getBeanInfoMap(); 
66          assertTrue( beanInfoMap.containsKey(TestDigestMultiMapping.class));
67          
68          XMLBeanInfo xmlBeanInfo = (XMLBeanInfo) beanInfoMap.get(TestDigestMultiMapping.class);
69          assertNotNull(xmlBeanInfo);
70          
71          ElementDescriptor baseDescriptor = xmlBeanInfo.getElementDescriptor();
72          
73          assertEquals("Value one set on base", "test-value-a",  baseDescriptor.getOptions().getValue("test-key-a"));
74          assertEquals("Value two set on base", "test-value-b",  baseDescriptor.getOptions().getValue("test-key-b"));
75          assertNull("Value three not set on base",  baseDescriptor.getOptions().getValue("three"));
76  
77          assertEquals("Number of child elements", 1, baseDescriptor.getElementDescriptors().length);
78          
79          ElementDescriptor childDescriptor = baseDescriptor.getElementDescriptors()[0];
80          assertNull("Value one set on base",  childDescriptor.getOptions().getValue("test-key-a"));
81          assertNull("Value two set on base",  childDescriptor.getOptions().getValue("test-key-b"));
82          assertEquals("Value three set on child", "test-value-c",  childDescriptor.getOptions().getValue("test-key-c"));
83      }
84  }