1 15 package org.apache.tapestry.annotations; 16 17 import java.lang.reflect.Method ; 18 import java.lang.reflect.Modifier ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.Messages; 22 import org.apache.hivemind.impl.DefaultClassResolver; 23 import org.apache.hivemind.service.MethodSignature; 24 import org.apache.hivemind.service.impl.ClassFactoryImpl; 25 import org.apache.tapestry.enhance.EnhanceUtils; 26 import org.apache.tapestry.enhance.EnhancementOperation; 27 import org.apache.tapestry.enhance.EnhancementOperationImpl; 28 import org.apache.tapestry.services.ComponentConstructor; 29 import org.apache.tapestry.spec.ComponentSpecification; 30 import org.apache.tapestry.spec.IComponentSpecification; 31 import org.easymock.MockControl; 32 import org.easymock.internal.ArrayMatcher; 33 34 public class TestMessageAnnotationWorker extends BaseAnnotationTestCase 35 { 36 public void testConvertMethodNameToKeyName() 37 { 38 MessageAnnotationWorker w = new MessageAnnotationWorker(); 39 40 assertEquals("foo-bar", w.convertMethodNameToKeyName("fooBar")); 41 assertEquals("foo-bar", w.convertMethodNameToKeyName("FooBar")); 42 assertEquals("foo-bar", w.convertMethodNameToKeyName("getFooBar")); 43 assertEquals("foo", w.convertMethodNameToKeyName("foo")); 44 } 45 46 public void testNoArgsMessage() 47 { 48 attempt("noArgsMessage", "{\n return getMessages().getMessage(\"no-args-message\");\n}\n"); 49 } 50 51 public void testMessageWithSpecificKey() 52 { 53 attempt( 54 "messageWithSpecificKey", 55 "{\n return getMessages().getMessage(\"message-key\");\n}\n"); 56 } 57 58 public void testMessageWithParameters() 59 { 60 attempt("messageWithParameters", "{\n" 61 + " java.lang.Object[] params = new java.lang.Object[2];\n" 62 + " params[0] = $1;\n" + " params[1] = $2;\n" 63 + " return getMessages().format(\"message-with-parameters\", params);\n}\n"); 64 } 65 66 public void testMessageWithPrimitiveParameters() 67 { 68 attempt("messageWithPrimitives", "{\n" 69 + " java.lang.Object[] params = new java.lang.Object[2];\n" 70 + " params[0] = ($w) $1;\n" + " params[1] = ($w) $2;\n" 71 + " return getMessages().format(\"message-with-primitives\", params);\n}\n"); 72 } 73 74 public void testNotStringReturnType() 75 { 76 EnhancementOperation op = newOp(); 77 IComponentSpecification spec = newSpec(); 78 79 Method method = findMethod(AnnotatedPage.class, "voidMessage"); 80 81 replayControls(); 82 83 try 84 { 85 new MessageAnnotationWorker().performEnhancement(op, spec, method, null); 86 unreachable(); 87 } 88 catch (ApplicationRuntimeException ex) 89 { 90 assertEquals( 91 "The method's return type is void; this annotation is only allowed on methods that return java.lang.String.", 92 ex.getMessage()); 93 } 94 95 verifyControls(); 96 97 } 98 99 public void testSetterIsClaimed() 100 { 101 EnhancementOperation op = newOp(); 102 IComponentSpecification spec = newSpec(); 103 104 Method method = findMethod(AnnotatedPage.class, "getLikeGetter"); 105 106 op.addMethod( 107 Modifier.PUBLIC, 108 new MethodSignature(method), 109 "{\n return getMessages().getMessage(\"like-getter\");\n}\n"); 110 op.claimProperty("likeGetter"); 111 112 replayControls(); 113 114 new MessageAnnotationWorker().performEnhancement(op, spec, method, null); 115 116 verifyControls(); 117 } 118 119 private void attempt(String methodName, String codeBlock) 120 { 121 EnhancementOperation op = newOp(); 122 IComponentSpecification spec = newSpec(); 123 124 Method method = findMethod(AnnotatedPage.class, methodName); 125 126 op.addMethod(Modifier.PUBLIC, new MethodSignature(method), codeBlock); 127 128 replayControls(); 129 130 new MessageAnnotationWorker().performEnhancement(op, spec, method, null); 131 132 verifyControls(); 133 } 134 135 private Object construct(Class baseClass, String methodName, Messages messages) 136 { 137 ComponentSpecification spec = new ComponentSpecification(); 138 EnhancementOperationImpl op = new EnhancementOperationImpl(getClassResolver(), spec, 139 baseClass, new ClassFactoryImpl()); 140 141 op.addInjectedField("_messages", Messages.class, messages); 142 143 EnhanceUtils.createSimpleAccessor(op, "_messages", "messages", Messages.class); 144 145 Method method = findMethod(baseClass, methodName); 146 147 new MessageAnnotationWorker().performEnhancement(op, spec, method, null); 148 149 ComponentConstructor cc = op.getConstructor(); 150 151 return cc.newInstance(); 152 } 153 154 public void testNoParams() 155 { 156 MockControl control = newControl(Messages.class); 157 Messages messages = (Messages) control.getMock(); 158 159 messages.getMessage("no-params"); 160 control.setReturnValue("<no params>"); 161 162 MessagesTarget mt = (MessagesTarget) construct(MessagesTarget.class, "noParams", messages); 163 164 replayControls(); 165 166 assertEquals("<no params>", mt.noParams()); 167 168 verifyControls(); 169 } 170 171 public void testObjectParam() 172 { 173 MockControl control = newControl(Messages.class); 174 Messages messages = (Messages) control.getMock(); 175 176 Object [] params = new Object [] 177 { "PinkFloyd" }; 178 179 messages.format("object-param", params); 180 control.setMatcher(new ArrayMatcher()); 181 control.setReturnValue("<object param>"); 182 183 MessagesTarget mt = (MessagesTarget) construct( 184 MessagesTarget.class, 185 "objectParam", 186 messages); 187 188 replayControls(); 189 190 assertEquals("<object param>", mt.objectParam("PinkFloyd")); 191 192 verifyControls(); 193 } 194 195 public void testPrimitiveParam() 196 { 197 198 MockControl control = newControl(Messages.class); 199 Messages messages = (Messages) control.getMock(); 200 201 Object [] params = new Object [] 202 { 451 }; 203 204 messages.format("primitive-param", params); 205 control.setMatcher(new ArrayMatcher()); 206 control.setReturnValue("<primitive param>"); 207 208 MessagesTarget mt = (MessagesTarget) construct( 209 MessagesTarget.class, 210 "primitiveParam", 211 messages); 212 213 replayControls(); 214 215 assertEquals("<primitive param>", mt.primitiveParam(451)); 216 217 verifyControls(); 218 219 } 220 221 public void testInvalidBindings() 222 { 223 invalidBinding("no equals"); 224 invalidBinding("= at start"); 225 invalidBinding("equals at end="); 226 } 227 228 private void invalidBinding(String binding) 229 { 230 try 231 { 232 new ComponentAnnotationWorker().addBinding(null, binding, null); 233 unreachable(); 234 } 235 catch (ApplicationRuntimeException ex) 236 { 237 assertEquals(AnnotationMessages.bindingWrongFormat(binding), ex.getMessage()); 238 } 239 } 240 } 241 | Popular Tags |