cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skolbac...@apache.org
Subject [2/2] cayenne git commit: CAY-2039 | lifecycle events docs
Date Mon, 30 Nov 2015 16:06:04 GMT
CAY-2039 | lifecycle events docs


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

Branch: refs/heads/master
Commit: 5bb12cd36f0d87e3b217cdc20c22a0f6dc9613f3
Parents: 82c2ed5
Author: Savva Kolbachev <s.kolbachev@gmail.com>
Authored: Mon Nov 30 19:05:28 2015 +0300
Committer: Savva Kolbachev <s.kolbachev@gmail.com>
Committed: Mon Nov 30 19:05:28 2015 +0300

----------------------------------------------------------------------
 .../reflect/LifecycleCallbackOrderIT.java       | 100 +++++++++++++++++++
 .../lifecycle_callbacks_order/Lifecycle.java    |  38 +++++++
 .../auto/_Lifecycle.java                        |  27 +++++
 .../cayenne/unit/di/server/CayenneProjects.java |   1 +
 .../cayenne/unit/di/server/SchemaBuilder.java   |   2 +-
 .../cayenne-lifecycle-callbacks-order.xml       |   4 +
 .../resources/lifecycle-callbacks-order.map.xml |  16 +++
 .../src/docbkx/lifecycle-events.xml             |   4 +-
 8 files changed, 189 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/5bb12cd3/cayenne-server/src/test/java/org/apache/cayenne/reflect/LifecycleCallbackOrderIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/reflect/LifecycleCallbackOrderIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/reflect/LifecycleCallbackOrderIT.java
