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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
// Tags: JDK1.2
// Copyright (C) 2005 David Gilbert <david.gilbert@object-refinery.com>
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA. */
package gnu.testlet.javax.swing.JTable;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Arrays;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
/**
* Some checks for the setModel() method in the {@link JTable} class.
*/
public class setModel implements Testlet {
class PropertyChangeHandler implements PropertyChangeListener
{
/**
* Receives notification when a property changes.
*
* @param e the property change event
*/
public void propertyChange(PropertyChangeEvent e)
{
propertyChangeFired = true;
}
}
boolean propertyChangeFired;
/**
* Runs the test using the specified harness.
*
* @param harness the test harness (<code>null</code> not permitted).
*/
public void test(TestHarness harness)
{
test1(harness);
test2(harness);
testLeadAnchorSelectionUpdate(harness);
testSelectionModel(harness);
testPropertyFired(harness);
}
public void test1(TestHarness harness)
{
harness.checkPoint("setModel(TableModel) - test1");
DefaultTableModel m1 = new DefaultTableModel();
JTable t = new JTable(m1);
DefaultTableModel m2 = new DefaultTableModel();
t.setModel(m2);
TableModelListener[] listeners1 = m1.getTableModelListeners();
TableModelListener[] listeners2 = m2.getTableModelListeners();
harness.check(!Arrays.asList(listeners1).contains(t));
harness.check(Arrays.asList(listeners2).contains(t));
// try a null argument
boolean pass = false;
try
{
t.setModel(null);
}
catch (IllegalArgumentException e)
{
pass = true;
}
harness.check(pass);
}
/**
* This test checks that the autoCreateColumnsFromModel flag is correctly
* observed.
*
* @param harness the test harness.
*/
public void test2(TestHarness harness)
{
harness.checkPoint("setModel(TableModel) - test2");
DefaultTableModel m1 = new DefaultTableModel(2, 3);
JTable t = new JTable(m1);
TableColumnModel tcm = t.getColumnModel();
tcm.getColumn(1).setModelIndex(0);
tcm.getColumn(0).setModelIndex(1);
harness.check(t.getColumnCount(), 3);
harness.check(t.getColumnName(0), "B");
harness.check(t.getColumnName(1), "A");
harness.check(t.getColumnName(2), "C");
DefaultTableModel m2 = new DefaultTableModel(new String[] {"AA", "BB"}, 1);
t.setModel(m2);
harness.check(t.getColumnCount(), 2);
harness.check(t.getColumnName(0), "AA");
harness.check(t.getColumnName(1), "BB");
tcm = t.getColumnModel();
tcm.getColumn(1).setModelIndex(0);
tcm.getColumn(0).setModelIndex(1);
t.setAutoCreateColumnsFromModel(false);
DefaultTableModel m3 = new DefaultTableModel(
new String[] {"CC", "DD", "EE"}, 1);
t.setModel(m3);
harness.check(t.getColumnCount(), 2);
harness.check(t.getColumnName(0), "DD");
harness.check(t.getColumnName(1), "CC");
}
/**
* Tests if setModel updates the lead and anchor selection indices correctly.
*
* @param the test harness to use
*/
private void testLeadAnchorSelectionUpdate(TestHarness harness)
{
harness.checkPoint("leadAnchorSelectionUpdate");
JTable table = new JTable(0, 0);
// Test a model with 0 rows and 0 columns.
table.setModel(new DefaultTableModel(0, 0));
try { Thread.sleep(500); } catch (InterruptedException ex) {}
harness.check(table.getSelectionModel().getLeadSelectionIndex(), -1);
harness.check(table.getSelectionModel().getAnchorSelectionIndex(), -1);
harness.check(table.getColumnModel().getSelectionModel()
.getLeadSelectionIndex(), -1);
harness.check(table.getColumnModel().getSelectionModel()
.getAnchorSelectionIndex(), -1);
// Test a model with 1 row and 0 columns.
table.setModel(new DefaultTableModel(1, 0));
try { Thread.sleep(500); } catch (InterruptedException ex) {}
harness.check(table.getSelectionModel().getLeadSelectionIndex(), 0);
harness.check(table.getSelectionModel().getAnchorSelectionIndex(), 0);
harness.check(table.getColumnModel().getSelectionModel()
.getLeadSelectionIndex(), -1);
harness.check(table.getColumnModel().getSelectionModel()
.getAnchorSelectionIndex(), -1);
// Test a model with 0 rows and 1 column.
table.setModel(new DefaultTableModel(0, 1));
try { Thread.sleep(500); } catch (InterruptedException ex) {}
harness.check(table.getSelectionModel().getLeadSelectionIndex(), -1);
harness.check(table.getSelectionModel().getAnchorSelectionIndex(), -1);
harness.check(table.getColumnModel().getSelectionModel()
.getLeadSelectionIndex(), 0);
harness.check(table.getColumnModel().getSelectionModel()
.getAnchorSelectionIndex(), 0);
// Test a model with 1 row and 1 columns.
table.setModel(new DefaultTableModel(1, 1));
try { Thread.sleep(500); } catch (InterruptedException ex) {}
harness.check(table.getSelectionModel().getLeadSelectionIndex(), 0);
harness.check(table.getSelectionModel().getAnchorSelectionIndex(), 0);
harness.check(table.getColumnModel().getSelectionModel()
.getLeadSelectionIndex(), 0);
harness.check(table.getColumnModel().getSelectionModel()
.getAnchorSelectionIndex(), 0);
}
/**
* Tests if the selectionModel is changes when the table's model
* changes. The test shows that the selectionModel is not replaced but the
* selection is cleared.
*
* @oaram harness the test harness to use
*/
private void testSelectionModel(TestHarness harness)
{
harness.checkPoint("selectionModel");
JTable table = new JTable();
ListSelectionModel m1 = table.getSelectionModel();
m1.addSelectionInterval(1, 1);
harness.check(m1.isSelectedIndex(1), true);
table.setModel(new DefaultTableModel());
harness.check(table.getSelectionModel() == m1);
harness.check(m1.isSelectedIndex(1), false);
}
/**
* Tests if changing this property fires a property change event.
*
* @param harness the test harness to use
*/
private void testPropertyFired(TestHarness harness)
{
harness.checkPoint("propertyFired");
JTable table = new JTable();
table.addPropertyChangeListener(new PropertyChangeHandler());
DefaultTableModel m1 = new DefaultTableModel();
DefaultTableModel m2 = new DefaultTableModel();
propertyChangeFired = false;
table.setModel(m1);
harness.check(propertyChangeFired, true);
propertyChangeFired = false;
table.setModel(m1);
harness.check(propertyChangeFired, false);
propertyChangeFired = false;
table.setModel(m2);
harness.check(propertyChangeFired, true);
try
{
table.setModel(null);
harness.fail("IllegalArgumenException must be fired");
}
catch (IllegalArgumentException ex)
{
harness.check(true);
}
}
}
|