This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.models.impl-1.1.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit b5304a41d88d009265c5f40afbdac9654a21a610 Author: Justin Edelson <jus...@apache.org> AuthorDate: Mon Aug 25 11:44:46 2014 +0000 SLING-3879 - fixing NPE in PathInjector. Thanks Stefan for the patch! git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1620296 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/injectors/ResourcePathInjector.java | 4 +++- .../models/impl/ResourcePathInjectionTest.java | 23 ++++++++++++++++++++++ ...odel.java => ResourcePathAllOptionalModel.java} | 12 +++++++---- .../testmodels/classes/ResourcePathModel.java | 3 ++- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java b/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java index 3bc08b9..2dbfecf 100644 --- a/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java +++ b/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java @@ -66,7 +66,9 @@ public class ResourcePathInjector extends AbstractInjector implements Injector, if (resourcePath == null && name != null) { // try to get from value map ValueMap map = getValueMap(adaptable); - resourcePath = map.get(name, String.class); + if (map != null) { + resourcePath = map.get(name, String.class); + } } } diff --git a/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java b/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java index ee0cea8..b9d4805 100644 --- a/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java +++ b/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java @@ -25,6 +25,7 @@ import java.util.HashMap; import java.util.Hashtable; import java.util.Map; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ValueMap; @@ -32,6 +33,7 @@ import org.apache.sling.api.wrappers.ValueMapDecorator; import org.apache.sling.models.impl.injectors.ResourcePathInjector; import org.apache.sling.models.impl.injectors.SelfInjector; import org.apache.sling.models.impl.injectors.ValueMapInjector; +import org.apache.sling.models.testmodels.classes.ResourcePathAllOptionalModel; import org.apache.sling.models.testmodels.classes.ResourcePathModel; import org.junit.Before; import org.junit.Test; @@ -107,4 +109,25 @@ public class ResourcePathInjectionTest { assertEquals(byPropertyValueResource2, model.getByDerefProperty2()); } + @Test + public void testPathInjectionWithNonResourceAdaptable() { + SlingHttpServletRequest nonResourceAdaptable = mock(SlingHttpServletRequest.class); + ResourcePathModel model = factory.getAdapter(nonResourceAdaptable, ResourcePathModel.class); + // should be null because mandatory fields could not be injected + assertNull(model); + } + + @Test + public void testOptionalPathInjectionWithNonResourceAdaptable() { + SlingHttpServletRequest nonResourceAdaptable = mock(SlingHttpServletRequest.class); + ResourcePathAllOptionalModel model = factory.getAdapter(nonResourceAdaptable, ResourcePathAllOptionalModel.class); + // should not be null because resource paths fields are optional + assertNotNull(model); + // but the field itself are null + assertNull(model.getFromPath()); + assertNull(model.getByDerefProperty()); + assertNull(model.getFromPath2()); + assertNull(model.getByDerefProperty2()); + } + } \ No newline at end of file diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathAllOptionalModel.java similarity index 81% copy from src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModel.java copy to src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathAllOptionalModel.java index d18d4fe..d3e97cb 100644 --- a/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModel.java +++ b/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathAllOptionalModel.java @@ -21,26 +21,30 @@ package org.apache.sling.models.testmodels.classes; import javax.inject.Inject; import javax.inject.Named; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.Model; +import org.apache.sling.models.annotations.Optional; import org.apache.sling.models.annotations.Path; import org.apache.sling.models.annotations.injectorspecific.ResourcePath; -@Model(adaptables = Resource.class) -public class ResourcePathModel { +@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}) +public class ResourcePathAllOptionalModel { @Inject @Path("/some/path") + @Optional private Resource fromPath; @Inject @Named("propertyContainingAPath") + @Optional private Resource derefProperty; - @ResourcePath(path = "/some/path2") + @ResourcePath(path = "/some/path2", optional=true) private Resource fromPath2; - @ResourcePath(name = "anotherPropertyContainingAPath") + @ResourcePath(name = "anotherPropertyContainingAPath", optional=true) private Resource derefProperty2; public Resource getFromPath() { diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModel.java index d18d4fe..ec23150 100644 --- a/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModel.java +++ b/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModel.java @@ -21,12 +21,13 @@ package org.apache.sling.models.testmodels.classes; import javax.inject.Inject; import javax.inject.Named; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.Path; import org.apache.sling.models.annotations.injectorspecific.ResourcePath; -@Model(adaptables = Resource.class) +@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}) public class ResourcePathModel { @Inject -- To stop receiving notification emails like this one, please contact "commits@sling.apache.org" <commits@sling.apache.org>.