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.coerce.ValueConverter; 022 import org.apache.tapestry.services.ComponentPropertySource; 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 030 /** 031 * Tests for {@link org.apache.tapestry.enhance.InjectMetaWorker}. 032 * 033 * @author Howard M. Lewis Ship 034 * @since 4.0 035 */ 036 @Test 037 public class TestInjectMetaWorker extends BaseComponentTestCase 038 { 039 private InjectSpecification newSpec(String propertyName, String object, Location location) 040 { 041 InjectSpecificationImpl result = new InjectSpecificationImpl(); 042 043 result.setProperty(propertyName); 044 result.setObject(object); 045 result.setLocation(location); 046 047 return result; 048 } 049 050 private ComponentPropertySource newSource() 051 { 052 return newMock(ComponentPropertySource.class); 053 } 054 055 public void test_Primitive() 056 { 057 Location l = newLocation(); 058 InjectSpecification spec = newSpec("fooBar", "foo.bar", l); 059 060 ComponentPropertySource source = newSource(); 061 062 EnhancementOperation op = newMock(EnhancementOperation.class); 063 064 expect(op.getPropertyType("fooBar")).andReturn(int.class); 065 066 op.claimReadonlyProperty("fooBar"); 067 068 MethodSignature sig = new MethodSignature(int.class, "getFooBar", null, null); 069 070 expect(op.addInjectedField(InjectMetaWorker.SOURCE_NAME, ComponentPropertySource.class, source)) 071 .andReturn("_source"); 072 073 expect(op.getAccessorMethodName("fooBar")).andReturn("getFooBar"); 074 075 BodyBuilder builder = new BodyBuilder(); 076 builder.begin(); 077 builder.addln("java.lang.String meta = _source.getComponentProperty(this, \"foo.bar\");"); 078 builder.addln("return java.lang.Integer.parseInt(meta);"); 079 builder.end(); 080 081 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), l); 082 083 replay(); 084 085 InjectMetaWorker worker = new InjectMetaWorker(); 086 087 worker.setSource(source); 088 089 worker.performEnhancement(op, spec); 090 091 verify(); 092 } 093 094 public void test_Boolean() 095 { 096 Location l = newLocation(); 097 InjectSpecification spec = newSpec("fooBar", "foo.bar", l); 098 099 ComponentPropertySource source = newSource(); 100 101 EnhancementOperation op = newMock(EnhancementOperation.class); 102 103 expect(op.getPropertyType("fooBar")).andReturn(boolean.class); 104 105 op.claimReadonlyProperty("fooBar"); 106 107 MethodSignature sig = new MethodSignature(boolean.class, "getFooBar", null, null); 108 109 expect(op.addInjectedField(InjectMetaWorker.SOURCE_NAME, ComponentPropertySource.class, source)).andReturn("_source"); 110 111 expect(op.getAccessorMethodName("fooBar")).andReturn("getFooBar"); 112 113 BodyBuilder builder = new BodyBuilder(); 114 builder.begin(); 115 builder.addln("java.lang.String meta = _source.getComponentProperty(this, \"foo.bar\");"); 116 builder.addln("return java.lang.Boolean.valueOf(meta).booleanValue();"); 117 builder.end(); 118 119 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), l); 120 121 replay(); 122 123 InjectMetaWorker worker = new InjectMetaWorker(); 124 125 worker.setSource(source); 126 127 worker.performEnhancement(op, spec); 128 129 verify(); 130 } 131 132 public void test_Character() 133 { 134 Location l = newLocation(); 135 InjectSpecification spec = newSpec("fooBar", "foo.bar", l); 136 137 ComponentPropertySource source = newSource(); 138 139 EnhancementOperation op = newMock(EnhancementOperation.class); 140 141 expect(op.getPropertyType("fooBar")).andReturn(char.class); 142 143 op.claimReadonlyProperty("fooBar"); 144 145 MethodSignature sig = new MethodSignature(char.class, "getFooBar", null, null); 146 147 expect(op.addInjectedField(InjectMetaWorker.SOURCE_NAME, ComponentPropertySource.class, source)) 148 .andReturn("_source"); 149 150 expect(op.getAccessorMethodName("fooBar")).andReturn("getFooBar"); 151 152 BodyBuilder builder = new BodyBuilder(); 153 builder.begin(); 154 builder.addln("java.lang.String meta = _source.getComponentProperty(this, \"foo.bar\");"); 155 builder.addln("return meta.charAt(0);"); 156 builder.end(); 157 158 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), l); 159 160 replay(); 161 162 InjectMetaWorker worker = new InjectMetaWorker(); 163 164 worker.setSource(source); 165 166 worker.performEnhancement(op, spec); 167 168 verify(); 169 } 170 171 public void test_Object() 172 { 173 Location l = newLocation(); 174 InjectSpecification spec = newSpec("fooBar", "foo.bar", l); 175 176 ComponentPropertySource source = newSource(); 177 ValueConverter converter = newMock(ValueConverter.class); 178 179 EnhancementOperation op = newMock(EnhancementOperation.class); 180 181 expect(op.getPropertyType("fooBar")).andReturn(Object.class); 182 183 op.claimReadonlyProperty("fooBar"); 184 185 MethodSignature sig = new MethodSignature(Object.class, "getFooBar", null, null); 186 187 expect(op.addInjectedField(InjectMetaWorker.SOURCE_NAME, ComponentPropertySource.class, source)) 188 .andReturn("_source"); 189 190 expect(op.getAccessorMethodName("fooBar")).andReturn("getFooBar"); 191 192 expect(op.addInjectedField("_$valueConverter", ValueConverter.class, converter)) 193 .andReturn("vc"); 194 195 expect(op.getClassReference(Object.class)).andReturn("_$Object"); 196 197 BodyBuilder builder = new BodyBuilder(); 198 builder.begin(); 199 builder.addln("java.lang.String meta = _source.getComponentProperty(this, \"foo.bar\");"); 200 builder.addln("return (java.lang.Object) vc.coerceValue(meta, _$Object);"); 201 builder.end(); 202 203 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), l); 204 205 replay(); 206 207 InjectMetaWorker worker = new InjectMetaWorker(); 208 209 worker.setSource(source); 210 worker.setValueConverter(converter); 211 212 worker.performEnhancement(op, spec); 213 214 verify(); 215 } 216 217 public void test_Unimplemented_Property() 218 { 219 Location l = newLocation(); 220 InjectSpecification spec = newSpec("fooBar", "foo.bar", l); 221 222 ComponentPropertySource source = newSource(); 223 ValueConverter converter = newMock(ValueConverter.class); 224 225 EnhancementOperation op = newMock(EnhancementOperation.class); 226 227 expect(op.getPropertyType("fooBar")).andReturn(null); 228 229 op.claimReadonlyProperty("fooBar"); 230 231 MethodSignature sig = new MethodSignature(Object.class, "getFooBar", null, null); 232 233 expect(op.addInjectedField(InjectMetaWorker.SOURCE_NAME, ComponentPropertySource.class, source)).andReturn("_source"); 234 235 expect(op.getAccessorMethodName("fooBar")).andReturn("getFooBar"); 236 237 expect(op.addInjectedField("_$valueConverter", ValueConverter.class, converter)).andReturn("vc"); 238 239 expect(op.getClassReference(Object.class)).andReturn("_$Object"); 240 241 BodyBuilder builder = new BodyBuilder(); 242 builder.begin(); 243 builder.addln("java.lang.String meta = _source.getComponentProperty(this, \"foo.bar\");"); 244 builder.addln("return (java.lang.Object) vc.coerceValue(meta, _$Object);"); 245 builder.end(); 246 247 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), l); 248 249 replay(); 250 251 InjectMetaWorker worker = new InjectMetaWorker(); 252 worker.setSource(source); 253 worker.setValueConverter(converter); 254 255 worker.performEnhancement(op, spec); 256 257 verify(); 258 } 259 260 }