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 static org.easymock.EasyMock.expect; 018 import static org.easymock.EasyMock.expectLastCall; 019 020 import java.lang.reflect.Modifier; 021 import java.util.Collections; 022 023 import org.apache.hivemind.ApplicationRuntimeException; 024 import org.apache.hivemind.ErrorLog; 025 import org.apache.hivemind.Location; 026 import org.apache.hivemind.service.MethodSignature; 027 import org.apache.tapestry.BaseComponent; 028 import org.apache.tapestry.IAsset; 029 import org.apache.tapestry.IComponent; 030 import org.apache.tapestry.spec.AssetSpecification; 031 import org.apache.tapestry.spec.IAssetSpecification; 032 import org.apache.tapestry.spec.IComponentSpecification; 033 import org.testng.annotations.Test; 034 035 /** 036 * Tests for {@link org.apache.tapestry.enhance.InjectAssetWorker}. 037 * 038 * @author Howard M. Lewis Ship 039 * @since 4.0 040 */ 041 @Test 042 public class InjectAssetWorkerTest extends BaseEnhancementTestCase 043 { 044 private IComponentSpecification newSpec(String assetName, String propertyName, Location location) 045 { 046 IAssetSpecification as = new AssetSpecification(); 047 as.setPropertyName(propertyName); 048 as.setLocation(location); 049 050 IComponentSpecification spec = newSpec(); 051 052 expect(spec.getAssetNames()).andReturn(Collections.singletonList(assetName)); 053 054 expect(spec.getAsset(assetName)).andReturn(as); 055 056 return spec; 057 } 058 059 public void testNoWork() 060 { 061 IComponentSpecification spec = newSpec("fred", null, null); 062 EnhancementOperation op = newEnhancementOp(); 063 064 replay(); 065 066 new InjectAssetWorker().performEnhancement(op, spec); 067 068 verify(); 069 } 070 071 public void testSuccess() 072 { 073 Location l = newLocation(); 074 IComponentSpecification spec = newSpec("fred", "barney", l); 075 EnhancementOperation op = newEnhancementOp(); 076 077 trainGetPropertyType(op, "barney", IAsset.class); 078 079 op.claimReadonlyProperty("barney"); 080 081 trainGetAccessorMethodName(op, "barney", "getBarney"); 082 083 op.addMethod( 084 Modifier.PUBLIC, 085 new MethodSignature(IAsset.class, "getBarney", null, null), 086 "return getAsset(\"fred\");", 087 l); 088 089 replay(); 090 091 new InjectAssetWorker().performEnhancement(op, spec); 092 093 verify(); 094 } 095 096 public void testFailure() 097 { 098 Location l = newLocation(); 099 Throwable ex = new ApplicationRuntimeException(EnhanceMessages.claimedProperty("barney")); 100 EnhancementOperation op = newEnhancementOp(); 101 102 IComponentSpecification spec = newSpec("fred", "barney", l); 103 104 ErrorLog log = newMock(ErrorLog.class); 105 106 trainGetPropertyType(op, "barney", IComponent.class); 107 108 op.claimReadonlyProperty("barney"); 109 expectLastCall().andThrow(ex); 110 111 trainGetBaseClass(op, BaseComponent.class); 112 113 log.error(EnhanceMessages.errorAddingProperty("barney", BaseComponent.class, ex), l, ex); 114 115 replay(); 116 117 InjectAssetWorker w = new InjectAssetWorker(); 118 119 w.setErrorLog(log); 120 121 w.performEnhancement(op, spec); 122 123 verify(); 124 } 125 126 public void testWrongPropertyType() 127 { 128 EnhancementOperation op = newEnhancementOp(); 129 130 trainGetPropertyType(op, "barney", IComponent.class); 131 132 op.claimReadonlyProperty("barney"); 133 134 replay(); 135 136 InjectAssetWorker w = new InjectAssetWorker(); 137 try 138 { 139 w.injectAsset(op, "fred", "barney", null); 140 unreachable(); 141 } 142 catch (ApplicationRuntimeException ex) 143 { 144 assertEquals( 145 "Property barney is type org.apache.tapestry.IComponent, which is not compatible with the expected type, org.apache.tapestry.IAsset.", 146 ex.getMessage()); 147 } 148 149 verify(); 150 151 } 152 }