You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2016/11/30 17:47:18 UTC

svn commit: r1772094 - in /sling/trunk/bundles/extensions/models/impl/src: main/java/org/apache/sling/models/impl/injectors/ test/java/org/apache/sling/models/impl/ test/java/org/apache/sling/models/testmodels/classes/

Author: justin
Date: Wed Nov 30 17:47:18 2016
New Revision: 1772094

URL: http://svn.apache.org/viewvc?rev=1772094&view=rev
Log:
SLING-6341 - fix array support for resource path injector with patch from Santiago Garc�a Pimentel

This closes #186

Added:
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModelWrapping.java
Modified:
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java

Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java?rev=1772094&r1=1772093&r2=1772094&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java Wed Nov 30 17:47:18 2016
@@ -21,6 +21,7 @@ package org.apache.sling.models.impl.inj
 import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import javax.annotation.Nonnull;
@@ -91,10 +92,13 @@ public class ResourcePathInjector extend
         if (resources == null || resources.isEmpty()) {
             return null;
         }
-        // unwrap if necessary
+        // unwrap/wrap if necessary
         if (isDeclaredTypeCollection(declaredType)) {
             return resources;
-        } else if (resources.size() == 1) {
+        } if (declaredType instanceof Class<?> && ((Class<?>)declaredType).isArray()){
+            return resources.toArray(new Resource[0]);
+        }
+         if (resources.size() == 1) {
             return resources.get(0);
         } else {
             // multiple resources to inject, but field is not a list

Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java?rev=1772094&r1=1772093&r2=1772094&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java Wed Nov 30 17:47:18 2016
@@ -36,6 +36,7 @@ import org.apache.sling.models.impl.inje
 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.apache.sling.models.testmodels.classes.ResourcePathModelWrapping;
 import org.apache.sling.models.testmodels.classes.ResourcePathPartialModel;
 import org.junit.Before;
 import org.junit.Test;
@@ -112,7 +113,8 @@ public class ResourcePathInjectionTest {
         factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(2, 2000));
         factory.bindInjector(new ResourcePathInjector(), new ServicePropertiesMap(3, 2500));
         factory.bindStaticInjectAnnotationProcessorFactory(new ResourcePathInjector(), new ServicePropertiesMap(3, 2500));
-        factory.adapterImplementations.addClassesAsAdapterAndImplementation(ResourcePathModel.class, ResourcePathPartialModel.class, ResourcePathAllOptionalModel.class);
+        factory.adapterImplementations.addClassesAsAdapterAndImplementation(ResourcePathModel.class, ResourcePathPartialModel.class,
+                ResourcePathAllOptionalModel.class, ResourcePathModelWrapping.class);
     }
 
     @Test
@@ -185,4 +187,12 @@ public class ResourcePathInjectionTest {
         assertNull(model);
     }
 
+    @Test
+    public void TestWithArrayWrapping() {
+        ResourcePathModelWrapping model = factory.getAdapter(adaptable, ResourcePathModelWrapping.class);
+        assertNotNull(model);
+        assertTrue(model.getFromPath().length > 0);
+        assertTrue(model.getMultipleResources().length > 0);
+    }
+
 }
\ No newline at end of file

Added: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModelWrapping.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModelWrapping.java?rev=1772094&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModelWrapping.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModelWrapping.java Wed Nov 30 17:47:18 2016
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.models.testmodels.classes;
+
+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.injectorspecific.ResourcePath;
+
+@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })
+public class ResourcePathModelWrapping {
+
+    @ResourcePath(path = "/some/path")
+    private Resource[] fromPath;
+
+    @ResourcePath(name = "propertyWithSeveralPaths")
+    private Resource[] multipleResources;
+
+    public Resource[] getFromPath() {
+        return fromPath;
+    }
+
+    public Resource[] getMultipleResources() {
+        return this.multipleResources;
+    }
+
+}