1 16 package org.apache.commons.betwixt; 17 18 import java.io.FileInputStream ; 19 import java.io.InputStream ; 20 import java.io.StringReader ; 21 import java.io.StringWriter ; 22 import java.io.Writer ; 23 import java.net.URL ; 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 41 public class TestRSSRoundTrip extends AbstractTestCase { 42 43 47 protected static final String 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 [] args ) { 55 TestRunner.run( suite() ); 56 } 57 58 public static Test suite() { 59 return new TestSuite(TestRSSRoundTrip.class); 60 } 61 62 public TestRSSRoundTrip(String testName) { 63 super(testName); 64 } 65 66 67 68 public void testRoundTrip() throws Exception { 69 RSSDigester digester = new RSSDigester(); 71 72 InputStream in = new FileInputStream ( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") ); 73 Object bean = digester.parse( in ); 74 in.close(); 75 76 StringWriter buffer = new StringWriter (); 78 write( bean, buffer ); 79 80 String text = buffer.toString(); 82 bean = digester.parse( new StringReader ( text ) ); 83 84 86 buffer = new StringWriter (); 88 write( bean, buffer ); 89 90 String text2 = buffer.toString(); 91 92 assertEquals( "Round trip value should remain unchanged", text, text2 ); 96 } 97 98 103 public void testBeanWriterRoundTrip() throws Exception { 104 RSSDigester digester = new RSSDigester(); 106 107 InputStream in = new FileInputStream ( getTestFile("src/test/org/apache/commons/betwixt/rss-example.xml") ); 108 Object bean = digester.parse( in ); 109 in.close(); 110 111 StringWriter buffer = new StringWriter (); 113 write( bean, buffer ); 114 115 116 BeanReader reader = new BeanReader(); 118 reader.registerBeanClass( Channel.class ); 119 120 for (int i = 0; i < registrations.length; i += 2) { 122 URL url = RSSDigester.class.getResource(registrations[i + 1]); 123 if (url != null) { 124 reader.register(registrations[i], url.toString()); 125 } 126 } 127 128 String text = buffer.toString(); 130 bean = reader.parse( new StringReader ( text ) ); 131 132 134 buffer = new StringWriter (); 136 write( bean, buffer ); 137 138 String text2 = buffer.toString(); 139 140 assertEquals( "Round trip value should remain unchanged", text, text2 ); 144 } 145 146 public void testRSSRead() throws Exception { 147 177 } 178 179 protected void write(Object bean, Writer out) throws Exception { 180 BeanWriter writer = new BeanWriter(out); 183 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 |