001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.test;
031:
032: import nextapp.echo2.app.CheckBox;
033: import nextapp.echo2.app.Component;
034: import nextapp.echo2.app.Label;
035: import nextapp.echo2.app.Table;
036: import nextapp.echo2.app.table.DefaultTableColumnModel;
037: import nextapp.echo2.app.table.DefaultTableModel;
038: import nextapp.echo2.app.table.TableCellRenderer;
039: import junit.framework.TestCase;
040:
041: /**
042: * Unit tests for <code>Table</code> components.
043: */
044: public class TableTest extends TestCase {
045:
046: private DefaultTableModel createEmployeeTableModel() {
047: DefaultTableModel model = new DefaultTableModel();
048: model.setColumnCount(3);
049:
050: model.setColumnName(0, "Employee Name");
051: model.setColumnName(1, "Age");
052: model.setColumnName(2, "Manager");
053:
054: model.insertRow(0, new Object[] { "Bob Johnson",
055: new Integer(32), Boolean.TRUE });
056: model.insertRow(1, new Object[] { "Bill Simmons",
057: new Integer(27), Boolean.TRUE });
058: model.insertRow(2, new Object[] { "Tracy Smith",
059: new Integer(54), Boolean.TRUE });
060: model.insertRow(3, new Object[] { "Cathy Rogers",
061: new Integer(21), Boolean.FALSE });
062: model.insertRow(4, new Object[] { "Xavier Doe",
063: new Integer(77), Boolean.TRUE });
064:
065: return model;
066: }
067:
068: public void testDefaultColumnNames() {
069: DefaultTableModel model = new DefaultTableModel();
070: model.setColumnCount(3);
071:
072: assertEquals("A", model.getColumnName(0));
073: assertEquals("B", model.getColumnName(1));
074: assertEquals("C", model.getColumnName(2));
075:
076: model = new DefaultTableModel();
077: model.setColumnCount(1379);
078:
079: assertEquals("A", model.getColumnName(0));
080: assertEquals("B", model.getColumnName(1));
081: assertEquals("C", model.getColumnName(2));
082: assertEquals("Y", model.getColumnName(24));
083: assertEquals("Z", model.getColumnName(25));
084: assertEquals("AA", model.getColumnName(26));
085: assertEquals("AB", model.getColumnName(27));
086: assertEquals("AC", model.getColumnName(28));
087: assertEquals("AY", model.getColumnName(50));
088: assertEquals("AZ", model.getColumnName(51));
089: assertEquals("BA", model.getColumnName(52));
090: assertEquals("ZZ", model.getColumnName(701));
091: assertEquals("AAA", model.getColumnName(702));
092: assertEquals("AAB", model.getColumnName(703));
093: assertEquals("AAC", model.getColumnName(704));
094: assertEquals("ABA", model.getColumnName(728));
095: assertEquals("AZZ", model.getColumnName(1377));
096: assertEquals("BAA", model.getColumnName(1378));
097: }
098:
099: public void testDefaultTableModel() {
100: DefaultTableModel model = createEmployeeTableModel();
101:
102: assertEquals("Employee Name", model.getColumnName(0));
103: assertEquals("Age", model.getColumnName(1));
104: assertEquals("Manager", model.getColumnName(2));
105: assertEquals(3, model.getColumnCount());
106: assertEquals(5, model.getRowCount());
107: assertEquals("Bob Johnson", model.getValueAt(0, 0));
108: assertEquals("Xavier Doe", model.getValueAt(0, 4));
109: assertEquals(new Integer(21), model.getValueAt(1, 3));
110: assertEquals(Boolean.FALSE, model.getValueAt(2, 3));
111:
112: model.deleteRow(1);
113: assertEquals(4, model.getRowCount());
114: assertEquals("Bob Johnson", model.getValueAt(0, 0));
115: assertEquals("Xavier Doe", model.getValueAt(0, 3));
116: assertEquals(new Integer(21), model.getValueAt(1, 2));
117: assertEquals(Boolean.FALSE, model.getValueAt(2, 2));
118:
119: model.insertRow(2, new Object[] { "Whitney Ford",
120: new Integer(33), Boolean.FALSE });
121: assertEquals(5, model.getRowCount());
122: assertEquals("Whitney Ford", model.getValueAt(0, 2));
123: assertEquals("Bob Johnson", model.getValueAt(0, 0));
124: assertEquals("Xavier Doe", model.getValueAt(0, 4));
125: assertEquals(new Integer(21), model.getValueAt(1, 3));
126: assertEquals(Boolean.FALSE, model.getValueAt(2, 3));
127: }
128:
129: public void testColumModelRendering() {
130: Label label;
131: DefaultTableModel model = createEmployeeTableModel();
132: Table table = new Table(model);
133: DefaultTableColumnModel columnModel = (DefaultTableColumnModel) table
134: .getColumnModel();
135: assertEquals(0, columnModel.getColumn(0).getModelIndex());
136: assertEquals(1, columnModel.getColumn(1).getModelIndex());
137: assertEquals(2, columnModel.getColumn(2).getModelIndex());
138: table.setAutoCreateColumnsFromModel(false);
139: table.validate();
140:
141: label = (Label) table.getComponent(4);
142: assertEquals("32", label.getText());
143: label = (Label) table.getComponent(5);
144: assertEquals("true", label.getText());
145:
146: columnModel.getColumn(2).setModelIndex(1);
147: columnModel.getColumn(1).setModelIndex(2);
148: table.setColumnModel(columnModel);
149: table.validate();
150:
151: // Indices should switch.
152: label = (Label) table.getComponent(4);
153: assertEquals("true", label.getText());
154: label = (Label) table.getComponent(5);
155: assertEquals("32", label.getText());
156: }
157:
158: public void testEmptyConstructor() {
159: Table table = new Table();
160: assertNotNull(table.getModel());
161: assertEquals(DefaultTableModel.class, table.getModel()
162: .getClass());
163: DefaultTableModel model = (DefaultTableModel) table.getModel();
164: assertEquals(0, model.getColumnCount());
165: assertEquals(0, model.getRowCount());
166: }
167:
168: public void testRender() {
169: Table table = new Table();
170: table.setDefaultRenderer(Object.class, new TableCellRenderer() {
171: public Component getTableCellRendererComponent(Table table,
172: Object value, int column, int row) {
173: switch (column) {
174: case 0:
175: case 1:
176: return new Label(value.toString());
177: case 2:
178: CheckBox checkBox = new CheckBox();
179: checkBox.setSelected(((Boolean) value)
180: .booleanValue());
181: return checkBox;
182: default:
183: throw new IndexOutOfBoundsException();
184: }
185: }
186: });
187: DefaultTableModel model = (DefaultTableModel) table.getModel();
188: model.setColumnCount(3);
189: model.insertRow(0, new Object[] { "Bob Johnson",
190: new Integer(32), Boolean.TRUE });
191: model.insertRow(1, new Object[] { "Bill Simmons",
192: new Integer(27), Boolean.TRUE });
193: model.insertRow(2, new Object[] { "Tracy Smith",
194: new Integer(54), Boolean.TRUE });
195: model.insertRow(3, new Object[] { "Cathy Rogers",
196: new Integer(21), Boolean.FALSE });
197: model.insertRow(4, new Object[] { "Xavier Doe",
198: new Integer(77), Boolean.TRUE });
199: table.validate();
200: assertEquals(18, table.getComponentCount());
201: Component[] components = table.getComponents();
202: for (int i = 3; i < components.length; ++i) {
203: if (i % 3 == 2) {
204: assertEquals(CheckBox.class, components[i].getClass());
205: } else {
206: assertEquals(Label.class, components[i].getClass());
207: }
208: }
209: assertTrue(components[0] instanceof Label);
210: assertEquals("A", ((Label) components[0]).getText());
211: assertTrue(components[5] instanceof CheckBox);
212: assertTrue(((CheckBox) components[5]).isSelected());
213: assertTrue(components[8] instanceof CheckBox);
214: assertTrue(((CheckBox) components[8]).isSelected());
215: assertTrue(components[14] instanceof CheckBox);
216: assertFalse(((CheckBox) components[14]).isSelected());
217:
218: assertTrue(table.getCellComponent(0, 0) instanceof Label);
219: assertEquals("A", ((Label) table.getCellComponent(0,
220: Table.HEADER_ROW)).getText());
221: assertTrue(table.getCellComponent(2, 0) instanceof CheckBox);
222: assertTrue(((CheckBox) table.getCellComponent(2, 0))
223: .isSelected());
224: assertTrue(table.getCellComponent(2, 1) instanceof CheckBox);
225: assertTrue(((CheckBox) table.getCellComponent(2, 1))
226: .isSelected());
227: assertTrue(table.getCellComponent(2, 3) instanceof CheckBox);
228: assertFalse(((CheckBox) table.getCellComponent(2, 3))
229: .isSelected());
230: }
231: }
|