cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject cayenne git commit: CAY-2276 PrePersist listener registered as PostPersist in LifecycleCallbackRegistry.addListener(Class<?>, LifecycleListener)
Date Wed, 05 Apr 2017 08:36:51 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master b3b6e03cf -> 4ff5e0e71


CAY-2276 PrePersist listener registered as PostPersist in LifecycleCallbackRegistry.addListener(Class<?>,
LifecycleListener)


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/4ff5e0e7
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/4ff5e0e7
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/4ff5e0e7

Branch: refs/heads/master
Commit: 4ff5e0e71409ef62d48616470a166f736d3bf11e
Parents: b3b6e03
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Wed Apr 5 11:36:45 2017 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Wed Apr 5 11:36:45 2017 +0300

----------------------------------------------------------------------
 .../reflect/LifecycleCallbackRegistry.java      |   2 +-
 .../reflect/LifecycleCallbackRegistryTest.java  | 184 +++++++++++++++++++
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |   1 +
 3 files changed, 186 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/4ff5e0e7/cayenne-server/src/main/java/org/apache/cayenne/reflect/LifecycleCallbackRegistry.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/reflect/LifecycleCallbackRegistry.java
b/cayenne-server/src/main/java/org/apache/cayenne/reflect/LifecycleCallbackRegistry.java
index 2dc60a2..38a6c3b 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/reflect/LifecycleCallbackRegistry.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/reflect/LifecycleCallbackRegistry.java
@@ -133,7 +133,7 @@ public class LifecycleCallbackRegistry {
 	 */
 	public void addListener(Class<?> entityClass, LifecycleListener listener) {
 		addListener(LifecycleEvent.POST_ADD, entityClass, listener, "postAdd");
-		addListener(LifecycleEvent.POST_PERSIST, entityClass, listener, "prePersist");
+		addListener(LifecycleEvent.PRE_PERSIST, entityClass, listener, "prePersist");
 		addListener(LifecycleEvent.POST_PERSIST, entityClass, listener, "postPersist");
 		addListener(LifecycleEvent.PRE_REMOVE, entityClass, listener, "preRemove");
 		addListener(LifecycleEvent.POST_REMOVE, entityClass, listener, "postRemove");

http://git-wip-us.apache.org/repos/asf/cayenne/blob/4ff5e0e7/cayenne-server/src/test/java/org/apache/cayenne/reflect/LifecycleCallbackRegistryTest.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/reflect/LifecycleCallbackRegistryTest.java
b/cayenne-server/src/test/java/org/apache/cayenne/reflect/LifecycleCallbackRegistryTest.java
new file mode 100644
index 0000000..aa34e1a
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/reflect/LifecycleCallbackRegistryTest.java
@@ -0,0 +1,184 @@
+/*****************************************************************
+ *   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.reflect;
+
+import org.apache.cayenne.LifecycleListener;
+import org.apache.cayenne.annotation.PostAdd;
+import org.apache.cayenne.annotation.PostLoad;
+import org.apache.cayenne.annotation.PostPersist;
+import org.apache.cayenne.annotation.PostRemove;
+import org.apache.cayenne.annotation.PostUpdate;
+import org.apache.cayenne.annotation.PrePersist;
+import org.apache.cayenne.annotation.PreRemove;
+import org.apache.cayenne.annotation.PreUpdate;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.map.LifecycleEvent;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+
+/**
+ * @since 4.0
+ */
+public class LifecycleCallbackRegistryTest {
+
+    LifecycleCallbackRegistry registry;
+
+    @Before
+    public void createRegistry() {
+        EntityResolver entityResolver = mock(EntityResolver.class);
+        registry = new LifecycleCallbackRegistry(entityResolver);
+        for(LifecycleEvent event : LifecycleEvent.values()) {
+            assertEquals(0, registry.getHandler(event).defaultListenersSize());
+            assertEquals(0, registry.getHandler(event).listenersSize());
+        }
+    }
+
+    @Test
+    public void addDefaultListener() throws Exception {
+        LifecycleListener listener = mock(LifecycleListener.class);
+        registry.addDefaultListener(listener);
+        for(LifecycleEvent event : LifecycleEvent.values()) {
+            assertEquals(1, registry.getHandler(event).defaultListenersSize());
+            assertEquals(0, registry.getHandler(event).listenersSize());
+        }
+    }
+
+    @Test
+    public void addDefaultListenerSingleType() throws Exception {
+        for(LifecycleEvent event : LifecycleEvent.values()) {
+            assertEquals(0, registry.getHandler(event).defaultListenersSize());
+            LifecycleListener listener = mock(LifecycleListener.class);
+            registry.addDefaultListener(event, listener, nameToCamelCase(event.name()));
+            assertEquals(1, registry.getHandler(event).defaultListenersSize());
+            assertEquals(0, registry.getHandler(event).listenersSize());
+        }
+    }
+
+    @Test
+    public void addListenerWithEntityClass() throws Exception {
+        LifecycleListener listener = mock(LifecycleListener.class);
+        registry.addListener(Object.class, listener);
+        for(LifecycleEvent event : LifecycleEvent.values()) {
+            assertEquals(1, registry.getHandler(event).listenersSize());
+            assertEquals(0, registry.getHandler(event).defaultListenersSize());
+        }
+    }
+
+    @Test
+    public void addListenerWithEntityClassSingleType() throws Exception {
+        for(LifecycleEvent event : LifecycleEvent.values()) {
+            assertEquals(0, registry.getHandler(event).listenersSize());
+            LifecycleListener listener = mock(LifecycleListener.class);
+            registry.addListener(event, Object.class, listener, nameToCamelCase(event.name()));
+            assertEquals(1, registry.getHandler(event).listenersSize());
+            assertEquals(0, registry.getHandler(event).defaultListenersSize());
+        }
+    }
+
+    @Test
+    public void addAnnotatedListener() {
+        registry.addListener(new AnnotatedListener());
+        for(LifecycleEvent event : LifecycleEvent.values()) {
+            assertEquals(1, registry.getHandler(event).defaultListenersSize());
+            assertEquals(0, registry.getHandler(event).listenersSize());
+        }
+    }
+
+    @Test
+    public void addAnnotatedListenerWithEntityClass() {
+        registry.addListener(new AnnotatedListenerWithEntity());
+        for(LifecycleEvent event : LifecycleEvent.values()) {
+            assertEquals(0, registry.getHandler(event).defaultListenersSize());
+            assertEquals(1, registry.getHandler(event).listenersSize());
+        }
+    }
+
+    private static class AnnotatedListener {
+        @PostAdd
+        public void postAdd(Object object) {}
+
+        @PostLoad
+        public void postLoad(Object object) {}
+
+        @PostPersist
+        public void postPersist(Object object) {}
+
+        @PostRemove
+        public void postRemove(Object object) {}
+
+        @PostUpdate
+        public void postUpdate(Object object) {}
+
+        @PrePersist
+        public void prePersist(Object object) {}
+
+        @PreRemove
+        public void preRemove(Object object) {}
+
+        @PreUpdate
+        public void preUpdate(Object object) {}
+    }
+
+    private static class AnnotatedListenerWithEntity {
+        @PostAdd(Object.class)
+        public void postAdd(Object object) {}
+
+        @PostLoad(Object.class)
+        public void postLoad(Object object) {}
+
+        @PostPersist(Object.class)
+        public void postPersist(Object object) {}
+
+        @PostRemove(Object.class)
+        public void postRemove(Object object) {}
+
+        @PostUpdate(Object.class)
+        public void postUpdate(Object object) {}
+
+        @PrePersist(Object.class)
+        public void prePersist(Object object) {}
+
+        @PreRemove(Object.class)
+        public void preRemove(Object object) {}
+
+        @PreUpdate(Object.class)
+        public void preUpdate(Object object) {}
+    }
+
+    private static String nameToCamelCase(String functionName) {
+        String[] parts = functionName.split("_");
+        StringBuilder sb = new StringBuilder();
+        boolean first = true;
+        for(String part : parts) {
+            if(first) {
+                sb.append(part.toLowerCase());
+                first = false;
+            } else {
+                char[] chars = part.toLowerCase().toCharArray();
+                chars[0] = Character.toTitleCase(chars[0]);
+                sb.append(chars);
+            }
+        }
+        return sb.toString();
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/4ff5e0e7/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index 22fc18a..b7f23af 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -38,6 +38,7 @@ CAY-2250 Remove: Incorrect text in Confirm Remove message when cursor is
set on
 CAY-2256 Cannot Save/Insert an Object With null Flattened (complex) toOne Relationship (see
also CAY-2146)
 CAY-2265 ServerRuntime.builder() fails to set default runtime name when a the project file
doesn't follow recognized pattern
 CAY-2273 Modeler: default suggested cgen location is rooted in subpackage
+CAY-2276 PrePersist listener registered as PostPersist in LifecycleCallbackRegistry.addListener(Class<?>,
LifecycleListener)
 
 ----------------------------------
 Release: 4.0.M5


Mime
View raw message