1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  package org.apache.commons.betwixt.derived;
17  
18  import java.io.FileInputStream;
19  import java.io.InputStream;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  import junit.textui.TestRunner;
24  
25  import org.apache.commons.betwixt.AbstractTestCase;
26  import org.apache.commons.betwixt.io.BeanReader;
27  
28  
29  /*** Test harness for the BeanReader
30    *
31    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32    * @version $Revision: 155402 $
33    */
34  public class TestDerived extends AbstractTestCase {
35      
36      public static void main( String[] args ) {
37          TestRunner.run( suite() );
38      }
39      
40      public static Test suite() {
41          return new TestSuite(TestDerived.class);
42      }
43      
44      public TestDerived(String testName) {
45          super(testName);
46      }
47      
48      public void testPersonList() throws Exception {
49  
50          BeanReader reader = new BeanReader();
51          reader.registerBeanClass( PersonListBean.class );
52          
53          InputStream in =  
54              new FileInputStream( getTestFile("src/test/org/apache/commons/betwixt/derived/person-list.xml") );
55          try {
56          
57              checkBean((PersonListBean) reader.parse( in ));
58              
59          }
60          finally {
61              in.close();
62          }   
63      }
64      
65      protected void checkBean(PersonListBean bean) throws Exception {
66          PersonBean owner = bean.getOwner();
67          assertTrue("should have found an owner", owner != null );
68          
69          assertEquals("should be derived class", "org.apache.commons.betwixt.derived.EmployeeBean", owner.getClass().getName());
70          
71          
72          assertEquals("PersonList size", 4, bean.getPersonList().size());
73          assertEquals("PersonList value (1)", "Athos", ((PersonBean) bean.getPersonList().get(0)).getName());
74          assertEquals("PersonList value (2)", "Porthos", ((PersonBean) bean.getPersonList().get(1)).getName());
75          assertEquals("PersonList value (3)", "Aramis", ((PersonBean) bean.getPersonList().get(2)).getName());
76          assertEquals("PersonList value (4)", "D'Artagnan", ((PersonBean) bean.getPersonList().get(3)).getName());
77          
78          PersonBean employee = (PersonBean) bean.getPersonList().get(1);
79          assertEquals("should be derived class", "org.apache.commons.betwixt.derived.EmployeeBean", employee.getClass().getName());
80          
81          PersonBean manager = (PersonBean) bean.getPersonList().get(2);
82          assertEquals("should be derived class", "org.apache.commons.betwixt.derived.ManagerBean", manager.getClass().getName());
83  
84          // test derived properties       
85          //assertEquals("should have a derived property", 12, ((ManagerBean) manager).getCheeseSize());
86      }
87      
88  }
89