001    // Copyright 2004, 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    package org.apache.tapestry.enhance;
015    
016    import org.apache.tapestry.IPage;
017    import org.apache.tapestry.TestBase;
018    import org.testng.annotations.Test;
019    
020    import java.lang.reflect.Method;
021    import java.util.Map;
022    
023    
024    /**
025     * Tests functionality of {@link MethodSignature} implementations.
026     */
027    @Test
028    public class MethodSignatureTest extends TestBase {
029        
030        // used for non generic tests
031        class Simple {
032            
033            public String getValue()
034            {
035                return "foo";
036            }
037            
038            public void setMax(Integer val)
039            {
040            }
041        }
042        
043        public void test_Simple_Properties()
044        {
045            ClassInspector ins = new ClassInspectorImpl();
046            
047            MethodSignature sig = ins.getPropertyAccessor(Simple.class, "value");
048            
049            assert sig != null;
050            assertEquals(sig.getReturnType(), String.class);
051            
052            sig = ins.getPropertyAccessor(Simple.class, "max");
053            
054            assert sig != null;
055            assertEquals(sig.getReturnType(), void.class);
056            assertEquals(sig.getParameterTypes().length, 1);
057            assertEquals(sig.getParameterTypes()[0], Integer.class);
058        }
059        
060        public void test_Generic_Properties()
061        {
062            ClassInspector ins = new GenericsClassInspectorImpl();
063            
064            MethodSignature sig = ins.getPropertyAccessor(FooGenericComponent.class, "value");
065            
066            assert sig != null;
067            assertEquals(sig.getReturnType(), FooGeneric.class);
068        }
069        
070        public void test_Generic_Parameters()
071        {
072            ClassInspector ins = new GenericsClassInspectorImpl();
073            
074            MethodSignature sig = ins.getPropertyAccessor(FooGenericComponent.class, "somethingCrazy");
075            
076            assert sig != null;
077            assertEquals(sig.getReturnType(), void.class);
078            assert sig.getParameterTypes() != null && sig.getParameterTypes().length > 0;
079            assertEquals(sig.getParameterTypes()[0], FooGeneric.class);
080            
081            sig = ins.getPropertyAccessor(AbstractGenericBase.class, "somethingCrazy");
082            
083            assert sig != null;
084            assertEquals(sig.getReturnType(), void.class);
085            assert sig.getParameterTypes() != null && sig.getParameterTypes().length > 0;
086            assertEquals(sig.getParameterTypes()[0], SimpleGeneric.class);
087        }
088        
089        public void test_Generic_Simple_Property()
090        {
091            ClassInspector ins = new GenericsClassInspectorImpl();
092            
093            MethodSignature sig = ins.getPropertyAccessor(FooGenericComponent.class, "map");
094            
095            assert sig != null;
096            assertEquals(sig.getReturnType(), Map.class);
097        }
098        
099        public void test_Generic_Inheritance()
100        {
101            ClassInspector ins = new GenericsClassInspectorImpl();
102            
103            MethodSignature child = ins.getPropertyAccessor(FooGenericComponent.class, "operationValue");
104            MethodSignature base = ins.getPropertyAccessor(AbstractGenericBase.class, "operationValue");
105            
106            assert child != null;
107            assert base != null;
108            
109            assert child.isOverridingSignatureOf(base);
110            
111            assert !child.equals(base);
112            assert !base.equals(child);
113        }
114    
115        public void test_Generic_Method_Hash()
116        {
117            Class testClass = MyTest.class;
118    
119            Method[] methods = testClass.getMethods();
120    
121            for (Method method : methods) {
122                
123                MethodSignatureImpl msi = new GenericsMethodSignatureImpl(testClass, method);
124                msi.hashCode();
125            }
126    
127            ClassInspector ins = new GenericsClassInspectorImpl();
128            MethodSignature sig = ins.getPropertyAccessor(MyTest.class, "relativeObject");
129    
130            assert sig.getReturnType() != null;
131            assertEquals(sig.getReturnType(), BaseTest.class);
132        }
133    
134        public void test_Generic_Service_Property()
135                throws Exception
136        {
137            ClassInspector ins = new GenericsClassInspectorImpl();
138            MethodSignature m  = ins.getPropertyAccessor(GenericServiceImpl.class, "currentFoo");
139    
140            assertEquals(m.getReturnType(), BasicObject.class);
141        }
142    
143        public static abstract class BaseTest<T>{ }
144        
145        public static abstract class MyTest<T,E extends BaseTest<T>> extends BaseTest<T> {
146    
147            public abstract E getRelativeObject();
148            public abstract void setRelativeObject(E e);
149        }
150    
151        public void test_Find_Type()
152        {
153            Class clazz=TestGeneric.class;
154            Method[] ms = clazz.getMethods();
155            
156            for (Method m : ms) {
157    
158                MethodSignature sig = new GenericsMethodSignatureImpl(clazz, m);
159    
160                assertEquals(sig.getName(), m.getName());
161                assertEquals(sig.getReturnType(), m.getReturnType());
162                assertEquals(sig.getParameterTypes(), m.getParameterTypes());
163                assertEquals(sig.getExceptionTypes(), m.getExceptionTypes());
164                assertEquals(sig.getReturnType(), m.getReturnType());
165            }
166        }
167    
168        public class TestGeneric<T> extends BaseGeneric<T>{
169    
170        }
171        public class BaseGeneric<T> {
172            public IPage doDeleteEntityAction(T entity) {
173                return null;
174            }
175        }
176    
177    }