KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > schema > TestSchema


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  
17 package org.apache.commons.betwixt.schema;
18
19 import java.io.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.betwixt.AbstractTestCase;
27 import org.apache.commons.betwixt.XMLIntrospector;
28 import org.apache.commons.betwixt.io.BeanReader;
29 import org.apache.commons.betwixt.io.BeanWriter;
30 import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
31 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
32 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
33
34 //import org.apache.commons.logging.impl.SimpleLog;
35
//import org.apache.commons.betwixt.io.BeanRuleSet;
36

37 /**
38  * This will test betwixt on handling a different kind of xml file, without
39  * a "collection" tag.
40  *
41  * @author <a HREF="mailto:martin@mvdb.net">Martin van den Bemt</a>
42  * @version $Id: TestSchema.java,v 1.11 2004/06/13 21:32:48 rdonkin Exp $
43  */

44 public class TestSchema extends AbstractTestCase
45 {
46     
47     public static Test suite()
48     {
49         return new TestSuite(TestSchema.class);
50     }
51
52     
53     public TestSchema(String JavaDoc testName)
54     {
55         super(testName);
56     }
57     
58     /**
59      * Test the roundtrip with an xml file that doesn't have
60      * collection elements, writes it with collection elements
61      * and then compares the 2 object, which should end up
62      * equal..
63      */

64     public void testCombinedRoundTrip()
65     throws Exception JavaDoc
66     {
67 // SimpleLog log = new SimpleLog("[CombinedRoundTrip:BeanRuleSet]");
68
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
69
// BeanRuleSet.setLog(log);
70

71 // log = new SimpleLog("[CombinedRoundTrip]");
72
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
73

74         BeanReader reader = createBeanReader();
75         
76         PhysicalSchema schema = (PhysicalSchema) reader.parse(
77             getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
78         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
79         write(schema, buffer, true);
80         
81 // log.debug(buffer.getBuffer().toString());
82

83         StringReader JavaDoc in = new StringReader JavaDoc(buffer.getBuffer().toString());
84         reader = createBeanReader();
85         XMLIntrospector intro = createXMLIntrospector();
86         DefaultXMLBeanInfoRegistry registry = new DefaultXMLBeanInfoRegistry();
87         intro.setRegistry(registry);
88         // we have written the xml file back with element collections,
89
// so we have to say to the reader we want to use that now
90
// (the default when creating in this test is not to use them)
91
intro.getConfiguration().setWrapCollectionsInElement(true);
92         // first flush the cash, else setting other options, doesn't
93
// end up in rereading / mapping the object model.
94
registry.flush();
95         // set the xmlIntrospector back to the reader
96
reader.setXMLIntrospector(intro);
97         reader.deregisterBeanClass(PhysicalSchema.class);
98         reader.getRules().clear();
99         reader.registerBeanClass(PhysicalSchema.class);
100         PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
101         buffer.close();
102         write(schema,buffer, true);
103         assertEquals(schema, schemaSecond);
104     }
105     /**
106      * Tests we can round trip from the XML -> bean -> XML -> bean.
107      * It will test if both object are identical.
108      * For this to actually work I implemented a details equals in my
109      * Beans..
110      */

111     public void testRoundTripWithoutCollectionElement()
112     throws Exception JavaDoc
113     {
114         BeanReader reader = createBeanReader();
115         PhysicalSchema schema = (PhysicalSchema) reader.parse(
116             getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
117         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
118         write(schema, buffer, false);
119         StringReader JavaDoc in = new StringReader JavaDoc(buffer.getBuffer().toString());
120         PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
121         assertEquals(schemaSecond, schema);
122     }
123     
124     /**
125      * Creates a beanReader
126      */

127     protected BeanReader createBeanReader()
128     throws Exception JavaDoc
129      {
130         BeanReader reader = new BeanReader();
131         reader.setXMLIntrospector(createXMLIntrospector());
132         // register the class which maps to the root element
133
// of the xml file (this depends on the NameMapper used.
134
reader.registerBeanClass(PhysicalSchema.class);
135         return reader;
136     }
137     
138     /**
139      * Set up the XMLIntroSpector
140      */

141     protected XMLIntrospector createXMLIntrospector() {
142         XMLIntrospector introspector = new XMLIntrospector();
143
144         // set elements for attributes to true
145
introspector.getConfiguration().setAttributesForPrimitives(true);
146
147         // Since we don't want to have collectionelements
148
// line <DBMSS>, we have to set this to false,
149
// since the default is true.
150
introspector.getConfiguration().setWrapCollectionsInElement(false);
151
152         // We have to use the HyphenatedNameMapper
153
// Since we want the names to resolve from eg PhysicalSchema
154
// to PHYSICAL_SCHEMA.
155
// we pass to the mapper we want uppercase and use _ for name
156
// seperation.
157
// This will set our ElementMapper.
158
introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper(true, "_"));
159         // since our attribute names will use a different
160
// naming convention in our xml file (just all lowercase)
161
// we set another mapper for the attributes
162
introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper());
163
164         return introspector;
165     }
166     
167     /**
168      * Opens a writer and writes an object model according to the
169      * retrieved bean
170      */

171     private void write(Object JavaDoc bean, Writer JavaDoc out, boolean wrapCollectionsInElement)
172     throws Exception JavaDoc
173     {
174         BeanWriter writer = new BeanWriter(out);
175         writer.setWriteEmptyElements( true );
176         writer.setXMLIntrospector(createXMLIntrospector());
177         // specifies weather to use collection elements or not.
178
writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(wrapCollectionsInElement);
179         // we don't want to write Id attributes to every element
180
// we just want our opbject model written nothing more..
181
writer.getBindingConfiguration().setMapIDs(false);
182         // the source has 2 spaces indention and \n as line seperator.
183
writer.setIndent(" ");
184         writer.setEndOfLine("\n");
185         writer.write(bean);
186     }
187 }
188
189
Popular Tags