Author: allee8285
Date: Wed Mar 28 20:52:08 2012
New Revision: 1306592
URL: http://svn.apache.org/viewvc?rev=1306592&view=rev
Log:
OPENJPA-2163 Change LifecycleEventManager PluginValue's singleton default value
from true to false. Add SingletonLifecycleEventManager property allow previous
property to switch singleton value back to true. Since LifecycleEventManager is
a internal property for BeanValidation support, the
SingletonLifecycleEventManager property will be kept as internal property as
well.
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/AddListenerEntity.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/ListenerInEntity.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/PerInstanceListener.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/TestMultiEmEntityListeners.java
(with props)
Modified:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java
Modified:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java?rev=1306592&r1=1306591&r2=1306592&view=diff
==============================================================================
---
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
(original)
+++
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
Wed Mar 28 20:52:08 2012
@@ -68,7 +68,6 @@ import org.apache.openjpa.lib.encryption
import org.apache.openjpa.lib.instrumentation.InstrumentationLevel;
import org.apache.openjpa.lib.instrumentation.InstrumentationProvider;
import org.apache.openjpa.lib.log.Log;
-import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.meta.MetaDataFactory;
import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.util.ClassResolver;
@@ -88,7 +87,7 @@ public class OpenJPAConfigurationImpl
extends ConfigurationImpl
implements OpenJPAConfiguration {
- private static final Localizer _loc =
Localizer.forPackage(OpenJPAConfigurationImpl.class);
+ // private static final Localizer _loc =
Localizer.forPackage(OpenJPAConfigurationImpl.class);
// cached state; some of this is created in getter methods, so make
// protected in case subclasses want to access without creating
@@ -168,7 +167,8 @@ public class OpenJPAConfigurationImpl
public StringValue validationMode;
public ObjectValue validationFactory;
public ObjectValue validator;
- public ObjectValue lifecycleEventManager;
+ public LEMValue lifecycleEventManager;
+ public SingletonLEMValue singletonLifecycleEventManager;
public StringValue validationGroupPrePersist;
public StringValue validationGroupPreUpdate;
public StringValue validationGroupPreRemove;
@@ -574,7 +574,7 @@ public class OpenJPAConfigurationImpl
queryTimeout.setDefault("-1");
queryTimeout.setDynamic(true);
- lifecycleEventManager = addPlugin("LifecycleEventManager", true);
+ lifecycleEventManager = addValue(new LEMValue("LifecycleEventManager",
false));
aliases = new String[] {
"default", LifecycleEventManager.class.getName(),
"validating", ValidatingLifecycleEventManager.class.getName(),
@@ -584,6 +584,11 @@ public class OpenJPAConfigurationImpl
lifecycleEventManager.setString(aliases[0]);
lifecycleEventManager.setInstantiatingGetter("getLifecycleEventManagerInstance");
+ singletonLifecycleEventManager = (SingletonLEMValue) addValue(new
SingletonLEMValue(
+ "SingletonLifecycleEventManager"));
+ singletonLifecycleEventManager.setDefault("false");
+ singletonLifecycleEventManager.set(false);
+
dynamicEnhancementAgent = addBoolean("DynamicEnhancementAgent");
dynamicEnhancementAgent.setDefault("true");
dynamicEnhancementAgent.set(true);
@@ -1772,6 +1777,41 @@ public class OpenJPAConfigurationImpl
}
}
+ class LEMValue extends PluginValue {
+ boolean singleton;
+
+ public LEMValue(String prop, boolean singleton) {
+ super(prop, true);
+ this.singleton = singleton;
+ }
+
+ public Object get() {
+ return this.singleton ? super.get() : null;
+ }
+ }
+
+ public boolean isSingletonLifecycleEventManager() {
+ return singletonLifecycleEventManager.get();
+ }
+
+ public void setSingletonLifecycleEventManager(boolean singleton) {
+ singletonLifecycleEventManager.set(singleton);
+ }
+
+ class SingletonLEMValue extends BooleanValue {
+
+ public SingletonLEMValue(String prop) {
+ super(prop);
+ }
+
+ public void set(boolean value) {
+ super.set(value);
+ if (value && !lifecycleEventManager.singleton) {
+ lifecycleEventManager.singleton = true;
+ }
+ }
+ }
+
public boolean getDynamicEnhancementAgent() {
return dynamicEnhancementAgent.get();
}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/AddListenerEntity.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/AddListenerEntity.java?rev=1306592&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/AddListenerEntity.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/AddListenerEntity.java
Wed Mar 28 20:52:08 2012
@@ -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.openjpa.persistence.callbacks;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+
+@Entity
+public class AddListenerEntity {
+ @Id @GeneratedValue
+ private long id;
+
+ private int value;
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/AddListenerEntity.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/ListenerInEntity.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/ListenerInEntity.java?rev=1306592&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/ListenerInEntity.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/ListenerInEntity.java
Wed Mar 28 20:52:08 2012
@@ -0,0 +1,97 @@
+/*
+ * 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.openjpa.persistence.callbacks;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.PostLoad;
+import javax.persistence.PostPersist;
+import javax.persistence.PostRemove;
+import javax.persistence.PostUpdate;
+import javax.persistence.PrePersist;
+import javax.persistence.PreRemove;
+import javax.persistence.PreUpdate;
+
+@Entity
+public class ListenerInEntity {
+ @Id @GeneratedValue
+ private long id;
+
+ private int value;
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ transient int prePersistCount;
+ transient int postPersistCount;
+ transient int preUpdateCount;
+ transient int postUpdateCount;
+ transient int preRemoveCount;
+ transient int postRemoveCount;
+ transient int postLoadCount;
+
+ @PrePersist
+ public void prePersist() {
+ prePersistCount++;
+ }
+
+ @PostPersist
+ public void postPersist() {
+ postPersistCount++;
+ }
+
+ @PostLoad
+ public void postLoad() {
+ postLoadCount++;
+ }
+
+ @PreUpdate
+ public void preUpdate() {
+ preUpdateCount++;
+ }
+
+ @PostUpdate
+ public void postUpdate() {
+ postUpdateCount++;
+ }
+
+ @PreRemove
+ public void preRemove() {
+ preRemoveCount++;
+ }
+
+ @PostRemove
+ public void postRemove() {
+ postRemoveCount++;
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/ListenerInEntity.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/PerInstanceListener.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/PerInstanceListener.java?rev=1306592&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/PerInstanceListener.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/PerInstanceListener.java
Wed Mar 28 20:52:08 2012
@@ -0,0 +1,115 @@
+/*
+ * 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.openjpa.persistence.callbacks;
+
+import org.apache.openjpa.event.DeleteListener;
+import org.apache.openjpa.event.DirtyListener;
+import org.apache.openjpa.event.LifecycleEvent;
+import org.apache.openjpa.event.LoadListener;
+import org.apache.openjpa.event.PersistListener;
+import org.apache.openjpa.event.PostDeleteListener;
+import org.apache.openjpa.event.PostPersistListener;
+import org.apache.openjpa.event.StoreListener;
+
+public class PerInstanceListener implements PersistListener, DeleteListener,
DirtyListener,
+ LoadListener, StoreListener, PostPersistListener, PostDeleteListener {
+
+ int beforeStore;
+ int afterStore;
+ int afterLoad;
+ int afterRefresh;
+ int beforeDirty;
+ int afterDirty;
+ int beforeDirtyFlushed;
+ int afterDirtyFlushed;
+ int beforeDelete;
+ int afterDelete;
+ int beforePersist;
+ int afterPersist;
+
+ @Override
+ public void afterDeletePerformed(LifecycleEvent event) {
+ afterDelete++;
+ }
+
+ @Override
+ public void afterPersistPerformed(LifecycleEvent event) {
+ afterPersist++;
+ }
+
+ @Override
+ public void beforeStore(LifecycleEvent event) {
+ beforeStore++;
+ }
+
+ @Override
+ public void afterStore(LifecycleEvent event) {
+ afterStore++;
+ }
+
+ @Override
+ public void afterLoad(LifecycleEvent event) {
+ afterLoad++;
+ }
+
+ @Override
+ public void afterRefresh(LifecycleEvent event) {
+ afterRefresh++;
+ }
+
+ @Override
+ public void beforeDirty(LifecycleEvent event) {
+ beforeDirty++;
+ }
+
+ @Override
+ public void afterDirty(LifecycleEvent event) {
+ afterDirty++;
+ }
+
+ @Override
+ public void beforeDirtyFlushed(LifecycleEvent event) {
+ beforeDirtyFlushed++;
+ }
+
+ @Override
+ public void afterDirtyFlushed(LifecycleEvent event) {
+ afterDirtyFlushed++;
+ }
+
+ @Override
+ public void beforeDelete(LifecycleEvent event) {
+ beforeDelete++;
+ }
+
+ @Override
+ public void afterDelete(LifecycleEvent event) {
+ afterDelete++;
+ }
+
+ @Override
+ public void beforePersist(LifecycleEvent event) {
+ beforePersist++;
+ }
+
+ @Override
+ public void afterPersist(LifecycleEvent event) {
+ afterPersist++;
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/PerInstanceListener.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/TestMultiEmEntityListeners.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/TestMultiEmEntityListeners.java?rev=1306592&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/TestMultiEmEntityListeners.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/TestMultiEmEntityListeners.java
Wed Mar 28 20:52:08 2012
@@ -0,0 +1,241 @@
+/*
+ * 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.openjpa.persistence.callbacks;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestMultiEmEntityListeners extends SingleEMFTestCase {
+
+ public void setUp() {
+ setUp(CLEAR_TABLES, ListenerInEntity.class, AddListenerEntity.class
+// , "openjpa.SingletonLifecycleEventManager", "true"
+ );
+ }
+
+ public void testListenerInEntity1() {
+ OpenJPAEntityManager em1 = null;
+ OpenJPAEntityManager em2 = null;
+ try {
+ em1 = emf.createEntityManager();
+ em2 = emf.createEntityManager();
+
+ ListenerInEntity o1 = new ListenerInEntity();
+ ListenerInEntity o2 = new ListenerInEntity();
+
+ em1.getTransaction().begin();
+ em1.persist(o1);
+ assertListenerInEntityStatus(o1, 1, 0, 0, 0, 0, 0, 0);
+ assertListenerInEntityStatus(o2, 0, 0, 0, 0, 0, 0, 0);
+
+ em2.getTransaction().begin();
+ em2.persist(o2);
+ assertListenerInEntityStatus(o1, 1, 0, 0, 0, 0, 0, 0);
+ assertListenerInEntityStatus(o2, 1, 0, 0, 0, 0, 0, 0);
+
+ em2.getTransaction().commit();
+ long id2 = o2.getId();
+ assertListenerInEntityStatus(o1, 1, 0, 0, 0, 0, 0, 0);
+ assertListenerInEntityStatus(o2, 1, 1, 0, 0, 0, 0, 0);
+
+ em1.getTransaction().commit();
+ long id1 = o1.getId();
+ assertListenerInEntityStatus(o1, 1, 1, 0, 0, 0, 0, 0);
+ assertListenerInEntityStatus(o2, 1, 1, 0, 0, 0, 0, 0);
+
+ em1.clear();
+ ListenerInEntity fo1 = em1.find(ListenerInEntity.class, id1);
+ assertNotNull(fo1);
+ assertListenerInEntityStatus(fo1, 0, 0, 0, 0, 0, 0, 1);
+
+ em2.clear();
+ ListenerInEntity fo2 = em2.find(ListenerInEntity.class, id2);
+ assertNotNull(fo2);
+ assertListenerInEntityStatus(fo1, 0, 0, 0, 0, 0, 0, 1);
+ assertListenerInEntityStatus(fo2, 0, 0, 0, 0, 0, 0, 1);
+
+ em1.getTransaction().begin();
+ fo1.setValue(fo1.getValue() + 1);
+
+ em1.flush();
+ assertListenerInEntityStatus(fo1, 0, 0, 1, 1, 0, 0, 1);
+ assertListenerInEntityStatus(fo2, 0, 0, 0, 0, 0, 0, 1);
+
+ em2.getTransaction().begin();
+ fo2.setValue(fo2.getValue() + 1);
+
+ em2.flush();
+ assertListenerInEntityStatus(fo1, 0, 0, 1, 1, 0, 0, 1);
+ assertListenerInEntityStatus(fo2, 0, 0, 1, 1, 0, 0, 1);
+
+ em1.remove(fo1);
+ assertListenerInEntityStatus(fo1, 0, 0, 1, 1, 1, 0, 1);
+ assertListenerInEntityStatus(fo2, 0, 0, 1, 1, 0, 0, 1);
+
+ em2.remove(fo2);
+ assertListenerInEntityStatus(fo1, 0, 0, 1, 1, 1, 0, 1);
+ assertListenerInEntityStatus(fo2, 0, 0, 1, 1, 1, 0, 1);
+
+ em1.getTransaction().commit();
+
+ assertListenerInEntityStatus(fo1, 0, 0, 1, 1, 1, 1, 1);
+ assertListenerInEntityStatus(fo2, 0, 0, 1, 1, 1, 0, 1);
+
+ em2.getTransaction().commit();
+
+ assertListenerInEntityStatus(fo1, 0, 0, 1, 1, 1, 1, 1);
+ assertListenerInEntityStatus(fo2, 0, 0, 1, 1, 1, 1, 1);
+
+ em1.close();
+ em2.close();
+ } finally {
+ if (em1 != null && em1.getTransaction().isActive())
+ em1.getTransaction().rollback();
+ if (em1 != null && em1.isOpen())
+ em1.close();
+ if (em2 != null && em2.getTransaction().isActive())
+ em2.getTransaction().rollback();
+ if (em2 != null && em2.isOpen())
+ em2.close();
+ }
+ }
+
+ private void assertListenerInEntityStatus(ListenerInEntity l,
+ int prePersist, int postPersist,
+ int preUpdate, int postUpdate,
+ int preRemove, int postRemove,
+ int postLoad) {
+ assertEquals(prePersist, l.prePersistCount);
+ assertEquals(postPersist, l.postPersistCount);
+ assertEquals(preUpdate, l.preUpdateCount);
+ assertEquals(postUpdate, l.postUpdateCount);
+ assertEquals(preRemove, l.preRemoveCount);
+ assertEquals(postRemove, l.postRemoveCount);
+ assertEquals(postLoad, l.postLoadCount);
+ }
+
+ public void testAddListenerEntity1() {
+ OpenJPAEntityManager em1 = null;
+ OpenJPAEntityManager em2 = null;
+ try {
+ em1 = emf.createEntityManager();
+ PerInstanceListener l1 = new PerInstanceListener();
+ ((OpenJPAEntityManagerSPI) em1).addLifecycleListener(l1,
(Class<?>[])null);
+
+ em2 = emf.createEntityManager();
+ PerInstanceListener l2 = new PerInstanceListener();
+ ((OpenJPAEntityManagerSPI) em2).addLifecycleListener(l2,
(Class<?>[])null);
+
+ AddListenerEntity o1 = new AddListenerEntity();
+ AddListenerEntity o2 = new AddListenerEntity();
+
+ em1.getTransaction().begin();
+ em1.persist(o1);
+ assertAddListenerEntityStatus(l1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
+ assertAddListenerEntityStatus(l2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+
+ em2.getTransaction().begin();
+ em2.persist(o2);
+ assertAddListenerEntityStatus(l1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
+
+ em2.getTransaction().commit();
+ assertAddListenerEntityStatus(l1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0);
+ long id2 = o2.getId();
+
+ em1.getTransaction().commit();
+ assertAddListenerEntityStatus(l1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0);
+ long id1 = o1.getId();
+
+ em1.clear();
+ AddListenerEntity fo1 = em1.find(AddListenerEntity.class, id1);
+ assertAddListenerEntityStatus(l1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0);
+ assertNotNull(fo1);
+
+ em2.clear();
+ AddListenerEntity fo2 = em2.find(AddListenerEntity.class, id2);
+ assertAddListenerEntityStatus(l1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);
+ assertNotNull(fo2);
+
+ em1.getTransaction().begin();
+ fo1.setValue(fo1.getValue() + 1);
+ assertAddListenerEntityStatus(l1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);
+
+ em1.flush();
+ assertAddListenerEntityStatus(l1, 1, 1, 2, 2, 1, 0, 1, 1, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);
+
+ em2.getTransaction().begin();
+ fo2.setValue(fo2.getValue() + 1);
+ assertAddListenerEntityStatus(l1, 1, 1, 2, 2, 1, 0, 1, 1, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0);
+
+ em2.flush();
+ assertAddListenerEntityStatus(l1, 1, 1, 2, 2, 1, 0, 1, 1, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 2, 2, 1, 0, 1, 1, 0, 0);
+
+ em2.remove(fo2);
+ assertAddListenerEntityStatus(l1, 1, 1, 2, 2, 1, 0, 1, 1, 0, 0);
+ assertAddListenerEntityStatus(l2, 1, 1, 2, 2, 1, 0, 1, 1, 1, 1);
+
+ em1.remove(fo1);
+ assertAddListenerEntityStatus(l1, 1, 1, 2, 2, 1, 0, 1, 1, 1, 1);
+ assertAddListenerEntityStatus(l2, 1, 1, 2, 2, 1, 0, 1, 1, 1, 1);
+
+ em1.getTransaction().commit();
+ em2.getTransaction().commit();
+
+ em1.close();
+ em2.close();
+ } finally {
+ if (em1 != null && em1.getTransaction().isActive())
+ em1.getTransaction().rollback();
+ if (em1 != null && em1.isOpen())
+ em1.close();
+ if (em2 != null && em2.getTransaction().isActive())
+ em2.getTransaction().rollback();
+ if (em2 != null && em2.isOpen())
+ em2.close();
+ }
+ }
+
+ private void assertAddListenerEntityStatus(PerInstanceListener l
+ , int beforePersist, int afterPersist
+ , int beforeStore, int afterStore
+ , int afterLoad, int afterRefresh
+ , int beforeDirty, int afterDirty
+ , int beforeDelete, int afterDelete) {
+ assertEquals(beforePersist, l.beforePersist);
+ assertEquals(afterPersist, l.afterPersist);
+ assertEquals(beforeStore, l.beforeStore);
+ assertEquals(afterStore, l.afterStore);
+ assertEquals(afterLoad, l.afterLoad);
+ assertEquals(afterRefresh, l.afterRefresh);
+ assertEquals(beforeDirty, l.beforeDirty);
+ assertEquals(afterDirty, l.afterDirty);
+ assertEquals(beforeDelete, l.beforeDelete);
+ assertEquals(afterDelete, l.afterDelete);
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/TestMultiEmEntityListeners.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java?rev=1306592&r1=1306591&r2=1306592&view=diff
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java
(original)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java
Wed Mar 28 20:52:08 2012
@@ -19,8 +19,11 @@ import java.util.Map;
import javax.persistence.ValidationMode;
import org.apache.openjpa.conf.OpenJPAConfiguration;
-import org.apache.openjpa.lib.log.Log;
+import org.apache.openjpa.event.LifecycleEvent;
+import org.apache.openjpa.event.PersistListener;
+import org.apache.openjpa.event.StoreListener;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.PersistenceException;
import org.apache.openjpa.persistence.query.SimpleEntity;
@@ -246,6 +249,91 @@ public class TestValidationMode extends
}
}
+ /**
+ * Scenario being tested:
+ * 8) Life cycle event should be entity manager (Broker) specific.
+ */
+ public void testUniqueLifecycleManager() {
+ getLog().trace("testUniqueLifecycleManager() - Life cycle event
tests");
+ // create our EMF
+ Map<String,String> prop = new HashMap<String,String>();
+ prop.put("openjpa.jdbc.SynchronizeMappings",
"buildSchema(ForeignKeys=true)");
+// prop.put("openjpa.SingletonLifecycleEventManager", "true");
+ OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI)
+ OpenJPAPersistence.createEntityManagerFactory(
+ "simple",
+ "org/apache/openjpa/persistence/validation/persistence.xml",
+ prop);
+ assertNotNull(emf);
+ try {
+ final OpenJPAEntityManagerSPI em = emf.createEntityManager();
+ final OpenJPAEntityManagerSPI em2 = emf.createEntityManager();
+ UniqueLifecycleListener l1 = new UniqueLifecycleListener();
+ UniqueLifecycleListener l2 = new UniqueLifecycleListener();
+ em.addLifecycleListener(l1, (Class<?>[])null);
+ em2.addLifecycleListener(l2, (Class<?>[])null);
+
+ l1.assertCounts(0, 0, 0, 0);
+ l2.assertCounts(0, 0, 0, 0);
+
+ em.getTransaction().begin();
+ SimpleEntity e1 = new SimpleEntity();
+ em.persist(e1);
+ l1.assertCounts(1, 1, 0, 0);
+ l2.assertCounts(0, 0, 0, 0);
+
+ em2.getTransaction().begin();
+ SimpleEntity e2 = new SimpleEntity();
+ em2.persist(e2);
+ l1.assertCounts(1, 1, 0, 0);
+ l2.assertCounts(1, 1, 0, 0);
+
+ em2.getTransaction().commit();
+ l1.assertCounts(1, 1, 0, 0);
+ l2.assertCounts(1, 1, 1, 1);
+
+ em.getTransaction().commit();
+ l1.assertCounts(1, 1, 1, 1);
+ l2.assertCounts(1, 1, 1, 1);
+ } finally {
+ cleanup(emf);
+ }
+ }
+
+ class UniqueLifecycleListener implements PersistListener, StoreListener {
+
+ public int beforePersistCount;
+ public int afterPersistCount;
+ public int beforeStoreCount;
+ public int afterStoreCount;
+
+ @Override
+ public void beforePersist(LifecycleEvent event) {
+ beforePersistCount++;
+ }
+
+ @Override
+ public void afterPersist(LifecycleEvent event) {
+ afterPersistCount++;
+ }
+
+ @Override
+ public void beforeStore(LifecycleEvent event) {
+ beforeStoreCount++;
+ }
+
+ @Override
+ public void afterStore(LifecycleEvent event) {
+ afterStoreCount++;
+ }
+
+ public void assertCounts(int beforePersist, int afterPersist, int
beforeStore, int afterStore) {
+ assertEquals(beforePersist, beforePersistCount);
+ assertEquals(afterPersist, afterPersistCount);
+ assertEquals(beforeStore, beforeStoreCount);
+ assertEquals(afterStore, afterStoreCount);
+ }
+ }
/**
* Helper method to remove entities and close the emf an any open em's.