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.dotbetwixt;
19  
20  import java.io.StringReader;
21  import java.io.StringWriter;
22  import java.util.List;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.commons.betwixt.ElementDescriptor;
28  import org.apache.commons.betwixt.XMLBeanInfo;
29  import org.apache.commons.betwixt.XMLIntrospector;
30  import org.apache.commons.betwixt.io.BeanReader;
31  import org.apache.commons.betwixt.io.BeanWriter;
32  import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
33  import org.apache.commons.betwixt.xmlunit.XmlTestCase;
34  
35  /** 
36    * Test customization of xml to bean mapping using .betwixt files.
37    *
38    * @author Robert Burrell Donkin
39    */
40  public class TestXmlToBean extends XmlTestCase {
41  
42  //--------------------------------- Test Suite
43      
44      public static Test suite() {
45          return new TestSuite(TestXmlToBean.class);
46      }
47      
48  //--------------------------------- Constructor
49          
50      public TestXmlToBean(String testName) {
51          super(testName);
52      }
53  
54  //---------------------------------- Tests
55      
56      public void testForceAccessibleSuper() throws Exception {
57          XMLIntrospector xmlIntrospector = new XMLIntrospector();
58          XMLBeanInfo xmlBeanInfo = xmlIntrospector.introspect(MixedUpdatersBean.class);
59          ElementDescriptor[] descriptors = xmlBeanInfo.getElementDescriptor().getElementDescriptors();
60          boolean propertyFound = false;
61          for (int i=0; i<descriptors.length ; i++) {
62              ElementDescriptor descriptor = descriptors[i];
63              if ("private-super".equals(descriptor.getLocalName())) {
64                  propertyFound = true;
65                  assertNotNull("Updater found", descriptor.getUpdater());
66                  assertNotNull("Expression found", descriptor.getTextExpression());
67              }
68          }
69          assertTrue("Found inaccessible super methods", propertyFound);
70      }
71      
72      public void testCustomUpdaters() throws Exception {
73          // might as well check writer whilst we're at it
74          MixedUpdatersBean bean = new MixedUpdatersBean("Lov");
75          bean.badNameSetter("Hate");
76          bean.addItem("White");
77          bean.badItemAdder("Black");
78          bean.addItem("Life");
79          bean.badItemAdder("Death");
80          bean.privatePropertyWorkaroundSetter("Private");
81          bean.getPrivateItems().add("private item 1");
82          bean.privateField = 100;
83  
84          StringWriter out = new StringWriter();
85          out.write("<?xml version='1.0'?>");
86          BeanWriter writer = new BeanWriter(out);
87          
88          writer.getBindingConfiguration().setMapIDs(false);
89          writer.write(bean);
90  
91      	String xml = "<?xml version='1.0'?><mixed><name>Lov</name><bad-name>Hate</bad-name>"
92            + "<items><item>White</item><item>Life</item></items>"
93            + "<bad-items><bad-item>Black</bad-item><bad-item>Death</bad-item></bad-items>"
94            + "<private-property>Private</private-property>"
95            + "<private-items><private-item>private item 1</private-item></private-items>" +
96              "<private-super>100</private-super>"
97            + "</mixed>";
98          
99          xmlAssertIsomorphicContent(
100                     parseString(xml),
101                     parseString(out.toString()),
102                     true);
103         
104         // now we'll test reading via round tripping
105         BeanReader reader = new BeanReader();
106         reader.getBindingConfiguration().setMapIDs(false);
107         reader.registerBeanClass("mixed", MixedUpdatersBean.class);
108         bean = (MixedUpdatersBean) reader.parse(new StringReader(xml));
109         
110         assertEquals("Name incorrect", "Lov", bean.getName());
111         assertEquals("BadName incorrect", "Hate", bean.getBadName());
112         List items = bean.getItems();
113         assertEquals("Wrong number of items", 2, items.size());
114         assertEquals("Item one wrong", "White", items.get(0));
115         assertEquals("Item two wrong", "Life", items.get(1));
116         List badItems = bean.getBadItems();
117         assertEquals("Wrong number of bad items", 2, badItems.size());
118         // awaiting implementation
119         //assertEquals("Bad item one wrong", "Black", badItems.get(0));
120         //assertEquals("Bad item two wrong", "Death", badItems.get(1));       
121         assertEquals("Private property incorrect", "Private", bean.getPrivateProperty());
122 
123         //this shows that a private adder can be utilized
124         List privateItems = bean.getPrivateItems();
125         assertEquals("Wrong number of private items", 1, privateItems.size());
126         //TODO can't assert contents - gets the right number of items, but each is null (badItems, too)
127         assertEquals("Private property accessed on super", 100, bean.privateField);
128     }
129 
130     
131     /** Test output of bean with mixed content */
132     public void testMixedContent() throws Exception {
133         
134         StringReader xml = new StringReader(
135             "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life' gamma='42'>"
136             + "The Universe And Everything</deep-thought>");
137             
138         BeanReader reader = new BeanReader();
139         reader.registerBeanClass(MixedContentOne.class);
140         Object resultObject = reader.parse(xml);
141         assertEquals("Object is MixedContentOne", true, resultObject instanceof MixedContentOne);
142         MixedContentOne result = (MixedContentOne) resultObject;
143         assertEquals("Property Alpha matches", "Life", result.getAlpha());
144         assertEquals("Property Beta matches", "The Universe And Everything", result.getBeta());
145         assertEquals("Property Gamma matches", 42, result.getGamma());
146     }
147     
148     
149     /** Tests basic use of an implementation for an interface */
150     public void _testBasicInterfaceImpl() throws Exception {
151     
152         ExampleBean bean = new ExampleBean("Alice");
153         bean.addExample(new ExampleImpl(1, "Mad Hatter"));
154         bean.addExample(new ExampleImpl(2, "March Hare"));
155         bean.addExample(new ExampleImpl(3, "Dormouse"));
156         
157         String xml = "<?xml version='1.0' encoding='UTF-8'?>"
158             + "<example-bean><name>Alice</name>"
159             + "<example><id>1</id><name>Mad Hatter</name></example>"
160             + "<example><id>2</id><name>March Hare</name></example>"
161             + "<example><id>3</id><name>Dormouse</name></example>"
162             + "</example-bean>";
163         
164         
165         BeanReader reader = new BeanReader();
166         reader.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
167         reader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
168         reader.registerBeanClass( ExampleBean.class );
169         
170         StringReader in = new StringReader( xml );
171         ExampleBean out = (ExampleBean) reader.parse( in ); 
172         assertEquals("Interface read failed", bean, out);
173         
174     }      
175 }
176