01: /*
02: * TestJDBCListener.java
03: *
04: * Created on October 6, 2006, 1:38 PM
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: /*
11: * Licensed to the Apache Software Foundation (ASF) under one
12: * or more contributor license agreements. See the NOTICE file
13: * distributed with this work for additional information
14: * regarding copyright ownership. The ASF licenses this file
15: * to you under the Apache License, Version 2.0 (the
16: * "License"); you may not use this file except in compliance
17: * with the License. You may obtain a copy of the License at
18: *
19: * http://www.apache.org/licenses/LICENSE-2.0
20: *
21: * Unless required by applicable law or agreed to in writing,
22: * software distributed under the License is distributed on an
23: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24: * KIND, either express or implied. See the License for the
25: * specific language governing permissions and limitations
26: * under the License.
27: */
28: package org.apache.openjpa.persistence.jdbc.schema;
29:
30: import java.util.*;
31: import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
32: import org.apache.openjpa.lib.jdbc.JDBCEvent;
33:
34: import org.apache.openjpa.persistence.jdbc.common.apps.*;
35:
36: import java.lang.annotation.Annotation;
37: import junit.framework.*;
38: import javax.persistence.EntityManager;
39: import javax.persistence.EntityManagerFactory;
40: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
41: import org.apache.openjpa.persistence.OpenJPAEntityManager;
42:
43: public class TestJDBCListener extends
44: org.apache.openjpa.persistence.jdbc.kernel.BaseJDBCTest {
45:
46: public static boolean commitOccurred;
47:
48: /** Creates a new instance of TestJDBCListener */
49: public TestJDBCListener() {
50: }
51:
52: public TestJDBCListener(String name) {
53: super (name);
54: }
55:
56: public void testJDBCListener() {
57: Map props = new HashMap();
58: props.put("openjpa.jdbc.JDBCListeners", Listener.class
59: .getName());
60:
61: OpenJPAEntityManagerFactory factory = (OpenJPAEntityManagerFactory) getEmf(props);
62:
63: commitOccurred = false;
64: OpenJPAEntityManager pm = factory.createEntityManager();
65:
66: pm.getTransaction().begin();
67: assertFalse(commitOccurred);
68: pm.persist(new RuntimeTest1("Listener test", 99));
69: pm.getTransaction().commit();
70: assertTrue("Commit event should have occurred, but did not",
71: commitOccurred);
72: pm.close();
73: }
74:
75: public static class Listener extends AbstractJDBCListener {
76:
77: public void beforeCommit(JDBCEvent event) {
78: commitOccurred = true;
79: }
80: }
81: }
|