View Javadoc
1 /* 2 * Copyright (C) The Apache Software Foundation. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE file. 7 * 8 * $Id: TestBeanReader.java,v 1.7 2002/05/28 23:01:08 jstrachan Exp $ 9 */ 10 package org.apache.commons.betwixt; 11 12 import java.io.FileInputStream; 13 import java.io.IOException; 14 import java.io.InputStream; 15 import java.io.StringReader; 16 import java.io.StringWriter; 17 import java.math.BigDecimal; 18 import java.math.BigInteger; 19 import java.sql.Date; 20 import java.sql.Time; 21 import java.sql.Timestamp; 22 import java.util.List; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 import junit.textui.TestRunner; 27 28 import org.apache.commons.beanutils.ConvertUtils; 29 import org.apache.commons.betwixt.io.BeanReader; 30 import org.apache.commons.betwixt.io.BeanWriter; 31 32 33 /*** Test harness for the BeanReader 34 * 35 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 36 * @version $Revision: 1.7 $ 37 */ 38 public class TestBeanReader extends AbstractTestCase { 39 40 public static void main( String[] args ) { 41 TestRunner.run( suite() ); 42 } 43 44 public static Test suite() { 45 return new TestSuite(TestBeanReader.class); 46 } 47 48 public TestBeanReader(String testName) { 49 super(testName); 50 } 51 52 public void testBeanWriter() throws Exception { 53 BeanReader reader = new BeanReader(); 54 reader.registerBeanClass( getBeanClass() ); 55 56 InputStream in = getXMLInput(); 57 try { 58 Object bean = reader.parse( in ); 59 60 testCustomer(bean); 61 62 System.out.println( "Read bean: " + bean ); 63 System.out.println(); 64 System.out.println( "Lets turn it back into XML" ); 65 66 writeBean( bean ); 67 } 68 finally { 69 in.close(); 70 } 71 } 72 73 public void testWriteThenRead() throws Exception { 74 // test defaults 75 PersonBean bean = new PersonBean(21, "Samual Smith"); 76 StringWriter stringWriter = new StringWriter(); 77 BeanWriter beanWriter = new BeanWriter(stringWriter); 78 beanWriter.write(bean); 79 stringWriter.flush(); 80 String xml = "<?xml version='1.0'?>" + stringWriter.toString(); 81 82 BeanReader reader = new BeanReader(); 83 reader.registerBeanClass( PersonBean.class ); 84 bean = (PersonBean) reader.parse(new StringReader(xml)); 85 86 assertEquals("Person age wrong", 21 , bean.getAge()); 87 assertEquals("Person name wrong", "Samual Smith" , bean.getName()); 88 89 // test now with attributes for primitives 90 bean = new PersonBean(19, "John Smith"); 91 stringWriter = new StringWriter(); 92 beanWriter = new BeanWriter(stringWriter); 93 beanWriter.getXMLIntrospector().setAttributesForPrimitives(true); 94 beanWriter.write(bean); 95 stringWriter.flush(); 96 xml = "<?xml version='1.0'?>" + stringWriter.toString(); 97 98 reader = new BeanReader(); 99 reader.getXMLIntrospector().setAttributesForPrimitives(true); 100 reader.registerBeanClass( PersonBean.class ); 101 bean = (PersonBean) reader.parse(new StringReader(xml)); 102 103 assertEquals("[Attribute] Person age wrong", 19 , bean.getAge()); 104 assertEquals("[Attribute] Person name wrong", "John Smith" , bean.getName()); 105 } 106 107 public void writeBean(Object bean) throws Exception { 108 BeanWriter writer = new BeanWriter(); 109 writer.enablePrettyPrint(); 110 writer.write( bean ); 111 } 112 113 /*** @return the bean class to use as the root */ 114 public Class getBeanClass() { 115 return CustomerBean.class; 116 } 117 118 /*** 119 * Asserts that the parsed CustomerBean looks fine 120 */ 121 protected void testCustomer(Object bean) throws Exception { 122 assertTrue( "Is a CustomerBean", bean instanceof CustomerBean ); 123 CustomerBean customer = (CustomerBean) bean; 124 125 assertEquals( "name", "James", customer.getName() ); 126 127 String[] emails = customer.getEmails(); 128 assertTrue( "contains some emails", emails != null ); 129 assertEquals( "emails.length", 2, emails.length ); 130 assertEquals( "emails[0]", "jstrachan@apache.org", emails[0] ); 131 assertEquals( "emails[1]", "james_strachan@yahoo.co.uk", emails[1] ); 132 133 int[] numbers = customer.getNumbers(); 134 assertTrue( "contains some numbers", numbers != null ); 135 assertEquals( "numbers.length", 3, numbers.length ); 136 assertEquals( "numbers[0]", 3, numbers[0] ); 137 assertEquals( "numbers[1]", 4, numbers[1] ); 138 assertEquals( "numbers[2]", 5, numbers[2] ); 139 140 List locations = customer.getLocations(); 141 assertTrue( "contains some locations", locations != null ); 142 assertEquals( "locations.size()", 2, locations.size() ); 143 assertEquals( "locations[0]", "London", locations.get(0) ); 144 assertEquals( "locations[1]", "Bath", locations.get(1) ); 145 146 assertEquals( ConvertUtils.convert("2002-03-17", Date.class), customer.getDate()); 147 assertEquals( ConvertUtils.convert("20:30:40", Time.class), customer.getTime()); 148 assertEquals( ConvertUtils.convert("2002-03-17 20:30:40.0", Timestamp.class), customer.getTimestamp()); 149 150 assertEquals( new BigDecimal("1234567890.12345"), customer.getBigDecimal() ); 151 assertEquals( new BigInteger("1234567890"), customer.getBigInteger() ); 152 } 153 154 protected InputStream getXMLInput() throws IOException { 155 return new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/customer.xml") ); 156 } 157 158 /*** 159 * This tests that you can read a bean which has an adder but not a property 160 */ 161 public void testAdderButNoProperty() throws Exception { 162 /* 163 // 164 // This is a test for an unfixed issue that might - or might not - be a bug 165 // a developer submitted a patch but this broke the other unit test 166 // a proper fix would require quite a lot of work including some refactoring 167 // of various interfaces 168 // 169 170 // check bean's still working 171 AdderButNoPropertyBean bean = new AdderButNoPropertyBean(); 172 bean.addString("one"); 173 bean.addString("two"); 174 bean.addString("three"); 175 checkBean(bean); 176 177 BeanReader reader = new BeanReader(); 178 reader.registerBeanClass( AdderButNoPropertyBean.class ); 179 180 InputStream in = 181 new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/adder-np.xml") ); 182 try { 183 184 checkBean((AdderButNoPropertyBean) reader.parse( in )); 185 186 } 187 finally { 188 in.close(); 189 } 190 */ 191 } 192 193 private void checkBean(AdderButNoPropertyBean bean) throws Exception { 194 assertEquals("Bad addString call count", 3, bean.stringCallCount()); 195 } 196 197 private void checkBean(PersonListBean bean) throws Exception { 198 assertEquals("PersonList size", 4, bean.getPersonList().size()); 199 assertEquals("PersonList value (1)", "Athos", ((PersonBean) bean.getPersonList().get(0)).getName()); 200 assertEquals("PersonList value (2)", "Porthos", ((PersonBean) bean.getPersonList().get(1)).getName()); 201 assertEquals("PersonList value (3)", "Aramis", ((PersonBean) bean.getPersonList().get(2)).getName()); 202 assertEquals("PersonList value (4)", "D'Artagnan", ((PersonBean) bean.getPersonList().get(3)).getName()); 203 } 204 205 public void testPersonList() throws Exception { 206 207 PersonListBean people = new PersonListBean(); 208 people.addPerson(new PersonBean(22, "Athos")); 209 people.addPerson(new PersonBean(25, "Porthos")); 210 people.addPerson(new PersonBean(23, "Aramis")); 211 people.addPerson(new PersonBean(18, "D'Artagnan")); 212 213 checkBean(people); 214 // 215 // Logging and debugging code for this method commented out 216 // 217 // writeBean(people); 218 219 // SimpleLog log = new SimpleLog("[TestPersonList:XMLIntrospectorHelper]"); 220 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE); 221 // XMLIntrospectorHelper.setLog(log); 222 223 224 // log = new SimpleLog("[TestPersonList:BeanCreateRule]"); 225 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE); 226 // BeanCreateRule.setLog(log); 227 228 // log = new SimpleLog("[TestPersonList:XMLIntrospector]"); 229 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE); 230 231 BeanReader reader = new BeanReader(); 232 // reader.getXMLIntrospector().setLog(log); 233 234 // log = new SimpleLog("[TestPersonList:BeanReader]"); 235 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE); 236 237 // reader.setLog(log); 238 reader.registerBeanClass( PersonListBean.class ); 239 240 InputStream in = 241 new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/person-list.xml") ); 242 try { 243 244 checkBean((PersonListBean) reader.parse( in )); 245 246 } 247 finally { 248 in.close(); 249 } 250 } 251 } 252

This page was automatically generated by Maven