001: /*
002: * TestConnectionRetainMode.java
003: *
004: * Created on October 10, 2006, 1:09 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: /*
011: * Licensed to the Apache Software Foundation (ASF) under one
012: * or more contributor license agreements. See the NOTICE file
013: * distributed with this work for additional information
014: * regarding copyright ownership. The ASF licenses this file
015: * to you under the Apache License, Version 2.0 (the
016: * "License"); you may not use this file except in compliance
017: * with the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing,
022: * software distributed under the License is distributed on an
023: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024: * KIND, either express or implied. See the License for the
025: * specific language governing permissions and limitations
026: * under the License.
027: */
028: package org.apache.openjpa.persistence.kernel;
029:
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import org.apache.openjpa.persistence.OpenJPAEntityManager;
034: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
035:
036: public class TestConnectionRetainMode extends BaseKernelTest {
037:
038: /**
039: * Creates a new instance of TestConnectionRetainMode
040: */
041: public TestConnectionRetainMode() {
042: }
043:
044: public TestConnectionRetainMode(String str) {
045: super (str);
046: }
047:
048: public void testOnDemand() throws Exception {
049: doTest("on-demand");
050: }
051:
052: public void testTransaction() throws Exception {
053: doTest("transaction");
054: }
055:
056: public void testPersistenceManager() throws Exception {
057: doTest("persistence-manager");
058: }
059:
060: public void doTest(String mode) throws Exception {
061: Map props = new HashMap();
062: props.put("openjpa.ConnectionRetainMode", mode);
063:
064: OpenJPAEntityManagerFactory factory = getEmf(props);
065: OpenJPAEntityManager pm = (OpenJPAEntityManager) factory
066: .createEntityManager();
067: Object initialConnection = pm.getConnection();
068:
069: try {
070: checkPM(pm, initialConnection, mode);
071:
072: pm.setOptimistic(true);
073: startTx(pm);
074:
075: Object optimisticConnection = pm.getConnection();
076: checkPM(pm, initialConnection, mode);
077: checkTransaction(pm, optimisticConnection, mode);
078:
079: rollbackTx(pm);
080: checkPM(pm, initialConnection, mode);
081:
082: pm.setOptimistic(false);
083: startTx(pm);
084:
085: Object pessimisticConnection = pm.getConnection();
086: checkPM(pm, initialConnection, mode);
087: checkTransaction(pm, pessimisticConnection, mode);
088:
089: rollbackTx(pm);
090: checkPM(pm, initialConnection, mode);
091: } finally {
092:
093: rollbackTx(pm);
094: endEm(pm);
095: }
096: }
097:
098: private void checkPM(OpenJPAEntityManager pm, Object c, String mode)
099: throws Exception {
100: if ("persistence-manager".equals(mode))
101: assertEquals(c, pm.getConnection());
102: }
103:
104: private void checkTransaction(OpenJPAEntityManager pm, Object c,
105: String mode) throws Exception {
106: if (!"on-demand".equals(mode) || !pm.getOptimistic())
107: assertEquals(c, pm.getConnection());
108: }
109: }
|