1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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