001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.schema;
019:
020: import java.io.StringReader;
021: import java.io.StringWriter;
022: import java.io.Writer;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.apache.commons.betwixt.AbstractTestCase;
028: import org.apache.commons.betwixt.XMLIntrospector;
029: import org.apache.commons.betwixt.io.BeanReader;
030: import org.apache.commons.betwixt.io.BeanWriter;
031: import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
032: import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
033: import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
034:
035: //import org.apache.commons.logging.impl.SimpleLog;
036: //import org.apache.commons.betwixt.io.BeanRuleSet;
037:
038: /**
039: * This will test betwixt on handling a different kind of xml file, without
040: * a "collection" tag.
041: *
042: * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
043: * @version $Id: TestSchema.java 438373 2006-08-30 05:17:21Z bayard $
044: */
045: public class TestSchema extends AbstractTestCase {
046:
047: public static Test suite() {
048: return new TestSuite(TestSchema.class);
049: }
050:
051: public TestSchema(String testName) {
052: super (testName);
053: }
054:
055: /**
056: * Test the roundtrip with an xml file that doesn't have
057: * collection elements, writes it with collection elements
058: * and then compares the 2 object, which should end up
059: * equal..
060: */
061: public void testCombinedRoundTrip() throws Exception {
062: // SimpleLog log = new SimpleLog("[CombinedRoundTrip:BeanRuleSet]");
063: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
064: // BeanRuleSet.setLog(log);
065:
066: // log = new SimpleLog("[CombinedRoundTrip]");
067: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
068:
069: BeanReader reader = createBeanReader();
070:
071: PhysicalSchema schema = (PhysicalSchema) reader
072: .parse(getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
073: StringWriter buffer = new StringWriter();
074: write(schema, buffer, true);
075:
076: // log.debug(buffer.getBuffer().toString());
077:
078: StringReader in = new StringReader(buffer.getBuffer()
079: .toString());
080: reader = createBeanReader();
081: XMLIntrospector intro = createXMLIntrospector();
082: DefaultXMLBeanInfoRegistry registry = new DefaultXMLBeanInfoRegistry();
083: intro.setRegistry(registry);
084: // we have written the xml file back with element collections,
085: // so we have to say to the reader we want to use that now
086: // (the default when creating in this test is not to use them)
087: intro.getConfiguration().setWrapCollectionsInElement(true);
088: // first flush the cash, else setting other options, doesn't
089: // end up in rereading / mapping the object model.
090: registry.flush();
091: // set the xmlIntrospector back to the reader
092: reader.setXMLIntrospector(intro);
093: reader.deregisterBeanClass(PhysicalSchema.class);
094: reader.getRules().clear();
095: reader.registerBeanClass(PhysicalSchema.class);
096: PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
097: buffer.close();
098: write(schema, buffer, true);
099: assertEquals(schema, schemaSecond);
100: }
101:
102: /**
103: * Tests we can round trip from the XML -> bean -> XML -> bean.
104: * It will test if both object are identical.
105: * For this to actually work I implemented a details equals in my
106: * Beans..
107: */
108: public void testRoundTripWithoutCollectionElement()
109: throws Exception {
110: BeanReader reader = createBeanReader();
111: PhysicalSchema schema = (PhysicalSchema) reader
112: .parse(getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
113: StringWriter buffer = new StringWriter();
114: write(schema, buffer, false);
115: StringReader in = new StringReader(buffer.getBuffer()
116: .toString());
117: PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
118: assertEquals(schemaSecond, schema);
119: }
120:
121: /**
122: * Creates a beanReader
123: */
124: protected BeanReader createBeanReader() throws Exception {
125: BeanReader reader = new BeanReader();
126: reader.setXMLIntrospector(createXMLIntrospector());
127: // register the class which maps to the root element
128: // of the xml file (this depends on the NameMapper used.
129: reader.registerBeanClass(PhysicalSchema.class);
130: return reader;
131: }
132:
133: /**
134: * Set up the XMLIntroSpector
135: */
136: protected XMLIntrospector createXMLIntrospector() {
137: XMLIntrospector introspector = new XMLIntrospector();
138:
139: // set elements for attributes to true
140: introspector.getConfiguration()
141: .setAttributesForPrimitives(true);
142:
143: // Since we don't want to have collectionelements
144: // line <DBMSS>, we have to set this to false,
145: // since the default is true.
146: introspector.getConfiguration().setWrapCollectionsInElement(
147: false);
148:
149: // We have to use the HyphenatedNameMapper
150: // Since we want the names to resolve from eg PhysicalSchema
151: // to PHYSICAL_SCHEMA.
152: // we pass to the mapper we want uppercase and use _ for name
153: // seperation.
154: // This will set our ElementMapper.
155: introspector.getConfiguration().setElementNameMapper(
156: new HyphenatedNameMapper(true, "_"));
157: // since our attribute names will use a different
158: // naming convention in our xml file (just all lowercase)
159: // we set another mapper for the attributes
160: introspector.getConfiguration().setAttributeNameMapper(
161: new DecapitalizeNameMapper());
162:
163: return introspector;
164: }
165:
166: /**
167: * Opens a writer and writes an object model according to the
168: * retrieved bean
169: */
170: private void write(Object bean, Writer out,
171: boolean wrapCollectionsInElement) throws Exception {
172: BeanWriter writer = new BeanWriter(out);
173: writer.setWriteEmptyElements(true);
174: writer.setXMLIntrospector(createXMLIntrospector());
175: // specifies weather to use collection elements or not.
176: writer.getXMLIntrospector().getConfiguration()
177: .setWrapCollectionsInElement(wrapCollectionsInElement);
178: // we don't want to write Id attributes to every element
179: // we just want our opbject model written nothing more..
180: writer.getBindingConfiguration().setMapIDs(false);
181: // the source has 2 spaces indention and \n as line seperator.
182: writer.setIndent(" ");
183: writer.setEndOfLine("\n");
184: writer.write(bean);
185: }
186: }
|