KickJava   Java API By Example, From Geeks To Geeks.

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


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
17
18 package org.apache.commons.betwixt;
19
20 import java.io.StringReader JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.commons.betwixt.io.BeanReader;
25 import org.apache.commons.betwixt.io.BeanWriter;
26 import org.apache.commons.betwixt.strategy.CapitalizeNameMapper;
27
28 /**
29  * @author <a HREF='http://jakarta.apache.org/'>Jakarta Commons Team</a>
30  * @version $Revision: 1.2 $
31  */

32 public class TestCollectives extends AbstractTestCase{
33     
34     private IntrospectionConfiguration categoriesIntrospectionConfiguration = new IntrospectionConfiguration();
35     private BindingConfiguration noIDsBindingConfiguration = new BindingConfiguration();
36     
37     public TestCollectives(String JavaDoc testName) {
38         super(testName);
39         
40         CapitalizeNameMapper capitalizeNameMapper = new CapitalizeNameMapper();
41         categoriesIntrospectionConfiguration.setAttributesForPrimitives(false);
42         categoriesIntrospectionConfiguration.setElementNameMapper(capitalizeNameMapper);
43         categoriesIntrospectionConfiguration.setAttributeNameMapper(capitalizeNameMapper);
44         categoriesIntrospectionConfiguration.setWrapCollectionsInElement(false);
45         
46         noIDsBindingConfiguration.setMapIDs(false);
47     }
48
49
50     public void testWriteCategories() throws Exception JavaDoc {
51         StringWriter JavaDoc out = new StringWriter JavaDoc();
52         out.write("<?xml version='1.0'?>");
53         BeanWriter writer = new BeanWriter(out);
54         writer.getXMLIntrospector().setConfiguration(categoriesIntrospectionConfiguration);
55         writer.setBindingConfiguration(noIDsBindingConfiguration);
56         
57         Categories categories = new Categories();
58         categories.addCategory(new Category("Runs"));
59         categories.addCategory(new Category("Innings"));
60         categories.addCategory(new Category("Dismissals"));
61         categories.addCategory(new Category("High Score"));
62         categories.addCategory(new Category("Average"));
63         
64         writer.write(categories);
65         
66         String JavaDoc xml = out.getBuffer().toString();
67         String JavaDoc expected = "<?xml version='1.0'?><Categories>" +
68             "<Category><Name>Runs</Name></Category>" +
69             "<Category><Name>Innings</Name></Category>" +
70             "<Category><Name>Dismissals</Name></Category>" +
71             "<Category><Name>High Score</Name></Category>" +
72             "<Category><Name>Average</Name></Category>" +
73             "</Categories>";
74             
75        xmlAssertIsomorphicContent(parseString(expected), parseString(xml));
76     }
77     
78     public void testReadCategories() throws Exception JavaDoc {
79         BeanReader beanReader = new BeanReader();
80         beanReader.getXMLIntrospector().setConfiguration(categoriesIntrospectionConfiguration);
81         beanReader.setBindingConfiguration(noIDsBindingConfiguration);
82         beanReader.registerBeanClass(Categories.class);
83
84         String JavaDoc xml = "<?xml version='1.0'?><Categories>" +
85             "<Category><Name>Runs</Name></Category>" +
86             "<Category><Name>Innings</Name></Category>" +
87             "<Category><Name>Dismissals</Name></Category>" +
88             "<Category><Name>High Score</Name></Category>" +
89             "<Category><Name>Average</Name></Category>" +
90             "</Categories>";
91        
92        StringReader JavaDoc in = new StringReader JavaDoc(xml);
93        
94        Categories bean = (Categories) beanReader.parse(in);
95        
96        assertEquals("5 categories", 5, bean.size());
97        
98        Iterator JavaDoc it = bean.getCategories();
99        assertEquals("Runs category", new Category("Runs"), it.next());
100        assertEquals("Runs category", new Category("Innings"), it.next());
101        assertEquals("Runs category", new Category("Dismissals"), it.next());
102        assertEquals("Runs category", new Category("High Score"), it.next());
103        assertEquals("Runs category", new Category("Average"), it.next());
104        
105     }
106     
107
108 }
109
Popular Tags