sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1628698 - in /sling/trunk/bundles/extensions/models/impl/src: main/java/org/apache/sling/models/impl/ test/java/org/apache/sling/models/impl/ test/java/org/apache/sling/models/testmodels/classes/
Date Wed, 01 Oct 2014 13:18:02 GMT
Author: justin
Date: Wed Oct  1 13:18:01 2014
New Revision: 1628698

URL: http://svn.apache.org/r1628698
Log:
SLING-3923 - fixing multiple calls to post construct method with patch from Konrad Windszus.
Thanks! This closes #27

Added:
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SubClassOverriddenPostConstruct.java
Modified:
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java

Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java?rev=1628698&r1=1628697&r2=1628698&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
(original)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
Wed Oct  1 13:18:01 2014
@@ -725,6 +725,18 @@ public class ModelAdapterFactory impleme
             return methodName;
         }
     }
+    
+    private boolean addMethodIfNotOverriden(List<Method> methods, Method newMethod)
{
+        for (Method method : methods) {
+            if (method.getName().equals(newMethod.getName())) {
+                if (Arrays.equals(method.getParameterTypes(),newMethod.getParameterTypes()))
{
+                    return false;
+                }
+            }
+        }
+        methods.add(newMethod);
+        return true;
+    }
 
     private void invokePostConstruct(Object object) throws Exception {
         Class<?> clazz = object.getClass();
@@ -733,7 +745,7 @@ public class ModelAdapterFactory impleme
             Method[] methods = clazz.getDeclaredMethods();
             for (Method method : methods) {
                 if (method.isAnnotationPresent(PostConstruct.class)) {
-                    postConstructMethods.add(method);
+                    addMethodIfNotOverriden(postConstructMethods, method);
                 }
             }
             clazz = clazz.getSuperclass();

Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java?rev=1628698&r1=1628697&r2=1628698&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
(original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
Wed Oct  1 13:18:01 2014
@@ -23,6 +23,7 @@ import java.util.Hashtable;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.models.testmodels.classes.SubClass;
+import org.apache.sling.models.testmodels.classes.SubClassOverriddenPostConstruct;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -57,4 +58,16 @@ public class PostConstructTest {
         assertTrue(sc.getPostConstructCalledTimestampInSub() > sc.getPostConstructCalledTimestampInSuper());
         assertTrue(sc.getPostConstructCalledTimestampInSuper() > 0);
     }
+    
+    @Test
+    public void testOverriddenPostConstruct() {
+        Resource r = mock(Resource.class);
+        ModelAdapterFactory factory = new ModelAdapterFactory();
+        factory.activate(componentCtx);
+        // no injectors are necessary
+
+        SubClassOverriddenPostConstruct sc = factory.getAdapter(r, SubClassOverriddenPostConstruct.class);
+        assertEquals("Post construct not called exactly one time in sub class!", 1, sc.getPostConstructorCalledCounter());
+        assertEquals("Post construct was called on super class although overridden in sub
class", 0, sc.getPostConstructCalledTimestampInSuper());
+    }
 }

Added: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SubClassOverriddenPostConstruct.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SubClassOverriddenPostConstruct.java?rev=1628698&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SubClassOverriddenPostConstruct.java
(added)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/SubClassOverriddenPostConstruct.java
Wed Oct  1 13:18:01 2014
@@ -0,0 +1,36 @@
+/*
+ * 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 javax.annotation.PostConstruct;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+
+@Model(adaptables=Resource.class)
+public class SubClassOverriddenPostConstruct extends SuperClass {
+    int postConstructorCalledCounter = 0;
+    
+    public int getPostConstructorCalledCounter() {
+        return postConstructorCalledCounter;
+    }
+    
+    @PostConstruct
+    protected void pc() {
+        postConstructorCalledCounter++;
+    }
+}



Mime
View raw message