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 org.apache.hivemind.ApplicationRuntimeException; 018 import org.testng.annotations.Test; 019 020 import java.lang.reflect.Method; 021 022 /** 023 * Tests for {@link org.apache.tapestry.annotations.AnnotationUtils}. 024 * 025 * @author Howard M. Lewis Ship 026 * @since 4.0 027 */ 028 @Test 029 public class TestAnnotationUtils extends BaseAnnotationTestCase 030 { 031 private String attemptGetPropertyName(Class clazz, String name) 032 { 033 Method m = findMethod(clazz, name); 034 035 return AnnotationUtils.getPropertyName(m); 036 } 037 038 public void testGetPropertyName() 039 { 040 assertEquals("stringValue", attemptGetPropertyName(TargetValues.class, "getStringValue")); 041 assertEquals("intValue", attemptGetPropertyName(TargetValues.class, "setIntValue")); 042 assertEquals("booleanValue", attemptGetPropertyName(TargetValues.class, "isBooleanValue")); 043 } 044 045 public void testGetPropertyNameNotAGetter() 046 { 047 try 048 { 049 attemptGetPropertyName(TargetValues.class, "notAGetter"); 050 unreachable(); 051 } 052 catch (ApplicationRuntimeException ex) 053 { 054 assertEquals( 055 "Annotated method public abstract java.lang.String org.apache.tapestry.annotations.TargetValues.notAGetter() " 056 + "should be an accessor (no parameters), or a mutator (single parameter, returns void).", 057 ex.getMessage()); 058 } 059 } 060 061 public void testGetPropertyNameSetterNoParameters() 062 { 063 try 064 { 065 attemptGetPropertyName(TargetValues.class, "setNoParameters"); 066 unreachable(); 067 } 068 catch (ApplicationRuntimeException ex) 069 { 070 assertEquals( 071 "Annotated method public abstract void org.apache.tapestry.annotations.TargetValues.setNoParameters() is named like a mutator method," 072 + " but takes an incorrect number of parameters (it should have exactly one parameter).", 073 ex.getMessage()); 074 } 075 } 076 077 public void testNonVoidSetter() 078 { 079 try 080 { 081 attemptGetPropertyName(TargetValues.class, "setNonVoidMethod"); 082 unreachable(); 083 } 084 catch (ApplicationRuntimeException ex) 085 { 086 assertEquals( 087 "Annotated method public abstract java.lang.String org.apache.tapestry.annotations.TargetValues.setNonVoidMethod(java.lang.String) " 088 + "is named like a mutator method, but does not return void.", 089 ex.getMessage()); 090 } 091 } 092 093 public void testGetterWithParameters() 094 { 095 try 096 { 097 attemptGetPropertyName(TargetValues.class, "getHasParameters"); 098 unreachable(); 099 } 100 catch (ApplicationRuntimeException ex) 101 { 102 assertEquals( 103 "Annotated method public abstract java.lang.String org.apache.tapestry.annotations.TargetValues.getHasParameters(java.lang.String) " 104 + "is expected to be an accessor, and should have no parameters.", 105 ex.getMessage()); 106 } 107 } 108 109 public void testGetterReturnsVoid() 110 { 111 try 112 { 113 attemptGetPropertyName(TargetValues.class, "isVoidGetter"); 114 unreachable(); 115 } 116 catch (ApplicationRuntimeException ex) 117 { 118 assertEquals( 119 "Annotated method public abstract void org.apache.tapestry.annotations.TargetValues.isVoidGetter() " 120 + "is named like an accessor method, but returns void.", 121 ex.getMessage()); 122 } 123 } 124 125 public void testConvertMethodNameToKeyName() 126 { 127 assertEquals("foo-bar", AnnotationUtils.convertMethodNameToKeyName("fooBar")); 128 assertEquals("foo-bar", AnnotationUtils.convertMethodNameToKeyName("FooBar")); 129 assertEquals("foo-bar", AnnotationUtils.convertMethodNameToKeyName("getFooBar")); 130 assertEquals("foo", AnnotationUtils.convertMethodNameToKeyName("foo")); 131 } 132 133 }