1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.ErrorLog; 25 import org.apache.hivemind.Location; 26 import org.apache.hivemind.service.BodyBuilder; 27 import org.apache.hivemind.service.MethodSignature; 28 import org.apache.hivemind.test.HiveMindTestCase; 29 import org.apache.tapestry.BaseComponent; 30 import org.apache.tapestry.IBinding; 31 import org.apache.tapestry.IComponent; 32 import org.apache.tapestry.binding.BindingSource; 33 import org.apache.tapestry.event.PageDetachListener; 34 import org.apache.tapestry.spec.IComponentSpecification; 35 import org.apache.tapestry.spec.IPropertySpecification; 36 import org.apache.tapestry.spec.PropertySpecification; 37 import org.easymock.MockControl; 38 39 45 public class TestSpecifiedPropertyWorker extends HiveMindTestCase 46 { 47 private List buildPropertySpecs(String name, String type, boolean persistent) 48 { 49 return buildPropertySpecs(name, type, persistent, null, null); 50 } 51 52 private List buildPropertySpecs(String name, String type, boolean persistent, 53 Location location, String initialValue) 54 { 55 PropertySpecification ps = new PropertySpecification(); 56 ps.setName(name); 57 ps.setType(type); 58 ps.setPersistence(persistent ? "session" : null); 59 ps.setInitialValue(initialValue); 60 ps.setLocation(location); 61 62 return Collections.singletonList(ps); 63 } 64 65 private IComponentSpecification buildComponentSpecification(List propertySpecs) 66 { 67 List names = new ArrayList (); 68 Iterator i = propertySpecs.iterator(); 69 while (i.hasNext()) 70 { 71 IPropertySpecification ps = (IPropertySpecification) i.next(); 72 73 names.add(ps.getName()); 74 } 75 76 MockControl c = newControl(IComponentSpecification.class); 77 IComponentSpecification result = (IComponentSpecification) c.getMock(); 78 79 result.getPropertySpecificationNames(); 80 c.setReturnValue(names); 81 82 i = propertySpecs.iterator(); 83 while (i.hasNext()) 84 { 85 IPropertySpecification ps = (IPropertySpecification) i.next(); 86 87 result.getPropertySpecification(ps.getName()); 88 c.setReturnValue(ps); 89 } 90 91 return result; 92 } 93 94 private IComponentSpecification buildComponentSpecification(String name, String type, 95 boolean persistent) 96 { 97 return buildComponentSpecification(buildPropertySpecs(name, type, persistent)); 98 } 99 100 public void testAddNormal() throws Exception 101 { 102 IComponentSpecification spec = buildComponentSpecification("fred", "boolean", false); 103 104 106 MockControl opc = newControl(EnhancementOperation.class); 107 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 108 109 op.convertTypeName("boolean"); 110 opc.setReturnValue(boolean.class); 111 112 op.validateProperty("fred", boolean.class); 113 op.claimProperty("fred"); 114 op.addField("_$fred", boolean.class); 115 116 op.getAccessorMethodName("fred"); 117 opc.setReturnValue("isFred"); 118 119 op.addMethod( 120 Modifier.PUBLIC, 121 new MethodSignature(boolean.class, "isFred", null, null), 122 "return _$fred;"); 123 124 op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setFred", new Class [] 125 { boolean.class }, null), "{\n _$fred = $1;\n}\n"); 126 127 op.addField("_$fred$default", boolean.class); 128 129 op.extendMethodImplementation( 130 IComponent.class, 131 EnhanceUtils.FINISH_LOAD_SIGNATURE, 132 "_$fred$default = _$fred;"); 133 134 op.extendMethodImplementation( 135 PageDetachListener.class, 136 EnhanceUtils.PAGE_DETACHED_SIGNATURE, 137 "_$fred = _$fred$default;"); 138 139 replayControls(); 140 141 SpecifiedPropertyWorker w = new SpecifiedPropertyWorker(); 142 143 w.performEnhancement(op, spec); 144 145 verifyControls(); 146 } 147 148 public void testAddWithInitialValue() throws Exception 149 { 150 BindingSource bs = (BindingSource) newMock(BindingSource.class); 151 Location l = fabricateLocation(12); 152 153 IComponentSpecification spec = buildComponentSpecification(buildPropertySpecs( 154 "fred", 155 "java.util.List", 156 false, 157 l, 158 "ognl:foo()")); 159 160 InitialValueBindingCreator expectedCreator = new InitialValueBindingCreator(bs, 161 EnhanceMessages.initialValueForProperty("fred"), "ognl:foo()", l); 162 163 165 MockControl opc = newControl(EnhancementOperation.class); 166 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 167 168 op.convertTypeName("java.util.List"); 169 opc.setReturnValue(List .class); 170 171 op.validateProperty("fred", List .class); 172 op.claimProperty("fred"); 173 op.addField("_$fred", List .class); 174 175 op.getAccessorMethodName("fred"); 176 opc.setReturnValue("getFred"); 177 178 op.addMethod( 179 Modifier.PUBLIC, 180 new MethodSignature(List .class, "getFred", null, null), 181 "return _$fred;"); 182 183 op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setFred", new Class [] 184 { List .class }, null), "{\n _$fred = $1;\n}\n"); 185 186 op.addInjectedField( 187 "_$fred$initialValueBindingCreator", 188 InitialValueBindingCreator.class, 189 expectedCreator); 190 opc.setReturnValue("_$fred$initialValueBindingCreator"); 191 op.addField("_$fred$initialValueBinding", IBinding.class); 192 op 193 .extendMethodImplementation( 194 IComponent.class, 195 EnhanceUtils.FINISH_LOAD_SIGNATURE, 196 "_$fred$initialValueBinding = _$fred$initialValueBindingCreator.createBinding(this);\n"); 197 198 op.getClassReference(List .class); 199 opc.setReturnValue("_$class$java$util$List"); 200 201 String code = "_$fred = (java.util.List) _$fred$initialValueBinding.getObject(_$class$java$util$List);\n"; 202 203 op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code); 204 op.extendMethodImplementation( 205 PageDetachListener.class, 206 EnhanceUtils.PAGE_DETACHED_SIGNATURE, 207 code); 208 209 replayControls(); 210 211 SpecifiedPropertyWorker w = new SpecifiedPropertyWorker(); 212 w.setBindingSource(bs); 213 214 w.performEnhancement(op, spec); 215 216 verifyControls(); 217 } 218 219 public void testAddPersistent() throws Exception 220 { 221 IComponentSpecification spec = buildComponentSpecification( 222 "barney", 223 "java.lang.String", 224 true); 225 226 228 MockControl opc = newControl(EnhancementOperation.class); 229 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 230 231 op.convertTypeName("java.lang.String"); 232 opc.setReturnValue(String .class); 233 234 op.validateProperty("barney", String .class); 235 op.claimProperty("barney"); 236 op.addField("_$barney", String .class); 237 238 op.getAccessorMethodName("barney"); 239 opc.setReturnValue("getBarney"); 240 241 op.addMethod( 242 Modifier.PUBLIC, 243 new MethodSignature(String .class, "getBarney", null, null), 244 "return _$barney;"); 245 246 BodyBuilder b = new BodyBuilder(); 247 b.begin(); 248 b.addln("org.apache.tapestry.Tapestry#fireObservedChange(this, \"barney\", ($w) $1);"); 249 b.addln("_$barney = $1;"); 250 b.end(); 251 252 op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setBarney", new Class [] 253 { String .class }, null), b.toString()); 254 255 op.addField("_$barney$default", String .class); 256 257 op.extendMethodImplementation( 258 IComponent.class, 259 EnhanceUtils.FINISH_LOAD_SIGNATURE, 260 "_$barney$default = _$barney;"); 261 262 op.extendMethodImplementation( 263 PageDetachListener.class, 264 EnhanceUtils.PAGE_DETACHED_SIGNATURE, 265 "_$barney = _$barney$default;"); 266 267 replayControls(); 268 269 SpecifiedPropertyWorker w = new SpecifiedPropertyWorker(); 270 271 w.performEnhancement(op, spec); 272 273 verifyControls(); 274 } 275 276 public void testFailure() throws Exception 277 { 278 Location l = fabricateLocation(207); 279 List propertySpecs = buildPropertySpecs("wilma", "Long", false, l, null); 281 IComponentSpecification spec = buildComponentSpecification(propertySpecs); 282 283 MockControl opc = newControl(EnhancementOperation.class); 284 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 285 286 op.convertTypeName("Long"); 287 Throwable ex = new ApplicationRuntimeException("Simulated error."); 288 opc.setThrowable(ex); 289 290 op.getBaseClass(); 291 opc.setReturnValue(BaseComponent.class); 292 293 ErrorLog log = (ErrorLog) newMock(ErrorLog.class); 294 295 log 296 .error( 297 "Error adding property wilma to class org.apache.tapestry.BaseComponent: Simulated error.", 298 l, 299 ex); 300 301 replayControls(); 302 303 SpecifiedPropertyWorker w = new SpecifiedPropertyWorker(); 304 w.setErrorLog(log); 305 306 w.performEnhancement(op, spec); 307 308 verifyControls(); 309 } 310 311 } | Popular Tags |