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.annotations; 016 017 import static org.easymock.EasyMock.eq; 018 import static org.easymock.EasyMock.expect; 019 import static org.easymock.EasyMock.isA; 020 021 import java.lang.reflect.Method; 022 import java.lang.reflect.Modifier; 023 024 import org.apache.hivemind.ApplicationRuntimeException; 025 import org.apache.hivemind.Location; 026 import org.apache.hivemind.impl.DefaultClassResolver; 027 import org.apache.hivemind.service.MethodSignature; 028 import org.apache.tapestry.IAsset; 029 import org.apache.tapestry.enhance.EnhancementOperation; 030 import org.apache.tapestry.enhance.InjectAssetWorker; 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.annotations.InjectAssetAnnotationWorker}. 037 * 038 * @author Howard M. Lewis Ship 039 * @since 4.0 040 */ 041 @Test 042 public class TestInjectAssetAnnotationWorker extends BaseAnnotationTestCase 043 { 044 public void test_Default() 045 { 046 InjectAssetAnnotationWorker worker = new InjectAssetAnnotationWorker(); 047 048 assertNotNull(worker._delegate); 049 } 050 051 public void test_Delegation() 052 { 053 Location l = newLocation(); 054 EnhancementOperation op = newOp(); 055 IComponentSpecification spec = newSpec(); 056 InjectAssetWorker delegate = new InjectAssetWorker(); 057 IAssetSpecification asset = newMock(IAssetSpecification.class); 058 059 Method m = findMethod(AnnotatedPage.class, "getStylesheetAsset"); 060 061 expect(spec.getAsset("stylesheet")).andReturn(asset); 062 063 expect(op.getPropertyType("stylesheetAsset")).andReturn(IAsset.class); 064 065 op.claimReadonlyProperty("stylesheetAsset"); 066 067 expect(op.getAccessorMethodName("stylesheetAsset")).andReturn("getStylesheetAsset"); 068 069 op.addMethod(eq(Modifier.PUBLIC), isA(MethodSignature.class), 070 eq("return getAsset(\"stylesheet\");"), eq(l)); 071 072 InjectAssetAnnotationWorker worker = new InjectAssetAnnotationWorker(delegate); 073 worker.setClassResolver(new DefaultClassResolver()); 074 075 replay(); 076 077 worker.performEnhancement(op, spec, m, l); 078 079 verify(); 080 } 081 082 public void test_Unknown_Asset() 083 { 084 Location l = newLocation(); 085 EnhancementOperation op = newOp(); 086 IComponentSpecification spec = newSpec(); 087 InjectAssetWorker delegate = new InjectAssetWorker(); 088 089 Method m = findMethod(AnnotatedPage.class, "getUnknownAsset"); 090 091 expect(spec.getAsset("homageDeFred")).andReturn(null); 092 093 InjectAssetAnnotationWorker worker = new InjectAssetAnnotationWorker(delegate); 094 worker.setClassResolver(new DefaultClassResolver()); 095 096 replay(); 097 098 try 099 { 100 worker.performEnhancement(op, spec, m, l); 101 unreachable(); 102 } 103 catch (ApplicationRuntimeException ex) 104 { 105 assertTrue(ex.getMessage().startsWith("No asset has been mapped with a name of 'homageDeFred': ")); 106 } 107 108 verify(); 109 } 110 }