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: package org.apache.commons.betwixt;
018:
019: import java.io.FileInputStream;
020: import java.io.InputStream;
021: import java.io.StringReader;
022: import java.io.StringWriter;
023: import java.io.Writer;
024: import java.net.URL;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028: import junit.textui.TestRunner;
029:
030: import org.apache.commons.betwixt.io.BeanReader;
031: import org.apache.commons.betwixt.io.BeanWriter;
032: import org.apache.commons.digester.rss.Channel;
033: import org.apache.commons.digester.rss.RSSDigester;
034:
035: /** Test harness which parses an RSS document using Digester
036: * then outputs it using Betwixt, then parses it again with Digester
037: * to check that the document is parseable again.
038: *
039: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
040: * @version $Revision: 438373 $
041: */
042: public class TestRSSRoundTrip extends AbstractTestCase {
043:
044: /**
045: * The set of public identifiers, and corresponding resource names,
046: * for the versions of the DTDs that we know about.
047: */
048: protected static final String registrations[] = {
049: "-//Netscape Communications//DTD RSS 0.9//EN",
050: "/org/apache/commons/digester/rss/rss-0.9.dtd",
051: "-//Netscape Communications//DTD RSS 0.91//EN",
052: "/org/apache/commons/digester/rss/rss-0.91.dtd", };
053:
054: public static void main(String[] args) {
055: TestRunner.run(suite());
056: }
057:
058: public static Test suite() {
059: return new TestSuite(TestRSSRoundTrip.class);
060: }
061:
062: public TestRSSRoundTrip(String testName) {
063: super (testName);
064: }
065:
066: public void testRoundTrip() throws Exception {
067: // lets parse the example
068: RSSDigester digester = new RSSDigester();
069:
070: InputStream in = new FileInputStream(
071: getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml"));
072: Object bean = digester.parse(in);
073: in.close();
074:
075: // now lets output it to a buffer
076: StringWriter buffer = new StringWriter();
077: write(bean, buffer);
078:
079: // now lets try parse again
080: String text = buffer.toString();
081: bean = digester.parse(new StringReader(text));
082:
083: // managed to parse it again!
084:
085: // now lets write it to another buffer
086: buffer = new StringWriter();
087: write(bean, buffer);
088:
089: String text2 = buffer.toString();
090:
091: // if the two strings are equal then we've done a full round trip
092: // with the XML staying the same. Though the original source XML
093: // could well be different
094: assertEquals("Round trip value should remain unchanged", text,
095: text2);
096: }
097:
098: /**
099: * 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 {
104: // lets parse the example using the RSSDigester
105: RSSDigester digester = new RSSDigester();
106:
107: InputStream in = new FileInputStream(
108: 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: // 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 url = RSSDigester.class
123: .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,
145: text2);
146: }
147:
148: public void testRSSRead() throws Exception {
149: /*
150: this test isn't working at the moment.
151: the problem seems to be that you can't configure betwixt to ignore empty elements
152:
153: // create a BeanReader
154: BeanReader reader = new BeanReader();
155: reader.registerBeanClass( Channel.class );
156:
157: // Register local copies of the DTDs we understand
158: for (int i = 0; i < registrations.length; i += 2) {
159: URL url = RSSDigester.class.getResource(registrations[i + 1]);
160: if (url != null) {
161: reader.register(registrations[i], url.toString());
162: }
163: }
164:
165: Object bean = reader.parse(
166: new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") ));
167:
168: StringWriter out = new StringWriter();
169: out.write( "<?xml version='1.0'?>" );
170: write( bean, out );
171:
172: String xml = out.toString();
173: System.out.println( xml );
174:
175: xmlAssertIsomorphic(
176: parseString( xml ),
177: parseFile( "src/test/org/apache/commons/betwixt/rss-example.xml" ));
178: */
179: }
180:
181: protected void write(Object bean, Writer out) throws Exception {
182: BeanWriter writer = new BeanWriter(out);
183: writer.setWriteEmptyElements(true);
184: writer.getXMLIntrospector().getConfiguration()
185: .setAttributesForPrimitives(false);
186: writer.getBindingConfiguration().setMapIDs(false);
187: writer.setEndOfLine("\n");
188: writer.enablePrettyPrint();
189: writer.write(bean);
190: }
191: }
|