1 /*
2 * $Header: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/TestSchema.java,v 1.5 2002/12/30 18:16:47 mvdb Exp $
3 * $Revision: 1.5 $
4 * $Date: 2002/12/30 18:16:47 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * $Id: TestSchema.java,v 1.5 2002/12/30 18:16:47 mvdb Exp $
61 */
62 package org.apache.commons.betwixt.schema;
63
64 import java.io.StringReader;
65 import java.io.StringWriter;
66 import java.io.Writer;
67
68 import junit.framework.Test;
69 import junit.framework.TestSuite;
70
71 import org.apache.commons.betwixt.AbstractTestCase;
72 import org.apache.commons.betwixt.XMLIntrospector;
73 import org.apache.commons.betwixt.io.BeanReader;
74 import org.apache.commons.betwixt.io.BeanWriter;
75 import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
76 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
77 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
78
79
80 /***
81 * This will test betwixt on handling a different kind of xml file, without
82 * a "collection" tag.
83 *
84 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
85 * @version $Id: TestSchema.java,v 1.5 2002/12/30 18:16:47 mvdb Exp $
86 */
87 public class TestSchema extends AbstractTestCase
88 {
89
90 public static Test suite()
91 {
92 return new TestSuite(TestSchema.class);
93 }
94
95
96 public TestSchema(String testName)
97 {
98 super(testName);
99 }
100
101 /***
102 * Test the roundtrip with an xml file that doesn't have
103 * collection elements, writes it with collection elements
104 * and then compares the 2 object, which should end up
105 * equal..
106 */
107 public void testCombinedRoundTrip()
108 throws Exception
109 {
110 BeanReader reader = createBeanReader();
111 PhysicalSchema schema = (PhysicalSchema) reader.parse(
112 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
113 StringWriter buffer = new StringWriter();
114 write(schema, buffer, true);
115 StringReader in = new StringReader(buffer.getBuffer().toString());
116 reader = createBeanReader();
117 XMLIntrospector intro = createXMLIntrospector();
118 DefaultXMLBeanInfoRegistry registry = new DefaultXMLBeanInfoRegistry();
119 intro.setRegistry(registry);
120 // we have written the xml file back with element collections,
121 // so we have to say to the reader we want to use that now
122 // (the default when creating in this test is not to use them)
123 intro.setWrapCollectionsInElement(true);
124 // first flush the cash, else setting other options, doesn't
125 // end up in rereading / mapping the object model.
126 registry.flush();
127 // set the xmlIntrospector back to the reader
128 reader.setXMLIntrospector(intro);
129 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
130 buffer.close();
131 write(schema,buffer, true);
132 assertEquals(schemaSecond, schema);
133 }
134 /***
135 * Tests we can round trip from the XML -> bean -> XML -> bean.
136 * It will test if both object are identical.
137 * For this to actually work I implemented a details equals in my
138 * Beans..
139 */
140 public void testRoundTripWithoutCollectionElement()
141 throws Exception
142 {
143 BeanReader reader = createBeanReader();
144 PhysicalSchema schema = (PhysicalSchema) reader.parse(
145 getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
146 StringWriter buffer = new StringWriter();
147 write(schema, buffer, false);
148 StringReader in = new StringReader(buffer.getBuffer().toString());
149 PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
150 assertEquals(schemaSecond, schema);
151 }
152
153 /***
154 * Creates a beanReader
155 */
156 protected BeanReader createBeanReader()
157 throws Exception
158 {
159 BeanReader reader = new BeanReader();
160 reader.setXMLIntrospector(createXMLIntrospector());
161 // register the class which maps to the root element
162 // of the xml file (this depends on the NameMapper used.
163 reader.registerBeanClass(PhysicalSchema.class);
164 return reader;
165 }
166
167 /***
168 * Set up the XMLIntroSpector
169 */
170 protected XMLIntrospector createXMLIntrospector() {
171 XMLIntrospector introspector = new XMLIntrospector();
172
173 // set elements for attributes to true
174 introspector.setAttributesForPrimitives(true);
175
176 // Since we don't want to have collectionelements
177 // line <DBMSS>, we have to set this to false,
178 // since the default is true.
179 introspector.setWrapCollectionsInElement(false);
180
181 // We have to use the HyphenatedNameMapper
182 // Since we want the names to resolve from eg PhysicalSchema
183 // to PHYSICAL_SCHEMA.
184 // we pass to the mapper we want uppercase and use _ for name
185 // seperation.
186 // This will set our ElementMapper.
187 introspector.setElementNameMapper(new HyphenatedNameMapper(true, "_"));
188 // since our attribute names will use a different
189 // naming convention in our xml file (just all lowercase)
190 // we set another mapper for the attributes
191 introspector.setAttributeNameMapper(new DecapitalizeNameMapper());
192
193 return introspector;
194 }
195
196 /***
197 * Opens a writer and writes an object model according to the
198 * retrieved bean
199 */
200 private void write(Object bean, Writer out, boolean wrapCollectionsInElement)
201 throws Exception
202 {
203 BeanWriter writer = new BeanWriter(out);
204 writer.setXMLIntrospector(createXMLIntrospector());
205 // specifies weather to use collection elements or not.
206 writer.getXMLIntrospector().setWrapCollectionsInElement(wrapCollectionsInElement);
207 // we don't want to write Id attributes to every element
208 // we just want our opbject model written nothing more..
209 writer.setWriteIDs(false);
210 // the source has 2 spaces indention and \n as line seperator.
211 writer.setIndent(" ");
212 writer.setEndOfLine("\n");
213 writer.write(bean);
214 }
215 }
216
This page was automatically generated by Maven