Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 337   Methods: 22
NCLOC: 155   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLTestCase.java 70% 91% 95.5% 88.1%
coverage coverage
 1   
 package junit.extensions.xml;
 2   
 
 3   
 import junit.framework.Test;
 4   
 import junit.framework.TestCase;
 5   
 
 6   
 import org.w3c.dom.Attr;
 7   
 import org.w3c.dom.Element;
 8   
 import org.w3c.dom.NamedNodeMap;
 9   
 import org.w3c.dom.NodeList;
 10   
 
 11   
 
 12   
 /**
 13   
  * Test Case for running XML Script based testing.
 14   
  *
 15   
  * @author Kevin Wilson
 16   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 17   
  */
 18   
 public class XMLTestCase extends TestCase implements IXMLTestCase,
 19   
     XMLConstants {
 20   
     /**
 21   
      * The element to be processed.
 22   
      */
 23   
     private Element m_element;
 24   
 
 25   
     /**
 26   
      * Parent test.
 27   
      */
 28   
     private IXMLTest m_parent;
 29   
 
 30   
     /**
 31   
      * The name of the test xml script file - found from the classpath.
 32   
      */
 33   
     private String m_filename;
 34   
 
 35   
     /**
 36   
      * Map of the procedures for this test case.
 37   
      */
 38   
     private XMLObjectCache m_procedures = new XMLObjectCache();
 39   
 
 40   
     /**
 41   
      * A Map of all objects that have been found - keyed by the string name.
 42   
      */
 43   
     private XMLPropertyCache m_properties = new XMLPropertyCache();
 44   
 
 45   
     /**
 46   
      * The default constructor that is needed to createa a test case.
 47   
      *
 48   
      * @param filename    The name of the test xml script file
 49   
      * (found from the classpath)
 50   
      * @param element     The Element to be processed
 51   
      */
 52  179
     public XMLTestCase(final String filename, final Element element) {
 53  179
         super(filename + ":" + XMLUtil.getPath(element));
 54  179
         m_filename     = filename;
 55  179
         m_element      = element;
 56   
     }
 57   
 
 58   
     /**
 59   
      * Get the debug state.
 60   
      * @return true if debug is enabled.
 61   
      */
 62  58
     public final boolean getDebug() {
 63  58
         Object str = getProperty(DEBUG);
 64   
 
 65  58
         if (str instanceof String) {
 66  6
             return Boolean.valueOf((String) str).booleanValue();
 67  52
         } else if (str instanceof Boolean) {
 68  2
             return ((Boolean) str).booleanValue();
 69   
         }
 70   
 
 71  50
         return (m_properties.get(DEBUG) != null);
 72   
     }
 73   
 
 74   
     /**
 75   
      * Set the parent of the test case.
 76   
      * @param parent of the test case.
 77   
      */
 78  159
     public final void setParent(final IXMLTest parent) {
 79  159
         this.m_parent = parent;
 80  159
         m_properties.setParent(((IXMLTest) parent).getPropertyCache());
 81  159
         m_procedures.setParent(((IXMLTest) parent).getProcedureCache());
 82   
     }
 83   
 
 84   
     /**
 85   
      * Get a procedure definition.
 86   
      * @param name Name of the procedure.
 87   
      * @return IXMLProcedure defined by name.
 88   
      */
 89  3
     public final IXMLProcedure getProcedure(final String name) {
 90  3
         return (IXMLProcedure) m_procedures.get(name);
 91   
     }
 92   
 
 93   
     /**
 94   
      * Get the procedure cache.
 95   
      * @return XMLObjectCache which contains the procedure.
 96   
      */
 97  3
     public final XMLObjectCache getProcedureCache() {
 98  3
         return m_procedures;
 99   
     }
 100   
 
 101   
     /**
 102   
      * Retrieve the object that was found previously.
 103   
      *
 104   
      * @param name    The name of the object that was found
 105   
      * @return    The object to be retrieved
 106   
      */
 107  182
     public final Object getProperty(final String name) {
 108  182
         return m_properties.get(name);
 109   
     }
 110   
 
 111   
     /**
 112   
      * Get the property cache.
 113   
      * @return XMLObjectCache containing the properties.
 114   
      */
 115  25
     public final XMLObjectCache getPropertyCache() {
 116  25
         return m_properties;
 117   
     }
 118   
 
 119   
     /**
 120   
      * Get the name of a component which has been found.
 121   
      * @param comp Component to locate
 122   
      * @return The name of the component.
 123   
      */
 124  1
     public final String getPropertyName(final Object comp) {
 125  1
         return m_properties.getName(comp);
 126   
     }
 127   
 
 128   
     /**
 129   
      * Get the names of the found objects.
 130   
      * @return array containing the names of the objects.
 131   
      */
 132  2
     public final String[] getPropertyNames() {
 133  2
         return m_properties.getNames();
 134   
     }
 135   
 
 136   
     /**
 137   
      * Default setUp which does nothing. Override this to set up the environment
 138   
      * for your test.
 139   
      *
 140   
      * @exception  Exception   An instance of java.lang.Exception can be thrown
 141   
      * @see junit.framework.TestCase#setUp()
 142   
      */
 143  2
     public void setUp() throws Exception {
 144  2
         super.setUp();
 145   
     }
 146   
 
 147   
     /**
 148   
      * Add a procedure definition.
 149   
      * @param proc Procedure to be added.
 150   
      */
 151  2
     public final void addProcedure(final IXMLProcedure proc) {
 152  2
         m_procedures.put(
 153   
             proc.getName(),
 154   
             proc);
 155   
     }
 156   
 
 157   
     /**
 158   
      * Call a procedure by name.
 159   
      * @param name Name of the procedure to be called.
 160   
      * @param element Element of the call
 161   
      * @throws XMLException may be thrown.
 162   
      */
 163  2
     public final void callProcedure(final String name, final Element element)
 164   
         throws XMLException {
 165  2
         XMLPropertyCache cache = m_properties;
 166  2
         m_properties = new XMLPropertyCache();
 167  2
         m_properties.setParent(cache);
 168   
 
 169  2
         NamedNodeMap atts = element.getAttributes();
 170  2
         int          size = atts.getLength();
 171   
 
 172  2
         for (int i = 0; i < size; i++) {
 173  3
             Attr   node = (Attr) atts.item(i);
 174  3
             String an = node.getName();
 175   
 
 176  3
             if (!CALL.equals(an)) {
 177  1
                 String value = XMLUtil.getAttribute(element, an);
 178  1
                 value = this.resolveProperties(value);
 179  1
                 addProperty(an, value);
 180   
             }
 181   
         }
 182   
 
 183  2
         IXMLProcedure proc = getProcedure(name);
 184  2
         this.processChildren(proc.getElement());
 185  2
         m_properties.setParent(null);
 186  2
         m_properties = cache;
 187   
     }
 188   
 
 189   
     /**
 190   
      * Process the child XML Elements.
 191   
      * @param element Element which has children to be processed.
 192   
      * @throws XMLException may be thrown during processing.
 193   
      */
 194  8
     public final void processChildren(final Element element)
 195   
         throws XMLException {
 196   
         // Get the children and add to the suite.
 197  8
         NodeList children = element.getChildNodes();
 198  8
         Element  child;
 199   
 
 200  8
         for (int i = 0; i < children.getLength(); i++) {
 201  6
             if (children.item(i) instanceof Element) {
 202  6
                 child = (Element) children.item(i);
 203   
 
 204  6
                 String name = child.getTagName();
 205   
 
 206  6
                 try {
 207  6
                     XMLTagResourceBundle.getTagHandler(child, this, name)
 208   
                                         .processElement();
 209   
                 } catch (XMLException xe) {
 210  0
                     throw xe;
 211   
                 } catch (Throwable t) {
 212  0
                     throw new XMLException(
 213   
                         t.getMessage(),
 214   
                         t,
 215   
                         child,
 216   
                         getPropertyCache());
 217   
                 }
 218   
 
 219  6
                 if (child != children.item(i)) {
 220  0
                     for (;
 221  0
                             (child != children.item(i))
 222   
                             && (i < children.getLength()); i++) {
 223   
                         // Nothing to do;
 224   
                     }
 225   
 
 226  0
                     if (i == children.getLength()) {
 227  0
                         throw new junit.extensions.xml.XMLException("Lost where we were at current node was removed.",
 228   
                             null,
 229   
                             m_element,
 230   
                             getPropertyCache());
 231   
                     }
 232   
                 }
 233   
             }
 234   
         }
 235   
     }
 236   
 
 237   
     /**
 238   
      * Remove the object with the name given.
 239   
      *
 240   
      * @param name Name of the object to be removed
 241   
      */
 242  3
     public final void removeProperty(final String name) {
 243  3
         m_properties.remove(name);
 244   
     }
 245   
 
 246   
     /**
 247   
      * Resolve embeded property names withing a string.
 248   
      * @param s String to be resolved.
 249   
      * @return String without property names.
 250   
      */
 251  465
     public final String resolveProperties(final String s) {
 252  465
         return m_properties.resolve(s);
 253   
     }
 254   
 
 255   
     /**
 256   
      * Sets up, executes and then tears down a test.
 257   
      *
 258   
      * @exception Throwable exceptions thrown by code.
 259   
      */
 260  2
     public void runBare() throws Throwable {
 261  2
         setUp();
 262   
 
 263  2
         try {
 264  2
             runTest();
 265   
         } finally {
 266  2
             tearDown();
 267   
         }
 268   
     }
 269   
 
 270   
     /**
 271   
      * This method is the one that actually performs the test by processing the elements.
 272   
      *
 273   
      * @exception  Exception   An instance of java.lang.Exception can be thrown.
 274   
      */
 275  2
     public final void runXMLTest() throws Exception {
 276  2
         clearProperties();
 277   
 
 278  2
         boolean debug = XMLUtil.getBooleanAttributeValue(m_element, DEBUG);
 279   
 
 280  2
         if (debug) {
 281  1
             m_properties.put(DEBUG, Boolean.TRUE);
 282   
         } else {
 283  1
             m_properties.remove(DEBUG);
 284   
         }
 285   
 
 286  2
         processChildren(m_element);
 287   
 
 288   
         //    XMLTagResourceBundle.getTagHandler(m_element, this, TEST).processElement();
 289   
     }
 290   
 
 291   
     /**
 292   
      * Returns an empty suite.
 293   
      *
 294   
      * @return Test   An empty XMLTestSuite is returned by default.
 295   
      */
 296  0
     public static Test suite() {
 297  0
         return new XMLTestSuite();
 298   
     }
 299   
 
 300   
     /**
 301   
      * Add each found object into the cache map.
 302   
      *
 303   
      * @param name    The name of the found object.
 304   
      * @param obj     The actual object that was found.
 305   
      */
 306  130
     public final void addProperty(final String name, final Object obj) {
 307  130
         m_properties.put(name, obj);
 308   
     }
 309   
 
 310   
     /**
 311   
      * For each test (method) element, the found objects has to be cleared.
 312   
      */
 313  3
     public final void clearProperties() {
 314  3
         m_properties.clear();
 315   
     }
 316   
 
 317   
     /**
 318   
      * Default tearDown which does nothing. Override this to tear down the
 319   
      * environment for your test.
 320   
      *
 321   
      * @exception  Exception   An instance of java.lang.Exception can be thrown
 322   
      * @see junit.framework.TestCase#setUp()
 323   
      */
 324  2
     public void tearDown() throws Exception {
 325  2
         super.tearDown();
 326   
     }
 327   
 
 328   
     /**
 329   
      * Executes a test.
 330   
      *
 331   
      * @exception Throwable exceptions thrown by code.
 332   
      */
 333  2
     protected void runTest() throws Throwable {
 334  2
         runXMLTest();
 335   
     }
 336   
 }
 337