1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
78 StringWriter buffer = new StringWriter();
79 write( bean, buffer );
80
81
82 String text = buffer.toString();
83 bean = digester.parse( new StringReader( text ) );
84
85
86
87
88 buffer = new StringWriter();
89 write( bean, buffer );
90
91 String text2 = buffer.toString();
92
93
94
95
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
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
113 StringWriter buffer = new StringWriter();
114 write( bean, buffer );
115
116
117
118 BeanReader reader = new BeanReader();
119 reader.registerBeanClass( Channel.class );
120
121
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
130 String text = buffer.toString();
131 bean = reader.parse( new StringReader( text ) );
132
133
134
135
136 buffer = new StringWriter();
137 write( bean, buffer );
138
139 String text2 = buffer.toString();
140
141
142
143
144 assertEquals( "Round trip value should remain unchanged", text, text2 );
145 }
146
147 public void testRSSRead() throws Exception {
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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