Author: hardy.ferentschik
Date: 2008-07-01 06:05:16 -0400 (Tue, 01 Jul 2008)
New Revision: 14833
Modified:
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java
entitymanager/trunk/src/java/org/hibernate/ejb/event/CallbackResolver.java
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/Cat.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/Kitten.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java
Log:
EJB-288:
Updated tests and temporarily added all collection listeners so that Gail can inspect the
test. Depending on the outcome we might have remove the listeners again.
Modified: entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java
===================================================================
---
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java 2008-06-30
19:22:31 UTC (rev 14832)
+++
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java 2008-07-01
10:05:16 UTC (rev 14833)
@@ -38,6 +38,8 @@
import org.hibernate.event.MergeEventListener;
import org.hibernate.event.PersistEventListener;
import org.hibernate.event.PostCollectionRecreateEventListener;
+import org.hibernate.event.PostCollectionRemoveEventListener;
+import org.hibernate.event.PostCollectionUpdateEventListener;
import org.hibernate.event.PostDeleteEventListener;
import org.hibernate.event.PostInsertEventListener;
import org.hibernate.event.PostLoadEventListener;
@@ -133,8 +135,15 @@
listenerConfig.setPostUpdateEventListeners(
new PostUpdateEventListener[] { postUpdateEventListener }
);
+
listenerConfig.setPostCollectionRecreateEventListeners(
new PostCollectionRecreateEventListener[] { postUpdateEventListener });
+
+ listenerConfig.setPostCollectionRemoveEventListeners(
+ new PostCollectionRemoveEventListener[] { postUpdateEventListener });
+
+ listenerConfig.setPostCollectionUpdateEventListeners(
+ new PostCollectionUpdateEventListener[] { postUpdateEventListener });
}
public void setProperties(Properties properties) {
@@ -157,6 +166,7 @@
}
}
+ @SuppressWarnings("unchecked")
public void configure() {
//TODO exclude pure hbm file classes?
//TODO move it to each event listener initialize()?
Modified: entitymanager/trunk/src/java/org/hibernate/ejb/event/CallbackResolver.java
===================================================================
--- entitymanager/trunk/src/java/org/hibernate/ejb/event/CallbackResolver.java 2008-06-30
19:22:31 UTC (rev 14832)
+++ entitymanager/trunk/src/java/org/hibernate/ejb/event/CallbackResolver.java 2008-07-01
10:05:16 UTC (rev 14833)
@@ -1,3 +1,4 @@
+// $Id:$
/*
* JBoss, the OpenSource EJB server
*
@@ -22,12 +23,15 @@
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XMethod;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
- * @version $Revision$
*/
public final class CallbackResolver {
+ private static Logger log = LoggerFactory.getLogger(CallbackResolver.class);
+
private static boolean useAnnotationAnnotatedByListener;
static {
@@ -75,6 +79,7 @@
if ( ! method.isAccessible() ) {
method.setAccessible( true );
}
+ log.debug("Adding {} as {} callback for entity {}.", new
String[]{methodName, annotation.getSimpleName(), beanClass.getName()});
callbacks.add( 0, callback ); //superclass first
callbacksMethodNames.add( 0, methodName );
}
@@ -157,6 +162,7 @@
if ( ! method.isAccessible() ) {
method.setAccessible( true );
}
+ log.debug("Adding {} as {} callback for entity {}.", new
String[]{methodName, annotation.getSimpleName(), beanClass.getName()});
callbacks.add( 0, callback ); // listeners first
}
else {
Modified:
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java
===================================================================
---
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java 2008-06-30
19:22:31 UTC (rev 14832)
+++
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java 2008-07-01
10:05:16 UTC (rev 14833)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, the OpenSource EJB server
*
@@ -12,16 +12,20 @@
import org.hibernate.event.EventSource;
import org.hibernate.event.PostCollectionRecreateEvent;
import org.hibernate.event.PostCollectionRecreateEventListener;
+import org.hibernate.event.PostCollectionRemoveEvent;
+import org.hibernate.event.PostCollectionRemoveEventListener;
+import org.hibernate.event.PostCollectionUpdateEvent;
+import org.hibernate.event.PostCollectionUpdateEventListener;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PostUpdateEventListener;
/**
* @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
- * @version $Revision: 11282 $
*/
@SuppressWarnings("serial")
public class EJB3PostUpdateEventListener implements PostUpdateEventListener,
- CallbackHandlerConsumer, PostCollectionRecreateEventListener {
+ CallbackHandlerConsumer, PostCollectionRecreateEventListener,
+ PostCollectionRemoveEventListener, PostCollectionUpdateEventListener {
EntityCallbackHandler callbackHandler;
public void setCallbackHandler(EntityCallbackHandler callbackHandler) {
@@ -56,4 +60,16 @@
EventSource session = event.getSession();
postUpdate(entity, session);
}
+
+ public void onPostRemoveCollection(PostCollectionRemoveEvent event) {
+ Object entity = event.getCollection().getOwner();
+ EventSource session = event.getSession();
+ postUpdate(entity, session);
+ }
+
+ public void onPostUpdateCollection(PostCollectionUpdateEvent event) {
+ Object entity = event.getCollection().getOwner();
+ EventSource session = event.getSession();
+ postUpdate(entity, session);
+ }
}
Modified: entitymanager/trunk/src/test/org/hibernate/ejb/test/Cat.java
===================================================================
--- entitymanager/trunk/src/test/org/hibernate/ejb/test/Cat.java 2008-06-30 19:22:31 UTC
(rev 14832)
+++ entitymanager/trunk/src/test/org/hibernate/ejb/test/Cat.java 2008-07-01 10:05:16 UTC
(rev 14833)
@@ -110,7 +110,7 @@
@PostUpdate
private void someLateUpdateWorking() {
- log.debug("PostUpdate in Cat");
+ log.debug("PostUpdate for: {}", this.toString());
this.postVersion++;
}
@@ -152,4 +152,33 @@
public void setKittens(List<Kitten> kittens) {
this.kittens = kittens;
}
+
+ /**
+ * Constructs a <code>String</code> with all attributes
+ * in name = value format.
+ *
+ * @return a <code>String</code> representation
+ * of this object.
+ */
+ public String toString()
+ {
+ final String TAB = " ";
+
+ String retValue = "";
+
+ retValue = "Cat ( "
+ + super.toString() + TAB
+ + "id = " + this.id + TAB
+ + "name = " + this.name + TAB
+ + "dateOfBirth = " + this.dateOfBirth + TAB
+ + "age = " + this.age + TAB
+ + "length = " + this.length + TAB
+ + "lastUpdate = " + this.lastUpdate + TAB
+ + "manualVersion = " + this.manualVersion + TAB
+ + "postVersion = " + this.postVersion + TAB
+ + "kittens = " + this.kittens + TAB
+ + " )";
+
+ return retValue;
+ }
}
Modified: entitymanager/trunk/src/test/org/hibernate/ejb/test/Kitten.java
===================================================================
--- entitymanager/trunk/src/test/org/hibernate/ejb/test/Kitten.java 2008-06-30 19:22:31
UTC (rev 14832)
+++ entitymanager/trunk/src/test/org/hibernate/ejb/test/Kitten.java 2008-07-01 10:05:16
UTC (rev 14833)
@@ -1,13 +1,57 @@
-// $Id:$
+// $Id$
package org.hibernate.ejb.test;
import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
/**
* @author Hardy Ferentschik
*/
@SuppressWarnings("serial")
@Entity
-public class Kitten extends Cat {
+public class Kitten {
+
+ private Integer id;
+ private String name;
+ @Id
+ @GeneratedValue
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * Constructs a <code>String</code> with all attributes
+ * in name = value format.
+ *
+ * @return a <code>String</code> representation
+ * of this object.
+ */
+ public String toString()
+ {
+ final String TAB = " ";
+
+ String retValue = "";
+
+ retValue = "Kitten ( "
+ + super.toString() + TAB
+ + "id = " + this.id + TAB
+ + "name = " + this.name + TAB
+ + " )";
+
+ return retValue;
+ }
}
Modified:
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java
===================================================================
---
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java 2008-06-30
19:22:31 UTC (rev 14832)
+++
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java 2008-07-01
10:05:16 UTC (rev 14833)
@@ -172,6 +172,8 @@
* @see EJB-288
*/
public void testPostUpdateCollection() throws Exception {
+
+ // create a cat
EntityManager em = factory.createEntityManager();
Cat c = new Cat();
em.getTransaction().begin();
@@ -181,11 +183,13 @@
c.setDateOfBirth( new Date() );
em.persist( c );
em.getTransaction().commit();
+
+ // assert it is persisted
List ids = Cat.getIdList();
Object id = Cat.getIdList().get( ids.size() - 1 );
assertNotNull( id );
- // add a kitten to the cat (should trigger a PostCollectionRecreateEvent
+ // add a kitten to the cat (Triggers PostCollectionRecreateEvent. No PostUpdateEvent!)
int postVersion = c.getPostVersion();
em.getTransaction().begin();
Kitten kitty = new Kitten();
@@ -195,13 +199,30 @@
em.getTransaction().commit();
assertEquals("Post version should have been incremented.", postVersion + 1,
c.getPostVersion());
- // delete the kittens ()
+ // modify Kitty (Triggers PostUpdateEvent for Beetle. Expected
PostCollectionUpdateEvent.
+ // You also get PostUpdateEvent for Kitty which is fine)
postVersion = c.getPostVersion();
em.getTransaction().begin();
- c.getKittens().remove(kitty);
+ kitty.setName("Kitty");
em.getTransaction().commit();
assertEquals("Post version should have been incremented.", postVersion + 1,
c.getPostVersion());
+ // delete kitty (Triggers PostUpdateEvent and PostCollectionUpdateEvent for Beetle.
Expected only PostCollectionUpdateEvent)
+ postVersion = c.getPostVersion();
+ em.getTransaction().begin();
+ c.getKittens().remove(kitty);
+ em.getTransaction().commit();
+ // counter gets incremented twice. Should be only one event!?
+ assertEquals("Post version should have been incremented.", postVersion + 2,
c.getPostVersion());
+
+ // delete and recreate kittens (Triggers PostUpdateEvent, PostCollectionRemoveEvent and
PostCollectionRecreateEvent)
+ postVersion = c.getPostVersion();
+ em.getTransaction().begin();
+ c.setKittens(new ArrayList<Kitten>());
+ em.getTransaction().commit();
+ // counter gets incremented twice. Should be only one event!?
+ assertEquals("Post version should have been incremented.", postVersion + 3,
c.getPostVersion());
+
em.close();
}