KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > TestDynaBeanSupport


1 /*
2  * Copyright 2001-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;
17
18 import java.io.StringWriter JavaDoc;
19
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22 import junit.textui.TestRunner;
23
24 import org.apache.commons.beanutils.BasicDynaClass;
25 import org.apache.commons.beanutils.DynaBean;
26 import org.apache.commons.beanutils.DynaClass;
27 import org.apache.commons.beanutils.DynaProperty;
28 import org.apache.commons.betwixt.io.BeanWriter;
29 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
30
31 /** Test harness for the DynaBeans support
32   *
33   * @author <a HREF="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
34   * @version $Revision: 1.6 $
35   */

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