Moved tests from testsuite to jdbc
This commit is contained in:
@@ -1,901 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.jdbc.datasource;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import javax.transaction.Status;
|
||||
import javax.transaction.SystemException;
|
||||
import javax.transaction.Transaction;
|
||||
import javax.transaction.TransactionManager;
|
||||
import javax.transaction.UserTransaction;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.beans.factory.support.StaticListableBeanFactory;
|
||||
import org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup;
|
||||
import org.springframework.jdbc.datasource.lookup.IsolationLevelDataSourceRouter;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionException;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.jta.JtaTransactionManager;
|
||||
import org.springframework.transaction.jta.JtaTransactionObject;
|
||||
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 17.10.2005
|
||||
*/
|
||||
public class DataSourceJtaTransactionTests extends TestCase {
|
||||
|
||||
public void testJtaTransactionCommit() throws Exception {
|
||||
doTestJtaTransaction(false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionRollback() throws Exception {
|
||||
doTestJtaTransaction(true);
|
||||
}
|
||||
|
||||
private void doTestJtaTransaction(final boolean rollback) throws Exception {
|
||||
MockControl utControl = MockControl.createControl(UserTransaction.class);
|
||||
UserTransaction ut = (UserTransaction) utControl.getMock();
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(1);
|
||||
if (rollback) {
|
||||
ut.rollback();
|
||||
}
|
||||
else {
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
|
||||
ut.commit();
|
||||
}
|
||||
utControl.setVoidCallable(1);
|
||||
utControl.replay();
|
||||
|
||||
MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
final DataSource ds = (DataSource) dsControl.getMock();
|
||||
MockControl conControl = MockControl.createControl(Connection.class);
|
||||
Connection con = (Connection) conControl.getMock();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con, 1);
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
conControl.replay();
|
||||
dsControl.replay();
|
||||
|
||||
JtaTransactionManager ptm = new JtaTransactionManager(ut);
|
||||
TransactionTemplate tt = new TransactionTemplate(ptm);
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
|
||||
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
|
||||
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
|
||||
assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertTrue("Is new transaction", status.isNewTransaction());
|
||||
|
||||
Connection c = DataSourceUtils.getConnection(ds);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
|
||||
DataSourceUtils.releaseConnection(c, ds);
|
||||
|
||||
c = DataSourceUtils.getConnection(ds);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
|
||||
DataSourceUtils.releaseConnection(c, ds);
|
||||
|
||||
if (rollback) {
|
||||
status.setRollbackOnly();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
|
||||
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
|
||||
dsControl.verify();
|
||||
conControl.verify();
|
||||
utControl.verify();
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNew() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(false, false, false, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithAccessAfterResume() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(false, false, true, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithOpenOuterConnection() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(false, true, false, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithOpenOuterConnectionAccessed() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(false, true, true, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithTransactionAwareDataSource() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(false, false, true, true);
|
||||
}
|
||||
|
||||
public void testJtaTransactionRollbackWithPropagationRequiresNew() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(true, false, false, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionRollbackWithPropagationRequiresNewWithAccessAfterResume() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(true, false, true, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionRollbackWithPropagationRequiresNewWithOpenOuterConnection() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(true, true, false, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionRollbackWithPropagationRequiresNewWithOpenOuterConnectionAccessed() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(true, true, true, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionRollbackWithPropagationRequiresNewWithTransactionAwareDataSource() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNew(true, false, true, true);
|
||||
}
|
||||
|
||||
private void doTestJtaTransactionWithPropagationRequiresNew(
|
||||
final boolean rollback, final boolean openOuterConnection, final boolean accessAfterResume,
|
||||
final boolean useTransactionAwareDataSource) throws Exception {
|
||||
|
||||
MockControl utControl = MockControl.createControl(UserTransaction.class);
|
||||
UserTransaction ut = (UserTransaction) utControl.getMock();
|
||||
MockControl tmControl = MockControl.createControl(TransactionManager.class);
|
||||
TransactionManager tm = (TransactionManager) tmControl.getMock();
|
||||
MockControl txControl = MockControl.createControl(Transaction.class);
|
||||
Transaction tx = (Transaction) txControl.getMock();
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(1);
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 16);
|
||||
tm.suspend();
|
||||
tmControl.setReturnValue(tx, 5);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(5);
|
||||
ut.commit();
|
||||
utControl.setVoidCallable(5);
|
||||
tm.resume(tx);
|
||||
tmControl.setVoidCallable(5);
|
||||
if (rollback) {
|
||||
ut.rollback();
|
||||
}
|
||||
else {
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
|
||||
ut.commit();
|
||||
}
|
||||
utControl.setVoidCallable(1);
|
||||
utControl.replay();
|
||||
tmControl.replay();
|
||||
|
||||
final MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
final DataSource ds = (DataSource) dsControl.getMock();
|
||||
final MockControl conControl = MockControl.createControl(Connection.class);
|
||||
final Connection con = (Connection) conControl.getMock();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con, 1);
|
||||
con.isReadOnly();
|
||||
conControl.setReturnValue(true, 1);
|
||||
if (!openOuterConnection) {
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
}
|
||||
conControl.replay();
|
||||
dsControl.replay();
|
||||
|
||||
final DataSource dsToUse = useTransactionAwareDataSource ?
|
||||
new TransactionAwareDataSourceProxy(ds) : ds;
|
||||
|
||||
JtaTransactionManager ptm = new JtaTransactionManager(ut, tm);
|
||||
final TransactionTemplate tt = new TransactionTemplate(ptm);
|
||||
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
|
||||
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertTrue("Is new transaction", status.isNewTransaction());
|
||||
|
||||
Connection c = DataSourceUtils.getConnection(dsToUse);
|
||||
try {
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
c.isReadOnly();
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
|
||||
c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
if (!openOuterConnection) {
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertTrue("Is new transaction", status.isNewTransaction());
|
||||
|
||||
try {
|
||||
dsControl.verify();
|
||||
conControl.verify();
|
||||
dsControl.reset();
|
||||
conControl.reset();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con, 1);
|
||||
con.isReadOnly();
|
||||
conControl.setReturnValue(true, 1);
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
dsControl.replay();
|
||||
conControl.replay();
|
||||
|
||||
Connection c = DataSourceUtils.getConnection(dsToUse);
|
||||
c.isReadOnly();
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
|
||||
c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
if (rollback) {
|
||||
status.setRollbackOnly();
|
||||
}
|
||||
|
||||
if (accessAfterResume) {
|
||||
try {
|
||||
if (!openOuterConnection) {
|
||||
dsControl.verify();
|
||||
dsControl.reset();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con, 1);
|
||||
dsControl.replay();
|
||||
}
|
||||
conControl.verify();
|
||||
conControl.reset();
|
||||
con.isReadOnly();
|
||||
conControl.setReturnValue(true, 1);
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
conControl.replay();
|
||||
|
||||
if (!openOuterConnection) {
|
||||
c = DataSourceUtils.getConnection(dsToUse);
|
||||
}
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
c.isReadOnly();
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
|
||||
c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
if (openOuterConnection) {
|
||||
try {
|
||||
conControl.verify();
|
||||
conControl.reset();
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
conControl.replay();
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
}
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
|
||||
dsControl.verify();
|
||||
conControl.verify();
|
||||
utControl.verify();
|
||||
tmControl.verify();
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiredWithinSupports() throws Exception {
|
||||
doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction(false, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiredWithinNotSupported() throws Exception {
|
||||
doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction(false, true);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithinSupports() throws Exception {
|
||||
doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction(true, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithinNotSupported() throws Exception {
|
||||
doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction(true, true);
|
||||
}
|
||||
|
||||
private void doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction(
|
||||
final boolean requiresNew, boolean notSupported) throws Exception {
|
||||
|
||||
MockControl utControl = MockControl.createControl(UserTransaction.class);
|
||||
UserTransaction ut = (UserTransaction) utControl.getMock();
|
||||
MockControl tmControl = MockControl.createControl(TransactionManager.class);
|
||||
TransactionManager tm = (TransactionManager) tmControl.getMock();
|
||||
MockControl txControl = MockControl.createControl(Transaction.class);
|
||||
Transaction tx = (Transaction) txControl.getMock();
|
||||
if (notSupported) {
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
|
||||
tm.suspend();
|
||||
tmControl.setReturnValue(tx, 1);
|
||||
}
|
||||
else {
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
}
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(1);
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
|
||||
ut.commit();
|
||||
utControl.setVoidCallable(1);
|
||||
if (notSupported) {
|
||||
tm.resume(tx);
|
||||
tmControl.setVoidCallable(1);
|
||||
}
|
||||
utControl.replay();
|
||||
tmControl.replay();
|
||||
txControl.replay();
|
||||
|
||||
final MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
final DataSource ds = (DataSource) dsControl.getMock();
|
||||
final MockControl con1Control = MockControl.createControl(Connection.class);
|
||||
final Connection con1 = (Connection) con1Control.getMock();
|
||||
final MockControl con2Control = MockControl.createControl(Connection.class);
|
||||
final Connection con2 = (Connection) con2Control.getMock();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con1, 1);
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con2, 1);
|
||||
con2.close();
|
||||
con2Control.setVoidCallable(1);
|
||||
con1.close();
|
||||
con1Control.setVoidCallable(1);
|
||||
dsControl.replay();
|
||||
con1Control.replay();
|
||||
con2Control.replay();
|
||||
|
||||
final JtaTransactionManager ptm = new JtaTransactionManager(ut, tm);
|
||||
TransactionTemplate tt = new TransactionTemplate(ptm);
|
||||
tt.setPropagationBehavior(notSupported ?
|
||||
TransactionDefinition.PROPAGATION_NOT_SUPPORTED : TransactionDefinition.PROPAGATION_SUPPORTS);
|
||||
|
||||
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
|
||||
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
|
||||
assertSame(con1, DataSourceUtils.getConnection(ds));
|
||||
assertSame(con1, DataSourceUtils.getConnection(ds));
|
||||
|
||||
TransactionTemplate tt2 = new TransactionTemplate(ptm);
|
||||
tt2.setPropagationBehavior(requiresNew ?
|
||||
TransactionDefinition.PROPAGATION_REQUIRES_NEW : TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
tt2.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
|
||||
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
|
||||
assertSame(con2, DataSourceUtils.getConnection(ds));
|
||||
assertSame(con2, DataSourceUtils.getConnection(ds));
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
|
||||
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
|
||||
assertSame(con1, DataSourceUtils.getConnection(ds));
|
||||
}
|
||||
});
|
||||
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
|
||||
|
||||
utControl.verify();
|
||||
tmControl.verify();
|
||||
txControl.verify();
|
||||
dsControl.verify();
|
||||
con1Control.verify();
|
||||
con2Control.verify();
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewAndSuspendException() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNewAndBeginException(true, false, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithOpenOuterConnectionAndSuspendException() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNewAndBeginException(true, true, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithTransactionAwareDataSourceAndSuspendException() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNewAndBeginException(true, false, true);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithOpenOuterConnectionAndTransactionAwareDataSourceAndSuspendException() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNewAndBeginException(true, true, true);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewAndBeginException() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNewAndBeginException(false, false, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithOpenOuterConnectionAndBeginException() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNewAndBeginException(false, true, false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithOpenOuterConnectionAndTransactionAwareDataSourceAndBeginException() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNewAndBeginException(false, true, true);
|
||||
}
|
||||
|
||||
public void testJtaTransactionCommitWithPropagationRequiresNewWithTransactionAwareDataSourceAndBeginException() throws Exception {
|
||||
doTestJtaTransactionWithPropagationRequiresNewAndBeginException(false, false, true);
|
||||
}
|
||||
|
||||
private void doTestJtaTransactionWithPropagationRequiresNewAndBeginException(boolean suspendException,
|
||||
final boolean openOuterConnection, final boolean useTransactionAwareDataSource) throws Exception {
|
||||
|
||||
MockControl utControl = MockControl.createControl(UserTransaction.class);
|
||||
UserTransaction ut = (UserTransaction) utControl.getMock();
|
||||
MockControl tmControl = MockControl.createControl(TransactionManager.class);
|
||||
TransactionManager tm = (TransactionManager) tmControl.getMock();
|
||||
MockControl txControl = MockControl.createControl(Transaction.class);
|
||||
Transaction tx = (Transaction) txControl.getMock();
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(1);
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
|
||||
if (suspendException) {
|
||||
tm.suspend();
|
||||
tmControl.setThrowable(new SystemException(), 1);
|
||||
}
|
||||
else {
|
||||
tm.suspend();
|
||||
tmControl.setReturnValue(tx, 1);
|
||||
ut.begin();
|
||||
utControl.setThrowable(new SystemException(), 1);
|
||||
tm.resume(tx);
|
||||
tmControl.setVoidCallable(1);
|
||||
}
|
||||
ut.rollback();
|
||||
utControl.setVoidCallable(1);
|
||||
utControl.replay();
|
||||
tmControl.replay();
|
||||
|
||||
final MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
final DataSource ds = (DataSource) dsControl.getMock();
|
||||
final MockControl conControl = MockControl.createControl(Connection.class);
|
||||
final Connection con = (Connection) conControl.getMock();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con, 1);
|
||||
con.isReadOnly();
|
||||
conControl.setReturnValue(true, 1);
|
||||
if (!openOuterConnection || useTransactionAwareDataSource) {
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
}
|
||||
conControl.replay();
|
||||
dsControl.replay();
|
||||
|
||||
final DataSource dsToUse = useTransactionAwareDataSource ?
|
||||
new TransactionAwareDataSourceProxy(ds) : ds;
|
||||
if (dsToUse instanceof TransactionAwareDataSourceProxy) {
|
||||
((TransactionAwareDataSourceProxy) dsToUse).setReobtainTransactionalConnections(true);
|
||||
}
|
||||
|
||||
JtaTransactionManager ptm = new JtaTransactionManager(ut, tm);
|
||||
final TransactionTemplate tt = new TransactionTemplate(ptm);
|
||||
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
|
||||
|
||||
try {
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertTrue("Is new transaction", status.isNewTransaction());
|
||||
|
||||
Connection c = DataSourceUtils.getConnection(dsToUse);
|
||||
try {
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
c.isReadOnly();
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
|
||||
c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
if (!openOuterConnection) {
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
}
|
||||
|
||||
try {
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertTrue("Is new transaction", status.isNewTransaction());
|
||||
|
||||
try {
|
||||
dsControl.verify();
|
||||
conControl.verify();
|
||||
dsControl.reset();
|
||||
conControl.reset();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con, 1);
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
dsControl.replay();
|
||||
conControl.replay();
|
||||
|
||||
Connection c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
|
||||
c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
finally {
|
||||
if (openOuterConnection) {
|
||||
try {
|
||||
dsControl.verify();
|
||||
dsControl.reset();
|
||||
conControl.verify();
|
||||
conControl.reset();
|
||||
|
||||
if (useTransactionAwareDataSource) {
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con, 1);
|
||||
}
|
||||
con.isReadOnly();
|
||||
conControl.setReturnValue(true, 1);
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
dsControl.replay();
|
||||
conControl.replay();
|
||||
|
||||
c.isReadOnly();
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fail("Should have thrown TransactionException");
|
||||
}
|
||||
catch (TransactionException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
|
||||
dsControl.verify();
|
||||
conControl.verify();
|
||||
utControl.verify();
|
||||
tmControl.verify();
|
||||
}
|
||||
|
||||
public void testJtaTransactionWithConnectionHolderStillBound() throws Exception {
|
||||
MockControl utControl = MockControl.createControl(UserTransaction.class);
|
||||
UserTransaction ut = (UserTransaction) utControl.getMock();
|
||||
|
||||
MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
final DataSource ds = (DataSource) dsControl.getMock();
|
||||
MockControl conControl = MockControl.createControl(Connection.class);
|
||||
Connection con = (Connection) conControl.getMock();
|
||||
|
||||
JtaTransactionManager ptm = new JtaTransactionManager(ut) {
|
||||
protected void doRegisterAfterCompletionWithJtaTransaction(
|
||||
JtaTransactionObject txObject, final List synchronizations) {
|
||||
Thread async = new Thread() {
|
||||
public void run() {
|
||||
invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED);
|
||||
}
|
||||
};
|
||||
async.start();
|
||||
try {
|
||||
async.join();
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
TransactionTemplate tt = new TransactionTemplate(ptm);
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
|
||||
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
utControl.reset();
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
|
||||
utControl.replay();
|
||||
|
||||
dsControl.reset();
|
||||
conControl.reset();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con, 1);
|
||||
con.close();
|
||||
conControl.setVoidCallable(1);
|
||||
dsControl.replay();
|
||||
conControl.replay();
|
||||
|
||||
final boolean releaseCon = (i != 1);
|
||||
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertTrue("Is existing transaction", !status.isNewTransaction());
|
||||
|
||||
Connection c = DataSourceUtils.getConnection(ds);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
|
||||
DataSourceUtils.releaseConnection(c, ds);
|
||||
|
||||
c = DataSourceUtils.getConnection(ds);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
|
||||
if (releaseCon) {
|
||||
DataSourceUtils.releaseConnection(c, ds);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!releaseCon) {
|
||||
assertTrue("Still has connection holder", TransactionSynchronizationManager.hasResource(ds));
|
||||
}
|
||||
else {
|
||||
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
|
||||
}
|
||||
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
|
||||
|
||||
conControl.verify();
|
||||
dsControl.verify();
|
||||
utControl.verify();
|
||||
}
|
||||
}
|
||||
|
||||
public void testJtaTransactionWithIsolationLevelDataSourceAdapter() throws Exception {
|
||||
MockControl utControl = MockControl.createControl(UserTransaction.class);
|
||||
UserTransaction ut = (UserTransaction) utControl.getMock();
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(1);
|
||||
ut.commit();
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(1);
|
||||
ut.commit();
|
||||
utControl.setVoidCallable(1);
|
||||
utControl.replay();
|
||||
|
||||
MockControl ds1Control = MockControl.createControl(DataSource.class);
|
||||
final DataSource ds1 = (DataSource) ds1Control.getMock();
|
||||
MockControl con1Control = MockControl.createControl(Connection.class);
|
||||
final Connection con1 = (Connection) con1Control.getMock();
|
||||
ds1.getConnection();
|
||||
ds1Control.setReturnValue(con1, 1);
|
||||
con1.close();
|
||||
con1Control.setVoidCallable(1);
|
||||
ds1.getConnection();
|
||||
ds1Control.setReturnValue(con1, 1);
|
||||
con1.setReadOnly(true);
|
||||
con1Control.setVoidCallable(1);
|
||||
con1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
|
||||
con1Control.setVoidCallable(1);
|
||||
con1.close();
|
||||
con1Control.setVoidCallable(1);
|
||||
con1Control.replay();
|
||||
ds1Control.replay();
|
||||
|
||||
final IsolationLevelDataSourceAdapter dsToUse = new IsolationLevelDataSourceAdapter();
|
||||
dsToUse.setTargetDataSource(ds1);
|
||||
dsToUse.afterPropertiesSet();
|
||||
|
||||
JtaTransactionManager ptm = new JtaTransactionManager(ut);
|
||||
ptm.setAllowCustomIsolationLevels(true);
|
||||
|
||||
TransactionTemplate tt = new TransactionTemplate(ptm);
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
Connection c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertSame(con1, c);
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
});
|
||||
|
||||
tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
|
||||
tt.setReadOnly(true);
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
Connection c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertSame(con1, c);
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
});
|
||||
|
||||
ds1Control.verify();
|
||||
con1Control.verify();
|
||||
utControl.verify();
|
||||
}
|
||||
|
||||
public void testJtaTransactionWithIsolationLevelDataSourceRouter() throws Exception {
|
||||
doTestJtaTransactionWithIsolationLevelDataSourceRouter(false);
|
||||
}
|
||||
|
||||
public void testJtaTransactionWithIsolationLevelDataSourceRouterWithDataSourceLookup() throws Exception {
|
||||
doTestJtaTransactionWithIsolationLevelDataSourceRouter(true);
|
||||
}
|
||||
|
||||
private void doTestJtaTransactionWithIsolationLevelDataSourceRouter(boolean dataSourceLookup) throws Exception {
|
||||
MockControl utControl = MockControl.createControl(UserTransaction.class);
|
||||
UserTransaction ut = (UserTransaction) utControl.getMock();
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(1);
|
||||
ut.commit();
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
|
||||
ut.getStatus();
|
||||
utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
|
||||
ut.begin();
|
||||
utControl.setVoidCallable(1);
|
||||
ut.commit();
|
||||
utControl.setVoidCallable(1);
|
||||
utControl.replay();
|
||||
|
||||
MockControl ds1Control = MockControl.createControl(DataSource.class);
|
||||
final DataSource ds1 = (DataSource) ds1Control.getMock();
|
||||
MockControl con1Control = MockControl.createControl(Connection.class);
|
||||
final Connection con1 = (Connection) con1Control.getMock();
|
||||
ds1.getConnection();
|
||||
ds1Control.setReturnValue(con1, 1);
|
||||
con1.close();
|
||||
con1Control.setVoidCallable(1);
|
||||
con1Control.replay();
|
||||
ds1Control.replay();
|
||||
|
||||
MockControl ds2Control = MockControl.createControl(DataSource.class);
|
||||
final DataSource ds2 = (DataSource) ds2Control.getMock();
|
||||
MockControl con2Control = MockControl.createControl(Connection.class);
|
||||
final Connection con2 = (Connection) con2Control.getMock();
|
||||
ds2.getConnection();
|
||||
ds2Control.setReturnValue(con2, 1);
|
||||
con2.close();
|
||||
con2Control.setVoidCallable(1);
|
||||
con2Control.replay();
|
||||
ds2Control.replay();
|
||||
|
||||
final IsolationLevelDataSourceRouter dsToUse = new IsolationLevelDataSourceRouter();
|
||||
Map targetDataSources = new HashMap();
|
||||
if (dataSourceLookup) {
|
||||
targetDataSources.put("ISOLATION_REPEATABLE_READ", "ds2");
|
||||
dsToUse.setDefaultTargetDataSource("ds1");
|
||||
StaticListableBeanFactory beanFactory = new StaticListableBeanFactory();
|
||||
beanFactory.addBean("ds1", ds1);
|
||||
beanFactory.addBean("ds2", ds2);
|
||||
dsToUse.setDataSourceLookup(new BeanFactoryDataSourceLookup(beanFactory));
|
||||
}
|
||||
else {
|
||||
targetDataSources.put("ISOLATION_REPEATABLE_READ", ds2);
|
||||
dsToUse.setDefaultTargetDataSource(ds1);
|
||||
}
|
||||
dsToUse.setTargetDataSources(targetDataSources);
|
||||
dsToUse.afterPropertiesSet();
|
||||
|
||||
JtaTransactionManager ptm = new JtaTransactionManager(ut);
|
||||
ptm.setAllowCustomIsolationLevels(true);
|
||||
|
||||
TransactionTemplate tt = new TransactionTemplate(ptm);
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
Connection c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertSame(con1, c);
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
});
|
||||
|
||||
tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
|
||||
tt.execute(new TransactionCallbackWithoutResult() {
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
|
||||
Connection c = DataSourceUtils.getConnection(dsToUse);
|
||||
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
|
||||
assertSame(con2, c);
|
||||
DataSourceUtils.releaseConnection(c, dsToUse);
|
||||
}
|
||||
});
|
||||
|
||||
ds1Control.verify();
|
||||
con1Control.verify();
|
||||
ds2Control.verify();
|
||||
con2Control.verify();
|
||||
utControl.verify();
|
||||
}
|
||||
|
||||
protected void tearDown() {
|
||||
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
|
||||
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
|
||||
assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
|
||||
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
|
||||
assertNull(TransactionSynchronizationManager.getCurrentTransactionIsolationLevel());
|
||||
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,160 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.jdbc.datasource;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class DriverManagerDataSourceTests extends TestCase {
|
||||
|
||||
public void testStandardUsage() throws Exception {
|
||||
final String jdbcUrl = "url";
|
||||
final String uname = "uname";
|
||||
final String pwd = "pwd";
|
||||
|
||||
MockControl ctrlConnection =
|
||||
MockControl.createControl(Connection.class);
|
||||
final Connection mockConnection = (Connection) ctrlConnection.getMock();
|
||||
ctrlConnection.replay();
|
||||
|
||||
class TestDriverManagerDataSource extends DriverManagerDataSource {
|
||||
protected Connection getConnectionFromDriverManager(String url, Properties props) {
|
||||
assertEquals(jdbcUrl, url);
|
||||
assertEquals(uname, props.getProperty("user"));
|
||||
assertEquals(pwd, props.getProperty("password"));
|
||||
return mockConnection;
|
||||
}
|
||||
}
|
||||
|
||||
DriverManagerDataSource ds = new TestDriverManagerDataSource();
|
||||
//ds.setDriverClassName("foobar");
|
||||
ds.setUrl(jdbcUrl);
|
||||
ds.setUsername(uname);
|
||||
ds.setPassword(pwd);
|
||||
|
||||
Connection actualCon = ds.getConnection();
|
||||
assertTrue(actualCon == mockConnection);
|
||||
|
||||
assertTrue(ds.getUrl().equals(jdbcUrl));
|
||||
assertTrue(ds.getPassword().equals(pwd));
|
||||
assertTrue(ds.getUsername().equals(uname));
|
||||
|
||||
ctrlConnection.verify();
|
||||
}
|
||||
|
||||
public void testUsageWithConnectionProperties() throws Exception {
|
||||
final String jdbcUrl = "url";
|
||||
|
||||
final Properties connProps = new Properties();
|
||||
connProps.setProperty("myProp", "myValue");
|
||||
connProps.setProperty("yourProp", "yourValue");
|
||||
connProps.setProperty("user", "uname");
|
||||
connProps.setProperty("password", "pwd");
|
||||
|
||||
MockControl ctrlConnection =
|
||||
MockControl.createControl(Connection.class);
|
||||
final Connection mockConnection = (Connection) ctrlConnection.getMock();
|
||||
ctrlConnection.replay();
|
||||
|
||||
class TestDriverManagerDataSource extends DriverManagerDataSource {
|
||||
protected Connection getConnectionFromDriverManager(String url, Properties props) {
|
||||
assertEquals(jdbcUrl, url);
|
||||
assertEquals("uname", props.getProperty("user"));
|
||||
assertEquals("pwd", props.getProperty("password"));
|
||||
assertEquals("myValue", props.getProperty("myProp"));
|
||||
assertEquals("yourValue", props.getProperty("yourProp"));
|
||||
return mockConnection;
|
||||
}
|
||||
}
|
||||
|
||||
DriverManagerDataSource ds = new TestDriverManagerDataSource();
|
||||
//ds.setDriverClassName("foobar");
|
||||
ds.setUrl(jdbcUrl);
|
||||
ds.setConnectionProperties(connProps);
|
||||
|
||||
Connection actualCon = ds.getConnection();
|
||||
assertTrue(actualCon == mockConnection);
|
||||
|
||||
assertTrue(ds.getUrl().equals(jdbcUrl));
|
||||
|
||||
ctrlConnection.verify();
|
||||
}
|
||||
|
||||
public void testUsageWithConnectionPropertiesAndUserCredentials() throws Exception {
|
||||
final String jdbcUrl = "url";
|
||||
final String uname = "uname";
|
||||
final String pwd = "pwd";
|
||||
|
||||
final Properties connProps = new Properties();
|
||||
connProps.setProperty("myProp", "myValue");
|
||||
connProps.setProperty("yourProp", "yourValue");
|
||||
connProps.setProperty("user", "uname2");
|
||||
connProps.setProperty("password", "pwd2");
|
||||
|
||||
MockControl ctrlConnection =
|
||||
MockControl.createControl(Connection.class);
|
||||
final Connection mockConnection = (Connection) ctrlConnection.getMock();
|
||||
ctrlConnection.replay();
|
||||
|
||||
class TestDriverManagerDataSource extends DriverManagerDataSource {
|
||||
protected Connection getConnectionFromDriverManager(String url, Properties props) {
|
||||
assertEquals(jdbcUrl, url);
|
||||
assertEquals(uname, props.getProperty("user"));
|
||||
assertEquals(pwd, props.getProperty("password"));
|
||||
assertEquals("myValue", props.getProperty("myProp"));
|
||||
assertEquals("yourValue", props.getProperty("yourProp"));
|
||||
return mockConnection;
|
||||
}
|
||||
}
|
||||
|
||||
DriverManagerDataSource ds = new TestDriverManagerDataSource();
|
||||
//ds.setDriverClassName("foobar");
|
||||
ds.setUrl(jdbcUrl);
|
||||
ds.setUsername(uname);
|
||||
ds.setPassword(pwd);
|
||||
ds.setConnectionProperties(connProps);
|
||||
|
||||
Connection actualCon = ds.getConnection();
|
||||
assertTrue(actualCon == mockConnection);
|
||||
|
||||
assertTrue(ds.getUrl().equals(jdbcUrl));
|
||||
assertTrue(ds.getPassword().equals(pwd));
|
||||
assertTrue(ds.getUsername().equals(uname));
|
||||
|
||||
ctrlConnection.verify();
|
||||
}
|
||||
|
||||
public void testInvalidClassName() throws Exception {
|
||||
String bogusClassName = "foobar";
|
||||
DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
try {
|
||||
ds.setDriverClassName(bogusClassName);
|
||||
fail("Should have thrown IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// OK
|
||||
assertTrue(ex.getCause() instanceof ClassNotFoundException);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.jdbc.datasource;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 28.05.2004
|
||||
*/
|
||||
public class UserCredentialsDataSourceAdapterTests extends TestCase {
|
||||
|
||||
public void testStaticCredentials() throws SQLException {
|
||||
MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
DataSource ds = (DataSource) dsControl.getMock();
|
||||
MockControl conControl = MockControl.createControl(Connection.class);
|
||||
Connection con = (Connection) conControl.getMock();
|
||||
ds.getConnection("user", "pw");
|
||||
dsControl.setReturnValue(con);
|
||||
dsControl.replay();
|
||||
conControl.replay();
|
||||
|
||||
UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
|
||||
adapter.setTargetDataSource(ds);
|
||||
adapter.setUsername("user");
|
||||
adapter.setPassword("pw");
|
||||
assertEquals(con, adapter.getConnection());
|
||||
}
|
||||
|
||||
public void testNoCredentials() throws SQLException {
|
||||
MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
DataSource ds = (DataSource) dsControl.getMock();
|
||||
MockControl conControl = MockControl.createControl(Connection.class);
|
||||
Connection con = (Connection) conControl.getMock();
|
||||
ds.getConnection();
|
||||
dsControl.setReturnValue(con);
|
||||
dsControl.replay();
|
||||
conControl.replay();
|
||||
|
||||
UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
|
||||
adapter.setTargetDataSource(ds);
|
||||
assertEquals(con, adapter.getConnection());
|
||||
}
|
||||
|
||||
public void testThreadBoundCredentials() throws SQLException {
|
||||
MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
DataSource ds = (DataSource) dsControl.getMock();
|
||||
MockControl conControl = MockControl.createControl(Connection.class);
|
||||
Connection con = (Connection) conControl.getMock();
|
||||
ds.getConnection("user", "pw");
|
||||
dsControl.setReturnValue(con);
|
||||
dsControl.replay();
|
||||
conControl.replay();
|
||||
|
||||
UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
|
||||
adapter.setTargetDataSource(ds);
|
||||
|
||||
adapter.setCredentialsForCurrentThread("user", "pw");
|
||||
try {
|
||||
assertEquals(con, adapter.getConnection());
|
||||
}
|
||||
finally {
|
||||
adapter.removeCredentialsFromCurrentThread();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.jdbc.object;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Types;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.jdbc.AbstractJdbcTests;
|
||||
import org.springframework.jdbc.core.SqlParameter;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 22.02.2005
|
||||
*/
|
||||
public class BatchSqlUpdateTests extends AbstractJdbcTests {
|
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled();
|
||||
|
||||
|
||||
public void testBatchUpdateWithExplicitFlush() throws Exception {
|
||||
doTestBatchUpdate(false);
|
||||
}
|
||||
|
||||
public void testBatchUpdateWithFlushThroughBatchSize() throws Exception {
|
||||
doTestBatchUpdate(true);
|
||||
}
|
||||
|
||||
private void doTestBatchUpdate(boolean flushThroughBatchSize) throws Exception {
|
||||
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
|
||||
final int[] ids = new int[] { 100, 200 };
|
||||
final int[] rowsAffected = new int[] { 1, 2 };
|
||||
|
||||
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
|
||||
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
|
||||
mockPreparedStatement.getConnection();
|
||||
ctrlPreparedStatement.setReturnValue(mockConnection);
|
||||
mockPreparedStatement.setObject(1, new Integer(ids[0]), Types.INTEGER);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.addBatch();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setObject(1, new Integer(ids[1]), Types.INTEGER);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.addBatch();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeBatch();
|
||||
ctrlPreparedStatement.setReturnValue(rowsAffected);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
|
||||
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
|
||||
mockDatabaseMetaData.supportsBatchUpdates();
|
||||
ctrlDatabaseMetaData.setReturnValue(true);
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
mockConnection.getMetaData();
|
||||
ctrlConnection.setReturnValue(mockDatabaseMetaData, 1);
|
||||
|
||||
ctrlPreparedStatement.replay();
|
||||
ctrlDatabaseMetaData.replay();
|
||||
replay();
|
||||
|
||||
BatchSqlUpdate update = new BatchSqlUpdate(mockDataSource, sql);
|
||||
update.declareParameter(new SqlParameter(Types.INTEGER));
|
||||
if (flushThroughBatchSize) {
|
||||
update.setBatchSize(2);
|
||||
}
|
||||
|
||||
update.update(ids[0]);
|
||||
update.update(ids[1]);
|
||||
|
||||
if (flushThroughBatchSize) {
|
||||
assertEquals(0, update.getQueueCount());
|
||||
assertEquals(2, update.getRowsAffected().length);
|
||||
}
|
||||
else {
|
||||
assertEquals(2, update.getQueueCount());
|
||||
assertEquals(0, update.getRowsAffected().length);
|
||||
}
|
||||
|
||||
int[] actualRowsAffected = update.flush();
|
||||
assertEquals(0, update.getQueueCount());
|
||||
|
||||
if (flushThroughBatchSize) {
|
||||
assertTrue("flush did not execute updates", actualRowsAffected.length == 0);
|
||||
}
|
||||
else {
|
||||
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
|
||||
assertEquals(rowsAffected[0], actualRowsAffected[0]);
|
||||
assertEquals(rowsAffected[1], actualRowsAffected[1]);
|
||||
}
|
||||
|
||||
actualRowsAffected = update.getRowsAffected();
|
||||
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
|
||||
assertEquals(rowsAffected[0], actualRowsAffected[0]);
|
||||
assertEquals(rowsAffected[1], actualRowsAffected[1]);
|
||||
|
||||
update.reset();
|
||||
assertEquals(0, update.getRowsAffected().length);
|
||||
|
||||
ctrlPreparedStatement.verify();
|
||||
ctrlDatabaseMetaData.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.jdbc.object;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.SqlInOutParameter;
|
||||
import org.springframework.jdbc.core.SqlOutParameter;
|
||||
import org.springframework.jdbc.core.SqlParameter;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
/**
|
||||
* @author Trevor Cook
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class RdbmsOperationTests extends TestCase {
|
||||
|
||||
public void testEmptySql() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
try {
|
||||
operation.compile();
|
||||
fail("Shouldn't allow compiling without sql statement");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetTypeAfterCompile() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setDataSource(new DriverManagerDataSource());
|
||||
operation.setSql("select * from mytable");
|
||||
operation.compile();
|
||||
try {
|
||||
operation.setTypes(new int[] {Types.INTEGER });
|
||||
fail("Shouldn't allow setting parameters after compile");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testDeclareParameterAfterCompile() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setDataSource(new DriverManagerDataSource());
|
||||
operation.setSql("select * from mytable");
|
||||
operation.compile();
|
||||
try {
|
||||
operation.declareParameter(new SqlParameter(Types.INTEGER));
|
||||
fail("Shouldn't allow setting parameters after compile");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testTooFewParameters() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setSql("select * from mytable");
|
||||
operation.setTypes(new int[] { Types.INTEGER });
|
||||
try {
|
||||
operation.validateParameters((Object[]) null);
|
||||
fail("Shouldn't validate without enough parameters");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testTooFewMapParameters() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setSql("select * from mytable");
|
||||
operation.setTypes(new int[] { Types.INTEGER });
|
||||
try {
|
||||
operation.validateNamedParameters((Map) null);
|
||||
fail("Shouldn't validate without enough parameters");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testOperationConfiguredViaJdbcTemplateMustGetDataSource() throws Exception {
|
||||
try {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setSql("foo");
|
||||
operation.compile();
|
||||
fail("Can't compile without providing a DataSource for the JdbcTemplate");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException ex) {
|
||||
// Check for helpful error message. Omit leading character
|
||||
// so as not to be fussy about case
|
||||
assertTrue(ex.getMessage().indexOf("ataSource") != -1);
|
||||
}
|
||||
}
|
||||
|
||||
public void testTooManyParameters() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setSql("select * from mytable");
|
||||
try {
|
||||
operation.validateParameters(new Object[] {new Integer(1), new Integer(2)});
|
||||
fail("Shouldn't validate with too many parameters");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testUnspecifiedMapParameters() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setSql("select * from mytable");
|
||||
try {
|
||||
Map params = new HashMap();
|
||||
params.put("col1", "value");
|
||||
operation.validateNamedParameters(params);
|
||||
fail("Shouldn't validate with unspecified parameters");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testCompileTwice() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setDataSource(new DriverManagerDataSource());
|
||||
operation.setSql("select * from mytable");
|
||||
operation.setTypes(null);
|
||||
operation.compile();
|
||||
operation.compile();
|
||||
}
|
||||
|
||||
public void testEmptyDataSource() {
|
||||
SqlOperation operation = new SqlOperation() {
|
||||
};
|
||||
operation.setSql("select * from mytable");
|
||||
try {
|
||||
operation.compile();
|
||||
fail("Shouldn't allow compiling without data source");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testParameterPropagation() {
|
||||
SqlOperation operation = new SqlOperation() {
|
||||
};
|
||||
DataSource ds = new DriverManagerDataSource();
|
||||
operation.setDataSource(ds);
|
||||
operation.setFetchSize(10);
|
||||
operation.setMaxRows(20);
|
||||
JdbcTemplate jt = operation.getJdbcTemplate();
|
||||
assertEquals(ds, jt.getDataSource());
|
||||
assertEquals(10, jt.getFetchSize());
|
||||
assertEquals(20, jt.getMaxRows());
|
||||
}
|
||||
|
||||
public void testValidateInOutParameter() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setDataSource(new DriverManagerDataSource());
|
||||
operation.setSql("DUMMY_PROC");
|
||||
operation.declareParameter(new SqlOutParameter("DUMMY_OUT_PARAM", Types.VARCHAR));
|
||||
operation.declareParameter(new SqlInOutParameter("DUMMY_IN_OUT_PARAM", Types.VARCHAR));
|
||||
operation.validateParameters(new Object[] {"DUMMY_VALUE1", "DUMMY_VALUE2"});
|
||||
}
|
||||
|
||||
public void testParametersSetWithList() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
DataSource ds = new DriverManagerDataSource();
|
||||
operation.setDataSource(ds);
|
||||
operation.setSql("select * from mytable where one = ? and two = ?");
|
||||
List l = new ArrayList();
|
||||
l.add(new SqlParameter("one", Types.NUMERIC));
|
||||
l.add(new SqlParameter("two", Types.VARCHAR));
|
||||
operation.setParameters(new SqlParameter[] {
|
||||
new SqlParameter("one", Types.NUMERIC),
|
||||
new SqlParameter("two", Types.NUMERIC)});
|
||||
operation.afterPropertiesSet();
|
||||
try {
|
||||
operation.validateParameters(new Object[] {new Integer(1), new String("2")});
|
||||
assertEquals(2, operation.getDeclaredParameters().size());
|
||||
// OK
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
fail("Should have validated with parameters set using List: " + idaauex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestRdbmsOperation extends RdbmsOperation {
|
||||
|
||||
protected void compileInternal() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,339 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.jdbc.object;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.jdbc.AbstractJdbcTests;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* @author Trevor Cook
|
||||
*/
|
||||
public class SqlFunctionTests extends AbstractJdbcTests {
|
||||
|
||||
private static final String FUNCTION = "select count(id) from mytable";
|
||||
private static final String FUNCTION_INT =
|
||||
"select count(id) from mytable where myparam = ?";
|
||||
private static final String FUNCTION_MIXED =
|
||||
"select count(id) from mytable where myparam = ? and mystring = ?";
|
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled();
|
||||
|
||||
private MockControl ctrlPreparedStatement;
|
||||
private PreparedStatement mockPreparedStatement;
|
||||
private MockControl ctrlResultSet;
|
||||
private ResultSet mockResultSet;
|
||||
private MockControl ctrlResultSetMetaData;
|
||||
private ResultSetMetaData mockResultSetMetaData;
|
||||
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
|
||||
mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
|
||||
ctrlResultSet = MockControl.createControl(ResultSet.class);
|
||||
mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (shouldVerify()) {
|
||||
ctrlPreparedStatement.verify();
|
||||
ctrlResultSet.verify();
|
||||
ctrlResultSetMetaData.verify();
|
||||
}
|
||||
}
|
||||
|
||||
protected void replay() {
|
||||
super.replay();
|
||||
ctrlPreparedStatement.replay();
|
||||
ctrlResultSet.replay();
|
||||
ctrlResultSetMetaData.replay();
|
||||
}
|
||||
|
||||
|
||||
public void testFunction() throws SQLException {
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 1);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(14));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(FUNCTION);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
SqlFunction function = new SqlFunction();
|
||||
function.setDataSource(mockDataSource);
|
||||
function.setSql(FUNCTION);
|
||||
function.compile();
|
||||
|
||||
int count = function.run();
|
||||
assertTrue("Function returned value 14", count == 14);
|
||||
}
|
||||
|
||||
public void testTooManyRows() throws SQLException {
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1, 2);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 2);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(14), 1);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(15), 1);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(FUNCTION);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
SqlFunction function = new SqlFunction(mockDataSource, FUNCTION);
|
||||
function.compile();
|
||||
|
||||
try {
|
||||
int count = function.run();
|
||||
fail("Shouldn't continue when too many rows returned");
|
||||
}
|
||||
catch (IncorrectResultSizeDataAccessException idaauex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testFunctionInt() throws SQLException {
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 1);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(14));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.INTEGER);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(FUNCTION_INT);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
SqlFunction function = new SqlFunction(mockDataSource, FUNCTION_INT);
|
||||
function.setTypes(new int[] { Types.INTEGER });
|
||||
function.compile();
|
||||
|
||||
int count = function.run(1);
|
||||
assertTrue("Function returned value 14", count == 14);
|
||||
}
|
||||
|
||||
public void testFunctionMixed() throws SQLException {
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 1);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(14));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.INTEGER);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setString(2, "rod");
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(FUNCTION_MIXED);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
SqlFunction function = new SqlFunction(
|
||||
mockDataSource, FUNCTION_MIXED, new int[] { Types.INTEGER, Types.VARCHAR });
|
||||
function.compile();
|
||||
|
||||
int count = function.run(new Object[] { new Integer(1), "rod" });
|
||||
assertTrue("Function returned value 14", count == 14);
|
||||
}
|
||||
|
||||
public void testFunctionWithStringResult() throws SQLException {
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 1);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue("14");
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.INTEGER);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setString(2, "rod");
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(FUNCTION_MIXED);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
SqlFunction function = new SqlFunction( mockDataSource, FUNCTION_MIXED);
|
||||
function.setTypes(new int[] { Types.INTEGER, Types.VARCHAR });
|
||||
function.compile();
|
||||
|
||||
String result = (String) function.runGeneric(new Object[] { new Integer(1), "rod" });
|
||||
assertTrue("Function returned value 14", "14".equals(result));
|
||||
}
|
||||
|
||||
public void testFunctionWithStringConvertedResult() throws SQLException {
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 1);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getString(1);
|
||||
ctrlResultSet.setReturnValue("14");
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.INTEGER);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setString(2, "rod");
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(FUNCTION_MIXED);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
SqlFunction function = new SqlFunction( mockDataSource, FUNCTION_MIXED);
|
||||
function.setTypes(new int[] { Types.INTEGER, Types.VARCHAR });
|
||||
function.setResultType(String.class);
|
||||
function.compile();
|
||||
|
||||
String result = (String) function.runGeneric(new Object[] { new Integer(1), "rod" });
|
||||
assertTrue("Function returned value 14", "14".equals(result));
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,585 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.jdbc.object;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.core.JdkVersion;
|
||||
import org.springframework.jdbc.AbstractJdbcTests;
|
||||
import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;
|
||||
import org.springframework.jdbc.core.SqlParameter;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
|
||||
/**
|
||||
* @author Trevor Cook
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class SqlUpdateTests extends AbstractJdbcTests {
|
||||
|
||||
private static final String UPDATE =
|
||||
"update seat_status set booking_id = null";
|
||||
private static final String UPDATE_INT =
|
||||
"update seat_status set booking_id = null where performance_id = ?";
|
||||
private static final String UPDATE_INT_INT =
|
||||
"update seat_status set booking_id = null where performance_id = ? and price_band_id = ?";
|
||||
private static final String UPDATE_NAMED_PARAMETERS =
|
||||
"update seat_status set booking_id = null where performance_id = :perfId and price_band_id = :priceId";
|
||||
private static final String UPDATE_STRING =
|
||||
"update seat_status set booking_id = null where name = ?";
|
||||
private static final String UPDATE_OBJECTS =
|
||||
"update seat_status set booking_id = null where performance_id = ? and price_band_id = ? and name = ? and confirmed = ?";
|
||||
private static final String INSERT_GENERATE_KEYS =
|
||||
"insert into show (name) values(?)";
|
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled();
|
||||
|
||||
private MockControl ctrlPreparedStatement;
|
||||
private PreparedStatement mockPreparedStatement;
|
||||
private MockControl ctrlResultSet;
|
||||
private ResultSet mockResultSet;
|
||||
private MockControl ctrlResultSetMetaData;
|
||||
private ResultSetMetaData mockResultSetMetaData;
|
||||
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
|
||||
mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (shouldVerify()) {
|
||||
ctrlPreparedStatement.verify();
|
||||
}
|
||||
}
|
||||
|
||||
protected void replay() {
|
||||
super.replay();
|
||||
ctrlPreparedStatement.replay();
|
||||
}
|
||||
|
||||
|
||||
public void testUpdate() throws SQLException {
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(1);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
Updater pc = new Updater();
|
||||
int rowsAffected = pc.run();
|
||||
assertEquals(1, rowsAffected);
|
||||
}
|
||||
|
||||
public void testUpdateInt() throws SQLException {
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(1);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE_INT);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
IntUpdater pc = new IntUpdater();
|
||||
int rowsAffected = pc.run(1);
|
||||
assertEquals(1, rowsAffected);
|
||||
}
|
||||
|
||||
public void testUpdateIntInt() throws SQLException {
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
|
||||
mockPreparedStatement.setObject(2, new Integer(1), Types.NUMERIC);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(1);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE_INT_INT);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
IntIntUpdater pc = new IntIntUpdater();
|
||||
int rowsAffected = pc.run(1, 1);
|
||||
assertEquals(1, rowsAffected);
|
||||
}
|
||||
|
||||
public void testNamedParameterUpdateWithUnnamedDeclarations() throws SQLException {
|
||||
doTestNamedParameterUpdate(false);
|
||||
}
|
||||
|
||||
public void testNamedParameterUpdateWithNamedDeclarations() throws SQLException {
|
||||
doTestNamedParameterUpdate(true);
|
||||
}
|
||||
|
||||
private void doTestNamedParameterUpdate(final boolean namedDeclarations) throws SQLException {
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
|
||||
mockPreparedStatement.setObject(2, new Integer(1), Types.DECIMAL);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(1);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE_INT_INT);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
class NamedParameterUpdater extends SqlUpdate {
|
||||
|
||||
public NamedParameterUpdater() {
|
||||
setSql(UPDATE_NAMED_PARAMETERS);
|
||||
setDataSource(mockDataSource);
|
||||
if (namedDeclarations) {
|
||||
declareParameter(new SqlParameter("priceId", Types.DECIMAL));
|
||||
declareParameter(new SqlParameter("perfId", Types.NUMERIC));
|
||||
}
|
||||
else {
|
||||
declareParameter(new SqlParameter(Types.NUMERIC));
|
||||
declareParameter(new SqlParameter(Types.DECIMAL));
|
||||
}
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run(int performanceId, int type) {
|
||||
Map params = new HashMap();
|
||||
params.put("perfId", new Integer(performanceId));
|
||||
params.put("priceId", new Integer(type));
|
||||
return updateByNamedParam(params);
|
||||
}
|
||||
}
|
||||
|
||||
NamedParameterUpdater pc = new NamedParameterUpdater();
|
||||
int rowsAffected = pc.run(1, 1);
|
||||
assertEquals(1, rowsAffected);
|
||||
}
|
||||
|
||||
public void testUpdateString() throws SQLException {
|
||||
mockPreparedStatement.setString(1, "rod");
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(1);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE_STRING);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
StringUpdater pc = new StringUpdater();
|
||||
int rowsAffected = pc.run("rod");
|
||||
assertEquals(1, rowsAffected);
|
||||
}
|
||||
|
||||
public void testUpdateMixed() throws SQLException {
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
|
||||
mockPreparedStatement.setObject(2, new Integer(1), Types.NUMERIC, 2);
|
||||
mockPreparedStatement.setString(3, "rod");
|
||||
mockPreparedStatement.setObject(4, Boolean.TRUE, Types.BOOLEAN);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(1);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE_OBJECTS);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
MixedUpdater pc = new MixedUpdater();
|
||||
int rowsAffected = pc.run(1, 1, "rod", true);
|
||||
assertEquals(1, rowsAffected);
|
||||
}
|
||||
|
||||
public void testUpdateAndGeneratedKeys() throws SQLException {
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("1", 2);
|
||||
|
||||
ctrlResultSet = MockControl.createControl(ResultSet.class);
|
||||
mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setString(1, "rod");
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(1);
|
||||
mockPreparedStatement.getGeneratedKeys();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(INSERT_GENERATE_KEYS, PreparedStatement.RETURN_GENERATED_KEYS);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
ctrlResultSet.replay();
|
||||
ctrlResultSetMetaData.replay();
|
||||
|
||||
GeneratedKeysUpdater pc = new GeneratedKeysUpdater();
|
||||
KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
|
||||
int rowsAffected = pc.run("rod", generatedKeyHolder);
|
||||
assertEquals(1, rowsAffected);
|
||||
assertEquals(1, generatedKeyHolder.getKeyList().size());
|
||||
assertEquals(11, generatedKeyHolder.getKey().intValue());
|
||||
}
|
||||
|
||||
public void testUpdateConstructor() throws SQLException {
|
||||
mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
|
||||
mockPreparedStatement.setObject(2, new Integer(1), Types.NUMERIC);
|
||||
mockPreparedStatement.setString(3, "rod");
|
||||
mockPreparedStatement.setObject(4, Boolean.TRUE, Types.BOOLEAN);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(1);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE_OBJECTS);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
ConstructorUpdater pc = new ConstructorUpdater();
|
||||
int rowsAffected = pc.run(1, 1, "rod", true);
|
||||
assertEquals(1, rowsAffected);
|
||||
}
|
||||
|
||||
public void testUnderMaxRows() throws SQLException {
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(3);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
MaxRowsUpdater pc = new MaxRowsUpdater();
|
||||
int rowsAffected = pc.run();
|
||||
assertEquals(3, rowsAffected);
|
||||
}
|
||||
|
||||
public void testMaxRows() throws SQLException {
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(5);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
MaxRowsUpdater pc = new MaxRowsUpdater();
|
||||
int rowsAffected = pc.run();
|
||||
assertEquals(5, rowsAffected);
|
||||
}
|
||||
|
||||
public void testOverMaxRows() throws SQLException {
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(8);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
MaxRowsUpdater pc = new MaxRowsUpdater();
|
||||
try {
|
||||
int rowsAffected = pc.run();
|
||||
fail("Shouldn't continue when too many rows affected");
|
||||
}
|
||||
catch (JdbcUpdateAffectedIncorrectNumberOfRowsException juaicrex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testRequiredRows() throws SQLException {
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(3);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
RequiredRowsUpdater pc = new RequiredRowsUpdater();
|
||||
int rowsAffected = pc.run();
|
||||
assertEquals(3, rowsAffected);
|
||||
}
|
||||
|
||||
public void testNotRequiredRows() throws SQLException {
|
||||
mockPreparedStatement.executeUpdate();
|
||||
ctrlPreparedStatement.setReturnValue(2);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(UPDATE);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
RequiredRowsUpdater pc = new RequiredRowsUpdater();
|
||||
try {
|
||||
int rowsAffected = pc.run();
|
||||
fail("Shouldn't continue when too many rows affected");
|
||||
}
|
||||
catch (JdbcUpdateAffectedIncorrectNumberOfRowsException juaicrex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class Updater extends SqlUpdate {
|
||||
|
||||
public Updater() {
|
||||
setSql(UPDATE);
|
||||
setDataSource(mockDataSource);
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run() {
|
||||
return update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class IntUpdater extends SqlUpdate {
|
||||
|
||||
public IntUpdater() {
|
||||
setSql(UPDATE_INT);
|
||||
setDataSource(mockDataSource);
|
||||
declareParameter(new SqlParameter(Types.NUMERIC));
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run(int performanceId) {
|
||||
return update(performanceId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class IntIntUpdater extends SqlUpdate {
|
||||
|
||||
public IntIntUpdater() {
|
||||
setSql(UPDATE_INT_INT);
|
||||
setDataSource(mockDataSource);
|
||||
declareParameter(new SqlParameter(Types.NUMERIC));
|
||||
declareParameter(new SqlParameter(Types.NUMERIC));
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run(int performanceId, int type) {
|
||||
return update(performanceId, type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class StringUpdater extends SqlUpdate {
|
||||
|
||||
public StringUpdater() {
|
||||
setSql(UPDATE_STRING);
|
||||
setDataSource(mockDataSource);
|
||||
declareParameter(new SqlParameter(Types.VARCHAR));
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run(String name) {
|
||||
return update(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class MixedUpdater extends SqlUpdate {
|
||||
|
||||
public MixedUpdater() {
|
||||
setSql(UPDATE_OBJECTS);
|
||||
setDataSource(mockDataSource);
|
||||
declareParameter(new SqlParameter(Types.NUMERIC));
|
||||
declareParameter(new SqlParameter(Types.NUMERIC, 2));
|
||||
declareParameter(new SqlParameter(Types.VARCHAR));
|
||||
declareParameter(new SqlParameter(Types.BOOLEAN));
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run(int performanceId, int type, String name, boolean confirmed) {
|
||||
Object[] params =
|
||||
new Object[] {new Integer(performanceId), new Integer(type), name,
|
||||
new Boolean(confirmed)};
|
||||
return update(params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class GeneratedKeysUpdater extends SqlUpdate {
|
||||
|
||||
public GeneratedKeysUpdater() {
|
||||
setSql(INSERT_GENERATE_KEYS);
|
||||
setDataSource(mockDataSource);
|
||||
declareParameter(new SqlParameter(Types.VARCHAR));
|
||||
setReturnGeneratedKeys(true);
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run(String name, KeyHolder generatedKeyHolder) {
|
||||
Object[] params = new Object[] {name};
|
||||
return update(params, generatedKeyHolder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class ConstructorUpdater extends SqlUpdate {
|
||||
|
||||
public ConstructorUpdater() {
|
||||
super(mockDataSource, UPDATE_OBJECTS,
|
||||
new int[] {Types.NUMERIC, Types.NUMERIC, Types.VARCHAR, Types.BOOLEAN });
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run(int performanceId, int type, String name, boolean confirmed) {
|
||||
Object[] params =
|
||||
new Object[] {
|
||||
new Integer(performanceId), new Integer(type), name, new Boolean(confirmed)};
|
||||
return update(params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class MaxRowsUpdater extends SqlUpdate {
|
||||
|
||||
public MaxRowsUpdater() {
|
||||
setSql(UPDATE);
|
||||
setDataSource(mockDataSource);
|
||||
setMaxRowsAffected(5);
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run() {
|
||||
return update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class RequiredRowsUpdater extends SqlUpdate {
|
||||
|
||||
public RequiredRowsUpdater() {
|
||||
setSql(UPDATE);
|
||||
setDataSource(mockDataSource);
|
||||
setRequiredRowsAffected(3);
|
||||
compile();
|
||||
}
|
||||
|
||||
public int run() {
|
||||
return update();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user