001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.test;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.tapestry.enhance.EnhanceUtils;
019    import org.apache.tapestry.enhance.EnhancementOperation;
020    import org.apache.tapestry.enhance.EnhancementWorker;
021    import org.apache.tapestry.spec.IComponentSpecification;
022    
023    /**
024     * Forces the creation of a property, overrriding an existing implementation. Allows test code to
025     * set the specification and messages properties of components.
026     * 
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    public class CreatePropertyWorker implements EnhancementWorker
031    {
032        private final String _propertyName;
033    
034        private final Location _location;
035    
036        public CreatePropertyWorker(String propertyName, Location location)
037        {
038            _propertyName = propertyName;
039            _location = location;
040        }
041    
042        public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
043        {
044            Class propertyType = EnhanceUtils.extractPropertyType(op, _propertyName, null);
045    
046            op.claimProperty(_propertyName);
047    
048            String field = "_$" + _propertyName;
049    
050            op.addField(field, propertyType);
051    
052            EnhanceUtils.createSimpleAccessor(op, field, _propertyName, propertyType, _location);
053    
054            EnhanceUtils.createSimpleMutator(op, field, _propertyName, propertyType, _location);
055        }
056    }