Coverage Report - org.apache.tapestry.test.Creator
 
Classes in this File Line Coverage Branch Coverage Complexity
Creator
0%
0/46
0%
0/14
3.2
 
 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.ArrayList;
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.apache.hivemind.ApplicationRuntimeException;
 24  
 import org.apache.hivemind.ClassResolver;
 25  
 import org.apache.hivemind.Location;
 26  
 import org.apache.hivemind.Resource;
 27  
 import org.apache.hivemind.impl.DefaultClassResolver;
 28  
 import org.apache.hivemind.service.ClassFactory;
 29  
 import org.apache.hivemind.service.impl.ClassFactoryImpl;
 30  
 import org.apache.hivemind.util.ClasspathResource;
 31  
 import org.apache.hivemind.util.PropertyUtils;
 32  
 import org.apache.tapestry.Tapestry;
 33  
 import org.apache.tapestry.enhance.AbstractPropertyWorker;
 34  
 import org.apache.tapestry.enhance.EnhancementOperationImpl;
 35  
 import org.apache.tapestry.enhance.EnhancementWorker;
 36  
 import org.apache.tapestry.enhance.InjectRenderWorker;
 37  
 import org.apache.tapestry.services.ComponentConstructor;
 38  
 import org.apache.tapestry.services.ComponentRenderWorker;
 39  
 import org.apache.tapestry.spec.ComponentSpecification;
 40  
 import org.apache.tapestry.spec.IComponentSpecification;
 41  
 import org.apache.tapestry.util.DescribedLocation;
 42  
 
 43  
 /**
 44  
  * A utility class that is used to instantiate abstract Tapestry pages and components. It creates,
 45  
  * at runtime, a subclass where all abstract properties are filled in (each property complete with
 46  
  * an instance variable, an accessor method and a mutator method). This isn't quite the same as how
 47  
  * the class is enhanced at runtime (though it does use a subset of the same
 48  
  * {@link org.apache.tapestry.enhance.EnhancementWorker code}), but is sufficient to unit test the
 49  
  * class, especially listener methods.
 50  
  * <p>
 51  
  * One part of the enhancement is that the
 52  
  * {@link org.apache.tapestry.IComponent#getSpecification() specification}&nbsp;and
 53  
  * {@link org.apache.tapestry.IComponent#getMessages() messages}&nbsp;properties of the page or
 54  
  * component class are converted into read/write properties that can be set via reflection
 55  
  * (including {@link #newInstance(Class, Map)}.
 56  
  * 
 57  
  * @author Howard Lewis Ship
 58  
  * @since 4.0
 59  
  */
 60  0
 public class Creator
 61  
 {
 62  
     /**
 63  
      * Keyed on Class, value is an {@link ComponentConstructor}.
 64  
      */
 65  0
     private final Map _constructors = new HashMap();
 66  
 
 67  0
     private final ClassFactory _classFactory = new ClassFactoryImpl();
 68  
 
 69  0
     private final ClassResolver _classResolver = new DefaultClassResolver();
 70  
 
 71  0
     private final List _workers = new ArrayList();
 72  
 
 73  0
     private final Resource _creatorResource = new ClasspathResource(_classResolver,
 74  
             "/CreatorLocation");
 75  
 
 76  0
     private final Location _creatorLocation = new DescribedLocation(_creatorResource,
 77  
             "Creator Location");
 78  
     
 79  0
     private final ComponentRenderWorker _renderWorker = new MockComponentRenderWorker();
 80  
     
 81  0
     private final InjectRenderWorker _injectRender = new InjectRenderWorker();
 82  
     
 83  
     {
 84  
         
 85  0
         _injectRender.setRenderWorker(_renderWorker);
 86  
         
 87  
         // Overrride AbstractComponent's implementations of
 88  
         // these two properties (making them read/write).
 89  
 
 90  0
         _workers.add(new CreatePropertyWorker("messages", _creatorLocation));
 91  0
         _workers.add(new CreatePropertyWorker("specification", _creatorLocation));
 92  0
         _workers.add(_injectRender);
 93  
         
 94  
         // Implement any abstract properties.
 95  
         // Note that we don't bother setting the errorLog property
 96  
         // so failures may turn into NPEs.
 97  
         
 98  0
         _workers.add(new AbstractPropertyWorker());
 99  0
     }
 100  
 
 101  
     private ComponentConstructor createComponentConstructor(Class inputClass)
 102  
     {
 103  0
         if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray())
 104  0
             throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));
 105  
 
 106  0
         EnhancementOperationImpl op = new EnhancementOperationImpl(_classResolver,
 107  
                 new ComponentSpecification(), inputClass, _classFactory, null);
 108  
 
 109  0
         IComponentSpecification spec = new ComponentSpecification();
 110  0
         spec.setLocation(_creatorLocation);
 111  
 
 112  0
         Iterator i = _workers.iterator();
 113  0
         while (i.hasNext())
 114  
         {
 115  0
             EnhancementWorker worker = (EnhancementWorker) i.next();
 116  
 
 117  0
             worker.performEnhancement(op, spec);
 118  0
         }
 119  
 
 120  0
         return op.getConstructor();
 121  
     }
 122  
 
 123  
     private ComponentConstructor getComponentConstructor(Class inputClass)
 124  
     {
 125  0
         ComponentConstructor result = (ComponentConstructor) _constructors.get(inputClass);
 126  
 
 127  0
         if (result == null)
 128  
         {
 129  0
             result = createComponentConstructor(inputClass);
 130  
 
 131  0
             _constructors.put(inputClass, result);
 132  
         }
 133  
 
 134  0
         return result;
 135  
     }
 136  
 
 137  
     /**
 138  
      * Given a particular abstract class; will create an instance of that class. A subclass is
 139  
      * created with all abstract properties filled in with ordinary implementations.
 140  
      */
 141  
     public Object newInstance(Class abstractClass)
 142  
     {
 143  0
         ComponentConstructor constructor = getComponentConstructor(abstractClass);
 144  
 
 145  
         try
 146  
         {
 147  0
             return constructor.newInstance();
 148  
         }
 149  0
         catch (Exception ex)
 150  
         {
 151  0
             throw new ApplicationRuntimeException(ScriptMessages.unableToInstantiate(
 152  
                     abstractClass,
 153  
                     ex));
 154  
         }
 155  
     }
 156  
 
 157  
     /**
 158  
      * Creates a new instance of a given class, and then initializes properties of the instance. The
 159  
      * map contains string keys that are property names, and object values.
 160  
      */
 161  
     public Object newInstance(Class abstractClass, Map properties)
 162  
     {
 163  0
         Object result = newInstance(abstractClass);
 164  
 
 165  0
         if (properties != null)
 166  
         {
 167  0
             Iterator i = properties.entrySet().iterator();
 168  
 
 169  0
             while (i.hasNext())
 170  
             {
 171  0
                 Map.Entry e = (Map.Entry) i.next();
 172  
 
 173  0
                 String propertyName = (String) e.getKey();
 174  
 
 175  0
                 PropertyUtils.write(result, propertyName, e.getValue());
 176  0
             }
 177  
         }
 178  
 
 179  0
         return result;
 180  
     }
 181  
 
 182  
     /**
 183  
      * A convienience (useful in test code) for invoking {@link #newInstance(Class, Map)}. The Map
 184  
      * is constructed from the properties array, which consists of alternating keys and values.
 185  
      */
 186  
 
 187  
     public Object newInstance(Class abstractClass, Object[] properties)
 188  
     {
 189  0
         Map propertyMap = Tapestry.convertArrayToMap(properties);
 190  
 
 191  0
         return newInstance(abstractClass, propertyMap);
 192  
     }
 193  
 }