Author: aadamchik
Date: Wed Sep 20 19:43:45 2006
New Revision: 448439
URL: http://svn.apache.org/viewvc?view=rev&rev=448439
Log:
CAY-660 "pre" callbacks
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/DataChannelDecorator.java
- copied, changed from r448357,
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/util/DataChannelDecorator.java
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/EntityCallback.java
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ListenerCallback.java
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextDecorator.java
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/EntityCallbackTst.java
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ListenerCallbackTst.java
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackEntity.java
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackListener.java
Removed:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/util/DataChannelDecorator.java
Modified:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextDecoratedStackTst.java
Copied:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/DataChannelDecorator.java
(from r448357,
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/util/DataChannelDecorator.java)
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/DataChannelDecorator.java?view=diff&rev=448439&p1=incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/util/DataChannelDecorator.java&r1=448357&p2=incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/DataChannelDecorator.java&r2=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/util/DataChannelDecorator.java
(original)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/DataChannelDecorator.java
Wed Sep 20 19:43:45 2006
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
****************************************************************/
-package org.apache.cayenne.util;
+package org.apache.cayenne.intercept;
import org.apache.cayenne.DataChannel;
import org.apache.cayenne.ObjectContext;
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/EntityCallback.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/EntityCallback.java?view=auto&rev=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/EntityCallback.java
(added)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/EntityCallback.java
Wed Sep 20 19:43:45 2006
@@ -0,0 +1,82 @@
+/*****************************************************************
+ * 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.intercept;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.util.Util;
+import org.apache.commons.collections.Closure;
+
+/**
+ * Defines a generic callback operation executed via reflection on a
persistent object.
+ * Note that the method must be declared in the class itself. Callback will
not look up
+ * the class hierarchy.
+ *
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+class EntityCallback implements Closure {
+
+ private Method callbackMethod;
+
+ EntityCallback(Class objectClass, String methodName) throws
IllegalArgumentException {
+ this.callbackMethod = findMethod(objectClass, methodName);
+ }
+
+ public void execute(Object object) {
+ try {
+ callbackMethod.invoke(object, null);
+ }
+ catch (Exception e) {
+ throw new CayenneRuntimeException("Error invoking entity callback
method "
+ + callbackMethod.getName(), e);
+ }
+ }
+
+ private Method findMethod(Class objectClass, String methodName)
+ throws IllegalArgumentException {
+ Method[] methods = objectClass.getDeclaredMethods();
+ for (int i = 0; i < methods.length; i++) {
+ if (methodName.equals(methods[i].getName())) {
+
+ // must be non-static, void, with no args
+ // JPA spec also requires it to be non-final, but we don't care
+ int modifiers = methods[i].getModifiers();
+ if (!Modifier.isStatic(modifiers)
+ &&
Void.TYPE.isAssignableFrom(methods[i].getReturnType())
+ && methods[i].getParameterTypes().length == 0) {
+
+ if (!Util.isAccessible(methods[i])) {
+ methods[i].setAccessible(true);
+ }
+
+ return methods[i];
+ }
+ }
+ }
+
+ throw new IllegalArgumentException("Class "
+ + objectClass.getName()
+ + " has no valid callback method '"
+ + methodName
+ + "'");
+ }
+}
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ListenerCallback.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ListenerCallback.java?view=auto&rev=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ListenerCallback.java
(added)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ListenerCallback.java
Wed Sep 20 19:43:45 2006
@@ -0,0 +1,105 @@
+/*****************************************************************
+ * 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.intercept;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.util.Util;
+import org.apache.commons.collections.Closure;
+
+/**
+ * Defines a generic callback operation executed via reflection on an
arbitrary listener
+ * object. Note that the method must be declared in the class itself. Callback
will not
+ * look up the class hierarchy.
+ *
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+class ListenerCallback implements Closure {
+
+ private Method callbackMethod;
+ private Object listener;
+
+ ListenerCallback(Class objectClass, String methodName, Class entityType)
+ throws IllegalArgumentException {
+ this.callbackMethod = findMethod(objectClass, methodName, entityType);
+ try {
+ this.listener = objectClass.newInstance();
+ }
+ catch (Exception e) {
+ throw new IllegalArgumentException(
+ "Error instantiating callback listener class "
+ + objectClass.getName()
+ + ": "
+ + e.getMessage());
+ }
+ }
+
+ Object getListener() {
+ return listener;
+ }
+
+ public void execute(Object object) {
+ try {
+ callbackMethod.invoke(listener, new Object[] {
+ object
+ });
+ }
+ catch (Exception e) {
+ throw new CayenneRuntimeException(
+ "Error invoking entity listener callback method "
+ + callbackMethod.getName(),
+ e);
+ }
+ }
+
+ private Method findMethod(Class objectClass, String methodName, Class
entityType)
+ throws IllegalArgumentException {
+
+ Method[] methods = objectClass.getDeclaredMethods();
+ for (int i = 0; i < methods.length; i++) {
+ if (methodName.equals(methods[i].getName())) {
+
+ // must be non-static, void, with a single arg assinable to
entity type
+ // JPA spec also requires it to be non-final, but we don't care
+ int modifiers = methods[i].getModifiers();
+ Class[] parameters = methods[i].getParameterTypes();
+ if (!Modifier.isStatic(modifiers)
+ &&
Void.TYPE.isAssignableFrom(methods[i].getReturnType())
+ && parameters.length == 1
+ && parameters[0].isAssignableFrom(entityType)) {
+
+ if (!Util.isAccessible(methods[i])) {
+ methods[i].setAccessible(true);
+ }
+
+ return methods[i];
+ }
+ }
+ }
+
+ throw new IllegalArgumentException("Class "
+ + objectClass.getName()
+ + " has no valid listener callback method '"
+ + methodName
+ + "'");
+ }
+}
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java?view=auto&rev=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java
(added)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextCallbackInterceptor.java
Wed Sep 20 19:43:45 2006
@@ -0,0 +1,231 @@
+/*****************************************************************
+ * 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.intercept;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.cayenne.DeleteDenyException;
+import org.apache.cayenne.PersistenceState;
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.map.DeleteRule;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
+import org.apache.cayenne.property.ClassDescriptor;
+import org.apache.commons.collections.Closure;
+
+/**
+ * Implements JPA-compliant "PrePersist", "PreRemove", "PreUpdate" callbacks
for the
+ * ObjectContext operations. Callbacks can be invoked either on the persistent
object
+ * instances themselves or on an instance of an arbitrary class. Signature of
a method of
+ * a persistent object is {code}"void method()"{code}, while for a
non-persistent object
+ * it is {code}"void method(Object)"{code}. Note that if a new object is later
removed,
+ * "PrePersist" callbacks will be invoked, while "PreRemove" will not.
+ *
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public class ObjectContextCallbackInterceptor extends ObjectContextDecorator {
+
+ protected Collection prePersistCallbacks;
+ protected Collection preRemoveCallbacks;
+ protected Collection preUpdateCallbacks;
+
+ /**
+ * Creates a new object, applying "PrePersist" callbacks to it.
+ */
+ public Persistent newObject(Class persistentClass) {
+ Persistent object = super.newObject(persistentClass);
+
+ if (prePersistCallbacks != null) {
+ applyCallbacks(prePersistCallbacks, object);
+ }
+
+ return object;
+ }
+
+ /**
+ * Deletes an object, applying "PreRemove" callbacks to it and all its
cascaded
+ * dependencies.
+ */
+ public void deleteObject(Persistent object) throws DeleteDenyException {
+
+ if (preRemoveCallbacks != null
+ && object.getPersistenceState() != PersistenceState.NEW) {
+ applyPreRemoveCallbacks(object);
+ }
+
+ super.deleteObject(object);
+ }
+
+ public void commitChanges() {
+ if (preUpdateCallbacks != null) {
+ applyPreUpdateCallbacks();
+ }
+
+ super.commitChanges();
+ }
+
+ public void commitChangesToParent() {
+
+ if (preUpdateCallbacks != null) {
+ applyPreUpdateCallbacks();
+ }
+
+ super.commitChangesToParent();
+ }
+
+ protected void applyPreUpdateCallbacks() {
+ Collection callbacks = getPreUpdateCallbacks();
+
+ Iterator it = context.modifiedObjects().iterator();
+ while (it.hasNext()) {
+ applyCallbacks(callbacks, it.next());
+ }
+ }
+
+ /**
+ * Recursively applies PreRemove callbacks to an object and objects that
will be
+ * cascaded
+ */
+ protected void applyPreRemoveCallbacks(Object object) {
+
+ Collection callbacks = getPreRemoveCallbacks();
+
+ applyCallbacks(callbacks, object);
+
+ ObjEntity entity = getEntityResolver().lookupObjEntity(object);
+ ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(
+ entity.getClassName());
+
+ Iterator it = entity.getRelationships().iterator();
+ while (it.hasNext()) {
+
+ ObjRelationship relationship = (ObjRelationship) it.next();
+ if (relationship.getDeleteRule() == DeleteRule.CASCADE) {
+
+ Object related = descriptor
+ .getProperty(relationship.getName())
+ .readProperty(object);
+
+ if (related == null) {
+ // do nothing
+ }
+ else if (related instanceof Collection) {
+ Iterator relatedObjects = ((Collection)
related).iterator();
+ while (relatedObjects.hasNext()) {
+ applyPreRemoveCallbacks(relatedObjects.next());
+ }
+ }
+ else {
+ applyPreRemoveCallbacks(related);
+ }
+ }
+ }
+ }
+
+ protected void applyCallbacks(Collection callbacks, Object object) {
+ Iterator it = (Iterator) callbacks.iterator();
+ while (it.hasNext()) {
+ ((Closure) it.next()).execute(object);
+ }
+ }
+
+ public void addPrePersistCallback(Class objectClass, String methodName) {
+ getPrePersistCallbacks().add(new EntityCallback(objectClass,
methodName));
+ }
+
+ public void addPrePersistCallback(
+ Class objectClass,
+ String methodName,
+ Class entityClass) {
+
+ ObjEntity entity =
context.getEntityResolver().lookupObjEntity(entityClass);
+ if (entity == null) {
+ throw new IllegalArgumentException(entityClass.getName()
+ + " is not a persistent entity");
+ }
+
+ getPrePersistCallbacks().add(
+ new ListenerCallback(objectClass, methodName,
entity.getJavaClass()));
+ }
+
+ public void addPreRemoveCallback(Class objectClass, String methodName) {
+ getPreRemoveCallbacks().add(new EntityCallback(objectClass,
methodName));
+ }
+
+ public void addPreRemoveCallback(
+ Class objectClass,
+ String methodName,
+ Class entityClass) {
+
+ ObjEntity entity =
context.getEntityResolver().lookupObjEntity(entityClass);
+ if (entity == null) {
+ throw new IllegalArgumentException(entityClass.getName()
+ + " is not a persistent entity");
+ }
+
+ getPreRemoveCallbacks().add(
+ new ListenerCallback(objectClass, methodName,
entity.getJavaClass()));
+ }
+
+ public void addPreUpdateCallback(Class objectClass, String methodName) {
+ getPreUpdateCallbacks().add(new EntityCallback(objectClass,
methodName));
+ }
+
+ public void addPreUpdateCallback(
+ Class objectClass,
+ String methodName,
+ Class entityClass) {
+
+ ObjEntity entity =
context.getEntityResolver().lookupObjEntity(entityClass);
+ if (entity == null) {
+ throw new IllegalArgumentException(entityClass.getName()
+ + " is not a persistent entity");
+ }
+
+ getPreRemoveCallbacks().add(
+ new ListenerCallback(objectClass, methodName,
entity.getJavaClass()));
+ }
+
+ protected Collection getPrePersistCallbacks() {
+ if (prePersistCallbacks == null) {
+ prePersistCallbacks = new ArrayList();
+ }
+
+ return prePersistCallbacks;
+ }
+
+ protected Collection getPreRemoveCallbacks() {
+ if (preRemoveCallbacks == null) {
+ preRemoveCallbacks = new ArrayList();
+ }
+
+ return preRemoveCallbacks;
+ }
+
+ protected Collection getPreUpdateCallbacks() {
+ if (preUpdateCallbacks == null) {
+ preUpdateCallbacks = new ArrayList();
+ }
+
+ return preUpdateCallbacks;
+ }
+}
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextDecorator.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextDecorator.java?view=auto&rev=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextDecorator.java
(added)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/intercept/ObjectContextDecorator.java
Wed Sep 20 19:43:45 2006
@@ -0,0 +1,128 @@
+/*****************************************************************
+ * 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.intercept;
+
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.cayenne.DataChannel;
+import org.apache.cayenne.DeleteDenyException;
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.ObjectId;
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.QueryResponse;
+import org.apache.cayenne.graph.GraphManager;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.query.Query;
+
+/**
+ * A pass-through decorator of an ObjectContext. Can serve as a superclass of
various
+ * ObjectContext interceptors.
+ *
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public class ObjectContextDecorator implements ObjectContext {
+
+ protected ObjectContext context;
+
+ public void commitChanges() {
+ context.commitChanges();
+ }
+
+ public void commitChangesToParent() {
+ context.commitChangesToParent();
+ }
+
+ public Collection deletedObjects() {
+ return context.deletedObjects();
+ }
+
+ public void deleteObject(Persistent object) throws DeleteDenyException {
+ context.deleteObject(object);
+ }
+
+ public DataChannel getChannel() {
+ return context.getChannel();
+ }
+
+ public EntityResolver getEntityResolver() {
+ return context.getEntityResolver();
+ }
+
+ public GraphManager getGraphManager() {
+ return context.getGraphManager();
+ }
+
+ public Persistent localObject(ObjectId id, Persistent prototype) {
+ return context.localObject(id, prototype);
+ }
+
+ public Collection modifiedObjects() {
+ return context.modifiedObjects();
+ }
+
+ public Persistent newObject(Class persistentClass) {
+ return context.newObject(persistentClass);
+ }
+
+ public Collection newObjects() {
+ return context.newObjects();
+ }
+
+ public QueryResponse performGenericQuery(Query query) {
+ return context.performGenericQuery(query);
+ }
+
+ public List performQuery(Query query) {
+ return context.performQuery(query);
+ }
+
+ public void prepareForAccess(Persistent object, String property) {
+ context.prepareForAccess(object, property);
+ }
+
+ public void propertyChanged(
+ Persistent object,
+ String property,
+ Object oldValue,
+ Object newValue) {
+ context.propertyChanged(object, property, oldValue, newValue);
+ }
+
+ public void rollbackChanges() {
+ context.rollbackChanges();
+ }
+
+ public void rollbackChangesLocally() {
+ context.rollbackChangesLocally();
+ }
+
+ public Collection uncommittedObjects() {
+ return context.uncommittedObjects();
+ }
+
+ public ObjectContext getContext() {
+ return context;
+ }
+
+ public void setContext(ObjectContext context) {
+ this.context = context;
+ }
+}
Modified:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextDecoratedStackTst.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextDecoratedStackTst.java?view=diff&rev=448439&r1=448438&r2=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextDecoratedStackTst.java
(original)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextDecoratedStackTst.java
Wed Sep 20 19:43:45 2006
@@ -23,9 +23,9 @@
import org.apache.art.Artist;
import org.apache.cayenne.DataChannel;
import org.apache.cayenne.DataObjectUtils;
+import org.apache.cayenne.intercept.DataChannelDecorator;
import org.apache.cayenne.query.SQLTemplate;
import org.apache.cayenne.unit.CayenneTestCase;
-import org.apache.cayenne.util.DataChannelDecorator;
public class DataContextDecoratedStackTst extends CayenneTestCase {
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/EntityCallbackTst.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/EntityCallbackTst.java?view=auto&rev=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/EntityCallbackTst.java
(added)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/EntityCallbackTst.java
Wed Sep 20 19:43:45 2006
@@ -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.cayenne.intercept;
+
+import junit.framework.TestCase;
+
+public class EntityCallbackTst extends TestCase {
+
+ public void testPublicCallbackMethod() {
+ EntityCallback callback = new EntityCallback(
+ MockCallingBackEntity.class,
+ "publicCallback");
+
+ MockCallingBackEntity e = new MockCallingBackEntity();
+ callback.execute(e);
+ assertTrue(e.publicCallbackInvoked);
+ assertFalse(e.protectedCallbackInvoked);
+ assertFalse(e.privateCallbackInvoked);
+ assertFalse(e.defaultCallbackInvoked);
+ }
+
+ public void testProtectedCallbackMethod() {
+ EntityCallback callback = new EntityCallback(
+ MockCallingBackEntity.class,
+ "protectedCallback");
+
+ MockCallingBackEntity e = new MockCallingBackEntity();
+ callback.execute(e);
+ assertFalse(e.publicCallbackInvoked);
+ assertTrue(e.protectedCallbackInvoked);
+ assertFalse(e.privateCallbackInvoked);
+ assertFalse(e.defaultCallbackInvoked);
+ }
+
+ public void testPrivateCallbackMethod() {
+ EntityCallback callback = new EntityCallback(
+ MockCallingBackEntity.class,
+ "privateCallback");
+
+ MockCallingBackEntity e = new MockCallingBackEntity();
+ callback.execute(e);
+ assertFalse(e.publicCallbackInvoked);
+ assertFalse(e.protectedCallbackInvoked);
+ assertTrue(e.privateCallbackInvoked);
+ assertFalse(e.defaultCallbackInvoked);
+ }
+
+ public void testDefaultCallbackMethod() {
+ EntityCallback callback = new EntityCallback(
+ MockCallingBackEntity.class,
+ "defaultCallback");
+
+ MockCallingBackEntity e = new MockCallingBackEntity();
+ callback.execute(e);
+ assertFalse(e.publicCallbackInvoked);
+ assertFalse(e.protectedCallbackInvoked);
+ assertFalse(e.privateCallbackInvoked);
+ assertTrue(e.defaultCallbackInvoked);
+ }
+
+ public void testStaticCallbackMethod() {
+ try {
+ new EntityCallback(MockCallingBackEntity.class, "staticCallback");
+ fail("Static methods can't be used as callbacks");
+ }
+ catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+}
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ListenerCallbackTst.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ListenerCallbackTst.java?view=auto&rev=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ListenerCallbackTst.java
(added)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/ListenerCallbackTst.java
Wed Sep 20 19:43:45 2006
@@ -0,0 +1,45 @@
+/*****************************************************************
+ * 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.intercept;
+
+import junit.framework.TestCase;
+
+public class ListenerCallbackTst extends TestCase {
+
+ public void testPublicCallbackMethod() {
+
+ ListenerCallback callback = new ListenerCallback(
+ MockCallingBackListener.class,
+ "publicCallback",
+ Object.class);
+
+ MockCallingBackEntity e = new MockCallingBackEntity();
+ callback.execute(e);
+
+ // entity itself should not be called back...
+ assertFalse(e.publicCallbackInvoked);
+ assertFalse(e.protectedCallbackInvoked);
+ assertFalse(e.privateCallbackInvoked);
+ assertFalse(e.defaultCallbackInvoked);
+
+ MockCallingBackListener listener = (MockCallingBackListener) callback
+ .getListener();
+ assertSame(e, listener.publicCalledbackEntity);
+ }
+}
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackEntity.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackEntity.java?view=auto&rev=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackEntity.java
(added)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackEntity.java
Wed Sep 20 19:43:45 2006
@@ -0,0 +1,47 @@
+/*****************************************************************
+ * 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.intercept;
+
+public class MockCallingBackEntity {
+
+ public boolean publicCallbackInvoked;
+ public boolean protectedCallbackInvoked;
+ public boolean privateCallbackInvoked;
+ public boolean defaultCallbackInvoked;
+
+ public static void staticCallback() {
+
+ }
+
+ public void publicCallback() {
+ publicCallbackInvoked = true;
+ }
+
+ protected void protectedCallback() {
+ protectedCallbackInvoked = true;
+ }
+
+ private void privateCallback() {
+ privateCallbackInvoked = true;
+ }
+
+ void defaultCallback() {
+ defaultCallbackInvoked = true;
+ }
+}
Added:
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackListener.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackListener.java?view=auto&rev=448439
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackListener.java
(added)
+++
incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/intercept/MockCallingBackListener.java
Wed Sep 20 19:43:45 2006
@@ -0,0 +1,28 @@
+/*****************************************************************
+ * 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.intercept;
+
+public class MockCallingBackListener {
+
+ public Object publicCalledbackEntity;
+
+ public void publicCallback(Object entity) {
+ publicCalledbackEntity = entity;
+ }
+}