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