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 015 package org.apache.tapestry.enhance; 016 017 import static org.easymock.EasyMock.expect; 018 019 import java.lang.reflect.Modifier; 020 import java.util.Collections; 021 022 import org.apache.hivemind.ApplicationRuntimeException; 023 import org.apache.hivemind.ErrorLog; 024 import org.apache.hivemind.Location; 025 import org.apache.hivemind.service.MethodSignature; 026 import org.apache.tapestry.BaseComponent; 027 import org.apache.tapestry.IComponent; 028 import org.apache.tapestry.event.PageDetachListener; 029 import org.apache.tapestry.spec.IComponentSpecification; 030 import org.testng.annotations.Test; 031 032 /** 033 * Tests {@link org.apache.tapestry.enhance.AbstractPropertyWorker}. 034 * 035 * @author Howard M. Lewis Ship 036 * @since 4.0 037 */ 038 @Test 039 public class TestAbstractPropertyWorker extends BaseEnhancementTestCase 040 { 041 042 public void testSuccess() 043 { 044 Location l = newLocation(); 045 046 IComponentSpecification spec = newSpec(l); 047 EnhancementOperation op = newOp(); 048 049 expect(op.findUnclaimedAbstractProperties()).andReturn(Collections.singletonList("fred")); 050 051 expect(op.getPropertyType("fred")).andReturn(String.class); 052 053 op.addField("_$fred", String.class); 054 op.addField("_$fred$defaultValue", String.class); 055 056 expect(op.getAccessorMethodName("fred")).andReturn("getFred"); 057 058 op.addMethod( 059 Modifier.PUBLIC, 060 new MethodSignature(String.class, "getFred", null, null), 061 "return _$fred;", 062 l); 063 064 op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "setFred", new Class[] 065 { String.class }, null), "_$fred = $1;", l); 066 067 op.extendMethodImplementation( 068 IComponent.class, 069 EnhanceUtils.FINISH_LOAD_SIGNATURE, 070 "_$fred$defaultValue = _$fred;"); 071 072 op.extendMethodImplementation( 073 PageDetachListener.class, 074 EnhanceUtils.PAGE_DETACHED_SIGNATURE, 075 "_$fred = _$fred$defaultValue;"); 076 077 op.claimProperty("fred"); 078 079 replay(); 080 081 new AbstractPropertyWorker().performEnhancement(op, spec); 082 083 verify(); 084 } 085 086 public void testFailure() 087 { 088 EnhancementOperation op = newOp(); 089 Location l = fabricateLocation(21); 090 091 IComponentSpecification spec = newSpec(l); 092 093 ErrorLog log = newMock(ErrorLog.class); 094 095 expect(op.findUnclaimedAbstractProperties()).andReturn(Collections.singletonList("fred")); 096 Throwable ex = new ApplicationRuntimeException("Arbitrary error."); 097 098 expect(op.getPropertyType("fred")).andThrow(ex); 099 100 expect(op.getBaseClass()).andReturn(BaseComponent.class); 101 102 log.error(EnhanceMessages.errorAddingProperty("fred", BaseComponent.class, ex), l, ex); 103 104 replay(); 105 106 AbstractPropertyWorker w = new AbstractPropertyWorker(); 107 w.setErrorLog(log); 108 109 w.performEnhancement(op, spec); 110 111 verify(); 112 } 113 }