1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   */
38  package com.gargoylesoftware.base.gui;
39  
40  import com.gargoylesoftware.base.testing.SampleBean;
41  import java.beans.IntrospectionException;
42  import java.util.Arrays;
43  import java.util.List;
44  import junit.framework.TestCase;
45  
46  /***
47   * Tests for ReflectedTableModel
48   *
49   * @version  $Revision: 1.6 $
50   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51   */
52  public class ReflectedTableModelTest extends TestCase {
53  
54      /***
55       * Create a new test.
56       * @param name The name of the test.
57       */
58      public ReflectedTableModelTest( final String name ) {
59          super(name);
60      }
61  
62      /***
63       * Test the constructor that takes a class.
64       * @throws IntrospectionException If an error occurs while getting the property information
65       * for the class.
66       */
67      public void testClassConstructor() throws IntrospectionException {
68          final ReflectedTableModel model = new ReflectedTableModel( SampleBean.class );
69          final List columns = model.getColumns();
70  
71          final String sampleBeanProperties[] = {
72              "age",
73              "class",
74              "name"
75          };
76  
77          ReflectedTableModel.ColumnInfo columnInfo;
78          final int maxCount = columns.size();
79          final String foundProperties[] = new String[maxCount];
80          assertEquals( "Number of columns", sampleBeanProperties.length, columns.size() );
81          int i;
82          for( i=0; i<maxCount; i++ ) {
83              columnInfo = (ReflectedTableModel.ColumnInfo)columns.get(i);
84              foundProperties[i] = columnInfo.getPropertyName();
85          }
86  
87          Arrays.sort(foundProperties);
88          if( Arrays.equals( foundProperties, sampleBeanProperties ) == false ) {
89              fail("Properties were different from expected");
90          }
91      }
92  
93      /***
94       * Test getValueAt()
95       * @throws IntrospectionException If an error occurs while getting the property information
96       * for the class.
97       */
98      public void testGetValueAt() throws IntrospectionException {
99          final ReflectedTableModel model = new ReflectedTableModel( SampleBean.class );
100         final List rows = model.getRows();
101 
102         final Object data[][] = {
103             { "Mickey", new Integer( 5)},
104             { "Minnie", new Integer(10)},
105             { "Goofy",  new Integer(15)},
106             { "Pluto",  new Integer(20)},
107         };
108 
109         int i;
110         for( i=0; i<data.length; i++ ) {
111             rows.add( new SampleBean( (String)data[i][0], ((Integer)data[i][1]).intValue() ) );
112         }
113 
114         assertEquals("Number of rows", data.length, model.getRowCount() );
115 
116         int nameColumnIndex = -1;
117         int ageColumnIndex = -1;
118 
119         String columnName;
120 
121         final int columnCount = model.getColumnCount();
122         for( i=0; i<columnCount; i++ ) {
123             columnName = model.getColumnName(i);
124             if( columnName.equals("name") ) {
125                 nameColumnIndex = i;
126             }
127             else if( columnName.equals("age") ) {
128                 ageColumnIndex = i;
129             }
130         }
131 
132         assertTrue( "nameColumnIndex not found", nameColumnIndex != -1 );
133         assertTrue( "ageColumnIndex not found", ageColumnIndex != -1 );
134 
135         for( i=0; i<data.length; i++ ) {
136             assertEquals( "Name["+i+"]",
137                           data[i][0],
138                           model.getValueAt(i, nameColumnIndex) );
139 
140             assertEquals( "Age["+i+"]",
141                           data[i][1],
142                           model.getValueAt(i, ageColumnIndex) );
143         }
144     }
145 }
146 
147 
148 
149 
150