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;
18  
19  import java.io.StringWriter;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  import junit.textui.TestRunner;
24  
25  import org.apache.commons.beanutils.BasicDynaClass;
26  import org.apache.commons.beanutils.DynaBean;
27  import org.apache.commons.beanutils.DynaClass;
28  import org.apache.commons.beanutils.DynaProperty;
29  import org.apache.commons.betwixt.io.BeanWriter;
30  import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
31  
32  /** Test harness for the DynaBeans support
33    *
34    * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
35    * @version $Revision: 438373 $
36    */
37  public class TestDynaBeanSupport extends AbstractTestCase {
38      
39      public static void main( String[] args ) {
40          TestRunner.run( suite() );
41      }
42      
43      public static Test suite() {
44          return new TestSuite(TestDynaBeanSupport.class);
45      }
46      
47      public TestDynaBeanSupport(String testName) {
48          super(testName);
49      }
50          
51      public void testIntrospectDynaBean() throws Exception
52      {
53          XMLIntrospector introspector = new XMLIntrospector();
54          introspector.getConfiguration().setAttributesForPrimitives(false);
55          XMLBeanInfo beanInfo = introspector.introspect(createDynasaurClass());
56          ElementDescriptor baseElement = beanInfo.getElementDescriptor();
57          // no attributes
58          assertEquals("Correct number of attributes", 0, baseElement.getAttributeDescriptors().length);
59          ElementDescriptor[] descriptors = baseElement.getElementDescriptors();
60          assertEquals("Correct number of elements", 3, descriptors.length);
61          
62          boolean matchedSpecies = false;
63          boolean matchedIsRaptor = false;
64          boolean matchedPeriod = false;
65          
66          for (int i=0, size = descriptors.length; i< size; i++) {
67              if ("Species".equals(descriptors[i].getPropertyName())) {
68                  matchedSpecies = true;
69              }
70              
71              if ("isRaptor".equals(descriptors[i].getPropertyName())) {
72                  matchedIsRaptor = true;
73              }
74              
75              if ("Period".equals(descriptors[i].getPropertyName())) {
76                  matchedPeriod = true;
77              }
78          }
79          
80          assertTrue("Species descriptor not found", matchedSpecies);
81          assertTrue("isRaptor descriptor not found", matchedIsRaptor);
82          assertTrue("Period descriptor not found", matchedPeriod);
83      }
84      
85      public void testWriteDynaBean() throws Exception {
86          DynaBean dynasaur = createDynasaurClass().newInstance();
87          dynasaur.set("Species", "Allosaurus");
88          dynasaur.set("isRaptor", Boolean.TRUE);
89          dynasaur.set("Period", "Jurassic");
90          
91          StringWriter out = new StringWriter();
92          out.write("<?xml version='1.0'?>");
93          BeanWriter writer = new BeanWriter(out);
94  		writer.getBindingConfiguration().setMapIDs(false);
95          writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new DecapitalizeNameMapper());
96          writer.write(dynasaur);
97          
98          String xml = "<?xml version='1.0'?><dynasaur><species>Allosaurus</species>"
99              + "<isRaptor>true</isRaptor><period>Jurassic</period></dynasaur>";
100         
101         xmlAssertIsomorphicContent(	
102                             "Test write dyna beans",
103                             parseString(xml), 
104                             parseString(out.getBuffer().toString()),
105                             true); 
106     }
107     
108     public void testOverrideWithDotBetwixt() throws Exception {
109         DynaWithDotBetwixt bean = new DynaWithDotBetwixt("Tweedledum","Tweedledee");
110         StringWriter out = new StringWriter();
111         out.write("<?xml version='1.0'?>");
112         BeanWriter writer = new BeanWriter(out);
113 		writer.getBindingConfiguration().setMapIDs(false);
114         writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new DecapitalizeNameMapper());
115         writer.write("bean", bean);
116         
117         String xml = "<?xml version='1.0'?><bean><ndp>Tweedledum</ndp></bean>";
118         xmlAssertIsomorphicContent(	
119                             "Test write dyna beans with dt betwixt",
120                             parseString(xml), 
121                             parseString(out.getBuffer().toString()),
122                             true); 
123         
124     }
125     
126     private DynaClass createDynasaurClass() throws Exception {
127         DynaClass dynaClass = new BasicDynaClass
128                 ("Dynasaur", null,
129                         new DynaProperty[]{
130                             new DynaProperty("Species", String.class),
131                             new DynaProperty("isRaptor", Boolean.TYPE),
132                             new DynaProperty("Period", String.class),
133                         });
134         return (dynaClass);
135     }
136 }
137 
138