new file mode 100644
index 0000000..309013a
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/reflect/LifecycleCallbackOrderIT.java
@@ -0,0 +1,100 @@
+/*****************************************************************
+ *   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.ObjectContext;
+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.di.Inject;
+import org.apache.cayenne.testdo.lifecycle_callbacks_order.Lifecycle;
+import org.apache.cayenne.unit.di.server.CayenneProjects;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+@UseServerRuntime(CayenneProjects.LIFECYCLE_CALLBACKS_ORDER_PROJECT)
+public class LifecycleCallbackOrderIT extends ServerCase {
+
+    @Inject
+    private ObjectContext context;
+
+    @Test
+    public void testLifecycleCallbackOrder() {
+        LifecycleCallbackRegistry registry = new LifecycleCallbackRegistry(context.getEntityResolver());
+        context.getEntityResolver().setCallbackRegistry(registry);
+
+        LifecycleEventListener eventListener = new LifecycleEventListener();
+        registry.addListener(eventListener);
+
+        Lifecycle lifecycle = context.newObject(Lifecycle.class);
+        context.commitChanges();
+        assertEquals("validateForInsert;PrePersist;PostPersist;", lifecycle.getCallbackBufferValueAndReset());
+
+        lifecycle.setName("CallbackOrderTest");
+        context.commitChanges();
+        assertEquals("validateForUpdate;PreUpdate;PostUpdate;", lifecycle.getCallbackBufferValueAndReset());
+
+        context.deleteObject(lifecycle);
+        assertEquals("PreRemove;", lifecycle.getCallbackBuffer().toString());
+        context.commitChanges();
+        assertEquals("PreRemove;validateForDelete;PostRemove;", lifecycle.getCallbackBufferValueAndReset());
+    }
+
+    class LifecycleEventListener {
+
+        @PrePersist(Lifecycle.class)
+        void prePersist(Lifecycle lifecycle) {
+            lifecycle.getCallbackBuffer().append("PrePersist;");
+        }
+
+        @PostPersist(Lifecycle.class)
+        void postPersist(Lifecycle lifecycle) {
+            lifecycle.getCallbackBuffer().append("PostPersist;");
+        }
+
+        @PreUpdate(Lifecycle.class)
+        void preUpdate(Lifecycle lifecycle) {
+            lifecycle.getCallbackBuffer().append("PreUpdate;");
+        }
+
+        @PostUpdate(Lifecycle.class)
+        void postUpdate(Lifecycle lifecycle) {
+            lifecycle.getCallbackBuffer().append("PostUpdate;");
+        }
+
+        @PreRemove(Lifecycle.class)
+        void preRemove(Lifecycle lifecycle) {
+            lifecycle.getCallbackBuffer().append("PreRemove;");
+        }
+
+        @PostRemove(Lifecycle.class)
+        void postRemove(Lifecycle lifecycle) {
+            lifecycle.getCallbackBuffer().append("PostRemove;");
+        }
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/5bb12cd3/cayenne-server/src/test/java/org/apache/cayenne/testdo/lifecycle_callbacks_order/Lifecycle.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/lifecycle_callbacks_order/Lifecycle.java
b/cayenne-server/src/test/java/org/apache/cayenne/testdo/lifecycle_callbacks_order/Lifecycle.java
new file mode 100644
index 0000000..49ee979
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/lifecycle_callbacks_order/Lifecycle.java
@@ -0,0 +1,38 @@
+package org.apache.cayenne.testdo.lifecycle_callbacks_order;
+
+import org.apache.cayenne.testdo.lifecycle_callbacks_order.auto._Lifecycle;
+import org.apache.cayenne.validation.ValidationResult;
+
+public class Lifecycle extends _Lifecycle {
+
+    private static final long serialVersionUID = 1L;
+    private StringBuilder callbackBuffer = new StringBuilder();
+
+    @Override
+    public void validateForInsert(ValidationResult validationResult) {
+        callbackBuffer.append("validateForInsert;");
+        super.validateForInsert(validationResult);
+    }
+
+    @Override
+    public void validateForUpdate(ValidationResult validationResult) {
+        callbackBuffer.append("validateForUpdate;");
+        super.validateForUpdate(validationResult);
+    }
+
+    @Override
+    public void validateForDelete(ValidationResult validationResult) {
+        callbackBuffer.append("validateForDelete;");
+        super.validateForDelete(validationResult);
+    }
+
+    public StringBuilder getCallbackBuffer() {
+        return callbackBuffer;
+    }
+
+    public String getCallbackBufferValueAndReset() {
+        String v = callbackBuffer.toString();
+        callbackBuffer = new StringBuilder();
+        return v;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/5bb12cd3/cayenne-server/src/test/java/org/apache/cayenne/testdo/lifecycle_callbacks_order/auto/_Lifecycle.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/lifecycle_callbacks_order/auto/_Lifecycle.java
b/cayenne-server/src/test/java/org/apache/cayenne/testdo/lifecycle_callbacks_order/auto/_Lifecycle.java
new file mode 100644
index 0000000..76958cd
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/lifecycle_callbacks_order/auto/_Lifecycle.java
@@ -0,0 +1,27 @@
+package org.apache.cayenne.testdo.lifecycle_callbacks_order.auto;
+
+import org.apache.cayenne.CayenneDataObject;
+import org.apache.cayenne.exp.Property;
+
+/**
+ * Class _Lifecycle was generated by Cayenne.
+ * It is probably a good idea to avoid changing this class manually,
+ * since it may be overwritten next time code is regenerated.
+ * If you need to make any customizations, please use subclass.
+ */
+public abstract class _Lifecycle extends CayenneDataObject {
+
+    private static final long serialVersionUID = 1L; 
+
+    public static final String ID_PK_COLUMN = "ID";
+
+    public static final Property<String> NAME = new Property<String>("name");
+
+    public void setName(String name) {
+        writeProperty("name", name);
+    }
+    public String getName() {
+        return (String)readProperty("name");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/5bb12cd3/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/CayenneProjects.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/CayenneProjects.java
b/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/CayenneProjects.java
index 2d92da8..99a98da 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/CayenneProjects.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/CayenneProjects.java
@@ -40,6 +40,7 @@ public class CayenneProjects {
     public static final String INHERITANCE_PROJECT = "cayenne-inheritance.xml";
     public static final String INHERITANCE_SINGLE_TABLE1_PROJECT = "cayenne-inheritance-single-table1.xml";
     public static final String INHERITANCE_VERTICAL_PROJECT = "cayenne-inheritance-vertical.xml";
+    public static final String LIFECYCLE_CALLBACKS_ORDER_PROJECT = "cayenne-lifecycle-callbacks-order.xml";
     public static final String LIFECYCLES_PROJECT = "cayenne-lifecycles.xml";
     public static final String LOB_PROJECT = "cayenne-lob.xml";
     public static final String LOCKING_PROJECT = "cayenne-locking.xml";

http://git-wip-us.apache.org/repos/asf/cayenne/blob/5bb12cd3/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/SchemaBuilder.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/SchemaBuilder.java
b/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/SchemaBuilder.java
index 8cba086..8c8a208 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/SchemaBuilder.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/SchemaBuilder.java
@@ -79,7 +79,7 @@ public class SchemaBuilder {
 			"relationships-clob.map.xml", "relationships-flattened.map.xml", "relationships-set-to-many.map.xml",
 			"relationships-to-many-fk.map.xml", "relationships-to-one-fk.map.xml", "return-types.map.xml",
 			"uuid.map.xml", "multi-tier.map.xml", "persistent.map.xml", "reflexive.map.xml", "delete-rules.map.xml",
-			"lifecycles.map.xml", "map-to-many.map.xml", "toone.map.xml", "meaningful-pk.map.xml",
+            "lifecycle-callbacks-order.map.xml", "lifecycles.map.xml", "map-to-many.map.xml",
"toone.map.xml", "meaningful-pk.map.xml",
 			"table-primitives.map.xml", "generic.map.xml", "map-db1.map.xml", "map-db2.map.xml", "embeddable.map.xml",
 			"qualified.map.xml", "quoted-identifiers.map.xml", "inheritance-single-table1.map.xml",
 			"inheritance-vertical.map.xml", "oneway-rels.map.xml", "unsupported-distinct-types.map.xml",

http://git-wip-us.apache.org/repos/asf/cayenne/blob/5bb12cd3/cayenne-server/src/test/resources/cayenne-lifecycle-callbacks-order.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/resources/cayenne-lifecycle-callbacks-order.xml b/cayenne-server/src/test/resources/cayenne-lifecycle-callbacks-order.xml
new file mode 100644
index 0000000..2764943
--- /dev/null
+++ b/cayenne-server/src/test/resources/cayenne-lifecycle-callbacks-order.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<domain project-version="7">
+    <map name="lifecycle-callbacks-order"/>
+</domain>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/5bb12cd3/cayenne-server/src/test/resources/lifecycle-callbacks-order.map.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/resources/lifecycle-callbacks-order.map.xml b/cayenne-server/src/test/resources/lifecycle-callbacks-order.map.xml
new file mode 100644
index 0000000..6ca4768
--- /dev/null
+++ b/cayenne-server/src/test/resources/lifecycle-callbacks-order.map.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map xmlns="http://cayenne.apache.org/schema/7/modelMap"
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:schemaLocation="http://cayenne.apache.org/schema/7/modelMap http://cayenne.apache.org/schema/7/modelMap.xsd"
+          project-version="7">
+    <property name="defaultPackage" value="org.apache.cayenne.testdo.lifecycle_callbacks_order"/>
+    <property name="clientSupported" value="true"/>
+    <property name="defaultClientPackage" value="org.apache.cayenne.testdo.lifecycle_callbacks_order"/>
+    <db-entity name="LIFECYCLE">
+        <db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+        <db-attribute name="NAME" type="VARCHAR" length="100"/>
+    </db-entity>
+    <obj-entity name="Lifecycle" className="org.apache.cayenne.testdo.lifecycle_callbacks_order.Lifecycle"
dbEntityName="LIFECYCLE">
+        <obj-attribute name="name" type="java.lang.String" db-attribute-path="NAME"/>
+    </obj-entity>
+</data-map>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/5bb12cd3/docs/docbook/cayenne-guide/src/docbkx/lifecycle-events.xml
----------------------------------------------------------------------
diff --git a/docs/docbook/cayenne-guide/src/docbkx/lifecycle-events.xml b/docs/docbook/cayenne-guide/src/docbkx/lifecycle-events.xml
index 253dc25..09eefd6 100644
--- a/docs/docbook/cayenne-guide/src/docbkx/lifecycle-events.xml
+++ b/docs/docbook/cayenne-guide/src/docbkx/lifecycle-events.xml
@@ -60,14 +60,14 @@
                         <td>PrePersist</td>
                         <td>right before a new object is committed, inside
                                 <code>ObjectContext.commitChanges()</code> and
-                                <code>ObjectContext.commitChangesToParent()</code>
(and prior to
+                                <code>ObjectContext.commitChangesToParent()</code>
(and after
                                 "<code>validateForInsert()</code>").</td>
                     </tr>
                     <tr>
                         <td>PreUpdate</td>
                         <td>right before a modified object is committed, inside
                                 <code>ObjectContext.commitChanges()</code> and
-                                <code>ObjectContext.commitChangesToParent()</code>
(and prior to
+                                <code>ObjectContext.commitChangesToParent()</code>
(and after
                                 "<code>validateForUpdate()</code>").</td>
                     </tr>
                     <tr>


Mime
View raw message