View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestRSSRoundTrip.java,v 1.5 2002/05/28 11:49:29 jstrachan Exp $ 3 * $Revision: 1.5 $ 4 * $Date: 2002/05/28 11:49:29 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: TestRSSRoundTrip.java,v 1.5 2002/05/28 11:49:29 jstrachan Exp $ 61 */ 62 package org.apache.commons.betwixt; 63 64 import java.io.FileInputStream; 65 import java.io.InputStream; 66 import java.io.StringReader; 67 import java.io.StringWriter; 68 import java.io.Writer; 69 import java.net.URL; 70 71 import junit.framework.Test; 72 import junit.framework.TestSuite; 73 import junit.textui.TestRunner; 74 75 import org.apache.commons.betwixt.io.BeanReader; 76 import org.apache.commons.betwixt.io.BeanWriter; 77 import org.apache.commons.digester.rss.Channel; 78 import org.apache.commons.digester.rss.RSSDigester; 79 80 81 /*** Test harness which parses an RSS document using Digester 82 * then outputs it using Betwixt, then parses it again with Digester 83 * to check that the document is parseable again. 84 * 85 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 86 * @version $Revision: 1.5 $ 87 */ 88 public class TestRSSRoundTrip extends AbstractTestCase { 89 90 /*** 91 * The set of public identifiers, and corresponding resource names, 92 * for the versions of the DTDs that we know about. 93 */ 94 protected static final String registrations[] = { 95 "-//Netscape Communications//DTD RSS 0.9//EN", 96 "/org/apache/commons/digester/rss/rss-0.9.dtd", 97 "-//Netscape Communications//DTD RSS 0.91//EN", 98 "/org/apache/commons/digester/rss/rss-0.91.dtd", 99 }; 100 101 public static void main( String[] args ) { 102 TestRunner.run( suite() ); 103 } 104 105 public static Test suite() { 106 return new TestSuite(TestRSSRoundTrip.class); 107 } 108 109 public TestRSSRoundTrip(String testName) { 110 super(testName); 111 } 112 113 114 115 public void testRoundTrip() throws Exception { 116 // lets parse the example 117 RSSDigester digester = new RSSDigester(); 118 119 InputStream in = new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") ); 120 Object bean = digester.parse( in ); 121 in.close(); 122 123 // now lets output it to a buffer 124 StringWriter buffer = new StringWriter(); 125 write( bean, buffer ); 126 127 // now lets try parse again 128 String text = buffer.toString(); 129 bean = digester.parse( new StringReader( text ) ); 130 131 // managed to parse it again! 132 133 // now lets write it to another buffer 134 buffer = new StringWriter(); 135 write( bean, buffer ); 136 137 String text2 = buffer.toString(); 138 139 // if the two strings are equal then we've done a full round trip 140 // with the XML staying the same. Though the original source XML 141 // could well be different 142 assertEquals( "Round trip value should remain unchanged", text, text2 ); 143 } 144 145 /*** 146 * This tests using the both the RSSDigester 147 * and the BeanReader to parse an RSS and output it 148 * using the BeanWriter 149 */ 150 public void testBeanWriterRoundTrip() throws Exception { 151 // lets parse the example using the RSSDigester 152 RSSDigester digester = new RSSDigester(); 153 154 InputStream in = new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") ); 155 Object bean = digester.parse( in ); 156 in.close(); 157 158 // now lets output it to a buffer 159 StringWriter buffer = new StringWriter(); 160 write( bean, buffer ); 161 162 163 // create a BeanReader 164 BeanReader reader = new BeanReader(); 165 reader.registerBeanClass( Channel.class ); 166 167 // Register local copies of the DTDs we understand 168 for (int i = 0; i < registrations.length; i += 2) { 169 URL url = RSSDigester.class.getResource(registrations[i + 1]); 170 if (url != null) { 171 reader.register(registrations[i], url.toString()); 172 } 173 } 174 175 // now lets try parse the output sing the BeanReader 176 String text = buffer.toString(); 177 bean = reader.parse( new StringReader( text ) ); 178 179 // managed to parse it again! 180 181 // now lets write it to another buffer 182 buffer = new StringWriter(); 183 write( bean, buffer ); 184 185 String text2 = buffer.toString(); 186 187 // if the two strings are equal then we've done a full round trip 188 // with the XML staying the same. Though the original source XML 189 // could well be different 190 assertEquals( "Round trip value should remain unchanged", text, text2 ); 191 } 192 193 public void testRSSRead() throws Exception { 194 /* 195 this test isn't working at the moment. 196 the problem seems to be that you can't configure betwixt to ignore empty elements 197 198 // create a BeanReader 199 BeanReader reader = new BeanReader(); 200 reader.registerBeanClass( Channel.class ); 201 202 // Register local copies of the DTDs we understand 203 for (int i = 0; i < registrations.length; i += 2) { 204 URL url = RSSDigester.class.getResource(registrations[i + 1]); 205 if (url != null) { 206 reader.register(registrations[i], url.toString()); 207 } 208 } 209 210 Object bean = reader.parse( 211 new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") )); 212 213 StringWriter out = new StringWriter(); 214 out.write( "<?xml version='1.0'?>" ); 215 write( bean, out ); 216 217 String xml = out.toString(); 218 System.out.println( xml ); 219 220 xmlAssertIsomorphic( 221 parseString( xml ), 222 parseFile( "src/test/org/apache/commons/betwixt/rss-example.xml" )); 223 */ 224 } 225 226 protected void write(Object bean, Writer out) throws Exception { 227 BeanWriter writer = new BeanWriter(out); 228 writer.getXMLIntrospector().setAttributesForPrimitives(false); 229 writer.setWriteIDs(false); 230 writer.enablePrettyPrint(); 231 writer.write( bean ); 232 } 233 } 234

This page was automatically generated by Maven