You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/08/13 15:35:35 UTC

cvs commit: jakarta-commons/betwixt/src/test/org/apache/commons/betwixt CustomerBean.java customer.xml TestBeanWriter.java TestBeanReader.java

jstrachan    2002/08/13 06:35:35

  Modified:    betwixt/src/test/org/apache/commons/betwixt
                        CustomerBean.java customer.xml TestBeanWriter.java
                        TestBeanReader.java
  Log:
  created adder methods on the test CustomerBean so that all the array based properties now round trip to XML and back again fine.
  
  Also added more rigorous testing that this is the case.
  
  Thanks go to Diego Tognola for bringing this up and providing the code for the adder methods.
  
  Revision  Changes    Path
  1.2       +30 -0     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java
  
  Index: CustomerBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CustomerBean.java	10 Jun 2002 17:53:32 -0000	1.1
  +++ CustomerBean.java	13 Aug 2002 13:35:34 -0000	1.2
  @@ -132,6 +132,10 @@
           return new IteratorEnumeration( projectMap.values().iterator() );
       }
       
  +    public List getLocations() {
  +        return locations;
  +    }
  +    
       /** An indexed property */
       public String getLocation(int index) {
           return (String) locations.get(index);
  @@ -149,9 +153,31 @@
           this.emails = emails;
       }
       
  +    public void addEmail(String email) {
  +        int newLength = (emails == null) ? 1 : emails.length+1;
  +        String[] newArray = new String[newLength];
  +        for (int i=0; i< newLength-1; i++) {
  +            newArray[i] = emails[i];
  +        }
  +        newArray[newLength-1] = email;
  +        emails = newArray;
  +    }    
  +    
       public void setNumbers(int[] numbers) {
           this.numbers = numbers;
       }
  +
  +    public void addNumber(int number) {
  +        System.out.println( "Adding number: " + number );
  +        
  +        int newLength = (numbers == null) ? 1 : numbers.length+1;
  +        int[] newArray = new int[newLength];
  +        for (int i=0; i< newLength-1; i++) {
  +            newArray[i] = numbers[i];
  +        }
  +        newArray[newLength-1] = number;
  +        numbers = newArray;
  +    }
       
       public void setAddress(AddressBean address) {
           this.address = address;
  @@ -163,6 +189,10 @@
   
       public void setProjectMap(Map projectMap) {
           this.projectMap = projectMap;
  +    }
  +    
  +    public void addLocation(String location) {
  +        locations.add(location);
       }
       
       /** An indexed property */
  
  
  
  1.2       +11 -6     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/customer.xml
  
  Index: customer.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/customer.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- customer.xml	10 Jun 2002 17:53:33 -0000	1.1
  +++ customer.xml	13 Aug 2002 13:35:34 -0000	1.2
  @@ -1,5 +1,6 @@
   <?xml version="1.0" encoding="UTF-8" ?>
  -<CustomerBean name="James" ID="1">
  +<CustomerBean ID="1">
  +  <name>James</name>
     <address code="N5" city="London" country="UK" street="Near the park"/>
     <projectMap>
       <entry>
  @@ -26,8 +27,8 @@
       <String>jakarta-commons</String>
     </projectNames>
     <emails>
  -    <String>jstrachan@apache.org</String>
  -    <String>james_strachan@yahoo.co.uk</String>
  +    <email>jstrachan@apache.org</email>
  +    <email>james_strachan@yahoo.co.uk</email>
     </emails>
     <projectURLs>
       <String>http://jaxen.org</String>
  @@ -36,8 +37,12 @@
       <String>http://jakarta.apache.org/commons/</String>
     </projectURLs>
     <numbers>
  -    <Integer>3</Integer>
  -    <Integer>4</Integer>
  -    <Integer>5</Integer>
  +    <number>3</number>
  +    <number>4</number>
  +    <number>5</number>
     </numbers>
  +  <locations>
  +    <location>London</location>
  +    <location>Bath</location>
  +  </locations>
   </CustomerBean>
  
  
  
  1.3       +9 -5      jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java
  
  Index: TestBeanWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestBeanWriter.java	15 Jun 2002 23:24:32 -0000	1.2
  +++ TestBeanWriter.java	13 Aug 2002 13:35:34 -0000	1.3
  @@ -164,12 +164,16 @@
           writer.write(bean);
           out.flush();
           String result = out.toString();
  +        
  +        System.out.println( "Created..." );
  +        System.out.println( result );
  +        
           // check for the elemant content..
  -        assertTrue(result.indexOf("<String>Escape&lt;LessThan</String>") > -1 );
  -        assertTrue(result.indexOf("<String>Escape&gt;GreaterThan</String>") > -1);
  -        assertTrue(result.indexOf("<String>Escape&amp;amphersand</String>") != -1);
  -        assertTrue(result.indexOf("<String>Escape'apostrophe</String>") != -1);
  -        assertTrue(result.indexOf("<String>Escape\"Quote</String>") != -1);
  +        assertTrue(result.indexOf("<email>Escape&lt;LessThan</email>") > -1 );
  +        assertTrue(result.indexOf("<email>Escape&gt;GreaterThan</email>") > -1);
  +        assertTrue(result.indexOf("<email>Escape&amp;amphersand</email>") != -1);
  +        assertTrue(result.indexOf("<email>Escape'apostrophe</email>") != -1);
  +        assertTrue(result.indexOf("<email>Escape\"Quote</email>") != -1);
           // check for the attributes..
           assertTrue(result.indexOf("name=\"Escape&lt;LessThan\"") > -1 );
           assertTrue(result.indexOf("code=\"Escape&gt;GreaterThan\"") > -1);
  
  
  
  1.3       +51 -17    jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestBeanReader.java
  
  Index: TestBeanReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestBeanReader.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestBeanReader.java	26 Jul 2002 21:04:05 -0000	1.2
  +++ TestBeanReader.java	13 Aug 2002 13:35:34 -0000	1.3
  @@ -14,6 +14,7 @@
   import java.io.IOException;
   import java.io.StringReader;
   import java.io.StringWriter;
  +import java.util.List;
   
   import junit.framework.Test;
   import junit.framework.TestCase;
  @@ -55,6 +56,8 @@
           try {
               Object bean = reader.parse( in );
   
  +            testCustomer(bean);
  +            
               System.out.println( "Read bean: " + bean );
               System.out.println();
               System.out.println( "Lets turn it back into XML" );
  @@ -66,23 +69,7 @@
           }
       }
       
  -    public void writeBean(Object bean) throws Exception {
  -        BeanWriter writer = new BeanWriter();
  -        writer.enablePrettyPrint();
  -        writer.write( bean );
  -    }
  -    
  -    /** @return the bean class to use as the root */
  -    public Class getBeanClass() {
  -        return CustomerBean.class;
  -    }
  -    
  -    protected InputStream getXMLInput() throws IOException {
  -        return new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/customer.xml") );
  -    }
  -    
  -    public void testWriteThenRead() throws Exception
  -    {
  +    public void testWriteThenRead() throws Exception {
           // test defaults
           PersonBean bean = new PersonBean(21, "Samual Smith");
           StringWriter stringWriter = new StringWriter();
  @@ -115,5 +102,52 @@
           assertEquals("[Attribute] Person age wrong", 19 , bean.getAge());
           assertEquals("[Attribute] Person name wrong", "John Smith" , bean.getName());
       }
  +
  +    public void writeBean(Object bean) throws Exception {
  +        BeanWriter writer = new BeanWriter();
  +        writer.enablePrettyPrint();
  +        writer.write( bean );
  +    }
  +    
  +    /** @return the bean class to use as the root */
  +    public Class getBeanClass() {
  +        return CustomerBean.class;
  +    }
  +    
  +    /** 
  +     * Asserts that the parsed CustomerBean looks fine
  +     */
  +    protected void testCustomer(Object bean) throws Exception {
  +        assertTrue( "Is a CustomerBean", bean instanceof CustomerBean );
  +        CustomerBean customer = (CustomerBean) bean;
  +     
  +        assertEquals( "name", "James", customer.getName() );
  +        
  +        String[] emails = customer.getEmails();
  +        assertTrue( "contains some emails", emails != null );
  +        assertEquals( "emails.length", 2, emails.length );
  +        assertEquals( "emails[0]", "jstrachan@apache.org", emails[0] );
  +        assertEquals( "emails[1]", "james_strachan@yahoo.co.uk", emails[1] );
  +        
  +        int[] numbers = customer.getNumbers();
  +        assertTrue( "contains some numbers", numbers != null );
  +        assertEquals( "numbers.length", 3, numbers.length );
  +        assertEquals( "numbers[0]", 3, numbers[0] );
  +        assertEquals( "numbers[1]", 4, numbers[1] );
  +        assertEquals( "numbers[2]", 5, numbers[2] );
  +        
  +        List locations = customer.getLocations();
  +        assertTrue( "contains some locations", locations != null );
  +        assertEquals( "locations.size()", 2, locations.size() );
  +        assertEquals( "locations[0]", "London", locations.get(0) );
  +        assertEquals( "locations[1]", "Bath", locations.get(1) );
  +        
  +        
  +    }
  +    
  +    protected InputStream getXMLInput() throws IOException {
  +        return new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/customer.xml") );
  +    }
  +    
   }
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>