SLING-5726 allow both ValueMapInjector and ResourcePathInjector to act on adaptable...
authorKonrad Windszus <kwin@apache.org>
Fri, 13 May 2016 10:35:03 +0000 (10:35 +0000)
committerKonrad Windszus <kwin@apache.org>
Fri, 13 May 2016 10:35:03 +0000 (10:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1743648 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/injectors/AbstractInjector.java
src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java
src/main/java/org/apache/sling/models/impl/injectors/ValueMapInjector.java
src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java
src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathAllOptionalModel.java

index 8ef3797..cce4938 100644 (file)
@@ -44,12 +44,30 @@ abstract class AbstractInjector {
         return resolver;
     }
 
+    /** 
+     * Retrieve the ValueMap from the given adaptable. This succeeds, if the adaptable is either
+     * <ul>
+     * <li>a {@link ValueMap},</li>
+     * <li>a {@link SlingHttpServletRequest}, in which case the returned {@link ValueMap} is the one derived from the request's resource or</li>
+     * <li>adaptable to a {@link ValueMap}.</li>
+     * </ul>
+     * Otherwise {@code null} is returned.
+     * @param adaptable
+     * @return a ValueMap or {@code null}.
+     */
     protected ValueMap getValueMap(Object adaptable) {
         if (adaptable instanceof ValueMap) {
             return (ValueMap) adaptable;
+        } else if (adaptable instanceof SlingHttpServletRequest) {
+            final Resource resource = ((SlingHttpServletRequest)adaptable).getResource();
+            // resource may be null for mocked adaptables, therefore do a check here
+            if (resource != null) {
+                return resource.adaptTo(ValueMap.class);
+            } else {
+                return null;
+            }
         } else if (adaptable instanceof Adaptable) {
-            ValueMap map = ((Adaptable) adaptable).adaptTo(ValueMap.class);
-            return map;
+            return ((Adaptable) adaptable).adaptTo(ValueMap.class);
         } else {
             return null;
         }
index 330ba5b..ebc7db0 100644 (file)
@@ -84,6 +84,7 @@ public class ResourcePathInjector extends AbstractInjector implements Injector,
 
         ResourceResolver resolver = getResourceResolver(adaptable);
         if (resolver == null) {
+            LOG.debug("Could not get resolver from adaptable {}", adaptable);
             return null;
         }
         List<Resource> resources = getResources(resolver, resourcePaths, name);
index c76a553..bbb1e07 100644 (file)
@@ -169,12 +169,7 @@ public class ValueMapInjector extends AbstractInjector implements Injector, Inje
             if (StringUtils.isNotBlank(annotation.via())) {
                 return annotation.via();
             }
-            // automatically go via resource, if this is the httprequest
-            if (adaptable instanceof SlingHttpServletRequest) {
-                return "resource";
-            } else {
-                return null;
-            }
+            return null;
         }
         
         @Override
index 6a3e6ab..681ccd5 100644 (file)
@@ -57,10 +57,10 @@ public class ResourcePathInjectionTest {
     private ModelAdapterFactory factory;
 
     @Mock
-    private Resource adaptable;
+    private Resource adaptableResource;
     
     @Mock
-    SlingHttpServletRequest nonResourceAdaptable;
+    SlingHttpServletRequest adaptableRequest;
 
     @Mock
     private Resource byPathResource;
@@ -98,13 +98,16 @@ public class ResourcePathInjectionTest {
         when(componentCtx.getBundleContext()).thenReturn(bundleContext);
         when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
 
-        when(adaptable.getResourceResolver()).thenReturn(resourceResolver);
-        when(adaptable.adaptTo(ValueMap.class)).thenReturn(properties);
+        when(adaptableResource.getResourceResolver()).thenReturn(resourceResolver);
+        when(adaptableResource.adaptTo(ValueMap.class)).thenReturn(properties);
 
         when(resourceResolver.getResource("/some/path")).thenReturn(byPathResource);
         when(resourceResolver.getResource("/some/path2")).thenReturn(byPathResource2);
         when(resourceResolver.getResource("/some/other/path")).thenReturn(byPropertyValueResource);
         when(resourceResolver.getResource("/some/other/path2")).thenReturn(byPropertyValueResource2);
+        
+        when(adaptableRequest.getResource()).thenReturn(byPathResource);
+        when(adaptableRequest.getResourceResolver()).thenReturn(resourceResolver);
 
         factory = new ModelAdapterFactory();
         factory.activate(componentCtx);
@@ -116,8 +119,8 @@ public class ResourcePathInjectionTest {
     }
 
     @Test
-    public void testPathInjection() {
-        ResourcePathModel model = factory.getAdapter(adaptable, ResourcePathModel.class);
+    public void testPathInjectionFromResource() {
+        ResourcePathModel model = factory.getAdapter(adaptableResource, ResourcePathModel.class);
         assertNotNull(model);
         assertEquals(byPathResource, model.getFromPath());
         assertEquals(byPropertyValueResource, model.getByDerefProperty());
@@ -126,15 +129,20 @@ public class ResourcePathInjectionTest {
     }
 
     @Test
-    public void testPathInjectionWithNonResourceAdaptable() {
-        ResourcePathModel model = factory.getAdapter(nonResourceAdaptable, ResourcePathModel.class);
-        // should be null because mandatory fields could not be injected
-        assertNull(model);
+    public void testPathInjectionFromRequest() {
+        // return the same properties through this request's resource, as through adaptableResource
+        doReturn(adaptableResource.adaptTo(ValueMap.class)).when(byPathResource).adaptTo(ValueMap.class);
+        ResourcePathModel model = factory.getAdapter(adaptableRequest, ResourcePathModel.class);
+        assertNotNull(model);
+        assertEquals(byPathResource, model.getFromPath());
+        assertEquals(byPropertyValueResource, model.getByDerefProperty());
+        assertEquals(byPathResource2, model.getFromPath2());
+        assertEquals(byPropertyValueResource2, model.getByDerefProperty2());
     }
 
     @Test
     public void testOptionalPathInjectionWithNonResourceAdaptable() {
-        ResourcePathAllOptionalModel model = factory.getAdapter(nonResourceAdaptable, ResourcePathAllOptionalModel.class);
+        ResourcePathAllOptionalModel model = factory.getAdapter(adaptableRequest, ResourcePathAllOptionalModel.class);
         // should not be null because resource paths fields are optional
         assertNotNull(model);
         // but the field itself are null
@@ -146,7 +154,7 @@ public class ResourcePathInjectionTest {
 
     @Test
     public void testMultiplePathInjection() {
-        ResourcePathModel model = factory.getAdapter(adaptable, ResourcePathModel.class);
+        ResourcePathModel model = factory.getAdapter(adaptableResource, ResourcePathModel.class);
         assertNotNull(model);
         List<Resource> resources=model.getMultipleResources();
         assertNotNull(resources);
@@ -172,16 +180,16 @@ public class ResourcePathInjectionTest {
     public void testPartialInjectionFailure1() {
         when(resourceResolver.getResource("/some/other/path")).thenReturn(null);
         
-        ResourcePathPartialModel model = factory.getAdapter(adaptable, ResourcePathPartialModel.class);
+        ResourcePathPartialModel model = factory.getAdapter(adaptableResource, ResourcePathPartialModel.class);
         assertNull(model);
     }
 
     @Test
-    public void testPartialInjectionFailure2() {       
+    public void testPartialInjectionFailure2() {
         when(resourceResolver.getResource("/some/other/path")).thenReturn(null);
         when(resourceResolver.getResource("/some/other/path2")).thenReturn(null);
         
-        ResourcePathPartialModel model = factory.getAdapter(adaptable, ResourcePathPartialModel.class);
+        ResourcePathPartialModel model = factory.getAdapter(adaptableResource, ResourcePathPartialModel.class);
         assertNull(model);
     }
 
index a7dcb62..38fc7f9 100644 (file)
@@ -32,7 +32,7 @@ import org.apache.sling.models.annotations.injectorspecific.ResourcePath;
 public class ResourcePathAllOptionalModel {
 
     @Inject
-    @Path("/some/path")
+    @Path("/some/invalidpath")
     @Optional
     private Resource fromPath;
 
@@ -42,11 +42,11 @@ public class ResourcePathAllOptionalModel {
     private Resource derefProperty;
     
     @Inject
-    @Path(paths={"/some/path", "/some/path2"})
+    @Path(paths={"/some/invalidpath", "/some/invalidpath2"})
     @Optional
     private Resource manyFromPathNonList;
 
-    @ResourcePath(path = "/some/path2", optional=true)
+    @ResourcePath(path = "/some/invalidpath2", optional=true)
     private Resource fromPath2;
 
     @ResourcePath(name = "anotherPropertyContainingAPath", optional=true)