001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.callbacks;
020:
021: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
022: import org.apache.openjpa.persistence.OpenJPAEntityManager;
023:
024: public class TestEntityListeners extends SingleEMFTestCase {
025:
026: public void setUp() {
027: setUp(CLEAR_TABLES);
028: ListenerImpl.prePersistCount = 0;
029: ListenerImpl.postPersistCount = 0;
030: ListenerImpl.preUpdateCount = 0;
031: ListenerImpl.postUpdateCount = 0;
032: ListenerImpl.preRemoveCount = 0;
033: ListenerImpl.postRemoveCount = 0;
034: ListenerImpl.postLoadCount = 0;
035: }
036:
037: @Override
038: protected String getPersistenceUnitName() {
039: return "listener-pu";
040: }
041:
042: public void testEntityListeners() {
043: helper(true);
044: }
045:
046: public void testGlobalListeners() {
047: helper(false);
048: }
049:
050: public void helper(boolean entityListeners) {
051: OpenJPAEntityManager em = emf.createEntityManager();
052: try {
053: em.getTransaction().begin();
054: ListenerTestEntity o;
055: if (entityListeners)
056: o = new EntityListenerEntity();
057: else
058: o = new GlobalListenerEntity();
059: em.persist(o);
060:
061: assertStatus(1, 0, 0, 0, 0, 0, 0);
062:
063: em.getTransaction().commit();
064: long id = o.getId();
065: em.close();
066:
067: assertStatus(1, 1, 0, 0, 0, 0, 0);
068:
069: em = emf.createEntityManager();
070: em.getTransaction().begin();
071: if (entityListeners)
072: o = em.find(EntityListenerEntity.class, id);
073: else
074: o = em.find(GlobalListenerEntity.class, id);
075:
076: assertNotNull(o);
077: assertStatus(1, 1, 0, 0, 0, 0, 1);
078:
079: o.setValue(o.getValue() + 1);
080:
081: em.flush();
082: assertStatus(1, 1, 1, 1, 0, 0, 1);
083:
084: em.remove(o);
085: assertStatus(1, 1, 1, 1, 1, 0, 1);
086:
087: em.getTransaction().commit();
088:
089: assertStatus(1, 1, 1, 1, 1, 1, 1);
090:
091: em.close();
092: } finally {
093: if (em != null && em.getTransaction().isActive())
094: em.getTransaction().rollback();
095: if (em != null && em.isOpen())
096: em.close();
097: }
098: }
099:
100: private void assertStatus(int prePersist, int postPersist,
101: int preUpdate, int postUpdate, int preRemove,
102: int postRemove, int postLoad) {
103: assertEquals(prePersist, ListenerImpl.prePersistCount);
104: assertEquals(postPersist, ListenerImpl.postPersistCount);
105: assertEquals(preUpdate, ListenerImpl.preUpdateCount);
106: assertEquals(postUpdate, ListenerImpl.postUpdateCount);
107: assertEquals(preRemove, ListenerImpl.preRemoveCount);
108: assertEquals(postRemove, ListenerImpl.postRemoveCount);
109: assertEquals(postLoad, ListenerImpl.postLoadCount);
110: }
111:
112: }
|