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.ApplicationRuntimeException;
020    import org.apache.hivemind.Location;
021    import org.apache.tapestry.enhance.EnhancementOperation;
022    import org.apache.tapestry.spec.ComponentSpecification;
023    import org.apache.tapestry.spec.IComponentSpecification;
024    import org.apache.tapestry.spec.IParameterSpecification;
025    import static org.easymock.EasyMock.expect;
026    import org.testng.annotations.Test;
027    
028    /**
029     * Tests for {@link org.apache.tapestry.annotations.ParameterAnnotationWorker}.
030     * 
031     * @author Howard Lewis Ship
032     * @since 4.0
033     */
034    @Test
035    public class TestParameterAnnotationWorker extends BaseAnnotationTestCase
036    {
037        private IParameterSpecification attempt(String propertyName, Location location)
038        {
039            return attempt(propertyName, propertyName, location);
040        }
041    
042        private IParameterSpecification attempt(String propertyName, String parameterName,
043                Location location)
044        {
045            Method m = findMethod(AnnotatedComponent.class, "get"
046                    + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
047            
048            EnhancementOperation op = newMock(EnhancementOperation.class);
049    
050            expect(op.getPropertyType(propertyName)).andReturn(m.getReturnType());
051            
052            IComponentSpecification spec = new ComponentSpecification();
053    
054            replay();
055    
056            new ParameterAnnotationWorker().performEnhancement(op, spec, m, location);
057    
058            verify();
059    
060            return spec.getParameter(parameterName);
061        }
062    
063        public void testSimple()
064        {
065            Location l = newLocation();
066    
067            IParameterSpecification ps = attempt("simpleParameter", l);
068    
069            assertListEquals(new Object[] {}, ps.getAliasNames().toArray());
070            assertEquals(true, ps.getCache());
071            assertEquals(null, ps.getDefaultValue());
072            assertEquals(null, ps.getDescription());
073            assertSame(l, ps.getLocation());
074            assertEquals("simpleParameter", ps.getParameterName());
075            assertEquals("simpleParameter", ps.getPropertyName());
076            assertEquals("java.lang.String", ps.getType());
077        }
078    
079        public void testRequired()
080        {
081            IParameterSpecification ps = attempt("requiredParameter", null);
082    
083            assertEquals(true, ps.isRequired());
084        }
085    
086        public void testCacheOff()
087        {
088            IParameterSpecification ps = attempt("nonCachedParameter", null);
089    
090            assertEquals(false, ps.getCache());
091        }
092    
093        public void testAliases()
094        {
095            IParameterSpecification ps = attempt("aliasedParameter", null);
096    
097            assertListEquals(new String[]{ "fred" }, ps.getAliasNames().toArray());
098        }
099    
100        public void testDeprecated()
101        {
102            IParameterSpecification ps = attempt("deprecatedParameter", null);
103            assertEquals(true, ps.isDeprecated());
104        }
105    
106        public void testNamed()
107        {
108            IParameterSpecification ps = attempt("namedParameter", "fred", null);
109    
110            assertEquals("fred", ps.getParameterName());
111            assertEquals("namedParameter", ps.getPropertyName());
112        }
113    
114        public void testDefaultValue()
115        {
116            IParameterSpecification ps = attempt("defaultValue", null);
117    
118            assertEquals("myDefault", ps.getDefaultValue());
119        }
120        
121        public void testParameterNotAllowed()
122        {
123            Method m = findMethod(AnnotatedPage.class, "getSimpleParameter");
124            
125            EnhancementOperation op = newMock(EnhancementOperation.class);
126    
127            IComponentSpecification spec = new ComponentSpecification();
128    
129            replay();
130    
131            try
132            {
133                new ParameterAnnotationWorker().performEnhancement(op, spec, m, null);
134                unreachable();
135            }
136            catch (ApplicationRuntimeException ex)
137            {            
138            }
139    
140            verify();        
141        }
142    
143    }