1 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 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 []{"Bob Johnson", new Integer (32), Boolean.TRUE}); 55 model.insertRow(1, new Object []{"Bill Simmons", new Integer (27), Boolean.TRUE}); 56 model.insertRow(2, new Object []{"Tracy Smith", new Integer (54), Boolean.TRUE}); 57 model.insertRow(3, new Object []{"Cathy Rogers", new Integer (21), Boolean.FALSE}); 58 model.insertRow(4, new Object []{"Xavier Doe", new Integer (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 (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 (21), model.getValueAt(1, 2)); 112 assertEquals(Boolean.FALSE, model.getValueAt(2, 2)); 113 114 model.insertRow(2, new Object []{"Whitney Ford", new Integer (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 (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 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 .class, new TableCellRenderer() { 163 public Component getTableCellRendererComponent(Table table, Object 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 ) value).booleanValue()); 171 return checkBox; 172 default: 173 throw new IndexOutOfBoundsException (); 174 } 175 } 176 }); 177 DefaultTableModel model = (DefaultTableModel) table.getModel(); 178 model.setColumnCount(3); 179 model.insertRow(0, new Object []{"Bob Johnson", new Integer (32), Boolean.TRUE}); 180 model.insertRow(1, new Object []{"Bill Simmons", new Integer (27), Boolean.TRUE}); 181 model.insertRow(2, new Object []{"Tracy Smith", new Integer (54), Boolean.TRUE}); 182 model.insertRow(3, new Object []{"Cathy Rogers", new Integer (21), Boolean.FALSE}); 183 model.insertRow(4, new Object []{"Xavier Doe", new Integer (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 |