cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r450477 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src: main/java/org/apache/cayenne/map/LifecycleEventCallbackMap.java test/java/org/apache/cayenne/map/LifecycleEventCallbackMapTst.java
Date Wed, 27 Sep 2006 15:30:22 GMT
Author: aadamchik
Date: Wed Sep 27 08:30:22 2006
New Revision: 450477

URL: http://svn.apache.org/viewvc?view=rev&rev=450477
Log:
CAY-669

Added:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/map/LifecycleEventCallbackMapTst.java
Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/LifecycleEventCallbackMap.java

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/LifecycleEventCallbackMap.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/LifecycleEventCallbackMap.java?view=diff&rev=450477&r1=450476&r2=450477
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/LifecycleEventCallbackMap.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/map/LifecycleEventCallbackMap.java
Wed Sep 27 08:30:22 2006
@@ -90,7 +90,23 @@
      * Invokes callbacks for a given entity object.
      */
     public void performCallbacks(Object object) {
-        String key = object.getClass().getName();
+        performCallbacks(object, object.getClass());
+    }
+
+    /**
+     * Invokes callbacks for the class hierarchy, starting from the most generic
+     * superclass.
+     */
+    protected void performCallbacks(Object object, Class callbackEntityClass) {
+        if (Object.class.equals(callbackEntityClass) || callbackEntityClass == null) {
+            return;
+        }
+
+        // recursively perform super callbacks first
+        performCallbacks(object, callbackEntityClass.getSuperclass());
+
+        // perform callbacks on provided class
+        String key = callbackEntityClass.getName();
         Collection entityListeners = (Collection) listeners.get(key);
 
         if (entityListeners != null) {

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/map/LifecycleEventCallbackMapTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/map/LifecycleEventCallbackMapTst.java?view=auto&rev=450477
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/map/LifecycleEventCallbackMapTst.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/map/LifecycleEventCallbackMapTst.java
Wed Sep 27 08:30:22 2006
@@ -0,0 +1,96 @@
+/*****************************************************************
+ *   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.cayenne.map;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class LifecycleEventCallbackMapTst extends TestCase {
+
+    public void testCallbackOnSuperclass() {
+
+        LifecycleEventCallbackMap map = new LifecycleEventCallbackMap();
+        map.addListener(C1.class, "c1Callback");
+
+        C3 subclass = new C3();
+
+        assertEquals(0, subclass.callbacks.size());
+        map.performCallbacks(subclass);
+        assertEquals(1, subclass.callbacks.size());
+    }
+
+    public void testCallbackOnSuperclassWithSublcassOverrides() {
+
+        LifecycleEventCallbackMap map = new LifecycleEventCallbackMap();
+        map.addListener(C1.class, "c1Callback");
+
+        C4 subclass = new C4();
+
+        assertEquals(0, subclass.callbacks.size());
+        map.performCallbacks(subclass);
+        assertEquals(1, subclass.callbacks.size());
+        assertEquals("c4Callback", subclass.callbacks.get(0));
+    }
+
+    public void testCallbackOrderInInheritanceHierarchy() {
+
+        LifecycleEventCallbackMap map = new LifecycleEventCallbackMap();
+        map.addListener(C2.class, "c2Callback");
+        map.addListener(C1.class, "c1Callback");
+
+        C2 c = new C2();
+
+        assertTrue(c.callbacks.isEmpty());
+        map.performCallbacks(c);
+        assertEquals(2, c.callbacks.size());
+
+        // superclass callbacks should be invoked first
+        assertEquals("c1Callback", c.callbacks.get(0));
+        assertEquals("c2Callback", c.callbacks.get(1));
+    }
+
+    static class C1 {
+
+        protected List callbacks = new ArrayList();
+
+        void c1Callback() {
+            callbacks.add("c1Callback");
+        }
+    }
+
+    static class C2 extends C1 {
+
+        void c2Callback() {
+            callbacks.add("c2Callback");
+        }
+    }
+
+    static class C3 extends C1 {
+
+    }
+
+    static class C4 extends C1 {
+
+        void c1Callback() {
+            callbacks.add("c4Callback");
+        }
+    }
+}



Mime
View raw message