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.enhance; 016 017 import org.apache.hivemind.Location; 018 import org.apache.hivemind.service.BodyBuilder; 019 import org.apache.hivemind.service.MethodSignature; 020 import org.apache.tapestry.BaseComponentTestCase; 021 import org.apache.tapestry.engine.state.ApplicationStateManager; 022 import org.apache.tapestry.event.PageDetachListener; 023 import org.apache.tapestry.spec.InjectSpecification; 024 import org.apache.tapestry.spec.InjectSpecificationImpl; 025 import static org.easymock.EasyMock.expect; 026 import org.testng.annotations.Test; 027 028 import java.lang.reflect.Modifier; 029 import java.util.Map; 030 031 /** 032 * Tests for {@link org.apache.tapestry.enhance.InjectStateWorker}. 033 * 034 * @author Howard M. Lewis Ship 035 * @since 4.0 036 */ 037 @Test 038 public class TestInjectStateWorker extends BaseComponentTestCase 039 { 040 private ApplicationStateManager newASM() 041 { 042 return newMock(ApplicationStateManager.class); 043 } 044 045 private InjectSpecification newSpec(String propertyName, String objectName, Location l) 046 { 047 InjectSpecification spec = new InjectSpecificationImpl(); 048 049 spec.setProperty(propertyName); 050 spec.setObject(objectName); 051 spec.setLocation(l); 052 053 return spec; 054 } 055 056 public void testSuccess() 057 { 058 Location l = newLocation(); 059 InjectSpecification spec = newSpec("fred", "barney", l); 060 061 EnhancementOperation op = newMock(EnhancementOperation.class); 062 063 ApplicationStateManager asm = newASM(); 064 065 expect(op.getPropertyType("fred")).andReturn(Map.class); 066 067 op.claimProperty("fred"); 068 op.addField("_$fred", Map.class); 069 070 expect(op.addInjectedField("_$applicationStateManager", ApplicationStateManager.class, asm)) 071 .andReturn("_$applicationStateManager"); 072 073 expect(op.getAccessorMethodName("fred")).andReturn("getFred"); 074 075 BodyBuilder builder = new BodyBuilder(); 076 077 // Accessor 078 079 builder.begin(); 080 builder.addln("if (_$fred == null)"); 081 builder.addln(" _$fred = (java.util.Map) _$applicationStateManager.get(\"barney\");"); 082 builder.addln("return _$fred;"); 083 builder.end(); 084 085 MethodSignature sig = new MethodSignature(Map.class, "getFred", null, null); 086 087 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), l); 088 089 builder.clear(); 090 builder.begin(); 091 builder.addln("_$applicationStateManager.store(\"barney\", $1);"); 092 builder.addln("_$fred = $1;"); 093 builder.end(); 094 095 sig = new MethodSignature(void.class, "setFred", new Class[] 096 { Map.class }, null); 097 098 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), l); 099 op.extendMethodImplementation( 100 PageDetachListener.class, 101 EnhanceUtils.PAGE_DETACHED_SIGNATURE, 102 "_$fred = null;"); 103 104 replay(); 105 106 InjectStateWorker w = new InjectStateWorker(); 107 w.setApplicationStateManager(asm); 108 109 w.performEnhancement(op, spec); 110 111 verify(); 112 } 113 }