Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 217   Methods: 9
NCLOC: 103   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLTagResourceBundle.java 81.2% 84.2% 100% 85.7%
coverage coverage
 1   
 package junit.extensions.xml;
 2   
 
 3   
 import junit.extensions.xml.elements.AbstractTagHandler;
 4   
 
 5   
 import org.w3c.dom.Element;
 6   
 
 7   
 import java.lang.reflect.Constructor;
 8   
 import java.lang.reflect.InvocationTargetException;
 9   
 
 10   
 import java.util.HashMap;
 11   
 import java.util.Map;
 12   
 import java.util.ResourceBundle;
 13   
 
 14   
 
 15   
 /**
 16   
  * This class is used to read in the properties and provide access to the tag handlers for various
 17   
  * elements based on their attribute values.
 18   
  *
 19   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 20   
  * @author Kevin Wilson
 21   
  */
 22   
 public final class XMLTagResourceBundle {
 23   
     /**
 24   
      * Handle to the singleton instance.
 25   
      */
 26   
     private static XMLTagResourceBundle s_singleton;
 27   
 
 28   
     /**
 29   
      * Map of the API specified Tag Handlers.
 30   
      */
 31   
     private final Map m_map = new HashMap();
 32   
 
 33   
     /**
 34   
      * The ResourceBundle used to read in the properties.
 35   
      */
 36   
     private ResourceBundle m_bundle = null;
 37   
 
 38   
     /**
 39   
      * A default private constructor.
 40   
      * @see java.lang.Object#Object()
 41   
      */
 42  45
     private XMLTagResourceBundle() {
 43  45
         m_bundle = ResourceBundle.getBundle("TagMapping");
 44   
     }
 45   
 
 46   
     /**
 47   
          * A convenience method to get a handle to the AbstractTagHandler implementation.
 48   
      * @param element     The element for which we need the tag handler.
 49   
      * @param test   The IXMLTestSuite being run.
 50   
      * @param type        The type of element to be handled.
 51   
      * @return AbstractTagHandler    An implementation of the AbstractTagHandler which will be used
 52   
      * to process the element.
 53   
      * @throws XMLException if the tag handle cannot be instanciated.
 54   
      */
 55  2485
     public static AbstractTagHandler getTagHandler(final Element element,
 56   
         final IXMLTest test, final String type) throws XMLException {
 57  2485
         return getSingleton().getTagHandlerImpl(element, test, type);
 58   
     }
 59   
 
 60   
     /**
 61   
      * Add a tag handler. If the tag exists in the
 62   
      * resource bundle, then the added tag handler will
 63   
      * take precedence over the tag from the resource
 64   
      * bundle.
 65   
      * @param tagname Tag name to be registered.
 66   
      * @param classname Classname to be registered.
 67   
      */
 68  2
     public static void addTagHandler(final String tagname,
 69   
         final String classname) {
 70  2
         getSingleton().addTagHandlerImpl(tagname, classname);
 71   
     }
 72   
 
 73   
     /**
 74   
      * Remove a tag handler. If the tag name was over
 75   
      * ridden by a added tag, then access will be restored
 76   
      * to the tag provided by the resource bundle. Tags
 77   
      * in the resource bundle may not be removed.
 78   
      *
 79   
      * @param tagname Tagname to be removed.
 80   
      */
 81  1
     public static void removeTagHandler(final String tagname) {
 82  1
         getSingleton().removeTagHandlerImpl(tagname);
 83   
     }
 84   
 
 85   
     /**
 86   
      * The "default" method that is used to create and initialize a singleton instance.
 87   
      * @return XMLTagResourceBundle    The one and only instance of this class.
 88   
      */
 89  2488
     private static XMLTagResourceBundle getSingleton() {
 90  2488
         if (s_singleton == null) {
 91  45
             s_singleton = new XMLTagResourceBundle();
 92   
         }
 93   
 
 94  2488
         return s_singleton;
 95   
     }
 96   
 
 97   
     /**
 98   
      * Get the Class for the type of tag handler given.
 99   
      * The local tag map will be used first. If the tag is
 100   
      * not found in the map, the bundle will be searched.
 101   
      * @param tagname TagName to be
 102   
      * @param debug True if debugging.
 103   
      * @return Class instance for the tagtype
 104   
      * @throws XMLException may be thrown.
 105   
      */
 106  2485
     private Class getClassFromTag(final String tagname, final boolean debug)
 107   
         throws XMLException {
 108  2485
         String clazzName = (String) m_map.get(tagname);
 109   
 
 110  2485
         if (clazzName == null) {
 111  2483
             clazzName = m_bundle.getString(tagname);
 112   
 
 113  2481
             if (clazzName != null) {
 114  2481
                 if (debug) {
 115  1
                     System.err.println("Found tag(" + tagname + "/" + clazzName
 116   
                         + ") in properties");
 117   
                 }
 118   
             }
 119   
         } else {
 120  2
             if (debug) {
 121  0
                 System.err.println("Found tag(" + tagname + "/" + clazzName
 122   
                     + ") in map");
 123   
             }
 124   
         }
 125   
 
 126  2483
         try {
 127  2483
             Class clazz = Class.forName(clazzName);
 128   
 
 129  2483
             return clazz;
 130   
         } catch (Exception e) {
 131   
             // Ignore
 132   
         }
 133   
 
 134  0
         throw new XMLException("Could not load class:" + clazzName, null, null,
 135   
             null);
 136   
     }
 137   
 
 138   
     /**
 139   
      * Reads the short name (type) and creates an instance of the tag handler that corresponds to
 140   
      * the value from the resource bundle.
 141   
      * @param element     The element for which we need the tag handler.
 142   
      * @param test        The testCase being run.
 143   
      * @param type        The type of element to be handled.
 144   
      * @return AbstractTagHandler    An implementation of the AbstractTagHandler which will be used
 145   
      * to process the element.
 146   
      * @throws XMLException if the tag handler cannot be instanciated.
 147   
      */
 148  2485
     private AbstractTagHandler getTagHandlerImpl(final Element element,
 149   
         final IXMLTest test, final String type) throws XMLException {
 150  2485
         Class clazz = getClassFromTag(
 151   
                 type,
 152   
                 test.getDebug());
 153  2483
         Object[] params = new Object[] {element, test};
 154   
 
 155  2483
         try {
 156  2483
             Constructor cnstr;
 157   
 
 158  2483
             if (test instanceof IXMLTestCase) {
 159  1244
                 cnstr = clazz.getConstructor(
 160   
                         new Class[] {Element.class, IXMLTestCase.class});
 161  1239
             } else if (test instanceof IXMLTestSuite) {
 162  1239
                 cnstr = clazz.getConstructor(
 163   
                         new Class[] {Element.class, IXMLTestSuite.class});
 164   
             } else {
 165  0
                 throw new XMLException(
 166   
                     "Test is not instanceof IXMLTestSuite or IXMLTestCase");
 167   
             }
 168   
 
 169  2483
             return (AbstractTagHandler) cnstr.newInstance(params);
 170   
         } catch (InvocationTargetException ite) {
 171  309
             Throwable t = ite.getCause();
 172   
 
 173  309
             if (t instanceof XMLException) {
 174  306
                 throw (XMLException) t;
 175   
             }
 176   
 
 177  3
             throw new XMLException(
 178   
                 t.getMessage(),
 179   
                 t,
 180   
                 element,
 181   
                 null);
 182   
         } catch (XMLException xmle) {
 183  0
             throw xmle;
 184   
         } catch (Exception e) {
 185  0
             throw new XMLException("Could not create tag handler for:" + type
 186   
                 + "\n", e, element, null);
 187   
         } catch (Error er) {
 188  0
             throw new XMLException("Could not create tag handler for:" + type
 189   
                 + "\n", er, element, null);
 190   
         }
 191   
     }
 192   
 
 193   
     /**
 194   
      * Add a tag handler. If the tag exists in the
 195   
      * resource bundle, then the added tag handler will
 196   
      * take precedence over the tag from the resource
 197   
      * bundle.
 198   
      * @param tagname Tag name to be registered.
 199   
      * @param classname Classname to be registered.
 200   
      */
 201  2
     private void addTagHandlerImpl(final String tagname, final String classname) {
 202  2
         m_map.put(tagname, classname);
 203   
     }
 204   
 
 205   
     /**
 206   
      * Remove a tag handler. If the tag name was over
 207   
      * ridden by a added tag, then access will be restored
 208   
      * to the tag provided by the resource bundle. Tags
 209   
      * in the resource bundle may not be removed.
 210   
      *
 211   
      * @param tagname Tagname to be removed.
 212   
      */
 213  1
     private void removeTagHandlerImpl(final String tagname) {
 214  1
         m_map.remove(tagname);
 215   
     }
 216   
 }
 217