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