This is the mail archive of the mauve-patches@sourceware.org mailing list for the Mauve project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FYI: New JTable tests


Hi,

I implemented some new tests for JTable.

2005-10-28  Roman Kennke  <kennke@aicas.com>

        * gnu/testlet/javax/swing/JTable/setModel.java
        (testPropertyFired): New test method.
        (testSelectionModel): New test method.
        (testLeadAnchorSelectionUpdate): New test method.

/Roman
Index: gnu/testlet/javax/swing/JTable/setModel.java
===================================================================
RCS file: /cvs/mauve/mauve/gnu/testlet/javax/swing/JTable/setModel.java,v
retrieving revision 1.2
diff -u -r1.2 setModel.java
--- gnu/testlet/javax/swing/JTable/setModel.java	6 Jul 2005 09:30:34 -0000	1.2
+++ gnu/testlet/javax/swing/JTable/setModel.java	28 Oct 2005 13:45:04 -0000
@@ -22,9 +22,12 @@
 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;
@@ -34,6 +37,23 @@
  */
 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.
    * 
@@ -43,6 +63,9 @@
   {
     test1(harness);
     test2(harness);
+    testLeadAnchorSelectionUpdate(harness);
+    testSelectionModel(harness);
+    testPropertyFired(harness);
   }
 
   public void test1(TestHarness harness)      
@@ -106,4 +129,108 @@
     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);
+      }
+    
+  }
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]