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  import java.io.StringWriter;
21  
22  import org.apache.commons.betwixt.AbstractTestCase;
23  import org.apache.commons.betwixt.ElementDescriptor;
24  import org.apache.commons.betwixt.XMLBeanInfo;
25  import org.apache.commons.betwixt.XMLIntrospector;
26  import org.apache.commons.betwixt.io.BeanReader;
27  import org.apache.commons.betwixt.io.BeanWriter;
28  import org.xml.sax.InputSource;
29  
30  /**
31   * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
32   */
33  public class TestCustomDotBetwixt extends AbstractTestCase {
34  
35      public TestCustomDotBetwixt(String testName) {
36          super(testName);
37      }
38      
39      public void testIntrospectWithCustomDotBetwixt() throws Exception {
40          StringReader reader = new StringReader(
41                  "<?xml version='1.0' ?>" +
42                  "<info>" +
43                  "    <element name='jelly'>" +
44                  "        <element name='wibble' property='alpha'/>" +
45                  "        <element name='wobble' property='beta'/>" +
46                  "    </element>" +
47                  "</info>");
48          XMLIntrospector introspector = new XMLIntrospector();
49          XMLBeanInfo xmlBeanInfo = introspector.introspect(SimpleTestBean.class, new InputSource(reader));
50          
51          ElementDescriptor elementDescriptor = xmlBeanInfo.getElementDescriptor();
52          assertEquals("Root is jelly", "jelly", elementDescriptor.getLocalName());
53          ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors();
54          assertEquals("Expected two child elements", 2, childDescriptors.length);
55          assertEquals("Wibble comes first", "wibble", childDescriptors[0].getLocalName());
56          assertEquals("Wobble comes last", "wobble", childDescriptors[1].getLocalName());
57          
58          reader = new StringReader(
59                  "<?xml version='1.0' ?>" +
60                  "<info>" +
61                  "    <element name='not-jelly'>" +
62                  "        <element name='no-wibble' property='alpha'/>" +
63                  "        <element name='no-wobble' property='beta'/>" +
64                  "    </element>" +
65                  "</info>");
66  
67          xmlBeanInfo = introspector.introspect(SimpleTestBean.class, new InputSource(reader));
68          
69          elementDescriptor = xmlBeanInfo.getElementDescriptor();
70          assertEquals("Root is not-jelly", "not-jelly", elementDescriptor.getLocalName());
71          childDescriptors = elementDescriptor.getElementDescriptors();
72          assertEquals("Expected two child elements", 2, childDescriptors.length);
73          assertEquals("No wibble comes first", "no-wibble", childDescriptors[0].getLocalName());
74          assertEquals("No wobble comes last", "no-wobble", childDescriptors[1].getLocalName());
75      }
76   
77      
78      public void testRegisterCustomDotBetwixt() throws Exception {
79          StringReader reader = new StringReader(
80                  "<?xml version='1.0' ?>" +
81                  "<info>" +
82                  "    <element name='jelly'>" +
83                  "        <element name='wibble' property='alpha'/>" +
84                  "        <element name='wobble' property='beta'/>" +
85                  "    </element>" +
86                  "</info>");
87          XMLIntrospector introspector = new XMLIntrospector();
88          introspector.register(SimpleTestBean.class, new InputSource(reader));
89          XMLBeanInfo xmlBeanInfo = introspector.introspect(SimpleTestBean.class);
90          
91          ElementDescriptor elementDescriptor = xmlBeanInfo.getElementDescriptor();
92          assertEquals("Root is jelly", "jelly", elementDescriptor.getLocalName());
93          ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors();
94          assertEquals("Expected two child elements", 2, childDescriptors.length);
95          assertEquals("Wibble comes first", "wibble", childDescriptors[0].getLocalName());
96          assertEquals("Wobble comes last", "wobble", childDescriptors[1].getLocalName());
97      }
98      
99      public void testWriteCustomDotBetwixt() throws Exception {
100         StringReader reader = new StringReader(
101                 "<?xml version='1.0' ?>" +
102                 "<info>" +
103                 "    <element name='jelly'>" +
104                 "        <element name='wibble' property='alpha'/>" +
105                 "        <element name='wobble' property='beta'/>" +
106                 "    </element>" +
107                 "</info>");
108 
109         	StringWriter out = new StringWriter();
110         	out.write("<?xml version='1.0'?>");
111         	SimpleTestBean bean = new SimpleTestBean("one", "two", "three");
112         	
113         	BeanWriter writer = new BeanWriter(out);
114         	writer.getBindingConfiguration().setMapIDs(false);
115         	writer.write(bean, new InputSource(reader));
116         	
117         	String expected = "<?xml version='1.0'?>" +
118         			"<jelly><wibble>one</wibble><wobble>two</wobble></jelly>";
119         	xmlAssertIsomorphic(parseString(expected), parseString(out));
120     }
121 
122     
123     public void testReadCustomDotBetwixt() throws Exception {
124     	    String xml = "<?xml version='1.0'?>" +
125 		"<jelly><wibble>one</wibble><wobble>two</wobble></jelly>";
126     	    StringReader in = new StringReader(xml);
127     	    
128         StringReader dotBetwixt = new StringReader(
129                 "<?xml version='1.0' ?>" +
130                 "<info>" +
131                 "    <element name='jelly'>" +
132                 "        <element name='wibble' property='alpha'/>" +
133                 "        <element name='wobble' property='beta'/>" +
134                 "    </element>" +
135                 "</info>");
136 
137         	BeanReader reader = new BeanReader();
138         	reader.getBindingConfiguration().setMapIDs(false);
139         	reader.registerBeanClass(new InputSource(dotBetwixt), SimpleTestBean.class);
140         	SimpleTestBean bean = (SimpleTestBean) reader.parse(in);
141         	assertNotNull("Bean not mapped", bean);
142         	assertEquals("Property alpha mapping", "one", bean.getAlpha());
143         	assertEquals("Property beta mapping", "two", bean.getBeta());
144     }
145 }