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.annotations;
016    
017    import java.lang.reflect.Method;
018    
019    import org.apache.hivemind.ErrorLog;
020    import org.apache.hivemind.Location;
021    import org.apache.hivemind.Resource;
022    import org.apache.hivemind.util.ClasspathResource;
023    import org.apache.tapestry.BaseComponentTestCase;
024    import org.apache.tapestry.enhance.EnhancementOperation;
025    import org.apache.tapestry.spec.IComponentSpecification;
026    
027    /**
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    
032    public class BaseAnnotationTestCase extends BaseComponentTestCase
033    {
034    
035        protected Method findMethod(Class clazz, String name)
036        {
037            for (Method m : clazz.getMethods())
038            {
039                if (m.getName().equals(name))
040                    return m;
041            }
042    
043            throw new IllegalArgumentException("No method " + name + " in " + clazz);
044        }
045    
046        protected IComponentSpecification newSpec()
047        {
048            return newMock(IComponentSpecification.class);
049        }
050    
051        protected EnhancementOperation newOp()
052        {
053            return newMock(EnhancementOperation.class);
054        }
055    
056        protected Resource newResource(Class clazz)
057        {
058            return new ClasspathResource(getClassResolver(), clazz.getName().replace('.', '/'));
059        }
060    
061        protected ErrorLog newErrorLog()
062        {
063            return newMock(ErrorLog.class);
064        }
065    
066        protected Location newMethodLocation(Class baseClass, Method m, Class annotationClass)
067        {
068            Resource classResource = newResource(baseClass);
069        
070            return AnnotationUtils.buildLocationForAnnotation(
071                    m,
072                    m.getAnnotation(annotationClass),
073                    classResource);
074        }
075    }