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;
20  import java.io.StringWriter;
21  import java.io.Writer;
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.10 2004/02/28 13:38:36 yoavs 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 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
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 buffer = new StringWriter();
79          write(schema, buffer, true);
80          
81  //        log.debug(buffer.getBuffer().toString());
82          
83          StringReader in = new StringReader(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.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.registerBeanClass(PhysicalSchema.class);
99          PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
100         buffer.close();
101         write(schema,buffer, true);
102         assertEquals(schema, schemaSecond);
103     }
104     /***
105      * Tests we can round trip from the XML -> bean -> XML -> bean.
106      * It will test if both object are identical.
107      * For this to actually work I implemented a details equals in my
108      * Beans..
109      */
110     public void testRoundTripWithoutCollectionElement()
111     throws Exception
112     {
113         BeanReader reader = createBeanReader();
114         PhysicalSchema schema = (PhysicalSchema) reader.parse(
115             getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
116         StringWriter buffer = new StringWriter();
117         write(schema, buffer, false);
118         StringReader in = new StringReader(buffer.getBuffer().toString());
119         PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
120         assertEquals(schemaSecond, schema);
121     }
122     
123     /***
124      * Creates a beanReader
125      */
126     protected BeanReader createBeanReader()
127     throws Exception
128      {
129         BeanReader reader = new BeanReader();
130         reader.setXMLIntrospector(createXMLIntrospector());
131         // register the class which maps to the root element
132         // of the xml file (this depends on the NameMapper used.
133         reader.registerBeanClass(PhysicalSchema.class);
134         return reader;
135     } 
136     
137     /***
138      * Set up the XMLIntroSpector
139      */
140     protected XMLIntrospector createXMLIntrospector() {
141         XMLIntrospector introspector = new XMLIntrospector();
142 
143         // set elements for attributes to true
144         introspector.setAttributesForPrimitives(true);
145 
146         // Since we don't want to have collectionelements 
147         // line <DBMSS>, we have to set this to false,
148         // since the default is true.
149         introspector.setWrapCollectionsInElement(false);
150 
151         // We have to use the HyphenatedNameMapper
152         // Since we want the names to resolve from eg PhysicalSchema
153         // to PHYSICAL_SCHEMA.
154         // we pass to the mapper we want uppercase and use _ for name
155         // seperation.
156         // This will set our ElementMapper.
157         introspector.setElementNameMapper(new HyphenatedNameMapper(true, "_"));
158         // since our attribute names will use a different 
159         // naming convention in our xml file (just all lowercase)
160         // we set another mapper for the attributes
161         introspector.setAttributeNameMapper(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, boolean wrapCollectionsInElement)
171     throws Exception
172     {
173         BeanWriter writer = new BeanWriter(out);
174         writer.setWriteEmptyElements( true );
175         writer.setXMLIntrospector(createXMLIntrospector());
176         // specifies weather to use collection elements or not.
177         writer.getXMLIntrospector().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.setWriteIDs(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 }
187