1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt;
18  
19  import java.io.FileInputStream;
20  import java.io.InputStream;
21  import java.io.StringReader;
22  import java.io.StringWriter;
23  import java.io.Writer;
24  import java.net.URL;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  import junit.textui.TestRunner;
29  
30  import org.apache.commons.betwixt.io.BeanReader;
31  import org.apache.commons.betwixt.io.BeanWriter;
32  import org.apache.commons.digester.rss.Channel;
33  import org.apache.commons.digester.rss.RSSDigester;
34  
35  /*** Test harness which parses an RSS document using Digester
36    * then outputs it using Betwixt, then parses it again with Digester
37    * to check that the document is parseable again.
38    *
39    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
40    * @version $Revision: 438373 $
41    */
42  public class TestRSSRoundTrip extends AbstractTestCase {
43      
44      /***
45       * The set of public identifiers, and corresponding resource names,
46       * for the versions of the DTDs that we know about.
47       */
48      protected static final String registrations[] = {
49          "-//Netscape Communications//DTD RSS 0.9//EN",
50          "/org/apache/commons/digester/rss/rss-0.9.dtd",
51          "-//Netscape Communications//DTD RSS 0.91//EN",
52          "/org/apache/commons/digester/rss/rss-0.91.dtd",
53      };
54      
55      public static void main( String[] args ) {
56          TestRunner.run( suite() );
57      }
58      
59      public static Test suite() {
60          return new TestSuite(TestRSSRoundTrip.class);
61      }
62      
63      public TestRSSRoundTrip(String testName) {
64          super(testName);
65      }
66      
67      
68  
69      public void testRoundTrip() throws Exception {
70          // lets parse the example 
71          RSSDigester digester = new RSSDigester();
72          
73          InputStream in = new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") );
74          Object bean = digester.parse( in ); 
75          in.close();
76          
77          // now lets output it to a buffer
78          StringWriter buffer = new StringWriter();
79          write( bean, buffer );
80          
81          // now lets try parse again
82          String text = buffer.toString();        
83          bean = digester.parse( new StringReader( text ) );
84          
85          // managed to parse it again!
86          
87          // now lets write it to another buffer
88          buffer = new StringWriter();
89          write( bean, buffer );
90          
91          String text2 = buffer.toString();
92  
93          // if the two strings are equal then we've done a full round trip
94          // with the XML staying the same. Though the original source XML
95          // could well be different
96          assertEquals( "Round trip value should remain unchanged", text, text2 );
97      }
98      
99      /*** 
100      * This tests using the both the RSSDigester 
101      * and the BeanReader to parse an RSS and output it
102      * using the BeanWriter
103      */
104     public void testBeanWriterRoundTrip() throws Exception {
105         // lets parse the example using the RSSDigester
106         RSSDigester digester = new RSSDigester();
107         
108         InputStream in = new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") );
109         Object bean = digester.parse( in ); 
110         in.close();
111         
112         // now lets output it to a buffer
113         StringWriter buffer = new StringWriter();
114         write( bean, buffer );
115         
116 
117         // create a BeanReader
118         BeanReader reader = new BeanReader();
119         reader.registerBeanClass( Channel.class );
120 
121         // Register local copies of the DTDs we understand
122         for (int i = 0; i < registrations.length; i += 2) {
123             URL url = RSSDigester.class.getResource(registrations[i + 1]);
124             if (url != null) {
125                 reader.register(registrations[i], url.toString());
126             }
127         }
128         
129         // now lets try parse the output sing the BeanReader 
130         String text = buffer.toString();        
131         bean = reader.parse( new StringReader( text ) );
132         
133         // managed to parse it again!
134         
135         // now lets write it to another buffer
136         buffer = new StringWriter();
137         write( bean, buffer );
138         
139         String text2 = buffer.toString();
140 
141         // if the two strings are equal then we've done a full round trip
142         // with the XML staying the same. Though the original source XML
143         // could well be different
144         assertEquals( "Round trip value should remain unchanged", text, text2 );
145     }
146     
147     public void testRSSRead() throws Exception {
148     /* 
149         this test isn't working at the moment.
150         the problem seems to be that you can't configure betwixt to ignore empty elements
151     
152         // create a BeanReader
153         BeanReader reader = new BeanReader();
154         reader.registerBeanClass( Channel.class );
155 
156         // Register local copies of the DTDs we understand
157         for (int i = 0; i < registrations.length; i += 2) {
158             URL url = RSSDigester.class.getResource(registrations[i + 1]);
159             if (url != null) {
160                 reader.register(registrations[i], url.toString());
161             }
162         }
163         
164         Object bean = reader.parse(
165             new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") ));
166         
167         StringWriter out = new StringWriter();
168         out.write( "<?xml version='1.0'?>" );
169         write( bean, out );
170             
171         String xml = out.toString();
172         System.out.println( xml );
173         
174         xmlAssertIsomorphic(
175             parseString( xml ), 
176             parseFile( "src/test/org/apache/commons/betwixt/rss-example.xml" ));
177         */
178     }
179     
180     protected void write(Object bean, Writer out) throws Exception {
181         BeanWriter writer = new BeanWriter(out);
182         writer.setWriteEmptyElements(true);
183         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
184         writer.getBindingConfiguration().setMapIDs(false);
185         writer.setEndOfLine("\n");
186         writer.enablePrettyPrint();
187         writer.write( bean );
188     }
189 }
190