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.ArrayList; 022 import java.util.Collections; 023 024 import org.apache.hivemind.ApplicationRuntimeException; 025 import org.apache.hivemind.ErrorLog; 026 import org.apache.hivemind.Location; 027 import org.apache.hivemind.service.MethodSignature; 028 import org.apache.tapestry.BaseComponent; 029 import org.apache.tapestry.BaseComponentTestCase; 030 import org.apache.tapestry.spec.BeanSpecification; 031 import org.apache.tapestry.spec.IBeanSpecification; 032 import org.apache.tapestry.spec.IComponentSpecification; 033 import org.testng.annotations.Test; 034 035 /** 036 * Tests for {@link org.apache.tapestry.enhance.InjectBeanWorker}. 037 * 038 * @author Howard M. Lewis Ship 039 * @since 4.0 040 */ 041 @Test 042 public class TestInjectBeanWorker extends BaseComponentTestCase 043 { 044 private IComponentSpecification newSpec(String beanName, String propertyName, Location location) 045 { 046 IBeanSpecification bs = new BeanSpecification(); 047 bs.setPropertyName(propertyName); 048 bs.setLocation(location); 049 050 IComponentSpecification spec = newSpec(); 051 052 expect(spec.getBeanNames()).andReturn(Collections.singletonList(beanName)); 053 054 expect(spec.getBeanSpecification(beanName)).andReturn(bs); 055 056 return spec; 057 } 058 059 private EnhancementOperation newOp() 060 { 061 return newMock(EnhancementOperation.class); 062 } 063 064 public void testNoWork() 065 { 066 IComponentSpecification spec = newSpec("fred", null, null); 067 EnhancementOperation op = newMock(EnhancementOperation.class); 068 069 replay(); 070 071 new InjectBeanWorker().performEnhancement(op, spec); 072 073 verify(); 074 } 075 076 public void testSuccess() 077 { 078 Location l = newLocation(); 079 IComponentSpecification spec = newSpec("fred", "barney", l); 080 081 EnhancementOperation op = newOp(); 082 083 op.claimReadonlyProperty("barney"); 084 085 expect(op.getPropertyType("barney")).andReturn(ArrayList.class); 086 087 expect(op.getAccessorMethodName("barney")).andReturn("getBarney"); 088 089 op.addMethod( 090 Modifier.PUBLIC, 091 new MethodSignature(ArrayList.class, "getBarney", null, null), 092 "return (java.util.ArrayList) getBeans().getBean(\"fred\");", 093 l); 094 095 replay(); 096 097 new InjectBeanWorker().performEnhancement(op, spec); 098 099 verify(); 100 } 101 102 public void testFailure() 103 { 104 Location l = fabricateLocation(99); 105 Throwable ex = new ApplicationRuntimeException(EnhanceMessages.claimedProperty("barney")); 106 107 EnhancementOperation op = newOp(); 108 109 IComponentSpecification spec = newSpec("fred", "barney", l); 110 111 ErrorLog log = newMock(ErrorLog.class); 112 113 op.claimReadonlyProperty("barney"); 114 expectLastCall().andThrow(ex); 115 116 expect(op.getBaseClass()).andReturn(BaseComponent.class); 117 118 log.error(EnhanceMessages.errorAddingProperty("barney", BaseComponent.class, ex), l, ex); 119 120 replay(); 121 122 InjectBeanWorker w = new InjectBeanWorker(); 123 124 w.setErrorLog(log); 125 126 w.performEnhancement(op, spec); 127 128 verify(); 129 } 130 131 }