1 2 17 package org.apache.commons.betwixt.digester; 18 19 import java.io.FileInputStream ; 20 import java.io.InputStream ; 21 import java.io.StringWriter ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 import junit.textui.TestRunner; 26 27 import org.apache.commons.betwixt.AbstractTestCase; 28 import org.apache.commons.betwixt.io.BeanReader; 29 import org.apache.commons.betwixt.io.BeanWriter; 30 31 37 38 43 public class TestIDRead extends AbstractTestCase { 44 45 public static void main( String [] args ) { 46 TestRunner.run( suite() ); 47 } 48 49 public static Test suite() { 50 return new TestSuite(TestIDRead.class); 51 } 52 53 public TestIDRead(String testName) { 54 super(testName); 55 } 56 57 public void testSimpleRead() throws Exception { 58 StringWriter out = new StringWriter (); 59 out.write("<?xml version='1.0'?>"); 60 BeanWriter writer = new BeanWriter(out); 61 writer.getBindingConfiguration().setMapIDs(false); 62 IDBean bean = new IDBean("alpha","one"); 63 bean.addChild(new IDBean("beta","two")); 64 bean.addChild(new IDBean("gamma","three")); 65 writer.write(bean); 66 67 String xml = "<IDBean><name>one</name><children><child><name>two</name><children/>" 68 + "<id>beta</id></child><child><name>three</name><children/>" 69 + "<id>gamma</id></child></children><id>alpha</id></IDBean>"; 70 71 xmlAssertIsomorphicContent( 72 parseString(xml), 73 parseString(out.getBuffer().toString()), 74 true); 75 76 BeanReader reader = new BeanReader(); 77 78 83 87 91 95 99 103 107 reader.registerBeanClass( IDBean.class ); 108 109 InputStream in = new FileInputStream ( 110 getTestFile("src/test/org/apache/commons/betwixt/digester/SimpleReadTest.xml") ); 111 112 try { 113 Object obj = reader.parse( in ); 116 118 assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) ); 119 IDBean alpha = (IDBean) obj; 120 121 assertEquals("Wrong list size", 2 , alpha.getChildren().size()); 122 123 IDBean beta = (IDBean) alpha.getChildren().get(0); 124 assertEquals("Wrong name (A)", "beta" , beta.getName()); 125 126 IDBean gamma = (IDBean) alpha.getChildren().get(1); 127 assertEquals("Wrong name (B)", "gamma" , gamma.getName()); 128 } 129 finally { 130 in.close(); 131 } 132 } 133 134 public void testIDRead() throws Exception { 135 136 BeanReader reader = new BeanReader(); 137 138 155 reader.registerBeanClass( IDBean.class ); 156 157 InputStream in = new FileInputStream ( 158 getTestFile("src/test/org/apache/commons/betwixt/digester/IDTest1.xml") ); 159 160 try { 161 Object obj = reader.parse( in ); 162 163 assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) ); 164 IDBean alpha = (IDBean) obj; 165 166 assertEquals("Wrong list size (A)", 2 , alpha.getChildren().size()); 167 168 IDBean beta = (IDBean) alpha.getChildren().get(0); 169 assertEquals("Wrong name (A)", "beta" , beta.getName()); 170 171 IDBean gamma = (IDBean) alpha.getChildren().get(1); 172 assertEquals("Wrong name (B)", "gamma" , gamma.getName()); 173 assertEquals("Wrong list size (B)", 2 , gamma.getChildren().size()); 174 175 IDBean sonOfGamma = (IDBean) gamma.getChildren().get(1); 176 177 assertEquals("Wrong id (A)", "two" , sonOfGamma.getId()); 178 assertEquals("Wrong name (C)", "beta" , sonOfGamma.getName()); 179 180 assertEquals("IDREF bean not equal to ID bean", beta, sonOfGamma); 181 } 182 finally { 183 in.close(); 184 } 185 } 186 } 187 | Popular Tags |