KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > TestRSSRoundTrip


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 package org.apache.commons.betwixt;
17
18 import java.io.FileInputStream JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.StringReader JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28
29 import org.apache.commons.betwixt.io.BeanReader;
30 import org.apache.commons.betwixt.io.BeanWriter;
31 import org.apache.commons.digester.rss.Channel;
32 import org.apache.commons.digester.rss.RSSDigester;
33
34 /** Test harness which parses an RSS document using Digester
35   * then outputs it using Betwixt, then parses it again with Digester
36   * to check that the document is parseable again.
37   *
38   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
39   * @version $Revision: 1.5 $
40   */

41 public class TestRSSRoundTrip extends AbstractTestCase {
42     
43     /**
44      * The set of public identifiers, and corresponding resource names,
45      * for the versions of the DTDs that we know about.
46      */

47     protected static final String JavaDoc registrations[] = {
48         "-//Netscape Communications//DTD RSS 0.9//EN",
49         "/org/apache/commons/digester/rss/rss-0.9.dtd",
50         "-//Netscape Communications//DTD RSS 0.91//EN",
51         "/org/apache/commons/digester/rss/rss-0.91.dtd",
52     };
53     
54     public static void main( String JavaDoc[] args ) {
55         TestRunner.run( suite() );
56     }
57     
58     public static Test suite() {
59         return new TestSuite(TestRSSRoundTrip.class);
60     }
61     
62     public TestRSSRoundTrip(String JavaDoc testName) {
63         super(testName);
64     }
65     
66     
67
68     public void testRoundTrip() throws Exception JavaDoc {
69         // lets parse the example
70
RSSDigester digester = new RSSDigester();
71         
72         InputStream JavaDoc in = new FileInputStream JavaDoc( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") );
73         Object JavaDoc bean = digester.parse( in );
74         in.close();
75         
76         // now lets output it to a buffer
77
StringWriter JavaDoc buffer = new StringWriter JavaDoc();
78         write( bean, buffer );
79         
80         // now lets try parse again
81
String JavaDoc text = buffer.toString();
82         bean = digester.parse( new StringReader JavaDoc( text ) );
83         
84         // managed to parse it again!
85

86         // now lets write it to another buffer
87
buffer = new StringWriter JavaDoc();
88         write( bean, buffer );
89         
90         String JavaDoc text2 = buffer.toString();
91
92         // if the two strings are equal then we've done a full round trip
93
// with the XML staying the same. Though the original source XML
94
// could well be different
95
assertEquals( "Round trip value should remain unchanged", text, text2 );
96     }
97     
98     /**
99      * This tests using the both the RSSDigester
100      * and the BeanReader to parse an RSS and output it
101      * using the BeanWriter
102      */

103     public void testBeanWriterRoundTrip() throws Exception JavaDoc {
104         // lets parse the example using the RSSDigester
105
RSSDigester digester = new RSSDigester();
106         
107         InputStream JavaDoc in = new FileInputStream JavaDoc( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") );
108         Object JavaDoc bean = digester.parse( in );
109         in.close();
110         
111         // now lets output it to a buffer
112
StringWriter JavaDoc buffer = new StringWriter JavaDoc();
113         write( bean, buffer );
114         
115
116         // create a BeanReader
117
BeanReader reader = new BeanReader();
118         reader.registerBeanClass( Channel.class );
119
120         // Register local copies of the DTDs we understand
121
for (int i = 0; i < registrations.length; i += 2) {
122             URL JavaDoc url = RSSDigester.class.getResource(registrations[i + 1]);
123             if (url != null) {
124                 reader.register(registrations[i], url.toString());
125             }
126         }
127         
128         // now lets try parse the output sing the BeanReader
129
String JavaDoc text = buffer.toString();
130         bean = reader.parse( new StringReader JavaDoc( text ) );
131         
132         // managed to parse it again!
133

134         // now lets write it to another buffer
135
buffer = new StringWriter JavaDoc();
136         write( bean, buffer );
137         
138         String JavaDoc text2 = buffer.toString();
139
140         // if the two strings are equal then we've done a full round trip
141
// with the XML staying the same. Though the original source XML
142
// could well be different
143
assertEquals( "Round trip value should remain unchanged", text, text2 );
144     }
145     
146     public void testRSSRead() throws Exception JavaDoc {
147     /*
148         this test isn't working at the moment.
149         the problem seems to be that you can't configure betwixt to ignore empty elements
150     
151         // create a BeanReader
152         BeanReader reader = new BeanReader();
153         reader.registerBeanClass( Channel.class );
154
155         // Register local copies of the DTDs we understand
156         for (int i = 0; i < registrations.length; i += 2) {
157             URL url = RSSDigester.class.getResource(registrations[i + 1]);
158             if (url != null) {
159                 reader.register(registrations[i], url.toString());
160             }
161         }
162         
163         Object bean = reader.parse(
164             new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") ));
165         
166         StringWriter out = new StringWriter();
167         out.write( "<?xml version='1.0'?>" );
168         write( bean, out );
169             
170         String xml = out.toString();
171         System.out.println( xml );
172         
173         xmlAssertIsomorphic(
174             parseString( xml ),
175             parseFile( "src/test/org/apache/commons/betwixt/rss-example.xml" ));
176         */

177     }
178     
179     protected void write(Object JavaDoc bean, Writer JavaDoc out) throws Exception JavaDoc {
180         //SimpleLog log = new SimpleLog("[TestRSSRoundTrip:BeanWriter]");
181
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
182
BeanWriter writer = new BeanWriter(out);
183         //writer.setLog(log);
184
//log = new SimpleLog("[TestRSSRoundTrip:AbstractBeanWriter]");
185
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
186
//writer.setAbstractBeanWriterLog(log);
187
writer.setWriteEmptyElements(true);
188         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
189         writer.getBindingConfiguration().setMapIDs(false);
190         writer.enablePrettyPrint();
191         writer.write( bean );
192     }
193 }
194
195
Popular Tags