Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 280   Methods: 3
NCLOC: 160   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ForeachTagHandler.java 0% 0% 0% 0%
coverage
 1   
 package junit.extensions.xml.elements;
 2   
 
 3   
 import junit.extensions.xml.IXMLTestCase;
 4   
 import junit.extensions.xml.XMLException;
 5   
 
 6   
 import org.w3c.dom.Element;
 7   
 
 8   
 import javax.swing.JComboBox;
 9   
 import javax.swing.JList;
 10   
 import javax.swing.JTabbedPane;
 11   
 import javax.swing.JTable;
 12   
 import javax.swing.ListModel;
 13   
 import javax.swing.table.TableColumn;
 14   
 
 15   
 
 16   
 /**
 17   
  * <p>Title: class ForeachTagHandler</p>
 18   
  * <p>Description: This tag handler implements a looping
 19   
  * construct to visit each cell or index in a table or list.
 20   
  * <H3>Summary</H3>
 21   
  * &lt;for-each id="propname" refid="componentname"
 22   
  *    type="tablecell|listitem"
 23   
  *    [row="rownumber"] [column="columnumber"]/&gt;
 24   
  *
 25   
  * <H3>Required</H3>
 26   
  * id - property name for the output values <p>
 27   
  * refid - component to be iterated over.<p>
 28   
  * type  - tablecell or listitem depending on the component type. If tablecell
 29   
  * then the refid object is assumed to be a JTable instance. If listitem then
 30   
  * the refid object is assumed to be a JList object or JComboBox.
 31   
  * <H3>Optional</H3>
 32   
  * row - row will only be used when type = tablecell.
 33   
  *       when row is set the row specified will be scanned.
 34   
  * column - column will only be used when type = tabecell.
 35   
  *       When column is set only the column specified will be
 36   
  *       iterated.
 37   
  * <H3>Children</H3>
 38   
  * The child nodes of this element will be processed.<p>
 39   
  * <H3>Outputs</H3>
 40   
  * The following will be the outputs for type = listitem. The
 41   
  * <I>id</I> will be replaced by the value of the id attribute:<p>
 42   
  * <I>id</I> is the value of model item being iterated.<p>
 43   
  * <I>id</I>.index where id will be replaced by the value of the id attribute. The
 44   
  * value of the index wil be the index of the item being iterated.<p><p>
 45   
  *
 46   
  * The following will be the outputs for type = tablecell. The
 47   
  * <I>id</I> will be replaced by the value of the id attribute:<p>
 48   
  * <I>id</I> is the value of model item being iterated.<p>
 49   
  * <I>id</I>.row row number of the current cell<p>
 50   
  * <I>id</I>.column column number of the current cell<p>
 51   
  *
 52   
  * <H3>Examples</H3>
 53   
  * Iterate over all cells of a table.<p>
 54   
  * <pre>
 55   
  * &lt;for-each type="tablecell" refid="Table" id="cell" /&gt;
 56   
  *     &lt;echo message="tablecell(${cell.row}, ${cell.column}) = ${cell}" mode="stdout"/&gt;
 57   
  * &lt;/for-each&gt;
 58   
  * </pre>
 59   
  * Iterate over a column of a table.<p>
 60   
  * <pre>
 61   
  *
 62   
  * &lt;for-each type="tablecell" refid="Table" id="cell" column="1"/&gt;
 63   
  *     &lt;echo message="tablecell(${cell.row}, ${cell.column}) = ${cell}" mode="stdout"/&gt;
 64   
  * &lt;/for-each&gt;
 65   
  * </pre>
 66   
  * Iterate over a row of a table.<p>
 67   
  * <pre>
 68   
  * &lt;for-each type="tablecell" refid="Table" id="cell" row="1"/&gt;
 69   
  *     &lt;echo message="tablecell(${cell.row}, ${cell.column}) = ${cell}" mode="stdout"/&gt;
 70   
  * &lt;/for-each&gt;
 71   
  * </pre>
 72   
  * Iterate over a list.<p>
 73   
  * <pre>
 74   
  * &lt;for-each type="listitem" refid="List" id="item"/&gt;
 75   
  *     &lt;echo message="listitem(${item.index}) = ${item}"
 76   
  *     mode="stdout"/&gt;
 77   
  * &lt;/for-each&gt;
 78   
  * </pre>
 79   
  * </p>
 80   
  * <p>Copyright: Copyright (c) 2003</p>
 81   
  * <p>Company: </p>
 82   
  * @author not attributable
 83   
  * @version 1.0
 84   
  */
 85   
 public class ForeachTagHandler extends AbstractTagHandler {
 86   
     /**
 87   
      * Constructor.
 88   
      * @param element Element to be processed.
 89   
      * @param testCase Test case processing tag handler.
 90   
      */
 91  0
     public ForeachTagHandler(final Element element, final IXMLTestCase testCase) {
 92  0
         super(element, testCase);
 93   
     }
 94   
 
 95   
     /**
 96   
      * process the element.
 97   
      * @throws XMLException is thrown if the element cannot be understood.
 98   
      */
 99  0
     public void processElement() throws XMLException {
 100  0
         String type = getString(TYPE);
 101   
 
 102  0
         String id    = getString(ID);
 103  0
         String refid = getString(REFID);
 104   
 
 105  0
         Object obj = getXMLTestCase().getProperty(refid);
 106  0
         getTestCase().assertNotNull("for-each refid is null", obj);
 107   
 
 108  0
         boolean debug = getXMLTestCase().getDebug();
 109   
 
 110  0
         if (LISTITEM.equals(type)) {
 111  0
             ListModel model = null;
 112   
 
 113  0
             if (obj instanceof JList) {
 114  0
                 JList list = (JList) obj;
 115  0
                 model = list.getModel();
 116  0
             } else if (obj instanceof JComboBox) {
 117  0
                 model = ((JComboBox) obj).getModel();
 118   
             }
 119   
 
 120  0
             getTestCase().assertNotNull("could not get model from:"
 121   
                 + obj.getClass().getName(), model);
 122   
 
 123  0
             int size = model.getSize();
 124   
 
 125  0
             for (int i = 0; i < size; i++) {
 126  0
                 Object value = model.getElementAt(i);
 127   
 
 128  0
                 if (debug) {
 129  0
                     System.err.println("for-each: listitem for index(" + i
 130   
                         + ") " + value);
 131   
                 }
 132   
 
 133  0
                 getXMLTestCase().addProperty(id, value);
 134  0
                 getXMLTestCase().addProperty(
 135   
                     id + "." + INDEX,
 136   
                     new Integer(i));
 137  0
                 getXMLTestCase().processChildren(getElement());
 138   
             }
 139  0
         } else if ("tab".equals(type)) {
 140  0
             getTestCase().assertTrue(refid + " not instance of JTabbedPane",
 141   
                 (obj instanceof JTabbedPane));
 142   
 
 143  0
             JTabbedPane tab      = (JTabbedPane) obj;
 144  0
             int         columns  = tab.getTabCount();
 145  0
             String      columnid = id + "." + INDEX;
 146   
 
 147  0
             for (int c = 0; c < columns; c++) {
 148  0
                 String title = tab.getTitleAt(c);
 149   
 
 150  0
                 getXMLTestCase().addProperty(id, title);
 151  0
                 getXMLTestCase().addProperty(
 152   
                     columnid,
 153   
                     new Integer(c));
 154  0
                 getXMLTestCase().processChildren(getElement());
 155   
             }
 156  0
         } else if ("tablecolumn".equals(type)) {
 157  0
             getTestCase().assertTrue(refid + " not instance of JTable",
 158   
                 (obj instanceof JTable));
 159   
 
 160  0
             JTable table    = (JTable) obj;
 161  0
             int    columns  = table.getColumnCount();
 162  0
             String columnid = id + "." + COLUMN;
 163   
 
 164  0
             for (int c = 0; c < columns; c++) {
 165  0
                 TableColumn tc = table.getColumnModel().getColumn(c);
 166  0
                 Object      o = tc.getHeaderValue();
 167   
 
 168  0
                 if (o == null) {
 169  0
                     o = tc.getIdentifier();
 170   
                 }
 171   
 
 172  0
                 getXMLTestCase().addProperty(id, o);
 173  0
                 getXMLTestCase().addProperty(
 174   
                     columnid,
 175   
                     new Integer(c));
 176  0
                 getXMLTestCase().processChildren(getElement());
 177   
             }
 178  0
         } else if (TABLECELL.equals(type)) {
 179  0
             getTestCase().assertTrue(refid + " not instance of JTable",
 180   
                 (obj instanceof JTable));
 181   
 
 182  0
             JTable table   = (JTable) obj;
 183  0
             int    rows    = table.getRowCount();
 184  0
             int    columns = table.getColumnCount();
 185   
 
 186  0
             String rowid    = id + "." + ROW;
 187  0
             String columnid = id + "." + COLUMN;
 188   
 
 189  0
             int    row    = getInt(ROW, -1);
 190  0
             int    column = getInt(COLUMN, -1);
 191   
 
 192  0
             if ((row == -1) && (column == -1)) {
 193  0
                 for (int r = 0; r < rows; r++) {
 194  0
                     for (int c = 0; c < columns; c++) {
 195  0
                         Object cell = table.getValueAt(r, c);
 196   
 
 197  0
                         if (debug) {
 198  0
                             System.err.println("Foreach: tablecell(" + r + ","
 199   
                                 + c + ") " + cell);
 200   
                         }
 201   
 
 202  0
                         getXMLTestCase().addProperty(id, cell);
 203  0
                         getXMLTestCase().addProperty(
 204   
                             rowid,
 205   
                             new Integer(r));
 206  0
                         getXMLTestCase().addProperty(
 207   
                             columnid,
 208   
                             new Integer(c));
 209  0
                         getXMLTestCase().processChildren(getElement());
 210   
                     }
 211   
                 }
 212  0
             } else if (row == -1) {
 213  0
                 for (int r = 0; r < rows; r++) {
 214  0
                     Object cell = table.getValueAt(r, column);
 215   
 
 216  0
                     if (debug) {
 217  0
                         System.err.println("Foreach: tablecell(" + r + ","
 218   
                             + column + ") " + cell);
 219   
                     }
 220   
 
 221  0
                     getXMLTestCase().addProperty(id, cell);
 222  0
                     getXMLTestCase().addProperty(
 223   
                         rowid,
 224   
                         new Integer(r));
 225  0
                     getXMLTestCase().addProperty(
 226   
                         columnid,
 227   
                         new Integer(column));
 228  0
                     getXMLTestCase().processChildren(getElement());
 229   
                 }
 230  0
             } else if (column == -1) {
 231  0
                 for (int c = 0; c < columns; c++) {
 232  0
                     Object cell = table.getValueAt(row, c);
 233   
 
 234  0
                     if (debug) {
 235  0
                         System.err.println("Foreach: tablecell(" + row + ","
 236   
                             + c + ") " + cell);
 237   
                     }
 238   
 
 239  0
                     getXMLTestCase().addProperty(id, cell);
 240  0
                     getXMLTestCase().addProperty(
 241   
                         rowid,
 242   
                         new Integer(row));
 243  0
                     getXMLTestCase().addProperty(
 244   
                         columnid,
 245   
                         new Integer(c));
 246  0
                     getXMLTestCase().processChildren(getElement());
 247   
                 }
 248   
             } else {
 249  0
                 Object cell = table.getValueAt(row, column);
 250   
 
 251  0
                 if (debug) {
 252  0
                     System.err.println("Foreach: tablecell(" + row + ","
 253   
                         + column + ") " + cell);
 254   
                 }
 255   
 
 256  0
                 getXMLTestCase().addProperty(id, cell);
 257  0
                 getXMLTestCase().addProperty(
 258   
                     rowid,
 259   
                     new Integer(row));
 260  0
                 getXMLTestCase().addProperty(
 261   
                     columnid,
 262   
                     new Integer(column));
 263  0
                 getXMLTestCase().processChildren(getElement());
 264   
             }
 265   
         }
 266   
     }
 267   
 
 268   
     /**
 269   
      * Validate that the element is correct.
 270   
      * @throws XMLException may be thrown.
 271   
      */
 272  0
     public void validateElement() throws XMLException {
 273  0
         super.checkElementTagName(FOREACH);
 274   
 
 275  0
         super.checkRequiredAttribute(TYPE);
 276  0
         super.checkRequiredAttribute(ID);
 277  0
         super.checkRequiredAttribute(REFID);
 278   
     }
 279   
 }
 280