Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 592   Methods: 31
NCLOC: 289   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractTagHandler.java 87% 93% 100% 92.4%
coverage coverage
 1   
 package junit.extensions.xml.elements;
 2   
 
 3   
 import junit.extensions.xml.IXMLTest;
 4   
 import junit.extensions.xml.IXMLTestCase;
 5   
 import junit.extensions.xml.IXMLTestSuite;
 6   
 import junit.extensions.xml.XMLConstants;
 7   
 import junit.extensions.xml.XMLException;
 8   
 import junit.extensions.xml.XMLUtil;
 9   
 
 10   
 import junit.framework.TestCase;
 11   
 
 12   
 import org.w3c.dom.Element;
 13   
 
 14   
 import java.awt.Point;
 15   
 
 16   
 import java.text.MessageFormat;
 17   
 import java.text.ParseException;
 18   
 
 19   
 
 20   
 /**
 21   
  * Sub-classes of this class will handle the processing of elements.
 22   
  * It provides common access methods to the test case or test suite.
 23   
  * It also provides methods for reading attributes.
 24   
  *
 25   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 26   
  * @author Kevin Wilson
 27   
  */
 28   
 public abstract class AbstractTagHandler implements XMLConstants {
 29   
     /**
 30   
      * The Element to be processed.
 31   
      */
 32   
     private Element m_element;
 33   
 
 34   
     /**
 35   
      * The IXMLTestCase that uses this element.
 36   
      */
 37   
     private IXMLTestCase m_testCase;
 38   
 
 39   
     /**
 40   
      * The IXMLTestSuite that uses this element.
 41   
      */
 42   
     private IXMLTestSuite m_testSuite;
 43   
 
 44   
     /**
 45   
      * Constructor for AbstractTagHandler.
 46   
      *
 47   
      * @param element    The element to be processed.
 48   
      * @param testCase   The testCase to be associated with the test.
 49   
      */
 50  1383
     public AbstractTagHandler(final Element element, final IXMLTestCase testCase) {
 51  1383
         if (element == null) {
 52  0
             throw new java.lang.IllegalArgumentException(
 53   
                 "element must not be null");
 54   
         }
 55   
 
 56  1383
         if (testCase == null) {
 57  0
             throw new java.lang.IllegalArgumentException(
 58   
                 "testCase must not be null");
 59   
         }
 60   
 
 61  1383
         this.m_element      = element;
 62  1383
         this.m_testCase     = testCase;
 63   
     }
 64   
 
 65   
     /**
 66   
      * Constructor for AbstractTagHandler.
 67   
      *
 68   
      * @param element    The element to be processed.
 69   
      * @param testSuite   The testSuite to be associated with the test.
 70   
      */
 71  1250
     public AbstractTagHandler(final Element element,
 72   
         final IXMLTestSuite testSuite) {
 73  1250
         if (element == null) {
 74  0
             throw new java.lang.IllegalArgumentException(
 75   
                 "Element must not be null");
 76   
         }
 77   
 
 78  1250
         if (testSuite == null) {
 79  0
             throw new java.lang.IllegalArgumentException(
 80   
                 "testSuite must not be null");
 81   
         }
 82   
 
 83  1250
         this.m_element       = element;
 84  1250
         this.m_testSuite     = testSuite;
 85   
     }
 86   
 
 87   
     /**
 88   
      * Returns the element.
 89   
      *
 90   
      * @return Element.
 91   
      */
 92  14488
     public final Element getElement() {
 93  14488
         return m_element;
 94   
     }
 95   
 
 96   
     /**
 97   
      * The JUnit framework test case.
 98   
      * @return TestCase in JUnit framework.
 99   
      */
 100  481
     public final TestCase getTestCase() {
 101  481
         return (TestCase) m_testCase;
 102   
     }
 103   
 
 104   
     /**
 105   
      * Returns the testCase.
 106   
      *
 107   
      * @return IXMLTestCase.
 108   
      */
 109  15180
     public final IXMLTestCase getXMLTestCase() {
 110  15180
         return m_testCase;
 111   
     }
 112   
 
 113   
     /**
 114   
      * Returns the testSuite.
 115   
      *
 116   
      * @return IXMLTestSuite.
 117   
      */
 118  3435
     public final IXMLTestSuite getXMLTestSuite() {
 119  3435
         return m_testSuite;
 120   
     }
 121   
 
 122   
     /**
 123   
      * This method is used to process the xml Element (i.e. parsing and setting the attributes on
 124   
      * XYZ objects from those parsed values.
 125   
      *
 126   
      * @throws XMLException Some kind of Exception might be thrown.
 127   
      */
 128   
     public abstract void processElement() throws XMLException;
 129   
 
 130   
     /**
 131   
      * Return the tag name of this element.
 132   
      * @return Tag name.
 133   
      */
 134  1
     protected final String getTagName() {
 135  1
         return m_element.getTagName();
 136   
     }
 137   
 
 138   
     /**
 139   
      * Since all sub-classes might check for a required attribute,
 140   
      * this method can be used for consistent messages.
 141   
      *
 142   
      * @param attrName    The names of the attributes being checked.
 143   
      * @throws XMLException    A validation exception is thrown.
 144   
      */
 145  80
     protected final void checkAtLeastOneRequiredAttribute(
 146   
         final String[] attrName) throws XMLException {
 147  80
         checkAtLeastOneRequiredAttribute(
 148   
             getElement(),
 149   
             attrName);
 150   
     }
 151   
 
 152   
     /**
 153   
      * Since all sub-classes might check for a required attribute,
 154   
      * this method can be used for consistent messages.
 155   
      *
 156   
      * @param root    The root Element whose attributes are being checked
 157   
      * @param attrName    The names of the attributes being checked.
 158   
      * @throws XMLException    A validation exception is thrown.
 159   
      */
 160  99
     protected final void checkAtLeastOneRequiredAttribute(final Element root,
 161   
         final String[] attrName) throws XMLException {
 162  99
         int found = 0;
 163   
 
 164  99
         for (int i = 0; i < attrName.length; i++) {
 165  192
             if (XMLUtil.hasAttribute(root, attrName[i])) {
 166  93
                 found++;
 167   
             }
 168   
         }
 169   
 
 170  99
         if (found == 0) {
 171  6
             String buf = "";
 172   
 
 173  6
             for (int i = 0; i < attrName.length; i++) {
 174  10
                 buf = MessageFormat.format(
 175   
                         "{0} {1}",
 176   
                         new Object[] {buf, attrName[i]});
 177   
             }
 178   
 
 179  6
             throw new XMLException(root.getNodeName()
 180   
                 + " is missing one of the required attributes: " + buf, null,
 181   
                 root,
 182   
                 getTest().getPropertyCache());
 183   
         }
 184   
     }
 185   
 
 186   
     /**
 187   
      * Since all sub-classes will check for the element tag name,
 188   
      * this method can be used for consistent messages.
 189   
      *
 190   
      * @param expectedName    The non-null expected string name.
 191   
      * @throws XMLException    A validation exception is thrown.
 192   
      */
 193  1884
     protected final void checkElementTagName(final String expectedName)
 194   
         throws XMLException {
 195  1884
         if (!expectedName.equals(m_element.getTagName())) {
 196  2
             throw new XMLException("Cannot process "
 197   
                 + getElement().getTagName() + " node, expected '"
 198   
                 + expectedName + "'", null,
 199   
                 getElement(),
 200   
                 getTest().getPropertyCache());
 201   
         }
 202   
     }
 203   
 
 204   
     /**
 205   
      * Since all sub-classes might check for a required attribute,
 206   
      * this method can be used for consistent messages.
 207   
      * @param e Element to be checked for the attribute.
 208   
      * @param attrName    The name of the attribute being checked.
 209   
      * @throws XMLException    A validation exception is thrown.
 210   
      */
 211  3739
     protected final void checkRequiredAttribute(final Element e,
 212   
         final String attrName) throws XMLException {
 213  3739
         if (!XMLUtil.hasAttribute(e, attrName)) {
 214  2
             throw new XMLException("Missing the required attribute: "
 215   
                 + attrName + " in element: " + m_element.getNodeName(), null,
 216   
                 e,
 217   
                 getTest().getPropertyCache());
 218   
         }
 219   
     }
 220   
 
 221   
     /**
 222   
      * Since all sub-classes might check for a required attribute,
 223   
      * this method can be used for consistent messages.
 224   
      *
 225   
      * @param attrName    The name of the attribute being checked.
 226   
      * @throws XMLException    A validation exception is thrown.
 227   
      */
 228  3728
     protected final void checkRequiredAttribute(final String attrName)
 229   
         throws XMLException {
 230  3728
         checkRequiredAttribute(
 231   
             getElement(),
 232   
             attrName);
 233   
     }
 234   
 
 235   
     /**
 236   
      * This method checks whether the specified attribute is present, and if so, returns the value
 237   
      * parsed as a primitive boolean, if not, then returns false.
 238   
      * @param attributeName    The name of the attribute whose value is needed.
 239   
      * @return boolean    The value of the attribute (false if not found).
 240   
      * @depricated Specify the default value.
 241   
      */
 242  1187
     protected boolean getBoolean(final String attributeName) {
 243  1187
         return getBoolean(
 244   
             getElement(),
 245   
             attributeName,
 246   
             false);
 247   
     }
 248   
 
 249   
     /**
 250   
      * This method checks whether the specified attribute is present, and if so, returns the value
 251   
      * parsed as a primitive boolean, if not, then returns false.
 252   
      * @param attributeName    The name of the attribute whose value is needed.
 253   
      * @param defaultValue     The default value
 254   
      * @return boolean    The value of the attribute (defaultValue if not found).
 255   
      */
 256  190
     protected boolean getBoolean(final String attributeName,
 257   
         final boolean defaultValue) {
 258  190
         return getBoolean(
 259   
             getElement(),
 260   
             attributeName,
 261   
             defaultValue);
 262   
     }
 263   
 
 264   
     /**
 265   
      * This method checks whether the specified attribute is present, and if so, returns the value
 266   
      * parsed as a primitive boolean, if not, then returns false.
 267   
      * @param e Element to be searched.
 268   
      * @param attributeName    The name of the attribute whose value is needed.
 269   
      * @return boolean    The value of the attribute (false) if not found).
 270   
      * @depricated Specify the default value.
 271   
      */
 272  3
     protected final boolean getBoolean(final Element e,
 273   
         final String attributeName) {
 274  3
         return getBoolean(e, attributeName, false);
 275   
     }
 276   
 
 277   
     /**
 278   
      * This method checks whether the specified attribute is present, and if so, returns the value
 279   
      * parsed as a primitive boolean, if not, then returns false.
 280   
      * @param e Element to be searched.
 281   
      * @param attributeName    The name of the attribute whose value is needed.
 282   
      * @param defaultValue     The default value.
 283   
      * @return boolean    The value of the attribute (defaultValue if not found).
 284   
      */
 285  1384
     protected final boolean getBoolean(final Element e,
 286   
         final String attributeName, final boolean defaultValue) {
 287   
         // this will default to false
 288  1384
         if (XMLUtil.hasAttribute(e, attributeName)) {
 289  30
             String s = getString(e, attributeName);
 290   
 
 291  30
             if (defaultValue) {
 292  11
                 return !"false".equalsIgnoreCase(s);
 293   
             } else {
 294  19
                 return "true".equalsIgnoreCase(s);
 295   
             }
 296   
         } else {
 297  1354
             return defaultValue;
 298   
         }
 299   
     }
 300   
 
 301   
     /**
 302   
      * This method checks whether the specified attribute is present, and if so, returns the value
 303   
          * parsed as a primitive int, if not, then returns the default value specified.
 304   
      * @param attributeName    The name of the attribute whose value is needed.
 305   
      * @param defaultValue     The default value to use if such an attribute is not present.
 306   
      * @return int    The value of the attribute.
 307   
      */
 308  1469
     protected int getInt(final String attributeName, final int defaultValue) {
 309  1469
         return getInt(
 310   
             getElement(),
 311   
             attributeName,
 312   
             defaultValue);
 313   
     }
 314   
 
 315   
     /**
 316   
      * This method checks whether the specified attribute is present, and if so, returns the value
 317   
      * parsed as a primitive int, if not, then returns the default value specified.
 318   
      * @param e Element to be processed.
 319   
      * @param attributeName    The name of the attribute whose value is needed.
 320   
      * @param defaultValue     The default value to use if such an attribute is not present.
 321   
      * @return int    The value of the attribute.
 322   
      */
 323  1475
     protected int getInt(final Element e, final String attributeName,
 324   
         final int defaultValue) {
 325  1475
         String s = getString(e, attributeName);
 326   
 
 327  1475
         try {
 328  1475
             if (s != null) {
 329  820
                 return Integer.parseInt(s);
 330   
             }
 331   
         } catch (NumberFormatException nfe) {
 332   
             // Ignore the number format exception.
 333   
         }
 334   
 
 335  657
         return defaultValue;
 336   
     }
 337   
 
 338   
     /**
 339   
      * This method checks whether the specified attribute is present, and if so,
 340   
      * returns the value parsed as a primitive long, if not, then returns
 341   
      * the default value specified.
 342   
      *
 343   
      * @param attributeName    The name of the attribute whose value is needed.
 344   
      * @param defaultValue     The default value to use if such an attribute is not present.
 345   
      * @return long    The value of the attribute.
 346   
      */
 347  464
     protected long getLong(final String attributeName, final long defaultValue) {
 348  464
         return getLong(
 349   
             getElement(),
 350   
             attributeName,
 351   
             defaultValue);
 352   
     }
 353   
 
 354   
     /**
 355   
      * This method checks whether the specified attribute is present, and if so, returns the value
 356   
      * parsed as a primitive long, if not, then returns the default value specified.
 357   
      * @param e Element to be searched.
 358   
      * @param attributeName    The name of the attribute whose value is needed.
 359   
      * @param defaultValue     The default value to use if such an attribute is not present.
 360   
      * @return long    The value of the attribute.
 361   
      */
 362  470
     protected long getLong(final Element e, final String attributeName,
 363   
         final long defaultValue) {
 364  470
         String s = getString(e, attributeName);
 365   
 
 366  470
         try {
 367  470
             if (s != null) {
 368  48
                 return Long.parseLong(s);
 369   
             }
 370   
         } catch (NumberFormatException nfe) {
 371   
             ;
 372   
         }
 373   
 
 374  424
         return defaultValue;
 375   
     }
 376   
 
 377   
     /**
 378   
      * This method checks whether the specified attribute is present, and if so, returns the value
 379   
      * parsed as a {@link Point}, if not, then returns the default value specified.
 380   
      * @param attributeName    The name of the attribute whose value is needed.
 381   
      * @param defaultValue     The default value to use if such an attribute is not present.
 382   
      * @return Point    The value of the attribute.
 383   
      * @throws XMLException if the parse did not work.
 384   
      */
 385  429
     protected final Point getPoint(final String attributeName,
 386   
         final Point defaultValue) throws XMLException {
 387  429
         return getPoint(
 388   
             getElement(),
 389   
             attributeName,
 390   
             defaultValue);
 391   
     }
 392   
 
 393   
     /**
 394   
      * This method checks whether the specified attribute is present, and if so, returns the value
 395   
      * parsed as a {@link Point}, if not, then returns the default value specified.
 396   
      * @param e  Element to be searched.
 397   
      * @param attributeName    The name of the attribute whose value is needed.
 398   
      * @param defaultValue     The default value to use if such an attribute is not present.
 399   
      * @return Point    The value of the attribute.
 400   
      * @throws XMLException is thrown if the element cannot be understood.
 401   
      */
 402  440
     protected final Point getPoint(final Element e, final String attributeName,
 403   
         final Point defaultValue) throws XMLException {
 404  440
         String s = getString(e, attributeName);
 405   
 
 406  440
         if (s == null) {
 407  409
             return defaultValue;
 408   
         }
 409   
 
 410   
         // somehow, using this does not work!!!
 411   
         //MessageFormat mf = new MessageFormat("{0,number},{1,number}");
 412  31
         MessageFormat mf = new MessageFormat("{0},{1}");
 413  31
         int           x = 0;
 414  31
         int           y = 0;
 415   
 
 416  31
         try {
 417  31
             Object[] obs = mf.parse(s);
 418  29
             x     = Integer.parseInt((String) obs[0]);
 419  29
             y     = Integer.parseInt((String) obs[1]);
 420   
         } catch (NumberFormatException ex) {
 421  0
             throw new XMLException("Invalid number format:" + s, ex, e,
 422   
                 getTest().getPropertyCache());
 423   
         } catch (ParseException ex) {
 424  2
             throw new XMLException("Parsing of Point failed." + s, ex, e,
 425   
                 getTest().getPropertyCache());
 426   
         }
 427   
 
 428  29
         return new Point(x, y);
 429   
     }
 430   
 
 431   
     /**
 432   
      * This method returns the value of the specified attribute.
 433   
      * @param attributeName    The name of the attribute whose value is needed.
 434   
      * @return String    The value of the attribute.
 435   
      */
 436  4687
     protected final String getString(final String attributeName) {
 437  4687
         return getString(
 438   
             getElement(),
 439   
             attributeName);
 440   
     }
 441   
 
 442   
     /**
 443   
      * Get a the string assigned to a attribute from the current element.
 444   
      * @param attributeName Name of the attribute to find.
 445   
      * @param defaultValue Default value to return if the attribute is not
 446   
      * present.
 447   
      * @return String containing the attribute. Variable names will have been
 448   
      * resolved.
 449   
      */
 450  3
     protected final String getString(final String attributeName,
 451   
         final String defaultValue) {
 452  3
         return getString(
 453   
             getElement(),
 454   
             attributeName,
 455   
             defaultValue);
 456   
     }
 457   
 
 458   
     /**
 459   
      * This method returns the value of the specified attribute.
 460   
      * @param e          Element to be searched;
 461   
      * @param attributeName    The name of the attribute whose value is needed.
 462   
      * @return String    The value of the attribute.
 463   
      */
 464  7124
     protected final String getString(final Element e, final String attributeName) {
 465  7124
         String s = XMLUtil.getAttribute(e, attributeName);
 466   
 
 467  7124
         return resolveVariables(s);
 468   
     }
 469   
 
 470   
     /**
 471   
      * Get the string assigned to the attributeName from the element.
 472   
      * If the attributeName does not exist then return the default value.
 473   
      * @param e Element to be checked.
 474   
      * @param attributeName Name of the attribute to be returned.
 475   
      * @param defaultValue  Default value to be returned.
 476   
      * @return String which has had variable names resolved.
 477   
      */
 478  6
     protected final String getString(final Element e,
 479   
         final String attributeName, final String defaultValue) {
 480  6
         if (e.hasAttribute(attributeName)) {
 481  4
             return getString(e, attributeName);
 482   
         } else {
 483  2
             return resolveVariables(defaultValue);
 484   
         }
 485   
     }
 486   
 
 487   
     /**
 488   
      * Since all sub-classes might check for a required attribute,
 489   
      * this method can be used for consistent messages.
 490   
      *
 491   
      * @param attrName    The names of the attributes being checked.
 492   
      * @throws XMLException    A validation exception is thrown.
 493   
      */
 494  18
     protected void checkOneRequiredAttribute(final String[] attrName)
 495   
         throws XMLException {
 496  18
         checkOneRequiredAttribute(
 497   
             getElement(),
 498   
             attrName);
 499   
     }
 500   
 
 501   
     /**
 502   
      * Since all sub-classes might check for a required attribute,
 503   
      * this method can be used for consistent messages.
 504   
      *
 505   
      * @param root    The root Element whose attributes are being checked
 506   
      * @param attrName    The names of the attributes being checked.
 507   
      * @throws XMLException    A validation exception is thrown.
 508   
      */
 509  96
     protected void checkOneRequiredAttribute(final Element root,
 510   
         final String[] attrName) throws XMLException {
 511  96
         int found = 0;
 512   
 
 513  96
         if (attrName != null) {
 514  96
             for (int i = 0; i < attrName.length; i++) {
 515  186
                 if (XMLUtil.hasAttribute(root, attrName[i])) {
 516  96
                     found++;
 517   
                 }
 518   
             }
 519   
         }
 520   
 
 521  96
         if (found == 0) {
 522  2
             String buf = "";
 523   
 
 524  2
             for (int i = 0; i < attrName.length; i++) {
 525  2
                 buf = MessageFormat.format(
 526   
                         "{0} {1}",
 527   
                         new Object[] {buf, attrName[i]});
 528   
             }
 529   
 
 530  2
             throw new XMLException(root.getNodeName()
 531   
                 + " is missing one of the required attributes: " + buf, null,
 532   
                 root,
 533   
                 getTest().getPropertyCache());
 534   
         }
 535   
 
 536  94
         if (found > 1) {
 537  2
             String buf = "";
 538   
 
 539  2
             for (int i = 0; i < attrName.length; i++) {
 540  4
                 buf = MessageFormat.format(
 541   
                         "{0} {1}",
 542   
                         new Object[] {buf, attrName[i]});
 543   
             }
 544   
 
 545  2
             throw new XMLException("Too many attributes of:" + buf, null, root,
 546   
                 getTest().getPropertyCache());
 547   
         }
 548   
     }
 549   
 
 550   
     /**
 551   
      * Replace variable names with the string of
 552   
      * the variable values.
 553   
      * @param s String to be substituted.
 554   
      * @return String with substitutions made.
 555   
      */
 556  7131
     protected final String resolveVariables(final String s) {
 557  7131
         if (getXMLTestCase() != null) {
 558  5879
             return getXMLTestCase().resolveProperties(s);
 559  1252
         } else if (getXMLTestSuite() != null) {
 560  1252
             return getXMLTestSuite().resolveProperties(s);
 561   
         }
 562   
 
 563  0
         return s;
 564   
     }
 565   
 
 566   
     /**
 567   
      * Get the test.
 568   
      * @return IXMLTest IXMLTest test case or test suite.
 569   
      */
 570  332
     protected IXMLTest getTest() {
 571  332
         if (this.m_testCase != null) {
 572  19
             return m_testCase;
 573   
         } else {
 574  313
             return m_testSuite;
 575   
         }
 576   
     }
 577   
 
 578   
     /**
 579   
      * Sub-classes should implement this method to provide validation
 580   
      * of the element attributes, etc.
 581   
      *
 582   
      * @throws XMLException    A validation exception is thrown.
 583   
      */
 584  3146
     protected void validateElement() throws XMLException {
 585  3146
         if (m_element == null) {
 586  0
             throw new XMLException("Element could not be found.", null,
 587   
                 getElement(),
 588   
                 getTest().getPropertyCache());
 589   
         }
 590   
     }
 591   
 }
 592