001 package org.apache.tapestry.resolver; 002 003 import org.apache.hivemind.Location; 004 import org.apache.hivemind.Resource; 005 import org.apache.hivemind.impl.DefaultClassResolver; 006 import org.apache.hivemind.util.ClasspathResource; 007 import org.apache.tapestry.*; 008 import org.apache.tapestry.asset.AssetFactory; 009 import org.apache.tapestry.spec.ComponentSpecification; 010 import org.apache.tapestry.spec.IComponentSpecification; 011 import org.apache.tapestry.web.WebContext; 012 import org.apache.tapestry.web.WebContextResource; 013 import static org.easymock.EasyMock.checkOrder; 014 import static org.easymock.EasyMock.expect; 015 import org.testng.annotations.Test; 016 017 import java.net.URL; 018 import java.util.Locale; 019 020 /** 021 * Tests functionality of {@link ComponentResourceResolverImpl}. 022 */ 023 @Test 024 public class TestComponentResourceResolver extends TestBase { 025 026 public void test_Context_Spec_Resource() 027 { 028 IComponent comp = newMock(IComponent.class); 029 checkOrder(comp, false); 030 WebContext context = newMock(WebContext.class); 031 IRequestCycle cycle = newMock(IRequestCycle.class); 032 033 IComponentSpecification spec = new ComponentSpecification(); 034 WebContextResource base = new WebContextResource(context, "/WEB-INF/MyComponent.jwc"); 035 spec.setSpecificationLocation(base); 036 037 ComponentResourceResolverImpl resolver = new ComponentResourceResolverImpl(); 038 039 expect(comp.getSpecification()).andReturn(spec).anyTimes(); 040 expect(context.getResource("/WEB-INF/MyComponent.html")).andReturn(newURL()); 041 042 replay(); 043 044 Resource resolved = resolver.findComponentResource(comp, cycle, null, ".html", null); 045 assert resolved != null; 046 assert resolved.getResourceURL() != null; 047 048 verify(); 049 } 050 051 public void test_Context_Spec_Localized_Resource() 052 { 053 IComponent comp = newMock(IComponent.class); 054 checkOrder(comp, false); 055 WebContext context = newMock(WebContext.class); 056 IRequestCycle cycle = newMock(IRequestCycle.class); 057 058 IComponentSpecification spec = new ComponentSpecification(); 059 WebContextResource base = new WebContextResource(context, "/WEB-INF/MyComponent.jwc"); 060 spec.setSpecificationLocation(base); 061 062 ComponentResourceResolverImpl resolver = new ComponentResourceResolverImpl(); 063 064 expect(comp.getSpecification()).andReturn(spec).anyTimes(); 065 expect(context.getResource("/WEB-INF/MyComponent_en.html")).andReturn(newURL()).anyTimes(); 066 expect(context.getResource("/WEB-INF/MyComponent_en_US.html")).andReturn(null); 067 068 replay(); 069 070 Resource resolved = resolver.findComponentResource(comp, cycle, null, ".html", Locale.US); 071 assert resolved != null; 072 assert resolved.getResourceURL() != null; 073 074 verify(); 075 } 076 077 public void test_Classpath_Spec_Resource_App_Context_Resolved() 078 { 079 IComponent comp = newMock(IComponent.class); 080 checkOrder(comp, false); 081 082 INamespace namespace = newMock(INamespace.class); 083 IRequestCycle cycle = newMock(IRequestCycle.class); 084 085 IComponentSpecification spec = new ComponentSpecification(); 086 ClasspathResource base = new ClasspathResource(new DefaultClassResolver(), "/org/apache/tapestry/resolver/MyComponent.jwc"); 087 spec.setSpecificationLocation(base); 088 spec.setComponentClassName("org.apache.tapestry.resolver.MyComponent"); 089 090 AssetFactory classpathFactory = newMock(AssetFactory.class); 091 AssetFactory contextFactory = newMock(AssetFactory.class); 092 Resource contextRoot = newMock(Resource.class); 093 Resource webinfLocation = newMock(Resource.class); 094 Resource webinfAppLocation = newMock(Resource.class); 095 096 ComponentResourceResolverImpl resolver = new ComponentResourceResolverImpl(); 097 resolver.setApplicationId("foo"); 098 resolver.setClasspathAssetFactory(classpathFactory); 099 resolver.setContextAssetFactory(contextFactory); 100 resolver.setContextRoot(contextRoot); 101 102 expect(contextRoot.getRelativeResource("WEB-INF/")).andReturn(webinfLocation); 103 expect(webinfLocation.getRelativeResource("foo/")).andReturn(webinfAppLocation); 104 105 expect(comp.getSpecification()).andReturn(spec).anyTimes(); 106 expect(comp.getNamespace()).andReturn(namespace); 107 expect(namespace.getPropertyValue("org.apache.tapestry.component-class-packages")).andReturn("org.apache.tapestry.resolver"); 108 109 Location l = newMock(Location.class); 110 IAsset asset = newMock(IAsset.class); 111 Resource resource = newMock(Resource.class); 112 113 expect(contextFactory.assetExists(spec, webinfAppLocation, "MyComponent.html", null)).andReturn(true); 114 expect(comp.getLocation()).andReturn(l); 115 expect(contextFactory.createAsset(webinfAppLocation, spec, "MyComponent.html", null, l)).andReturn(asset); 116 expect(asset.getResourceLocation()).andReturn(resource); 117 118 replay(); 119 120 resolver.initializeService(); 121 122 Resource resolved = resolver.findComponentResource(comp, cycle, null, ".html", null); 123 assertEquals(resolved, resource); 124 125 verify(); 126 } 127 128 public void test_Classpath_Spec_Resource_WebInf_Context_Resolved() 129 { 130 IComponent comp = newMock(IComponent.class); 131 checkOrder(comp, false); 132 133 INamespace namespace = newMock(INamespace.class); 134 IRequestCycle cycle = newMock(IRequestCycle.class); 135 136 IComponentSpecification spec = new ComponentSpecification(); 137 ClasspathResource base = new ClasspathResource(new DefaultClassResolver(), "/org/apache/tapestry/resolver/MyComponent.jwc"); 138 spec.setSpecificationLocation(base); 139 spec.setComponentClassName("org.apache.tapestry.resolver.MyComponent"); 140 141 AssetFactory classpathFactory = newMock(AssetFactory.class); 142 AssetFactory contextFactory = newMock(AssetFactory.class); 143 Resource contextRoot = newMock(Resource.class); 144 Resource webinfLocation = newMock(Resource.class); 145 Resource webinfAppLocation = newMock(Resource.class); 146 147 ComponentResourceResolverImpl resolver = new ComponentResourceResolverImpl(); 148 resolver.setApplicationId("foo"); 149 resolver.setClasspathAssetFactory(classpathFactory); 150 resolver.setContextAssetFactory(contextFactory); 151 resolver.setContextRoot(contextRoot); 152 153 expect(contextRoot.getRelativeResource("WEB-INF/")).andReturn(webinfLocation); 154 expect(webinfLocation.getRelativeResource("foo/")).andReturn(webinfAppLocation); 155 156 expect(comp.getSpecification()).andReturn(spec).anyTimes(); 157 expect(comp.getNamespace()).andReturn(namespace); 158 expect(namespace.getPropertyValue("org.apache.tapestry.component-class-packages")).andReturn("org.apache.tapestry.resolver"); 159 160 Location l = newMock(Location.class); 161 IAsset asset = newMock(IAsset.class); 162 Resource resource = newMock(Resource.class); 163 164 expect(contextFactory.assetExists(spec, webinfAppLocation, "MyComponent.html", null)).andReturn(false); 165 expect(contextFactory.assetExists(spec, webinfLocation, "MyComponent.html", null)).andReturn(true); 166 expect(comp.getLocation()).andReturn(l); 167 expect(contextFactory.createAsset(webinfLocation, spec, "MyComponent.html", null, l)).andReturn(asset); 168 expect(asset.getResourceLocation()).andReturn(resource); 169 170 replay(); 171 172 resolver.initializeService(); 173 174 Resource resolved = resolver.findComponentResource(comp, cycle, null, ".html", null); 175 assertEquals(resolved, resource); 176 177 verify(); 178 } 179 180 public void test_Classpath_Spec_Resource_WebInf_Package_Name_Context_Resolved() 181 { 182 IComponent comp = newMock(IComponent.class); 183 checkOrder(comp, false); 184 185 INamespace namespace = newMock(INamespace.class); 186 IRequestCycle cycle = newMock(IRequestCycle.class); 187 188 IComponentSpecification spec = new ComponentSpecification(); 189 ClasspathResource base = new ClasspathResource(new DefaultClassResolver(), "/org/apache/tapestry/resolver/MyComponent.jwc"); 190 spec.setSpecificationLocation(base); 191 spec.setComponentClassName("org.apache.tapestry.resolver.MyComponent"); 192 193 AssetFactory classpathFactory = newMock(AssetFactory.class); 194 AssetFactory contextFactory = newMock(AssetFactory.class); 195 Resource contextRoot = newMock(Resource.class); 196 Resource webinfLocation = newMock(Resource.class); 197 Resource webinfAppLocation = newMock(Resource.class); 198 199 ComponentResourceResolverImpl resolver = new ComponentResourceResolverImpl(); 200 resolver.setApplicationId("foo"); 201 resolver.setClasspathAssetFactory(classpathFactory); 202 resolver.setContextAssetFactory(contextFactory); 203 resolver.setContextRoot(contextRoot); 204 205 expect(contextRoot.getRelativeResource("WEB-INF/")).andReturn(webinfLocation); 206 expect(webinfLocation.getRelativeResource("foo/")).andReturn(webinfAppLocation); 207 208 expect(comp.getSpecification()).andReturn(spec).anyTimes(); 209 expect(comp.getNamespace()).andReturn(namespace); 210 expect(namespace.getPropertyValue("org.apache.tapestry.component-class-packages")).andReturn("org.apache.tapestry.resolver"); 211 212 Location l = newMock(Location.class); 213 IAsset asset = newMock(IAsset.class); 214 Resource resource = newMock(Resource.class); 215 216 expect(contextFactory.assetExists(spec, webinfAppLocation, "MyComponent.html", null)).andReturn(false); 217 expect(contextFactory.assetExists(spec, webinfLocation, "MyComponent.html", null)).andReturn(false); 218 expect(classpathFactory.assetExists(spec, base, "MyComponent.html", null)).andReturn(false); 219 220 expect(contextFactory.assetExists(spec, webinfAppLocation, "resolver/MyComponent.html", null)).andReturn(false); 221 expect(contextFactory.assetExists(spec, webinfLocation, "resolver/MyComponent.html", null)).andReturn(true); 222 223 expect(comp.getLocation()).andReturn(l); 224 expect(contextFactory.createAsset(webinfLocation, spec, "resolver/MyComponent.html", null, l)).andReturn(asset); 225 expect(asset.getResourceLocation()).andReturn(resource); 226 227 replay(); 228 229 resolver.initializeService(); 230 231 Resource resolved = resolver.findComponentResource(comp, cycle, null, ".html", null); 232 assertEquals(resolved, resource); 233 234 verify(); 235 } 236 237 public void test_Classpath_Spec_Resource_Classpath_Resolved() 238 { 239 IComponent comp = newMock(IComponent.class); 240 checkOrder(comp, false); 241 242 INamespace namespace = newMock(INamespace.class); 243 IRequestCycle cycle = newMock(IRequestCycle.class); 244 245 IComponentSpecification spec = new ComponentSpecification(); 246 ClasspathResource base = new ClasspathResource(new DefaultClassResolver(), "/org/apache/tapestry/resolver/MyComponent.jwc"); 247 spec.setSpecificationLocation(base); 248 spec.setComponentClassName("org.apache.tapestry.resolver.MyComponent"); 249 250 AssetFactory classpathFactory = newMock(AssetFactory.class); 251 AssetFactory contextFactory = newMock(AssetFactory.class); 252 Resource contextRoot = newMock(Resource.class); 253 Resource webinfLocation = newMock(Resource.class); 254 Resource webinfAppLocation = newMock(Resource.class); 255 256 ComponentResourceResolverImpl resolver = new ComponentResourceResolverImpl(); 257 resolver.setApplicationId("foo"); 258 resolver.setClasspathAssetFactory(classpathFactory); 259 resolver.setContextAssetFactory(contextFactory); 260 resolver.setContextRoot(contextRoot); 261 262 expect(contextRoot.getRelativeResource("WEB-INF/")).andReturn(webinfLocation); 263 expect(webinfLocation.getRelativeResource("foo/")).andReturn(webinfAppLocation); 264 265 expect(comp.getSpecification()).andReturn(spec).anyTimes(); 266 expect(comp.getNamespace()).andReturn(namespace); 267 expect(namespace.getPropertyValue("org.apache.tapestry.component-class-packages")).andReturn("org.apache.tapestry.resolver"); 268 269 Location l = newMock(Location.class); 270 IAsset asset = newMock(IAsset.class); 271 Resource resource = newMock(Resource.class); 272 273 expect(contextFactory.assetExists(spec, webinfAppLocation, "MyComponent.html", null)).andReturn(false); 274 expect(contextFactory.assetExists(spec, webinfLocation, "MyComponent.html", null)).andReturn(false); 275 expect(classpathFactory.assetExists(spec, base, "MyComponent.html", null)).andReturn(true); 276 277 expect(comp.getLocation()).andReturn(l); 278 expect(classpathFactory.createAsset(base, spec, "MyComponent.html", null, l)).andReturn(asset); 279 expect(asset.getResourceLocation()).andReturn(resource); 280 281 replay(); 282 283 resolver.initializeService(); 284 285 Resource resolved = resolver.findComponentResource(comp, cycle, null, ".html", null); 286 assertEquals(resolved, resource); 287 288 verify(); 289 } 290 291 // Returns the same URL object pointing to any arbitrary test classpath resource 292 public URL newURL() 293 { 294 return this.getClass().getResource("MyComponent.jwc"); 295 } 296 }