Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 143   Methods: 5
NCLOC: 77   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
Creator.java 100% 96% 100% 97.2%
coverage coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.test;
 16   
 
 17   
 import java.util.HashMap;
 18   
 import java.util.Iterator;
 19   
 import java.util.Map;
 20   
 
 21   
 import org.apache.commons.logging.Log;
 22   
 import org.apache.commons.logging.LogFactory;
 23   
 import org.apache.hivemind.ApplicationRuntimeException;
 24   
 import org.apache.hivemind.ClassResolver;
 25   
 import org.apache.hivemind.impl.DefaultClassResolver;
 26   
 import org.apache.hivemind.impl.DefaultErrorHandler;
 27   
 import org.apache.hivemind.impl.ErrorLogImpl;
 28   
 import org.apache.hivemind.service.ClassFactory;
 29   
 import org.apache.hivemind.service.impl.ClassFactoryImpl;
 30   
 import org.apache.hivemind.util.PropertyUtils;
 31   
 import org.apache.tapestry.Tapestry;
 32   
 import org.apache.tapestry.enhance.AbstractPropertyWorker;
 33   
 import org.apache.tapestry.enhance.EnhancementOperationImpl;
 34   
 import org.apache.tapestry.services.ComponentConstructor;
 35   
 import org.apache.tapestry.spec.ComponentSpecification;
 36   
 
 37   
 /**
 38   
  * A utility class that is used to instantiate abstract Tapestry pages and components. It creates,
 39   
  * at runtime, a subclass where all abstract properties are filled in (complete with instance
 40   
  * variable, accessor and mutator methods). This isn't the same as how the class is enhanced at
 41   
  * runtime, but is sufficient to unit test the class, especially listener methods.
 42   
  * 
 43   
  * @author Howard Lewis Ship
 44   
  * @since 3.1
 45   
  */
 46   
 public class Creator
 47   
 {
 48   
     private static final Log LOG = LogFactory.getLog(Creator.class);
 49   
 
 50   
     /**
 51   
      * Keyed on Class, value is an {@link ComponentConstructor}.
 52   
      */
 53   
     private Map _constructors = new HashMap();
 54   
 
 55   
     private ClassFactory _classFactory = new ClassFactoryImpl();
 56   
 
 57   
     private ClassResolver _classResolver = new DefaultClassResolver();
 58   
 
 59  32
     private ComponentConstructor createComponentConstructor(Class inputClass)
 60   
     {
 61  32
         if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray())
 62  1
             throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));
 63   
 
 64  31
         AbstractPropertyWorker w = new AbstractPropertyWorker();
 65   
 
 66  31
         w.setErrorLog(new ErrorLogImpl(new DefaultErrorHandler(), LOG));
 67   
 
 68  31
         EnhancementOperationImpl op = new EnhancementOperationImpl(_classResolver,
 69   
                 new ComponentSpecification(), inputClass, _classFactory);
 70   
 
 71  31
         w.performEnhancement(op, null);
 72   
 
 73  31
         return op.getConstructor();
 74   
     }
 75   
 
 76  34
     private ComponentConstructor getComponentConstructor(Class inputClass)
 77   
     {
 78  34
         ComponentConstructor result = (ComponentConstructor) _constructors.get(inputClass);
 79   
 
 80  34
         if (result == null)
 81   
         {
 82  32
             result = createComponentConstructor(inputClass);
 83   
 
 84  31
             _constructors.put(inputClass, result);
 85   
         }
 86   
 
 87  33
         return result;
 88   
     }
 89   
 
 90   
     /**
 91   
      * Given a particular abstract class; will create an instance of that class. A subclass is
 92   
      * created with all abstract properties filled in with ordinary implementations.
 93   
      */
 94  34
     public Object newInstance(Class abstractClass)
 95   
     {
 96  34
         ComponentConstructor constructor = getComponentConstructor(abstractClass);
 97   
 
 98  33
         try
 99   
         {
 100  33
             return constructor.newInstance();
 101   
         }
 102   
         catch (Exception ex)
 103   
         {
 104  0
             throw new ApplicationRuntimeException(ScriptMessages.unableToInstantiate(
 105   
                     abstractClass,
 106   
                     ex));
 107   
         }
 108   
     }
 109   
 
 110   
     /**
 111   
      * Creates a new instance of a given class, and then initializes properties of the instance. The
 112   
      * map contains string keys that are property names, and object values.
 113   
      */
 114  14
     public Object newInstance(Class abstractClass, Map properties)
 115   
     {
 116  14
         Object result = newInstance(abstractClass);
 117   
 
 118  14
         Iterator i = properties.entrySet().iterator();
 119   
 
 120  14
         while (i.hasNext())
 121   
         {
 122  24
             Map.Entry e = (Map.Entry) i.next();
 123   
 
 124  24
             String propertyName = (String) e.getKey();
 125   
 
 126  24
             PropertyUtils.write(result, propertyName, e.getValue());
 127   
         }
 128   
 
 129  14
         return result;
 130   
     }
 131   
 
 132   
     /**
 133   
      * A convienience (useful in test code) for invoking {@link #newInstance(Class, Map)}. The Map
 134   
      * is constructed from the properties array, which consists of alternating keys and values.
 135   
      */
 136   
 
 137  14
     public Object newInstance(Class abstractClass, Object[] properties)
 138   
     {
 139  14
         Map propertyMap = Tapestry.convertArrayToMap(properties);
 140   
 
 141  14
         return newInstance(abstractClass, propertyMap);
 142   
     }
 143   
 }