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