Author: justin
Date: Tue May  3 16:47:38 2016
New Revision: 1742144

URL: http://svn.apache.org/viewvc?rev=1742144&view=rev
Log:
SLING-5710 - fixing issue where super interface methods weren't injected

Added:
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/InterfaceInheritanceTest.java
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SubClassModel.java
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SuperClassModel.java
Modified:
    
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ReflectionUtil.java

Modified: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ReflectionUtil.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ReflectionUtil.java?rev=1742144&r1=1742143&r2=1742144&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ReflectionUtil.java
 (original)
+++ 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ReflectionUtil.java
 Tue May  3 16:47:38 2016
@@ -55,9 +55,19 @@ public final class ReflectionUtil {
         while (type != null) {
             Method[] methods = type.getDeclaredMethods();
             addAnnotated(methods, result);
+            addAnnotatedMethodsFromInterfaces(type, result);
             type = type.getSuperclass();
         }
         return result;
+
+    }
+
+    private static void addAnnotatedMethodsFromInterfaces(Class<?> type, 
List<Method> result) {
+        for (Class<?> iface : type.getInterfaces()) {
+            Method[] methods = iface.getDeclaredMethods();
+            addAnnotated(methods, result);
+            addAnnotatedMethodsFromInterfaces(iface, result);
+        }
     }
 
     public static <T extends AnnotatedElement> void addAnnotated(T[] elements, 
List<T> set) {

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/InterfaceInheritanceTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/InterfaceInheritanceTest.java?rev=1742144&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/InterfaceInheritanceTest.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/InterfaceInheritanceTest.java
 Tue May  3 16:47:38 2016
@@ -0,0 +1,86 @@
+/*
+ * 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.impl;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.wrappers.ValueMapDecorator;
+import org.apache.sling.models.impl.injectors.ChildResourceInjector;
+import org.apache.sling.models.impl.injectors.ValueMapInjector;
+import org.apache.sling.models.testmodels.classes.*;
+import org.apache.sling.models.testmodels.interfaces.SubClassModel;
+import org.apache.sling.models.testmodels.interfaces.SuperClassModel;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.ComponentContext;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class InterfaceInheritanceTest {
+
+    @Mock
+    private ComponentContext componentCtx;
+
+    @Mock
+    private BundleContext bundleContext;
+
+    private ModelAdapterFactory factory;
+
+    @Before
+    public void setup() {
+        when(componentCtx.getBundleContext()).thenReturn(bundleContext);
+        when(componentCtx.getProperties()).thenReturn(new Hashtable<String, 
Object>());
+
+        factory = new ModelAdapterFactory();
+        factory.activate(componentCtx);
+        ValueMapInjector valueMapInjector = new ValueMapInjector();
+        factory.bindInjector(valueMapInjector, new ServicePropertiesMap(1, 2));
+
+        factory.bindInjectAnnotationProcessorFactory(valueMapInjector,
+                Collections.<String, Object> 
singletonMap(Constants.SERVICE_ID, 2L));
+        
factory.adapterImplementations.addClassesAsAdapterAndImplementation(SuperClassModel.class,
 SubClassModel.class);
+    }
+
+    @Test
+    public void testSimplePropertyModel() {
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("superClassString", "first-value");
+        map.put("subClassString", "second-value");
+        ValueMap vm = new ValueMapDecorator(map);
+
+        Resource res = mock(Resource.class);
+        when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+        SubClassModel model = factory.getAdapter(res, SubClassModel.class);
+        assertNotNull(model);
+        assertEquals("first-value", model.getSuperClassString());
+        assertEquals("second-value", model.getSubClassString());
+    }
+}

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SubClassModel.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SubClassModel.java?rev=1742144&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SubClassModel.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SubClassModel.java
 Tue May  3 16:47:38 2016
@@ -0,0 +1,29 @@
+/*
+ * 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.interfaces;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+
+import javax.inject.Inject;
+
+@Model(adaptables = Resource.class)
+public interface SubClassModel extends SuperClassModel {
+
+    @Inject
+    String getSubClassString();
+}

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SuperClassModel.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SuperClassModel.java?rev=1742144&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SuperClassModel.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/SuperClassModel.java
 Tue May  3 16:47:38 2016
@@ -0,0 +1,29 @@
+/*
+ * 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.interfaces;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+
+import javax.inject.Inject;
+
+@Model(adaptables = Resource.class)
+public interface SuperClassModel {
+
+    @Inject
+    String getSuperClassString();
+}


Reply via email to