Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 366   Methods: 19
NCLOC: 228   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JTableMouseEventDataT.java 100% 100% 100% 100%
coverage
 1   
 package junit.extensions.jfcunit.eventdata;
 2   
 
 3   
 import junit.extensions.jfcunit.JFCTestCase;
 4   
 import junit.extensions.jfcunit.JFCTestHelper;
 5   
 
 6   
 import java.awt.Dimension;
 7   
 import java.awt.Point;
 8   
 import java.awt.event.MouseAdapter;
 9   
 import java.awt.event.MouseEvent;
 10   
 import java.util.Vector;
 11   
 
 12   
 import javax.swing.JScrollPane;
 13   
 import javax.swing.JTable;
 14   
 import javax.swing.table.AbstractTableModel;
 15   
 
 16   
 import junit.extensions.jfcunit.tools.JFCUtilities;
 17   
 
 18   
 /**
 19   
  * Data container class that holds all the data necessary for jfcUnit to fire mouse events.
 20   
  * This class is specific to events on a JTable.
 21   
  *
 22   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 23   
  */
 24   
 public class JTableMouseEventDataT
 25   
     extends MouseEventDataBC {
 26   
   /**
 27   
    * The JTable on which to trigger the event.
 28   
    */
 29   
   private JTableMouseEventData m_event1 = null;
 30   
 
 31   
   /**
 32   
    * Second Event data to be validated.
 33   
    */
 34   
   private JTableMouseEventData m_event2 = null;
 35   
 
 36   
   /**
 37   
    * Component to be tested.
 38   
    */
 39   
   private static final JTable T1 = new JTable();
 40   
 
 41   
   /**
 42   
    * Second component to be tested.
 43   
    */
 44   
   private static final JTable T2 = new JTable();
 45   
 
 46   
   /**
 47   
    * Row to be tested.
 48   
    */
 49   
   private static final int R1 = 1;
 50   
 
 51   
   /**
 52   
    * Second Row to be tested.
 53   
    */
 54   
   private static final int R2 = 2;
 55   
 
 56   
   /**
 57   
    * Column to be tested.
 58   
    */
 59   
   private static final int C1 = 1;
 60   
 
 61   
   /**
 62   
    * Second Column to be tested.
 63   
    */
 64   
   private static final int C2 = 2;
 65   
 
 66   
   /**
 67   
    * Default constructor that accepts a String.
 68   
    *
 69   
    * @param   name      The name of the test method to execute.
 70   
    */
 71  11
   public JTableMouseEventDataT(final String name) {
 72  11
     super(name);
 73   
   }
 74   
 
 75   
   /**
 76   
    * Overridden method that is used to remove the test fixtures.
 77   
    *
 78   
    * @exception  Exception   An instance of java.lang.Exception can be thrown
 79   
    */
 80  11
   protected void tearDown() throws Exception {
 81  11
     super.tearDown();
 82  11
     m_event1 = null;
 83  11
     m_event2 = null;
 84   
   }
 85   
 
 86   
   /**
 87   
    * Validate the parameters of the event match the values
 88   
    * given.
 89   
    *
 90   
    * @param event Event to be tested.
 91   
    * @param tc TestCase
 92   
    * @param tp component
 93   
    * @param row Row
 94   
    * @param col Column
 95   
    * @param clicks mouse clicks.
 96   
    * @param modifiers Modifiers to be used.
 97   
    * @param popup Popup
 98   
    * @param sleepTime sleep duration.
 99   
    * @param position locatio of mouse.
 100   
    * @param ref Reference location.
 101   
    */
 102  16
   protected void validateEvent(
 103   
       final JTableMouseEventData event,
 104   
       final JFCTestCase tc,
 105   
       final JTable tp,
 106   
       final int row,
 107   
       final int col,
 108   
       final int clicks,
 109   
       final int modifiers,
 110   
       final boolean popup,
 111   
       final long sleepTime,
 112   
       final int position,
 113   
       final Point ref) {
 114   
 
 115  16
     super.validateEvent(event, tc, tp, clicks, modifiers, popup, sleepTime,
 116   
                         position, ref);
 117  16
     assertEquals("row:", row, event.getRowIndex());
 118  16
     assertEquals("col:", col, event.getColumnIndex());
 119  16
     assertSameOrNull("Source:", tp, event.getSource());
 120   
   }
 121   
 
 122   
   /**
 123   
    * Test the constructor.
 124   
    */
 125  1
   public void testJTableMouseEventData1() {
 126  1
     m_event1 = new JTableMouseEventData();
 127  1
     assertEquals("Should be invalid", false, m_event1.isValid());
 128  1
     assertEquals("Prepare result:", false, m_event1.prepareComponent());
 129   
   }
 130   
 
 131   
   /**
 132   
    * Test the constructor.
 133   
    */
 134  1
   public void testJTableMouseEventData2() {
 135  1
     m_event1 = new JTableMouseEventData(this, T1, R1, C2, 1);
 136  1
     validateEvent(m_event1, this, T1, R1, C2, 1, DEFAULT_MOUSE_MODIFIERS,
 137   
                   DEFAULT_ISPOPUPTRIGGER, DEFAULT_SLEEPTIME, DEFAULT_POSITION, null);
 138   
 
 139  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2);
 140  1
     validateEvent(m_event2, this, T2, R2, C2, 2, DEFAULT_MOUSE_MODIFIERS,
 141   
                   DEFAULT_ISPOPUPTRIGGER, DEFAULT_SLEEPTIME, DEFAULT_POSITION, null);
 142   
   }
 143   
 
 144   
   /**
 145   
    * Test the constructor.
 146   
    */
 147  1
   public void testJTableMouseEventData2a() {
 148  1
     m_event1 = new JTableMouseEventData(this, T1, R1, C1, 1, false);
 149  1
     validateEvent(m_event1, this, T1, R1, C1, 1, DEFAULT_MOUSE_MODIFIERS, false,
 150   
                   DEFAULT_SLEEPTIME, DEFAULT_POSITION, null);
 151   
 
 152  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2, true);
 153  1
     validateEvent(m_event2, this, T2, R2, C2, 2, MouseEvent.BUTTON3_MASK, true,
 154   
                   DEFAULT_SLEEPTIME, DEFAULT_POSITION, null);
 155   
   }
 156   
 
 157   
   /**
 158   
    * Test the constructor.
 159   
    */
 160  1
   public void testJTableMouseEventData3() {
 161  1
     m_event1 = new JTableMouseEventData(this, T1, R1, C1, 1, 500);
 162  1
     validateEvent(m_event1, this, T1, R1, C1, 1, DEFAULT_MOUSE_MODIFIERS,
 163   
                   DEFAULT_ISPOPUPTRIGGER, 500, DEFAULT_POSITION, null);
 164   
 
 165  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2, 1500);
 166  1
     validateEvent(m_event2, this, T2, R2, C2, 2, DEFAULT_MOUSE_MODIFIERS,
 167   
                   DEFAULT_ISPOPUPTRIGGER, 1500, DEFAULT_POSITION, null);
 168   
   }
 169   
 
 170   
   /**
 171   
    * Test the constructor.
 172   
    */
 173  1
   public void testJTableMouseEventData3a() {
 174  1
     m_event1 = new JTableMouseEventData(this, T1, R1, C1, 1, false, 500);
 175  1
     validateEvent(m_event1, this, T1, R1, C1, 1, DEFAULT_MOUSE_MODIFIERS, false,
 176   
                   500, DEFAULT_POSITION, null);
 177   
 
 178  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2, true, 1500);
 179  1
     validateEvent(m_event2, this, T2, R2, C2, 2, MouseEvent.BUTTON3_MASK, true,
 180   
                   1500, DEFAULT_POSITION, null);
 181   
   }
 182   
 
 183   
   /**
 184   
    * Test the constructor.
 185   
    */
 186  1
   public void testJTableMouseEventData4() {
 187  1
     m_event1 = new JTableMouseEventData(this, T1, 1, C1, 1, MODIFIERS1, true,
 188   
                                         500);
 189  1
     validateEvent(m_event1, this, T1, R1, C1, 1, MODIFIERS1, true, 500,
 190   
                   DEFAULT_POSITION, null);
 191   
 
 192  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2, MODIFIERS2, false,
 193   
                                         1500);
 194  1
     validateEvent(m_event2, this, T2, R2, C2, 2, MODIFIERS2, false, 1500,
 195   
                   DEFAULT_POSITION, null);
 196   
   }
 197   
 
 198   
   /**
 199   
    * Test the constructor.
 200   
    */
 201  1
   public void testJTableMouseEventData5() {
 202  1
     m_event1 = new JTableMouseEventData(this, T1, 1, C1, 1, MODIFIERS1, true,
 203   
                                         500, P1);
 204  1
     validateEvent(m_event1, this, T1, R1, C1, 1, MODIFIERS1, true, 500, CUSTOM,
 205   
                   P1);
 206   
 
 207  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2, MODIFIERS2, false,
 208   
                                         1500, P2);
 209  1
     validateEvent(m_event2, this, T2, R2, C2, 2, MODIFIERS2, false, 1500,
 210   
                   CUSTOM, P2);
 211   
   }
 212   
 
 213   
   /**
 214   
    * Test the constructor.
 215   
    */
 216  1
   public void testJTableMouseEventData6() {
 217  1
     m_event1 = new JTableMouseEventData(this, T1, R1, C1, 1, MODIFIERS1, true,
 218   
                                         500, WEST);
 219  1
     validateEvent(m_event1, this, T1, R1, C1, 1, MODIFIERS1, true, 500, WEST, null);
 220   
 
 221  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2, MODIFIERS2, false,
 222   
                                         1500, NORTH_WEST);
 223  1
     validateEvent(m_event2, this, T2, R2, C2, 2, MODIFIERS2, false, 1500,
 224   
                   NORTH_WEST, null);
 225   
   }
 226   
 
 227   
   /**
 228   
    * Test the constructor.
 229   
    */
 230  1
   public void testJTableMouseEventData7() {
 231  1
     m_event1 = new JTableMouseEventData(this, T1, R1, C1, 1, MODIFIERS1, true,
 232   
                                         500, CUSTOM, P1);
 233  1
     validateEvent(m_event1, this, T1, R1, C1, 1, MODIFIERS1, true, 500, CUSTOM,
 234   
                   P1);
 235   
 
 236  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2, MODIFIERS2, false,
 237   
                                         1500, PERCENT, P2);
 238  1
     validateEvent(m_event2, this, T2, R2, C2, 2, MODIFIERS2, false, 1500,
 239   
                   PERCENT, P2);
 240   
   }
 241   
 
 242   
   /**
 243   
    * Test the equals.
 244   
    */
 245  1
   public void testEquals() {
 246  1
     m_event1 = new JTableMouseEventData(this, T1, R1, C1, 1, MODIFIERS1, true,
 247   
                                         500, CUSTOM, P1);
 248  1
     m_event2 = new JTableMouseEventData(this, T1, R1, C1, 1, MODIFIERS1, true,
 249   
                                         500, CUSTOM, P1);
 250  1
     assertTrue("Not equals:", m_event1.equals(m_event2));
 251  1
     assertTrue("Null equals:", !m_event1.equals(null));
 252  1
     assertTrue("Invalid class:", !m_event1.equals(""));
 253  1
     m_event2 = new JTableMouseEventData(this, T2, R1, C1, 1, MODIFIERS1, true,
 254   
                                         500, CUSTOM, P1);
 255  1
     assertTrue("Equals but Component different:", !m_event1.equals(m_event2));
 256   
 
 257  1
     m_event2 = new JTableMouseEventData(this, T1, R2, C1, 1, MODIFIERS1, true,
 258   
                                         500, CUSTOM, P1);
 259  1
     assertTrue("Equals but Rows different:", !m_event1.equals(m_event2));
 260  1
     m_event2 = new JTableMouseEventData(this, T1, R1, C2, 1, MODIFIERS1, true,
 261   
                                         500, CUSTOM, P1);
 262  1
     assertTrue("Equals but Columns different:", !m_event1.equals(m_event2));
 263  1
     m_event2 = new JTableMouseEventData(this, T1, R1, C1, 2, MODIFIERS1, true,
 264   
                                         500, CUSTOM, P1);
 265  1
     assertTrue("Equals but Clicks different:", !m_event1.equals(m_event2));
 266  1
     m_event2 = new JTableMouseEventData(this, T1, R1, C1, 1, MODIFIERS2, true,
 267   
                                         500, CUSTOM, P1);
 268  1
     assertTrue("Equals but Modifiers different:", !m_event1.equals(m_event2));
 269  1
     m_event2 = new JTableMouseEventData(this, T1, R1, C1, 1, MODIFIERS1, false,
 270   
                                         500, CUSTOM, P1);
 271  1
     assertTrue("Equals but Popup different:", !m_event1.equals(m_event2));
 272  1
     m_event2 = new JTableMouseEventData(this, T1, R1, C1, 1, MODIFIERS1, true,
 273   
                                         500, PERCENT, P1);
 274  1
     assertTrue("Equals but Position different:", !m_event1.equals(m_event2));
 275  1
     m_event2 = new JTableMouseEventData(this, T1, R1, C1, 1, MODIFIERS1, true,
 276   
                                         500, CUSTOM, P2);
 277  1
     assertTrue("Equals but Reference different:", !m_event1.equals(m_event2));
 278  1
     m_event2 = new JTableMouseEventData(this, T2, R2, C2, 2, MODIFIERS2, false,
 279   
                                         1500, PERCENT, P2);
 280  1
     assertTrue("Equals but All different:", !m_event1.equals(m_event2));
 281   
   }
 282   
 
 283   
   /**
 284   
    * This method is used to test that when the JTableHeader item's width is much greater
 285   
    * than the view port, the click point is recalculated correctly.
 286   
    */
 287  1
   public void testPrepareComponentWithCenterPointOutsideVisibleArea() {
 288  1
     final String longString =
 289   
         "Trying to get a really long string as part of the cell values.";
 290  1
     setHelper(new JFCTestHelper());
 291  1
     final Vector msgs = new Vector();
 292  1
     final Object[][] rowData = {{"1,1" + longString, "1,2" + longString, "1,3" + longString},
 293   
         {"2,1" + longString, "2,2" + longString, "2,3" + longString},
 294   
         {"3,1" + longString, "3,2" + longString, "3,3" + longString},
 295   
         {"4,1" + longString, "4,2" + longString, "4,3" + longString},
 296   
         {"5,1" + longString, "5,2" + longString, "5,3" + longString}};
 297  1
     final Object[] columnNames = {"Col1",
 298   
         "Col2",
 299   
         "Col3"
 300   
     };
 301   
 
 302  1
     final JTable table = new JTable(rowData, columnNames);
 303  1
     table.setModel(new AbstractTableModel() {
 304  173
       public int getRowCount() {
 305  173
         return rowData.length;
 306   
       }
 307   
 
 308  4
       public int getColumnCount() {
 309  4
         return columnNames.length;
 310   
       }
 311   
 
 312  29
       public Object getValueAt(final int row, final int col) {
 313  29
         return rowData[row][col];
 314   
       }
 315   
 
 316  19
       public boolean isCellEditable(final int row, final int column) {
 317  19
         return false;
 318   
       }
 319   
     });
 320  1
     table.setRowSelectionAllowed(false);
 321  1
     table.setColumnSelectionAllowed(false);
 322  1
     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 323  1
     table.setPreferredSize(new Dimension(500, 100));
 324  1
     table.addMouseListener(new MouseAdapter() {
 325  9
       public void mouseClicked(final MouseEvent e) {
 326  9
         msgs.add("Selected: (" + table.getSelectedRow() + ", "
 327   
                  + table.getSelectedColumn() + "): Clicks: " + e.getClickCount());
 328   
       }
 329   
     });
 330   
 
 331  1
     setWindow(createJFrame(getName()));
 332   
     // for this test, the frame should not contain the full width of the JTable items
 333  1
     getFrame().setSize(35, 100);
 334   
 
 335  1
     JScrollPane scrollPane = new JScrollPane(JScrollPane.
 336   
                                              VERTICAL_SCROLLBAR_AS_NEEDED,
 337   
                                              JScrollPane.
 338   
                                              HORIZONTAL_SCROLLBAR_AS_NEEDED);
 339  1
     scrollPane.setPreferredSize(new Dimension(30, 100));
 340  1
     scrollPane.setViewportView(table);
 341   
 
 342  1
     getFrame().getContentPane().add(scrollPane);
 343   
     // do not use the packAndShow() method - since we want the viewport to be smaller than the combo width...
 344  1
     JFCUtilities.center(getFrame());
 345  1
     getFrame().setVisible(true);
 346   
 
 347  1
     for (int tries = 0; tries < 3; tries++) {
 348  3
       for (int clicks = 1; clicks < 3; clicks++) {
 349  6
         int row = (int) (Math.random() * 3);
 350  6
         int column = (int) (Math.random() * 3);
 351  6
         msgs.clear();
 352  6
         getHelper().enterClickAndLeave(new JTableMouseEventData(this, table, row,
 353   
             column, clicks));
 354   
 
 355  6
         sleep(getSleepTimer());
 356  6
         assertEquals("Number of messages showing is wrong", clicks, msgs.size());
 357  6
         for (int click = 1; click <= clicks; click++) {
 358  9
           assertEquals("Wrong message is showing up",
 359   
                        "Selected: (" + row + ", " + column + "): Clicks: "
 360   
                        + click, msgs.get(click - 1));
 361   
         }
 362   
       }
 363   
     }
 364   
   }
 365   
 }
 366