📄 persistencebrokertransactionmanagertests.java
字号:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed 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.springframework.orm.ojb;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import junit.framework.TestCase;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
import org.apache.ojb.broker.accesslayer.LookupException;
import org.easymock.MockControl;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionTemplate;
/**
* @author Juergen Hoeller
* @since 08.07.2004
*/
public class PersistenceBrokerTransactionManagerTests extends TestCase {
public void testTransactionCommit() throws LookupException, SQLException {
MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
final Object entity = new Object();
MockControl conControl = MockControl.createControl(Connection.class);
Connection con = (Connection) conControl.getMock();
pb.serviceConnectionManager();
pbControl.setReturnValue(cm, 2);
cm.getConnection();
cmControl.setReturnValue(con, 2);
con.isReadOnly();
conControl.setReturnValue(false, 1);
pb.beginTransaction();
pbControl.setVoidCallable(1);
pb.delete(entity);
pbControl.setVoidCallable(1);
pb.commitTransaction();
pbControl.setVoidCallable(1);
pb.close();
pbControl.setReturnValue(true, 1);
pbControl.replay();
cmControl.replay();
conControl.replay();
final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
protected PersistenceBroker getPersistenceBroker() {
return pb;
}
};
TransactionTemplate tt = new TransactionTemplate(tm);
assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
PersistenceBrokerTemplate pbt = new PersistenceBrokerTemplate();
pbt.delete(entity);
}
});
assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
pbControl.verify();
cmControl.verify();
conControl.verify();
}
public void testTransactionRollback() throws LookupException, SQLException {
MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
final Object entity = new Object();
MockControl conControl = MockControl.createControl(Connection.class);
Connection con = (Connection) conControl.getMock();
pb.serviceConnectionManager();
pbControl.setReturnValue(cm, 2);
cm.getConnection();
cmControl.setReturnValue(con, 2);
con.setReadOnly(true);
conControl.setVoidCallable(1);
con.getTransactionIsolation();
conControl.setReturnValue(Connection.TRANSACTION_READ_COMMITTED, 1);
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
conControl.setVoidCallable(1);
con.isReadOnly();
conControl.setReturnValue(true, 1);
con.setReadOnly(false);
conControl.setVoidCallable(1);
con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
conControl.setVoidCallable(1);
pb.beginTransaction();
pbControl.setVoidCallable(1);
pb.delete(entity);
pbControl.setVoidCallable(1);
pb.abortTransaction();
pbControl.setVoidCallable(1);
pb.close();
pbControl.setReturnValue(true, 1);
pbControl.replay();
cmControl.replay();
conControl.replay();
final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
protected PersistenceBroker getPersistenceBroker() {
return pb;
}
};
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setReadOnly(true);
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
PersistenceBrokerTemplate pbt = new PersistenceBrokerTemplate();
pbt.delete(entity);
status.setRollbackOnly();
}
});
assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
pbControl.verify();
cmControl.verify();
conControl.verify();
}
public void testParticipatingTransactionWithCommit() throws LookupException, SQLException {
MockControl pbControl = MockControl.createControl(PersistenceBroker.class);
final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock();
MockControl cmControl = MockControl.createControl(ConnectionManagerIF.class);
final ConnectionManagerIF cm = (ConnectionManagerIF) cmControl.getMock();
final Object entity = new Object();
MockControl conControl = MockControl.createControl(Connection.class);
Connection con = (Connection) conControl.getMock();
pb.serviceConnectionManager();
pbControl.setReturnValue(cm, 2);
cm.getConnection();
cmControl.setReturnValue(con, 2);
con.isReadOnly();
conControl.setReturnValue(false, 1);
pb.beginTransaction();
pbControl.setVoidCallable(1);
pb.delete(entity);
pbControl.setVoidCallable(1);
pb.commitTransaction();
pbControl.setVoidCallable(1);
pb.close();
pbControl.setReturnValue(true, 1);
pbControl.replay();
cmControl.replay();
conControl.replay();
final PersistenceBrokerTransactionManager tm = new PersistenceBrokerTransactionManager() {
protected PersistenceBroker getPersistenceBroker() {
return pb;
}
};
assertTrue("Hasn't thread broker", !TransactionSynchronizationManager.hasResource(tm.getPbKey()));
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
tt.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue("Has thread broker", TransactionSynchronizationManager.hasResource(tm.getPbKey()));
PersistenceBrokerTemplate pbt = new PersistenceBrokerTemplate();
pbt.delete(entity);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -