Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 443   Methods: 6
NCLOC: 245   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IndexOfTagHandler.java 23.3% 40.4% 100% 35.6%
coverage coverage
 1   
 /*
 2   
  * Created on Oct 27, 2003
 3   
  *
 4   
  * To change the template for this generated file go to
 5   
  * Window>Preferences>Java>Code Generation>Code and Comments
 6   
  */
 7   
 package junit.extensions.xml.elements;
 8   
 
 9   
 import junit.extensions.jfcunit.tools.Operator;
 10   
 
 11   
 import junit.extensions.xml.IXMLTestCase;
 12   
 import junit.extensions.xml.XMLException;
 13   
 
 14   
 import junit.framework.Assert;
 15   
 
 16   
 import org.w3c.dom.Element;
 17   
 
 18   
 import javax.swing.JComboBox;
 19   
 import javax.swing.JList;
 20   
 import javax.swing.JTabbedPane;
 21   
 import javax.swing.JTable;
 22   
 import javax.swing.ListModel;
 23   
 import javax.swing.table.TableColumn;
 24   
 import javax.swing.table.TableColumnModel;
 25   
 
 26   
 
 27   
 /**
 28   
  * Provide a mechanism for finding row/column in a table for a given value.
 29   
  * It will return throw an assertion if the value is not found.
 30   
  *
 31   
  * <H3>Tag Name:</H3>
 32   
  * indexof
 33   
  * <H3>Attributes</H3>
 34   
  * <pre>
 35   
  * refid   - the object to inspect                [required]
 36   
  * id      - the id of the results                [required]
 37   
  * value   - the expected value                   [required]
 38   
  * type    - the field type of the object         [required]
 39   
  *
 40   
  *   tablecell - locate a table cell in the
 41   
  *               specified row or column. row and column
 42   
  *               attributes may be used to narrow the search.
 43   
  *
 44   
  *   listcell  - locate a item in the list.
 45   
  *
 46   
  *   tab       - locate a tab.
 47   
  *
 48   
  *   tablecolumn - locate a table column.
 49   
  * </pre>
 50   
  * <H3>Optional</H3>
 51   
  * <pre>
 52   
  * index     - the x'th instance of the value to find [optional]
 53   
  * operation - The type of matching operation to perform.
 54   
  *             match - Use a regular expression match.
 55   
  *             equals - (default) if not specified.
 56   
  *             contains - The item contains the value.
 57   
  *             startswith - The item starts with the value.
 58   
  *             endswith   - The item ends with the value.
 59   
  *
 60   
  * useRE     - (depricated use operation="match")
 61   
  *             allow the value to contain
 62   
  *             a regular expression [optional]
 63   
  *           default - is false exact string match needed
 64   
  *           If set to true partial strings will work.
 65   
  * </pre>
 66   
  * <H3>Output</H3>
 67   
  * <pre>
 68   
  * Successful execution will set the following:<p>
 69   
  *   tablecell<p>
 70   
  *     id     to the value of the cell.
 71   
  *     id.row to the row of the tablecell.<p>
 72   
  *     id.column to the column of the tablecell.<p>
 73   
  *   tablecolumn<p>
 74   
  *     id        to the value of the header.
 75   
  *     id.index  to the column of the table header.
 76   
  *   listitem<p>
 77   
  *     id       to the value of the item.
 78   
  *     id.index to the index of the list item.<p>
 79   
  *   tab<p>
 80   
  *     id       to the value of the title.
 81   
  *     id.index to the index of the tab.<p>
 82   
  * Unsuccessfull id will be set to null and -1 will be set as
 83   
  * the index, row and column.<p>
 84   
  * </pre>
 85   
  * <H3>Examples:</H3>
 86   
  *   Return the cell where the value Joe is found in a specific column <p>
 87   
  * <pre>
 88   
  *     &lt;indexof id="cell" refid="JTable1" type="tablecell" column="1" value="Joe"/&gt;
 89   
  * </pre>
 90   
  *   Return the cell where the value Joe is found in a specific row
 91   
  * <pre>
 92   
  *     &lt;indexof id="cell" refid="JTable1" type="tablecell" row="1" value="Joe"/&gt;
 93   
  * </pre>
 94   
  *   Return the 2nd cell where the value Joe is found in the table
 95   
  * <pre>
 96   
  *     &lt;indexof id="cell" refid="JTable1" type="tablecell" value="Joe" index="2"/&gt;
 97   
  * </pre>
 98   
  *   Return the index of the column header containing SearchCode
 99   
  * <pre>
 100   
  *     &lt;indexof id="table" refid="JTable1" type="tablecolumn" value="SearchCode" index="0"/&gt;
 101   
  * </pre>
 102   
  *   Return the index of the tab containing the General.
 103   
  * <pre>
 104   
  *     &lt;indexof id="tab" refid="TabPane1" type="tab" value="General" index="0"/&gt;
 105   
  * </pre>
 106   
  *   Return the index of the tab containing the Peanut.
 107   
  * <pre>
 108   
  *     &lt;indexof id="item" refid="JList1" type="listitem" value="Peanut" index="0"/&gt;
 109   
  * </pre>
 110   
  *   Return the index of the list containing the Watermellon.
 111   
  * <pre>
 112   
  *     &lt;indexof id="item" refid="JComboBox1" type="listitem"
 113   
  *           value="Watermellon" index="0"/&gt;
 114   
  * </pre>
 115   
  * @author Kevin Wilson
 116   
  * @author John Yunker
 117   
  */
 118   
 public class IndexOfTagHandler extends AbstractTagHandler {
 119   
     /**
 120   
      * Constructor.
 121   
      * @param element Element to be processed.
 122   
      * @param testcase Containing test case.
 123   
      */
 124  2
     public IndexOfTagHandler(final Element element, final IXMLTestCase testcase) {
 125  2
         super(element, testcase);
 126   
     }
 127   
 
 128   
     /**
 129   
      * Locate the index or cell matching the value.
 130   
      * @throws XMLException exception maybe thrown.
 131   
      */
 132  1
     public void processElement() throws XMLException {
 133  1
         validateElement();
 134   
 
 135  1
         boolean debug     = getXMLTestCase().getDebug();
 136  1
         int     op        = getOperation();
 137  1
         String  id        = getString(ID);
 138  1
         int     findcount = getInt(INDEX, 1);
 139   
 
 140  1
         if (findcount <= 0) {
 141  0
             findcount = 1;
 142   
         }
 143   
 
 144  1
         String  refid = getString(REFID);
 145  1
         Object  o = ((IXMLTestCase) getTestCase()).getProperty(refid);
 146   
 
 147  1
         String  expectedval = getString(VALUE);
 148  1
         boolean useRegExp   = getBoolean(USERE);
 149   
 
 150  1
         if (useRegExp) {
 151  0
             op = Operator.Operation.MATCH;
 152   
 
 153  0
             if (debug) {
 154  0
                 System.err.println("Index of regular expression:" + expectedval
 155   
                     + " in " + o);
 156   
             }
 157  1
         } else if (debug) {
 158  0
             System.err.println("Index of:" + expectedval + " in " + o);
 159   
         }
 160   
 
 161  1
         Assert.assertNotNull("Error: Unable to locate object id:" + refid, o);
 162   
 
 163  1
         String type = getString(TYPE);
 164  1
         Assert.assertNotNull("indexof 'type' is null", type);
 165   
 
 166  1
         if (type.equals("tablecolumn")) {
 167  0
             if (o instanceof JTable) {
 168  0
                 JTable           table = (JTable) o;
 169  0
                 TableColumnModel model = table.getColumnModel();
 170   
 
 171  0
                 int              columns = model.getColumnCount();
 172   
 
 173  0
                 for (int i = 0; i < columns; i++) {
 174  0
                     TableColumn tc    = model.getColumn(i);
 175  0
                     Object      value = tc.getHeaderValue();
 176   
 
 177  0
                     if (value == null) {
 178  0
                         value = tc.getIdentifier();
 179   
                     }
 180   
 
 181  0
                     String s = null;
 182   
 
 183  0
                     if (value != null) {
 184  0
                         s = value.toString();
 185   
                     }
 186   
 
 187  0
                     if (evaluate(op, s, expectedval)) {
 188  0
                         if (debug) {
 189  0
                             System.err.println("Match." + (findcount - 1)
 190   
                                 + " more items to find");
 191   
                         }
 192   
 
 193  0
                         if (--findcount == 0) {
 194  0
                             getXMLTestCase().addProperty(id, value);
 195  0
                             getXMLTestCase().addProperty(
 196   
                                 id + ".index",
 197   
                                 new Integer(i));
 198   
 
 199  0
                             return;
 200   
                         }
 201   
                     }
 202   
                 }
 203   
 
 204   
                 // Not found
 205  0
                 getXMLTestCase().addProperty(id, null);
 206  0
                 getXMLTestCase().addProperty(
 207   
                     id + ".index",
 208   
                     new Integer(-1));
 209   
 
 210  0
                 if (debug) {
 211  0
                     System.err.println("No match found");
 212   
                 }
 213   
             } else {
 214  0
                 Assert.fail("refid must point to a JTable");
 215   
             }
 216  1
         } else if (type.equals("tab")) {
 217  0
             if (o instanceof JTabbedPane) {
 218  0
                 JTabbedPane tab = (JTabbedPane) o;
 219   
 
 220  0
                 int         count = tab.getTabCount();
 221   
 
 222  0
                 for (int i = 0; i < count; i++) {
 223  0
                     String title = tab.getTitleAt(i);
 224   
 
 225  0
                     if (evaluate(op, title, expectedval)) {
 226  0
                         if (debug) {
 227  0
                             System.err.println("Match." + (findcount - 1)
 228   
                                 + " more items to find");
 229   
                         }
 230   
 
 231  0
                         if (--findcount == 0) {
 232  0
                             getXMLTestCase().addProperty(id, title);
 233  0
                             getXMLTestCase().addProperty(
 234   
                                 id + ".index",
 235   
                                 new Integer(i));
 236   
 
 237  0
                             return;
 238   
                         }
 239   
                     }
 240   
                 }
 241   
 
 242   
                 // Not found
 243  0
                 getXMLTestCase().addProperty(id, null);
 244  0
                 getXMLTestCase().addProperty(
 245   
                     id + ".index",
 246   
                     new Integer(-1));
 247   
 
 248  0
                 if (debug) {
 249  0
                     System.err.println("No match found");
 250   
                 }
 251   
             } else {
 252  0
                 Assert.fail("refid must point to JTabbedPane");
 253   
             }
 254  1
         } else if (type.equals(TABLECELL)) {
 255  1
             if (o instanceof JTable) {
 256  1
                 JTable table = (JTable) o;
 257   
 
 258  1
                 int    startrow = getInt(ROW, -1);
 259  1
                 int    startcol = getInt(COLUMN, -1);
 260   
 
 261  1
                 int    endrow;
 262  1
                 int    endcol;
 263   
 
 264  1
                 if (startrow == -1) {
 265  1
                     startrow     = 0;
 266  1
                     endrow       = table.getRowCount();
 267   
                 } else {
 268  0
                     endrow = startrow + 1;
 269   
                 }
 270   
 
 271  1
                 if (startcol == -1) {
 272  1
                     startcol     = 0;
 273  1
                     endcol       = table.getColumnCount();
 274   
                 } else {
 275  0
                     endcol = startcol + 1;
 276   
                 }
 277   
 
 278  1
                 int rowindex = 0;
 279  1
                 int colindex = 0;
 280   
 
 281   
                 //System.out.println("rc:"+ovt.getRowCount()+" cc:"+ovt.getColumnCount());
 282  2
                 for (rowindex = startrow; rowindex < endrow; rowindex++) {
 283  2
                     for (colindex = startcol; colindex < endcol; colindex++) {
 284  7
                         Object cell     = table.getValueAt(rowindex, colindex);
 285  7
                         String tableval = null;
 286   
 
 287  7
                         if (cell != null) {
 288  6
                             tableval = cell.toString();
 289   
                         }
 290   
 
 291  7
                         if (debug) {
 292  0
                             System.err.println("Evaluate row=" + rowindex
 293   
                                 + " column=" + colindex + " value=" + tableval);
 294   
                         }
 295   
 
 296  7
                         if (evaluate(op, tableval, expectedval)) {
 297  1
                             if (debug) {
 298  0
                                 System.err.println("Match " + (findcount - 1)
 299   
                                     + "more items to find");
 300   
                             }
 301   
 
 302  1
                             if (--findcount == 0) {
 303  1
                                 getXMLTestCase().addProperty(id, cell);
 304  1
                                 getXMLTestCase().addProperty(
 305   
                                     id + ".row",
 306   
                                     new Integer(rowindex));
 307  1
                                 getXMLTestCase().addProperty(
 308   
                                     id + ".column",
 309   
                                     new Integer(colindex));
 310   
 
 311  1
                                 return;
 312   
                             }
 313   
                         }
 314   
                     }
 315   
                 }
 316   
 
 317  0
                 if (debug) {
 318  0
                     System.err.println("No match found");
 319   
                 }
 320   
 
 321  0
                 getXMLTestCase().addProperty(id, null);
 322  0
                 getXMLTestCase().addProperty(
 323   
                     id + ".column",
 324   
                     new Integer(-1));
 325  0
                 getXMLTestCase().addProperty(
 326   
                     id + ".row",
 327   
                     new Integer(-1));
 328   
             } else {
 329  0
                 Assert.fail("Invalid Table type:" + o.toString());
 330   
             }
 331  0
         } else if (type.equals(LISTITEM)) {
 332  0
             ListModel model = null;
 333   
 
 334  0
             if (o instanceof JList) {
 335  0
                 model = ((JList) o).getModel();
 336  0
             } else if (o instanceof JComboBox) {
 337  0
                 model = ((JComboBox) o).getModel();
 338   
             }
 339   
 
 340  0
             boolean found = false;
 341   
 
 342  0
             int     rowindex = 0;
 343   
 
 344   
             //System.out.println("rc:"+ovt.getRowCount()+" cc:"+ovt.getColumnCount());
 345  0
             for (rowindex = 0; (rowindex < model.getSize()) && !found;
 346   
                     rowindex++) {
 347  0
                 Object item     = model.getElementAt(rowindex);
 348  0
                 String listitem = null;
 349   
 
 350  0
                 if (item != null) {
 351  0
                     listitem = item.toString();
 352   
                 }
 353   
 
 354  0
                 if (debug) {
 355  0
                     System.err.println("Evaluate index=" + rowindex + " value="
 356   
                         + listitem);
 357   
                 }
 358   
 
 359  0
                 if (evaluate(op, listitem, expectedval)) {
 360  0
                     if (debug) {
 361  0
                         System.err.println("Match." + (findcount - 1)
 362   
                             + " more items to find");
 363   
                     }
 364   
 
 365  0
                     if (--findcount == 0) {
 366  0
                         getXMLTestCase().addProperty(id, item);
 367  0
                         getXMLTestCase().addProperty(
 368   
                             id + ".index",
 369   
                             new Integer(rowindex));
 370   
 
 371  0
                         return;
 372   
                     }
 373   
                 }
 374   
             }
 375   
 
 376  0
             if (debug) {
 377  0
                 System.err.println("List item match not found.");
 378   
             }
 379   
 
 380  0
             getXMLTestCase().addProperty(id, null);
 381  0
             getXMLTestCase().addProperty(
 382   
                 id + ".index",
 383   
                 new Integer(-1));
 384   
         } else {
 385  0
             Assert.fail("Unknown object type: " + o);
 386   
         }
 387   
     }
 388   
 
 389   
     /**
 390   
      * Insure that the id, refid, type and value attributes
 391   
      * are specified.
 392   
      * @throws XMLException if one or more of the above attributes are missing.
 393   
      */
 394  1
     public void validateElement() throws XMLException {
 395  1
         super.validateElement();
 396   
 
 397   
         // check the element tag name
 398   
         // id is a required attribute
 399  1
         checkRequiredAttribute(ID);
 400  1
         checkRequiredAttribute(REFID);
 401  1
         checkRequiredAttribute(TYPE);
 402  1
         checkRequiredAttribute(VALUE);
 403   
     }
 404   
 
 405   
     /**
 406   
      * Get the case independent attribute.
 407   
      * @return boolean true if case should be ignored.
 408   
      */
 409  7
     protected boolean getCaseIndependent() {
 410  7
         return getBoolean("caseindependent", true);
 411   
     }
 412   
 
 413   
     /**
 414   
      * Get the operation from the string value.
 415   
      * @return int representing the operation.
 416   
      */
 417  1
     protected int getOperation() {
 418  1
         String operation = getString("operation");
 419   
 
 420  1
         if (operation == null) {
 421  1
             return Operator.Operation.EQUALS;
 422   
         }
 423   
 
 424  0
         return Operator.Operation.getOperation(operation);
 425   
     }
 426   
 
 427   
     /**
 428   
      * Evaluate the operation.
 429   
      * @param operation Operation to be evaluated.
 430   
      * @param tableval Actual value.
 431   
      * @param expectedval expected value.
 432   
      * @return true if a match is found.
 433   
      */
 434  7
     private boolean evaluate(final int operation, final String tableval,
 435   
         final String expectedval) {
 436  7
         return Operator.evaluate(
 437   
             tableval,
 438   
             expectedval,
 439   
             operation,
 440   
             getCaseIndependent());
 441   
     }
 442   
 }
 443