KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > test > TableTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.test;
31
32 import nextapp.echo2.app.CheckBox;
33 import nextapp.echo2.app.Component;
34 import nextapp.echo2.app.Label;
35 import nextapp.echo2.app.Table;
36 import nextapp.echo2.app.table.DefaultTableColumnModel;
37 import nextapp.echo2.app.table.DefaultTableModel;
38 import nextapp.echo2.app.table.TableCellRenderer;
39 import junit.framework.TestCase;
40
41 /**
42  * Unit tests for <code>Table</code> components.
43  */

44 public class TableTest extends TestCase {
45     
46     private DefaultTableModel createEmployeeTableModel() {
47         DefaultTableModel model = new DefaultTableModel();
48         model.setColumnCount(3);
49
50         model.setColumnName(0, "Employee Name");
51         model.setColumnName(1, "Age");
52         model.setColumnName(2, "Manager");
53         
54         model.insertRow(0, new Object JavaDoc[]{"Bob Johnson", new Integer JavaDoc(32), Boolean.TRUE});
55         model.insertRow(1, new Object JavaDoc[]{"Bill Simmons", new Integer JavaDoc(27), Boolean.TRUE});
56         model.insertRow(2, new Object JavaDoc[]{"Tracy Smith", new Integer JavaDoc(54), Boolean.TRUE});
57         model.insertRow(3, new Object JavaDoc[]{"Cathy Rogers", new Integer JavaDoc(21), Boolean.FALSE});
58         model.insertRow(4, new Object JavaDoc[]{"Xavier Doe", new Integer JavaDoc(77), Boolean.TRUE});
59
60         return model;
61     }
62     
63     public void testDefaultColumnNames() {
64         DefaultTableModel model = new DefaultTableModel();
65         model.setColumnCount(3);
66         
67         assertEquals("A", model.getColumnName(0));
68         assertEquals("B", model.getColumnName(1));
69         assertEquals("C", model.getColumnName(2));
70         
71         model = new DefaultTableModel();
72         model.setColumnCount(1379);
73         
74         assertEquals("A", model.getColumnName(0));
75         assertEquals("B", model.getColumnName(1));
76         assertEquals("C", model.getColumnName(2));
77         assertEquals("Y", model.getColumnName(24));
78         assertEquals("Z", model.getColumnName(25));
79         assertEquals("AA", model.getColumnName(26));
80         assertEquals("AB", model.getColumnName(27));
81         assertEquals("AC", model.getColumnName(28));
82         assertEquals("AY", model.getColumnName(50));
83         assertEquals("AZ", model.getColumnName(51));
84         assertEquals("BA", model.getColumnName(52));
85         assertEquals("ZZ", model.getColumnName(701));
86         assertEquals("AAA", model.getColumnName(702));
87         assertEquals("AAB", model.getColumnName(703));
88         assertEquals("AAC", model.getColumnName(704));
89         assertEquals("ABA", model.getColumnName(728));
90         assertEquals("AZZ", model.getColumnName(1377));
91         assertEquals("BAA", model.getColumnName(1378));
92     }
93
94     public void testDefaultTableModel() {
95         DefaultTableModel model = createEmployeeTableModel();
96         
97         assertEquals("Employee Name", model.getColumnName(0));
98         assertEquals("Age", model.getColumnName(1));
99         assertEquals("Manager", model.getColumnName(2));
100         assertEquals(3, model.getColumnCount());
101         assertEquals(5, model.getRowCount());
102         assertEquals("Bob Johnson", model.getValueAt(0, 0));
103         assertEquals("Xavier Doe", model.getValueAt(0, 4));
104         assertEquals(new Integer JavaDoc(21), model.getValueAt(1, 3));
105         assertEquals(Boolean.FALSE, model.getValueAt(2, 3));
106
107         model.deleteRow(1);
108         assertEquals(4, model.getRowCount());
109         assertEquals("Bob Johnson", model.getValueAt(0, 0));
110         assertEquals("Xavier Doe", model.getValueAt(0, 3));
111         assertEquals(new Integer JavaDoc(21), model.getValueAt(1, 2));
112         assertEquals(Boolean.FALSE, model.getValueAt(2, 2));
113         
114         model.insertRow(2, new Object JavaDoc[]{"Whitney Ford", new Integer JavaDoc(33), Boolean.FALSE});
115         assertEquals(5, model.getRowCount());
116         assertEquals("Whitney Ford", model.getValueAt(0, 2));
117         assertEquals("Bob Johnson", model.getValueAt(0, 0));
118         assertEquals("Xavier Doe", model.getValueAt(0, 4));
119         assertEquals(new Integer JavaDoc(21), model.getValueAt(1, 3));
120         assertEquals(Boolean.FALSE, model.getValueAt(2, 3));
121     }
122
123     public void testColumModelRendering() {
124         Label label;
125         DefaultTableModel model = createEmployeeTableModel();
126         Table table = new Table(model);
127         DefaultTableColumnModel columnModel = (DefaultTableColumnModel) table.getColumnModel();
128         assertEquals(0, columnModel.getColumn(0).getModelIndex());
129         assertEquals(1, columnModel.getColumn(1).getModelIndex());
130         assertEquals(2, columnModel.getColumn(2).getModelIndex());
131         table.setAutoCreateColumnsFromModel(false);
132         table.validate();
133         
134         label = (Label) table.getComponent(4);
135         assertEquals("32", label.getText());
136         label = (Label) table.getComponent(5);
137         assertEquals("true", label.getText());
138         
139         columnModel.getColumn(2).setModelIndex(1);
140         columnModel.getColumn(1).setModelIndex(2);
141         table.setColumnModel(columnModel);
142         table.validate();
143         
144         // Indices should switch.
145
label = (Label) table.getComponent(4);
146         assertEquals("true", label.getText());
147         label = (Label) table.getComponent(5);
148         assertEquals("32", label.getText());
149     }
150     
151     public void testEmptyConstructor() {
152         Table table = new Table();
153         assertNotNull(table.getModel());
154         assertEquals(DefaultTableModel.class, table.getModel().getClass());
155         DefaultTableModel model = (DefaultTableModel) table.getModel();
156         assertEquals(0, model.getColumnCount());
157         assertEquals(0, model.getRowCount());
158     }
159     
160     public void testRender() {
161         Table table = new Table();
162         table.setDefaultRenderer(Object JavaDoc.class, new TableCellRenderer() {
163             public Component getTableCellRendererComponent(Table table, Object JavaDoc value, int column, int row) {
164                 switch (column) {
165                 case 0:
166                 case 1:
167                     return new Label(value.toString());
168                 case 2:
169                     CheckBox checkBox = new CheckBox();
170                     checkBox.setSelected(((Boolean JavaDoc) value).booleanValue());
171                     return checkBox;
172                 default:
173                     throw new IndexOutOfBoundsException JavaDoc();
174                 }
175             }
176         });
177         DefaultTableModel model = (DefaultTableModel) table.getModel();
178         model.setColumnCount(3);
179         model.insertRow(0, new Object JavaDoc[]{"Bob Johnson", new Integer JavaDoc(32), Boolean.TRUE});
180         model.insertRow(1, new Object JavaDoc[]{"Bill Simmons", new Integer JavaDoc(27), Boolean.TRUE});
181         model.insertRow(2, new Object JavaDoc[]{"Tracy Smith", new Integer JavaDoc(54), Boolean.TRUE});
182         model.insertRow(3, new Object JavaDoc[]{"Cathy Rogers", new Integer JavaDoc(21), Boolean.FALSE});
183         model.insertRow(4, new Object JavaDoc[]{"Xavier Doe", new Integer JavaDoc(77), Boolean.TRUE});
184         table.validate();
185         assertEquals(18, table.getComponentCount());
186         Component[] components = table.getComponents();
187         for (int i = 3; i < components.length; ++i) {
188             if (i % 3 == 2) {
189                 assertEquals(CheckBox.class, components[i].getClass());
190             } else {
191                 assertEquals(Label.class, components[i].getClass());
192             }
193         }
194         assertTrue(components[0] instanceof Label);
195         assertEquals("A", ((Label) components[0]).getText());
196         assertTrue(components[5] instanceof CheckBox);
197         assertTrue(((CheckBox) components[5]).isSelected());
198         assertTrue(components[8] instanceof CheckBox);
199         assertTrue(((CheckBox) components[8]).isSelected());
200         assertTrue(components[14] instanceof CheckBox);
201         assertFalse(((CheckBox) components[14]).isSelected());
202
203         assertTrue(table.getCellComponent(0, 0) instanceof Label);
204         assertEquals("A", ((Label) table.getCellComponent(0, Table.HEADER_ROW)).getText());
205         assertTrue(table.getCellComponent(2, 0) instanceof CheckBox);
206         assertTrue(((CheckBox) table.getCellComponent(2, 0)).isSelected());
207         assertTrue(table.getCellComponent(2, 1) instanceof CheckBox);
208         assertTrue(((CheckBox) table.getCellComponent(2, 1)).isSelected());
209         assertTrue(table.getCellComponent(2, 3) instanceof CheckBox);
210         assertFalse(((CheckBox) table.getCellComponent(2, 3)).isSelected());
211     }
212 }
213
Popular Tags