0001: /*
0002: * HA-JDBC: High-Availability JDBC
0003: * Copyright (c) 2004-2007 Paul Ferraro
0004: *
0005: * This library is free software; you can redistribute it and/or modify it
0006: * under the terms of the GNU Lesser General Public License as published by the
0007: * Free Software Foundation; either version 2.1 of the License, or (at your
0008: * option) any later version.
0009: *
0010: * This library is distributed in the hope that it will be useful, but WITHOUT
0011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
0013: * for more details.
0014: *
0015: * You should have received a copy of the GNU Lesser General Public License
0016: * along with this library; if not, write to the Free Software Foundation,
0017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0018: *
0019: * Contact: ferraro@users.sourceforge.net
0020: */
0021: package net.sf.hajdbc.sql;
0022:
0023: import java.lang.reflect.Proxy;
0024: import java.sql.Array;
0025: import java.sql.Blob;
0026: import java.sql.CallableStatement;
0027: import java.sql.Clob;
0028: import java.sql.Connection;
0029: import java.sql.DatabaseMetaData;
0030: import java.sql.NClob;
0031: import java.sql.PreparedStatement;
0032: import java.sql.ResultSet;
0033: import java.sql.SQLClientInfoException;
0034: import java.sql.SQLException;
0035: import java.sql.SQLWarning;
0036: import java.sql.SQLXML;
0037: import java.sql.Savepoint;
0038: import java.sql.Statement;
0039: import java.sql.Struct;
0040: import java.util.Collections;
0041: import java.util.Map;
0042: import java.util.Properties;
0043: import java.util.Set;
0044: import java.util.TreeMap;
0045: import java.util.concurrent.ExecutorService;
0046: import java.util.concurrent.Executors;
0047:
0048: import net.sf.hajdbc.Balancer;
0049: import net.sf.hajdbc.ColumnProperties;
0050: import net.sf.hajdbc.Database;
0051: import net.sf.hajdbc.DatabaseCluster;
0052: import net.sf.hajdbc.DatabaseMetaDataCache;
0053: import net.sf.hajdbc.DatabaseProperties;
0054: import net.sf.hajdbc.Dialect;
0055: import net.sf.hajdbc.MockDatabase;
0056: import net.sf.hajdbc.TableProperties;
0057: import net.sf.hajdbc.util.reflect.ProxyFactory;
0058:
0059: import org.easymock.EasyMock;
0060: import org.easymock.IAnswer;
0061: import org.testng.annotations.AfterMethod;
0062: import org.testng.annotations.BeforeClass;
0063: import org.testng.annotations.DataProvider;
0064: import org.testng.annotations.Test;
0065:
0066: /**
0067: * Unit test for {@link Connection}
0068: * @author Paul Ferraro
0069: */
0070: @SuppressWarnings({"unchecked","nls"})
0071: public class TestConnection implements Connection {
0072: private TransactionContext transactionContext = EasyMock
0073: .createStrictMock(TransactionContext.class);
0074: private Balancer balancer = EasyMock
0075: .createStrictMock(Balancer.class);
0076: private DatabaseCluster cluster = EasyMock
0077: .createStrictMock(DatabaseCluster.class);
0078: private FileSupport fileSupport = EasyMock
0079: .createStrictMock(FileSupport.class);
0080: private Dialect dialect = EasyMock.createStrictMock(Dialect.class);
0081: private DatabaseMetaDataCache metaData = EasyMock
0082: .createStrictMock(DatabaseMetaDataCache.class);
0083: private DatabaseProperties databaseProperties = EasyMock
0084: .createStrictMock(DatabaseProperties.class);
0085: private TableProperties tableProperties = EasyMock
0086: .createStrictMock(TableProperties.class);
0087: private ColumnProperties columnProperties = EasyMock
0088: .createStrictMock(ColumnProperties.class);
0089: private Connection connection1 = EasyMock
0090: .createStrictMock(java.sql.Connection.class);
0091: private Connection connection2 = EasyMock
0092: .createStrictMock(java.sql.Connection.class);
0093: private SQLProxy parent = EasyMock.createStrictMock(SQLProxy.class);
0094: private SQLProxy root = EasyMock.createStrictMock(SQLProxy.class);
0095: private Savepoint savepoint1 = EasyMock
0096: .createStrictMock(Savepoint.class);
0097: private Savepoint savepoint2 = EasyMock
0098: .createStrictMock(Savepoint.class);
0099:
0100: private Database database1 = new MockDatabase("1");
0101: private Database database2 = new MockDatabase("2");
0102: private Set<Database> databaseSet;
0103: private ExecutorService executor = Executors
0104: .newSingleThreadExecutor();
0105: private Connection connection;
0106: private ConnectionInvocationHandler handler;
0107: private IAnswer<InvocationStrategy> anwser = new IAnswer<InvocationStrategy>() {
0108: @Override
0109: public InvocationStrategy answer() throws Throwable {
0110: return (InvocationStrategy) EasyMock.getCurrentArguments()[0];
0111: }
0112: };
0113:
0114: @BeforeClass
0115: void init() throws Exception {
0116: Map<Database, Connection> map = new TreeMap<Database, Connection>();
0117: map.put(this .database1, this .connection1);
0118: map.put(this .database2, this .connection2);
0119:
0120: this .databaseSet = map.keySet();
0121:
0122: EasyMock.expect(this .parent.getDatabaseCluster()).andReturn(
0123: this .cluster);
0124:
0125: this .parent.addChild(EasyMock
0126: .isA(ConnectionInvocationHandler.class));
0127:
0128: this .replay();
0129:
0130: this .handler = new ConnectionInvocationHandler(new Object(),
0131: this .parent, EasyMock.createMock(Invoker.class), map,
0132: this .transactionContext, this .fileSupport);
0133: this .connection = ProxyFactory.createProxy(Connection.class,
0134: this .handler);
0135:
0136: this .verify();
0137: this .reset();
0138: }
0139:
0140: private Object[] objects() {
0141: return new Object[] { this .cluster, this .balancer,
0142: this .connection1, this .connection2, this .fileSupport,
0143: this .parent, this .root, this .savepoint1,
0144: this .savepoint2, this .dialect, this .metaData,
0145: this .databaseProperties, this .tableProperties,
0146: this .columnProperties, this .transactionContext };
0147: }
0148:
0149: void replay() {
0150: EasyMock.replay(this .objects());
0151: }
0152:
0153: void verify() {
0154: EasyMock.verify(this .objects());
0155: }
0156:
0157: @AfterMethod
0158: void reset() {
0159: EasyMock.reset(this .objects());
0160: }
0161:
0162: /**
0163: * @see java.sql.Connection#clearWarnings()
0164: */
0165: @Test
0166: public void clearWarnings() throws SQLException {
0167: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0168:
0169: this .connection1.clearWarnings();
0170: this .connection2.clearWarnings();
0171:
0172: this .replay();
0173:
0174: this .connection.clearWarnings();
0175:
0176: this .verify();
0177: }
0178:
0179: /**
0180: * @see java.sql.Connection#close()
0181: */
0182: @Test
0183: public void close() throws SQLException {
0184: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0185:
0186: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
0187: .andReturn(this .executor);
0188:
0189: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0190: this .balancer);
0191: EasyMock.expect(this .balancer.all())
0192: .andReturn(this .databaseSet);
0193:
0194: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
0195:
0196: this .root.retain(this .databaseSet);
0197:
0198: this .connection1.close();
0199: this .connection2.close();
0200:
0201: this .transactionContext.close();
0202:
0203: this .fileSupport.close();
0204:
0205: this .parent.removeChild(this .handler);
0206:
0207: this .replay();
0208:
0209: this .connection.close();
0210:
0211: this .verify();
0212: }
0213:
0214: /**
0215: * @see java.sql.Connection#commit()
0216: */
0217: @Test
0218: public void commit() throws SQLException {
0219: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0220:
0221: EasyMock.expect(this .cluster.getTransactionalExecutor())
0222: .andReturn(this .executor);
0223:
0224: EasyMock.expect(
0225: this .transactionContext.end(EasyMock
0226: .isA(DatabaseWriteInvocationStrategy.class)))
0227: .andAnswer(this .anwser);
0228:
0229: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0230: this .balancer);
0231: EasyMock.expect(this .balancer.all())
0232: .andReturn(this .databaseSet);
0233:
0234: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
0235:
0236: this .root.retain(this .databaseSet);
0237:
0238: this .connection1.commit();
0239: this .connection2.commit();
0240:
0241: this .replay();
0242:
0243: this .connection.commit();
0244:
0245: this .verify();
0246: }
0247:
0248: /**
0249: * @see java.sql.Connection#createStatement()
0250: */
0251: @SuppressWarnings("unchecked")
0252: @Test
0253: public Statement createStatement() throws SQLException {
0254: // Read/write connection
0255: Statement statement1 = EasyMock.createMock(Statement.class);
0256: Statement statement2 = EasyMock.createMock(Statement.class);
0257:
0258: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0259: 2);
0260:
0261: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
0262:
0263: EasyMock.expect(this .connection1.createStatement()).andReturn(
0264: statement1);
0265: EasyMock.expect(this .connection2.createStatement()).andReturn(
0266: statement2);
0267:
0268: this .replay();
0269:
0270: Statement result = this .connection.createStatement();
0271:
0272: this .verify();
0273:
0274: assert Proxy.isProxyClass(result.getClass());
0275:
0276: SQLProxy proxy = SQLProxy.class.cast(Proxy
0277: .getInvocationHandler(result));
0278:
0279: assert proxy.getObject(this .database1) == statement1;
0280: assert proxy.getObject(this .database2) == statement2;
0281:
0282: this .reset();
0283:
0284: // Read-only connection
0285: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0286: 2);
0287:
0288: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
0289:
0290: EasyMock.expect(this .connection1.createStatement()).andReturn(
0291: statement1);
0292:
0293: this .replay();
0294:
0295: result = this .connection.createStatement();
0296:
0297: this .verify();
0298:
0299: assert result == statement1;
0300:
0301: return result;
0302: }
0303:
0304: @DataProvider(name="int-int")
0305: Object[][] intIntProvider() {
0306: return new Object[][] { new Object[] {
0307: ResultSet.TYPE_SCROLL_SENSITIVE,
0308: ResultSet.CONCUR_UPDATABLE } };
0309: }
0310:
0311: /**
0312: * @see java.sql.Connection#createStatement(int, int)
0313: */
0314: @Test(dataProvider="int-int")
0315: @SuppressWarnings("unchecked")
0316: public Statement createStatement(int resultSetType,
0317: int resultSetConcurrency) throws SQLException {
0318: // Read/write connection
0319: Statement statement1 = EasyMock.createMock(Statement.class);
0320: Statement statement2 = EasyMock.createMock(Statement.class);
0321:
0322: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0323: 2);
0324:
0325: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
0326:
0327: EasyMock.expect(
0328: this .connection1.createStatement(resultSetType,
0329: resultSetConcurrency)).andReturn(statement1);
0330: EasyMock.expect(
0331: this .connection2.createStatement(resultSetType,
0332: resultSetConcurrency)).andReturn(statement2);
0333:
0334: this .replay();
0335:
0336: Statement result = this .connection.createStatement(
0337: resultSetType, resultSetConcurrency);
0338:
0339: this .verify();
0340:
0341: assert Proxy.isProxyClass(result.getClass());
0342:
0343: SQLProxy proxy = SQLProxy.class.cast(Proxy
0344: .getInvocationHandler(result));
0345:
0346: assert proxy.getObject(this .database1) == statement1;
0347: assert proxy.getObject(this .database2) == statement2;
0348:
0349: this .reset();
0350:
0351: // Read-only connection
0352: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0353: 2);
0354:
0355: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
0356:
0357: EasyMock.expect(
0358: this .connection1.createStatement(resultSetType,
0359: resultSetConcurrency)).andReturn(statement1);
0360:
0361: this .replay();
0362:
0363: result = this .connection.createStatement(resultSetType,
0364: resultSetConcurrency);
0365:
0366: this .verify();
0367:
0368: assert result == statement1;
0369:
0370: return result;
0371: }
0372:
0373: @DataProvider(name="int-int-int")
0374: Object[][] intIntIntProvider() {
0375: return new Object[][] { new Object[] {
0376: ResultSet.TYPE_SCROLL_SENSITIVE,
0377: ResultSet.CONCUR_UPDATABLE,
0378: ResultSet.HOLD_CURSORS_OVER_COMMIT } };
0379: }
0380:
0381: /**
0382: * @see java.sql.Connection#createStatement(int, int, int)
0383: */
0384: @Test(dataProvider="int-int-int")
0385: @SuppressWarnings("unchecked")
0386: public Statement createStatement(int resultSetType,
0387: int resultSetConcurrency, int resultSetHoldability)
0388: throws SQLException {
0389: // Read/write connection
0390: Statement statement1 = EasyMock.createMock(Statement.class);
0391: Statement statement2 = EasyMock.createMock(Statement.class);
0392:
0393: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0394: 2);
0395:
0396: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
0397:
0398: EasyMock.expect(
0399: this .connection1.createStatement(resultSetType,
0400: resultSetConcurrency, resultSetHoldability))
0401: .andReturn(statement1);
0402: EasyMock.expect(
0403: this .connection2.createStatement(resultSetType,
0404: resultSetConcurrency, resultSetHoldability))
0405: .andReturn(statement2);
0406:
0407: this .replay();
0408:
0409: Statement result = this .connection.createStatement(
0410: resultSetType, resultSetConcurrency,
0411: resultSetHoldability);
0412:
0413: this .verify();
0414:
0415: assert Proxy.isProxyClass(result.getClass());
0416:
0417: SQLProxy proxy = SQLProxy.class.cast(Proxy
0418: .getInvocationHandler(result));
0419:
0420: assert proxy.getObject(this .database1) == statement1;
0421: assert proxy.getObject(this .database2) == statement2;
0422:
0423: this .reset();
0424:
0425: // Read-only connection
0426: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0427: 2);
0428:
0429: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
0430:
0431: EasyMock.expect(
0432: this .connection1.createStatement(resultSetType,
0433: resultSetConcurrency, resultSetHoldability))
0434: .andReturn(statement1);
0435:
0436: this .replay();
0437:
0438: result = this .connection.createStatement(resultSetType,
0439: resultSetConcurrency, resultSetHoldability);
0440:
0441: this .verify();
0442:
0443: assert result == statement1;
0444:
0445: return result;
0446: }
0447:
0448: /**
0449: * @see java.sql.Connection#getAutoCommit()
0450: */
0451: @Test
0452: public boolean getAutoCommit() throws SQLException {
0453: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0454:
0455: EasyMock.expect(this .connection1.getAutoCommit()).andReturn(
0456: true);
0457:
0458: this .replay();
0459:
0460: boolean autoCommit = this .connection.getAutoCommit();
0461:
0462: this .verify();
0463:
0464: assert autoCommit;
0465:
0466: return autoCommit;
0467: }
0468:
0469: /**
0470: * @see java.sql.Connection#getCatalog()
0471: */
0472: @Test
0473: public String getCatalog() throws SQLException {
0474: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0475:
0476: EasyMock.expect(this .connection1.getCatalog()).andReturn(
0477: "catalog");
0478:
0479: this .replay();
0480:
0481: String catalog = this .connection.getCatalog();
0482:
0483: this .verify();
0484:
0485: assert catalog.equals("catalog") : catalog;
0486:
0487: return catalog;
0488: }
0489:
0490: /**
0491: * @see java.sql.Connection#getHoldability()
0492: */
0493: @Test
0494: public int getHoldability() throws SQLException {
0495: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0496:
0497: EasyMock.expect(this .connection1.getHoldability()).andReturn(
0498: ResultSet.HOLD_CURSORS_OVER_COMMIT);
0499:
0500: this .replay();
0501:
0502: int holdability = this .connection.getHoldability();
0503:
0504: this .verify();
0505:
0506: assert holdability == ResultSet.HOLD_CURSORS_OVER_COMMIT : holdability;
0507:
0508: return holdability;
0509: }
0510:
0511: /**
0512: * @see java.sql.Connection#getMetaData()
0513: */
0514: @Test
0515: @SuppressWarnings("unchecked")
0516: public DatabaseMetaData getMetaData() throws SQLException {
0517: DatabaseMetaData metaData = EasyMock
0518: .createMock(DatabaseMetaData.class);
0519:
0520: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0521:
0522: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0523: this .balancer);
0524: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
0525:
0526: this .balancer.beforeInvocation(this .database2);
0527:
0528: EasyMock.expect(this .connection2.getMetaData()).andReturn(
0529: metaData);
0530:
0531: this .balancer.afterInvocation(this .database2);
0532:
0533: this .replay();
0534:
0535: DatabaseMetaData result = this .connection.getMetaData();
0536:
0537: this .verify();
0538:
0539: assert result == metaData;
0540:
0541: return result;
0542: }
0543:
0544: /**
0545: * @see java.sql.Connection#getTransactionIsolation()
0546: */
0547: @Test
0548: @SuppressWarnings("unchecked")
0549: public int getTransactionIsolation() throws SQLException {
0550: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0551:
0552: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0553: this .balancer);
0554: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
0555:
0556: this .balancer.beforeInvocation(this .database2);
0557:
0558: EasyMock.expect(this .connection2.getTransactionIsolation())
0559: .andReturn(java.sql.Connection.TRANSACTION_NONE);
0560:
0561: this .balancer.afterInvocation(this .database2);
0562:
0563: this .replay();
0564:
0565: int isolation = this .connection.getTransactionIsolation();
0566:
0567: this .verify();
0568:
0569: assert isolation == java.sql.Connection.TRANSACTION_NONE : isolation;
0570:
0571: return isolation;
0572: }
0573:
0574: /**
0575: * @see java.sql.Connection#getTypeMap()
0576: */
0577: @Test
0578: public Map<String, Class<?>> getTypeMap() throws SQLException {
0579: Map<String, Class<?>> map = Collections.emptyMap();
0580:
0581: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0582:
0583: EasyMock.expect(this .connection1.getTypeMap()).andReturn(map);
0584:
0585: this .replay();
0586:
0587: Map<String, Class<?>> result = this .connection.getTypeMap();
0588:
0589: this .verify();
0590:
0591: assert result == map;
0592:
0593: return result;
0594: }
0595:
0596: /**
0597: * @see java.sql.Connection#getWarnings()
0598: */
0599: @Test
0600: public SQLWarning getWarnings() throws SQLException {
0601: SQLWarning warning = new SQLWarning();
0602:
0603: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0604:
0605: EasyMock.expect(this .connection1.getWarnings()).andReturn(
0606: warning);
0607:
0608: this .replay();
0609:
0610: SQLWarning result = this .connection.getWarnings();
0611:
0612: this .verify();
0613:
0614: assert result == warning;
0615:
0616: return result;
0617: }
0618:
0619: /**
0620: * @see java.sql.Connection#isClosed()
0621: */
0622: @Test
0623: public boolean isClosed() throws SQLException {
0624: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0625:
0626: EasyMock.expect(this .connection1.isClosed()).andReturn(true);
0627:
0628: this .replay();
0629:
0630: boolean closed = this .connection.isClosed();
0631:
0632: this .verify();
0633:
0634: assert closed;
0635:
0636: return closed;
0637: }
0638:
0639: /**
0640: * @see java.sql.Connection#isReadOnly()
0641: */
0642: @Test
0643: public boolean isReadOnly() throws SQLException {
0644: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0645:
0646: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
0647:
0648: this .replay();
0649:
0650: boolean readOnly = this .connection.isReadOnly();
0651:
0652: this .verify();
0653:
0654: assert readOnly;
0655:
0656: return readOnly;
0657: }
0658:
0659: @DataProvider(name="string")
0660: Object[][] stringProvider() {
0661: return new Object[][] { new Object[] { "sql" } };
0662: }
0663:
0664: /**
0665: * @see java.sql.Connection#nativeSQL(java.lang.String)
0666: */
0667: @Test(dataProvider="string")
0668: public String nativeSQL(String sql) throws SQLException {
0669: EasyMock.expect(this .cluster.isActive()).andReturn(true);
0670:
0671: EasyMock.expect(this .connection1.nativeSQL(sql)).andReturn(
0672: "native-sql");
0673:
0674: this .replay();
0675:
0676: String nativeSQL = this .connection.nativeSQL(sql);
0677:
0678: this .verify();
0679:
0680: assert nativeSQL.equals("native-sql") : nativeSQL;
0681:
0682: return nativeSQL;
0683: }
0684:
0685: /**
0686: * @see java.sql.Connection#prepareCall(java.lang.String)
0687: */
0688: @Test(dataProvider="string")
0689: @SuppressWarnings("unchecked")
0690: public CallableStatement prepareCall(String sql)
0691: throws SQLException {
0692: CallableStatement statement1 = EasyMock
0693: .createMock(CallableStatement.class);
0694: CallableStatement statement2 = EasyMock
0695: .createMock(CallableStatement.class);
0696:
0697: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0698: 2);
0699:
0700: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
0701:
0702: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
0703: .andReturn(this .executor);
0704:
0705: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0706: this .balancer);
0707: EasyMock.expect(this .balancer.all())
0708: .andReturn(this .databaseSet);
0709:
0710: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
0711:
0712: this .root.retain(this .databaseSet);
0713:
0714: EasyMock.expect(this .connection1.prepareCall(sql)).andReturn(
0715: statement1);
0716: EasyMock.expect(this .connection2.prepareCall(sql)).andReturn(
0717: statement2);
0718:
0719: this .replay();
0720:
0721: CallableStatement result = this .connection.prepareCall(sql);
0722:
0723: this .verify();
0724:
0725: assert Proxy.isProxyClass(result.getClass());
0726:
0727: SQLProxy proxy = SQLProxy.class.cast(Proxy
0728: .getInvocationHandler(result));
0729:
0730: assert proxy.getObject(this .database1) == statement1;
0731: assert proxy.getObject(this .database2) == statement2;
0732:
0733: this .reset();
0734:
0735: // Read-only connection
0736: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0737: 2);
0738:
0739: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
0740:
0741: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0742: this .balancer);
0743: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
0744:
0745: this .balancer.beforeInvocation(this .database2);
0746:
0747: EasyMock.expect(this .connection2.prepareCall(sql)).andReturn(
0748: statement2);
0749:
0750: this .balancer.afterInvocation(this .database2);
0751:
0752: this .replay();
0753:
0754: result = this .connection.prepareCall(sql);
0755:
0756: this .verify();
0757:
0758: assert result == statement2;
0759:
0760: return result;
0761: }
0762:
0763: @DataProvider(name="string-int-int")
0764: Object[][] stringIntIntProvider() {
0765: return new Object[][] { new Object[] { "sql",
0766: ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY } };
0767: }
0768:
0769: /**
0770: * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
0771: */
0772: @Test(dataProvider="string-int-int")
0773: @SuppressWarnings("unchecked")
0774: public CallableStatement prepareCall(String sql, int resultSetType,
0775: int resultSetConcurrency) throws SQLException {
0776: CallableStatement statement1 = EasyMock
0777: .createMock(CallableStatement.class);
0778: CallableStatement statement2 = EasyMock
0779: .createMock(CallableStatement.class);
0780:
0781: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0782: 2);
0783:
0784: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
0785:
0786: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
0787: .andReturn(this .executor);
0788:
0789: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0790: this .balancer);
0791: EasyMock.expect(this .balancer.all())
0792: .andReturn(this .databaseSet);
0793:
0794: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
0795:
0796: this .root.retain(this .databaseSet);
0797:
0798: EasyMock.expect(
0799: this .connection1.prepareCall(sql, resultSetType,
0800: resultSetConcurrency)).andReturn(statement1);
0801: EasyMock.expect(
0802: this .connection2.prepareCall(sql, resultSetType,
0803: resultSetConcurrency)).andReturn(statement2);
0804:
0805: this .replay();
0806:
0807: CallableStatement result = this .connection.prepareCall(sql,
0808: resultSetType, resultSetConcurrency);
0809:
0810: this .verify();
0811:
0812: assert Proxy.isProxyClass(result.getClass());
0813:
0814: SQLProxy proxy = SQLProxy.class.cast(Proxy
0815: .getInvocationHandler(result));
0816:
0817: assert proxy.getObject(this .database1) == statement1;
0818: assert proxy.getObject(this .database2) == statement2;
0819:
0820: this .reset();
0821:
0822: // Read-only connection
0823: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0824: 2);
0825:
0826: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
0827:
0828: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0829: this .balancer);
0830: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
0831:
0832: this .balancer.beforeInvocation(this .database2);
0833:
0834: EasyMock.expect(
0835: this .connection2.prepareCall(sql, resultSetType,
0836: resultSetConcurrency)).andReturn(statement2);
0837:
0838: this .balancer.afterInvocation(this .database2);
0839:
0840: this .replay();
0841:
0842: result = this .connection.prepareCall(sql, resultSetType,
0843: resultSetConcurrency);
0844:
0845: this .verify();
0846:
0847: assert result == statement2;
0848:
0849: return result;
0850: }
0851:
0852: @DataProvider(name="string-int-int-int")
0853: Object[][] stringIntIntIntProvider() {
0854: return new Object[][] { new Object[] { "sql",
0855: ResultSet.TYPE_FORWARD_ONLY,
0856: ResultSet.CONCUR_READ_ONLY,
0857: ResultSet.CLOSE_CURSORS_AT_COMMIT } };
0858: }
0859:
0860: /**
0861: * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
0862: */
0863: @Test(dataProvider="string-int-int-int")
0864: @SuppressWarnings("unchecked")
0865: public CallableStatement prepareCall(String sql, int resultSetType,
0866: int resultSetConcurrency, int resultSetHoldability)
0867: throws SQLException {
0868: CallableStatement statement1 = EasyMock
0869: .createMock(CallableStatement.class);
0870: CallableStatement statement2 = EasyMock
0871: .createMock(CallableStatement.class);
0872:
0873: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0874: 2);
0875:
0876: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
0877:
0878: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
0879: .andReturn(this .executor);
0880:
0881: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0882: this .balancer);
0883: EasyMock.expect(this .balancer.all())
0884: .andReturn(this .databaseSet);
0885:
0886: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
0887:
0888: this .root.retain(this .databaseSet);
0889:
0890: EasyMock.expect(
0891: this .connection1.prepareCall(sql, resultSetType,
0892: resultSetConcurrency, resultSetHoldability))
0893: .andReturn(statement1);
0894: EasyMock.expect(
0895: this .connection2.prepareCall(sql, resultSetType,
0896: resultSetConcurrency, resultSetHoldability))
0897: .andReturn(statement2);
0898:
0899: this .replay();
0900:
0901: CallableStatement result = this .connection.prepareCall(sql,
0902: resultSetType, resultSetConcurrency,
0903: resultSetHoldability);
0904:
0905: this .verify();
0906:
0907: assert Proxy.isProxyClass(result.getClass());
0908:
0909: SQLProxy proxy = SQLProxy.class.cast(Proxy
0910: .getInvocationHandler(result));
0911:
0912: assert proxy.getObject(this .database1) == statement1;
0913: assert proxy.getObject(this .database2) == statement2;
0914:
0915: this .reset();
0916:
0917: // Read-only connection
0918: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0919: 2);
0920:
0921: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
0922:
0923: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0924: this .balancer);
0925: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
0926:
0927: this .balancer.beforeInvocation(this .database2);
0928:
0929: EasyMock.expect(
0930: this .connection2.prepareCall(sql, resultSetType,
0931: resultSetConcurrency, resultSetHoldability))
0932: .andReturn(statement2);
0933:
0934: this .balancer.afterInvocation(this .database2);
0935:
0936: this .replay();
0937:
0938: result = this .connection.prepareCall(sql, resultSetType,
0939: resultSetConcurrency, resultSetHoldability);
0940:
0941: this .verify();
0942:
0943: assert result == statement2;
0944:
0945: return result;
0946: }
0947:
0948: /**
0949: * @see java.sql.Connection#prepareStatement(java.lang.String)
0950: */
0951: @Test(dataProvider="string")
0952: @SuppressWarnings("unchecked")
0953: public PreparedStatement prepareStatement(String sql)
0954: throws SQLException {
0955: PreparedStatement statement1 = EasyMock
0956: .createMock(PreparedStatement.class);
0957: PreparedStatement statement2 = EasyMock
0958: .createMock(PreparedStatement.class);
0959:
0960: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
0961: 2);
0962:
0963: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
0964:
0965: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
0966: .andReturn(this .executor);
0967:
0968: EasyMock.expect(
0969: this .cluster.isCurrentTimestampEvaluationEnabled())
0970: .andReturn(false);
0971: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
0972: .andReturn(false);
0973: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
0974: .andReturn(false);
0975: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
0976: .andReturn(false);
0977:
0978: EasyMock.expect(this .cluster.getBalancer()).andReturn(
0979: this .balancer);
0980: EasyMock.expect(this .balancer.all())
0981: .andReturn(this .databaseSet);
0982:
0983: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
0984:
0985: this .root.retain(this .databaseSet);
0986:
0987: EasyMock.expect(this .connection1.prepareStatement(sql))
0988: .andReturn(statement1);
0989: EasyMock.expect(this .connection2.prepareStatement(sql))
0990: .andReturn(statement2);
0991:
0992: this .extractIdentifiers(sql);
0993:
0994: this .replay();
0995:
0996: PreparedStatement result = this .connection
0997: .prepareStatement(sql);
0998:
0999: this .verify();
1000:
1001: assert Proxy.isProxyClass(result.getClass());
1002:
1003: SQLProxy proxy = SQLProxy.class.cast(Proxy
1004: .getInvocationHandler(result));
1005:
1006: assert proxy.getObject(this .database1) == statement1;
1007: assert proxy.getObject(this .database2) == statement2;
1008:
1009: this .reset();
1010:
1011: // Read-only connection
1012: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1013: 2);
1014:
1015: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
1016:
1017: EasyMock.expect(
1018: this .cluster.isCurrentTimestampEvaluationEnabled())
1019: .andReturn(false);
1020: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1021: .andReturn(false);
1022: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1023: .andReturn(false);
1024: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1025: .andReturn(false);
1026:
1027: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1028: this .balancer);
1029: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
1030:
1031: this .balancer.beforeInvocation(this .database2);
1032:
1033: EasyMock.expect(this .connection2.prepareStatement(sql))
1034: .andReturn(statement2);
1035:
1036: this .balancer.afterInvocation(this .database2);
1037:
1038: this .replay();
1039:
1040: result = this .connection.prepareStatement(sql);
1041:
1042: this .verify();
1043:
1044: assert result == statement2;
1045:
1046: return result;
1047: }
1048:
1049: @DataProvider(name="string-int")
1050: Object[][] stringIntProvider() {
1051: return new Object[][] { new Object[] { "sql",
1052: Statement.NO_GENERATED_KEYS } };
1053: }
1054:
1055: /**
1056: * @see java.sql.Connection#prepareStatement(java.lang.String, int)
1057: */
1058: @Test(dataProvider="string-int")
1059: @SuppressWarnings("unchecked")
1060: public PreparedStatement prepareStatement(String sql,
1061: int autoGeneratedKeys) throws SQLException {
1062: PreparedStatement statement1 = EasyMock
1063: .createMock(PreparedStatement.class);
1064: PreparedStatement statement2 = EasyMock
1065: .createMock(PreparedStatement.class);
1066:
1067: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1068: 2);
1069:
1070: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
1071:
1072: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
1073: .andReturn(this .executor);
1074:
1075: EasyMock.expect(
1076: this .cluster.isCurrentTimestampEvaluationEnabled())
1077: .andReturn(false);
1078: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1079: .andReturn(false);
1080: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1081: .andReturn(false);
1082: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1083: .andReturn(false);
1084:
1085: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1086: this .balancer);
1087: EasyMock.expect(this .balancer.all())
1088: .andReturn(this .databaseSet);
1089:
1090: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1091:
1092: this .root.retain(this .databaseSet);
1093:
1094: EasyMock.expect(
1095: this .connection1.prepareStatement(sql,
1096: autoGeneratedKeys)).andReturn(statement1);
1097: EasyMock.expect(
1098: this .connection2.prepareStatement(sql,
1099: autoGeneratedKeys)).andReturn(statement2);
1100:
1101: this .extractIdentifiers(sql);
1102:
1103: this .replay();
1104:
1105: PreparedStatement result = this .connection.prepareStatement(
1106: sql, autoGeneratedKeys);
1107:
1108: this .verify();
1109:
1110: assert Proxy.isProxyClass(result.getClass());
1111:
1112: SQLProxy proxy = SQLProxy.class.cast(Proxy
1113: .getInvocationHandler(result));
1114:
1115: assert proxy.getObject(this .database1) == statement1;
1116: assert proxy.getObject(this .database2) == statement2;
1117:
1118: this .reset();
1119:
1120: // Read-only connection
1121: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1122: 2);
1123:
1124: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
1125:
1126: EasyMock.expect(
1127: this .cluster.isCurrentTimestampEvaluationEnabled())
1128: .andReturn(false);
1129: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1130: .andReturn(false);
1131: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1132: .andReturn(false);
1133: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1134: .andReturn(false);
1135:
1136: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1137: this .balancer);
1138: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
1139:
1140: this .balancer.beforeInvocation(this .database2);
1141:
1142: EasyMock.expect(
1143: this .connection2.prepareStatement(sql,
1144: autoGeneratedKeys)).andReturn(statement2);
1145:
1146: this .balancer.afterInvocation(this .database2);
1147:
1148: this .replay();
1149:
1150: result = this .connection.prepareStatement(sql,
1151: autoGeneratedKeys);
1152:
1153: this .verify();
1154:
1155: assert result == statement2;
1156:
1157: return result;
1158: }
1159:
1160: /**
1161: * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
1162: */
1163: @Test(dataProvider="string-int-int")
1164: @SuppressWarnings("unchecked")
1165: public PreparedStatement prepareStatement(String sql,
1166: int resultSetType, int resultSetConcurrency)
1167: throws SQLException {
1168: PreparedStatement statement1 = EasyMock
1169: .createMock(PreparedStatement.class);
1170: PreparedStatement statement2 = EasyMock
1171: .createMock(PreparedStatement.class);
1172:
1173: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1174: 2);
1175:
1176: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
1177:
1178: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
1179: .andReturn(this .executor);
1180:
1181: EasyMock.expect(
1182: this .cluster.isCurrentTimestampEvaluationEnabled())
1183: .andReturn(false);
1184: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1185: .andReturn(false);
1186: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1187: .andReturn(false);
1188: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1189: .andReturn(false);
1190:
1191: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1192: this .balancer);
1193: EasyMock.expect(this .balancer.all())
1194: .andReturn(this .databaseSet);
1195:
1196: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1197:
1198: this .root.retain(this .databaseSet);
1199:
1200: EasyMock.expect(
1201: this .connection1.prepareStatement(sql, resultSetType,
1202: resultSetConcurrency)).andReturn(statement1);
1203: EasyMock.expect(
1204: this .connection2.prepareStatement(sql, resultSetType,
1205: resultSetConcurrency)).andReturn(statement2);
1206:
1207: this .extractIdentifiers(sql);
1208:
1209: this .replay();
1210:
1211: PreparedStatement result = this .connection.prepareStatement(
1212: sql, resultSetType, resultSetConcurrency);
1213:
1214: this .verify();
1215:
1216: assert Proxy.isProxyClass(result.getClass());
1217:
1218: SQLProxy proxy = SQLProxy.class.cast(Proxy
1219: .getInvocationHandler(result));
1220:
1221: assert proxy.getObject(this .database1) == statement1;
1222: assert proxy.getObject(this .database2) == statement2;
1223:
1224: this .reset();
1225:
1226: // Read-only connection
1227: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1228: 2);
1229:
1230: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
1231:
1232: EasyMock.expect(
1233: this .cluster.isCurrentTimestampEvaluationEnabled())
1234: .andReturn(false);
1235: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1236: .andReturn(false);
1237: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1238: .andReturn(false);
1239: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1240: .andReturn(false);
1241:
1242: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1243: this .balancer);
1244: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
1245:
1246: this .balancer.beforeInvocation(this .database2);
1247:
1248: EasyMock.expect(
1249: this .connection2.prepareStatement(sql, resultSetType,
1250: resultSetConcurrency)).andReturn(statement2);
1251:
1252: this .balancer.afterInvocation(this .database2);
1253:
1254: this .replay();
1255:
1256: result = this .connection.prepareStatement(sql, resultSetType,
1257: resultSetConcurrency);
1258:
1259: this .verify();
1260:
1261: assert result == statement2;
1262:
1263: return result;
1264: }
1265:
1266: /**
1267: * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
1268: */
1269: @Test(dataProvider="string-int-int-int")
1270: @SuppressWarnings("unchecked")
1271: public PreparedStatement prepareStatement(String sql,
1272: int resultSetType, int resultSetConcurrency,
1273: int resultSetHoldability) throws SQLException {
1274: PreparedStatement statement1 = EasyMock
1275: .createMock(PreparedStatement.class);
1276: PreparedStatement statement2 = EasyMock
1277: .createMock(PreparedStatement.class);
1278:
1279: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1280: 2);
1281:
1282: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
1283:
1284: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
1285: .andReturn(this .executor);
1286:
1287: EasyMock.expect(
1288: this .cluster.isCurrentTimestampEvaluationEnabled())
1289: .andReturn(false);
1290: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1291: .andReturn(false);
1292: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1293: .andReturn(false);
1294: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1295: .andReturn(false);
1296:
1297: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1298: this .balancer);
1299: EasyMock.expect(this .balancer.all())
1300: .andReturn(this .databaseSet);
1301:
1302: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1303:
1304: this .root.retain(this .databaseSet);
1305:
1306: EasyMock.expect(
1307: this .connection1.prepareStatement(sql, resultSetType,
1308: resultSetConcurrency, resultSetHoldability))
1309: .andReturn(statement1);
1310: EasyMock.expect(
1311: this .connection2.prepareStatement(sql, resultSetType,
1312: resultSetConcurrency, resultSetHoldability))
1313: .andReturn(statement2);
1314:
1315: this .extractIdentifiers(sql);
1316:
1317: this .replay();
1318:
1319: PreparedStatement result = this .connection.prepareStatement(
1320: sql, resultSetType, resultSetConcurrency,
1321: resultSetHoldability);
1322:
1323: this .verify();
1324:
1325: assert Proxy.isProxyClass(result.getClass());
1326:
1327: SQLProxy proxy = SQLProxy.class.cast(Proxy
1328: .getInvocationHandler(result));
1329:
1330: assert proxy.getObject(this .database1) == statement1;
1331: assert proxy.getObject(this .database2) == statement2;
1332:
1333: this .reset();
1334:
1335: // Read-only connection
1336: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1337: 2);
1338:
1339: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
1340:
1341: EasyMock.expect(
1342: this .cluster.isCurrentTimestampEvaluationEnabled())
1343: .andReturn(false);
1344: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1345: .andReturn(false);
1346: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1347: .andReturn(false);
1348: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1349: .andReturn(false);
1350:
1351: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1352: this .balancer);
1353: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
1354:
1355: this .balancer.beforeInvocation(this .database2);
1356:
1357: EasyMock.expect(
1358: this .connection2.prepareStatement(sql, resultSetType,
1359: resultSetConcurrency, resultSetHoldability))
1360: .andReturn(statement2);
1361:
1362: this .balancer.afterInvocation(this .database2);
1363:
1364: this .replay();
1365:
1366: result = this .connection.prepareStatement(sql, resultSetType,
1367: resultSetConcurrency, resultSetHoldability);
1368:
1369: this .verify();
1370:
1371: assert result == statement2;
1372:
1373: return result;
1374: }
1375:
1376: @DataProvider(name="string-ints")
1377: Object[][] stringIntsProvider() {
1378: return new Object[][] { new Object[] { "sql", new int[] { 1 } } };
1379: }
1380:
1381: /**
1382: * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
1383: */
1384: @Test(dataProvider="string-ints")
1385: @SuppressWarnings("unchecked")
1386: public PreparedStatement prepareStatement(String sql,
1387: int[] columnIndexes) throws SQLException {
1388: PreparedStatement statement1 = EasyMock
1389: .createMock(PreparedStatement.class);
1390: PreparedStatement statement2 = EasyMock
1391: .createMock(PreparedStatement.class);
1392:
1393: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1394: 2);
1395:
1396: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
1397:
1398: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
1399: .andReturn(this .executor);
1400:
1401: EasyMock.expect(
1402: this .cluster.isCurrentTimestampEvaluationEnabled())
1403: .andReturn(false);
1404: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1405: .andReturn(false);
1406: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1407: .andReturn(false);
1408: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1409: .andReturn(false);
1410:
1411: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1412: this .balancer);
1413: EasyMock.expect(this .balancer.all())
1414: .andReturn(this .databaseSet);
1415:
1416: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1417:
1418: this .root.retain(this .databaseSet);
1419:
1420: EasyMock.expect(
1421: this .connection1.prepareStatement(sql, columnIndexes))
1422: .andReturn(statement1);
1423: EasyMock.expect(
1424: this .connection2.prepareStatement(sql, columnIndexes))
1425: .andReturn(statement2);
1426:
1427: this .extractIdentifiers(sql);
1428:
1429: this .replay();
1430:
1431: PreparedStatement result = this .connection.prepareStatement(
1432: sql, columnIndexes);
1433:
1434: this .verify();
1435:
1436: assert Proxy.isProxyClass(result.getClass());
1437:
1438: SQLProxy proxy = SQLProxy.class.cast(Proxy
1439: .getInvocationHandler(result));
1440:
1441: assert proxy.getObject(this .database1) == statement1;
1442: assert proxy.getObject(this .database2) == statement2;
1443:
1444: this .reset();
1445:
1446: // Read-only connection
1447: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1448: 2);
1449:
1450: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
1451:
1452: EasyMock.expect(
1453: this .cluster.isCurrentTimestampEvaluationEnabled())
1454: .andReturn(false);
1455: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1456: .andReturn(false);
1457: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1458: .andReturn(false);
1459: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1460: .andReturn(false);
1461:
1462: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1463: this .balancer);
1464: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
1465:
1466: this .balancer.beforeInvocation(this .database2);
1467:
1468: EasyMock.expect(
1469: this .connection2.prepareStatement(sql, columnIndexes))
1470: .andReturn(statement2);
1471:
1472: this .balancer.afterInvocation(this .database2);
1473:
1474: this .replay();
1475:
1476: result = this .connection.prepareStatement(sql, columnIndexes);
1477:
1478: this .verify();
1479:
1480: assert result == statement2;
1481:
1482: return result;
1483: }
1484:
1485: @DataProvider(name="string-strings")
1486: Object[][] stringStringsProvider() {
1487: return new Object[][] { new Object[] { "sql",
1488: new String[] { "col1" } } };
1489: }
1490:
1491: /**
1492: * @see java.sql.Connection#prepareStatement(java.lang.String, java.lang.String[])
1493: */
1494: @Test(dataProvider="string-strings")
1495: @SuppressWarnings("unchecked")
1496: public PreparedStatement prepareStatement(String sql,
1497: String[] columnNames) throws SQLException {
1498: PreparedStatement statement1 = EasyMock
1499: .createMock(PreparedStatement.class);
1500: PreparedStatement statement2 = EasyMock
1501: .createMock(PreparedStatement.class);
1502:
1503: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1504: 2);
1505:
1506: EasyMock.expect(this .connection1.isReadOnly()).andReturn(false);
1507:
1508: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
1509: .andReturn(this .executor);
1510:
1511: EasyMock.expect(
1512: this .cluster.isCurrentTimestampEvaluationEnabled())
1513: .andReturn(false);
1514: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1515: .andReturn(false);
1516: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1517: .andReturn(false);
1518: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1519: .andReturn(false);
1520:
1521: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1522: this .balancer);
1523: EasyMock.expect(this .balancer.all())
1524: .andReturn(this .databaseSet);
1525:
1526: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1527:
1528: this .root.retain(this .databaseSet);
1529:
1530: EasyMock.expect(
1531: this .connection1.prepareStatement(sql, columnNames))
1532: .andReturn(statement1);
1533: EasyMock.expect(
1534: this .connection2.prepareStatement(sql, columnNames))
1535: .andReturn(statement2);
1536:
1537: this .extractIdentifiers(sql);
1538:
1539: this .replay();
1540:
1541: PreparedStatement result = this .connection.prepareStatement(
1542: sql, columnNames);
1543:
1544: this .verify();
1545:
1546: assert Proxy.isProxyClass(result.getClass());
1547:
1548: SQLProxy proxy = SQLProxy.class.cast(Proxy
1549: .getInvocationHandler(result));
1550:
1551: assert proxy.getObject(this .database1) == statement1;
1552: assert proxy.getObject(this .database2) == statement2;
1553:
1554: this .reset();
1555:
1556: // Read-only connection
1557: EasyMock.expect(this .cluster.isActive()).andReturn(true).times(
1558: 2);
1559:
1560: EasyMock.expect(this .connection1.isReadOnly()).andReturn(true);
1561:
1562: EasyMock.expect(
1563: this .cluster.isCurrentTimestampEvaluationEnabled())
1564: .andReturn(false);
1565: EasyMock.expect(this .cluster.isCurrentDateEvaluationEnabled())
1566: .andReturn(false);
1567: EasyMock.expect(this .cluster.isCurrentTimeEvaluationEnabled())
1568: .andReturn(false);
1569: EasyMock.expect(this .cluster.isRandEvaluationEnabled())
1570: .andReturn(false);
1571:
1572: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1573: this .balancer);
1574: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
1575:
1576: this .balancer.beforeInvocation(this .database2);
1577:
1578: EasyMock.expect(
1579: this .connection2.prepareStatement(sql, columnNames))
1580: .andReturn(statement2);
1581:
1582: this .balancer.afterInvocation(this .database2);
1583:
1584: this .replay();
1585:
1586: result = this .connection.prepareStatement(sql, columnNames);
1587:
1588: this .verify();
1589:
1590: assert result == statement2;
1591:
1592: return result;
1593: }
1594:
1595: @DataProvider(name="savepoint")
1596: Object[][] savepointProvider() throws Exception {
1597: Map<Database, Savepoint> map = new TreeMap<Database, Savepoint>();
1598: map.put(this .database1, this .savepoint1);
1599: map.put(this .database2, this .savepoint2);
1600:
1601: return new Object[][] { new Object[] { ProxyFactory
1602: .createProxy(
1603: Savepoint.class,
1604: new SavepointInvocationHandler(this .connection,
1605: this .handler, EasyMock
1606: .createMock(Invoker.class), map)) } };
1607: }
1608:
1609: /**
1610: * @see java.sql.Connection#releaseSavepoint(Savepoint)
1611: */
1612: @Test(dataProvider="savepoint")
1613: public void releaseSavepoint(Savepoint savepoint)
1614: throws SQLException {
1615: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1616:
1617: EasyMock.expect(this .cluster.getTransactionalExecutor())
1618: .andReturn(this .executor);
1619:
1620: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1621: this .balancer);
1622: EasyMock.expect(this .balancer.all())
1623: .andReturn(this .databaseSet);
1624:
1625: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1626:
1627: this .root.retain(this .databaseSet);
1628:
1629: this .connection1.releaseSavepoint(this .savepoint1);
1630: this .connection2.releaseSavepoint(this .savepoint2);
1631:
1632: this .replay();
1633:
1634: this .connection.releaseSavepoint(savepoint);
1635:
1636: this .verify();
1637: }
1638:
1639: /**
1640: * @see java.sql.Connection#rollback()
1641: */
1642: @Test
1643: public void rollback() throws SQLException {
1644: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1645:
1646: EasyMock.expect(this .cluster.getTransactionalExecutor())
1647: .andReturn(this .executor);
1648:
1649: EasyMock.expect(
1650: this .transactionContext.end(EasyMock
1651: .isA(DatabaseWriteInvocationStrategy.class)))
1652: .andAnswer(this .anwser);
1653:
1654: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1655: this .balancer);
1656: EasyMock.expect(this .balancer.all())
1657: .andReturn(this .databaseSet);
1658:
1659: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1660:
1661: this .root.retain(this .databaseSet);
1662:
1663: this .connection1.rollback();
1664: this .connection2.rollback();
1665:
1666: this .replay();
1667:
1668: this .connection.rollback();
1669:
1670: this .verify();
1671: }
1672:
1673: /**
1674: * @see java.sql.Connection#rollback(Savepoint)
1675: */
1676: @Test(dataProvider="savepoint")
1677: public void rollback(Savepoint savepoint) throws SQLException {
1678: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1679:
1680: EasyMock.expect(this .cluster.getTransactionalExecutor())
1681: .andReturn(this .executor);
1682:
1683: EasyMock.expect(
1684: this .transactionContext.end(EasyMock
1685: .isA(DatabaseWriteInvocationStrategy.class)))
1686: .andAnswer(this .anwser);
1687:
1688: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1689: this .balancer);
1690: EasyMock.expect(this .balancer.all())
1691: .andReturn(this .databaseSet);
1692:
1693: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1694:
1695: this .root.retain(this .databaseSet);
1696:
1697: this .connection1.rollback(this .savepoint1);
1698: this .connection2.rollback(this .savepoint2);
1699:
1700: this .replay();
1701:
1702: this .connection.rollback(savepoint);
1703:
1704: this .verify();
1705: }
1706:
1707: @DataProvider(name="boolean")
1708: Object[][] booleanProvider() {
1709: return new Object[][] { new Object[] { true } };
1710: }
1711:
1712: /**
1713: * @see java.sql.Connection#setAutoCommit(boolean)
1714: */
1715: @Test(dataProvider="boolean")
1716: public void setAutoCommit(boolean autoCommit) throws SQLException {
1717: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1718:
1719: this .connection1.setAutoCommit(autoCommit);
1720: this .connection2.setAutoCommit(autoCommit);
1721:
1722: this .replay();
1723:
1724: this .connection.setAutoCommit(autoCommit);
1725:
1726: this .verify();
1727: }
1728:
1729: /**
1730: * @see java.sql.Connection#setCatalog(java.lang.String)
1731: */
1732: @Test(dataProvider="string")
1733: public void setCatalog(String catalog) throws SQLException {
1734: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1735:
1736: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
1737: .andReturn(this .executor);
1738:
1739: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1740: this .balancer);
1741: EasyMock.expect(this .balancer.all())
1742: .andReturn(this .databaseSet);
1743:
1744: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1745:
1746: this .root.retain(this .databaseSet);
1747:
1748: this .connection1.setCatalog(catalog);
1749: this .connection2.setCatalog(catalog);
1750:
1751: this .replay();
1752:
1753: this .connection.setCatalog(catalog);
1754:
1755: this .verify();
1756: }
1757:
1758: @DataProvider(name="holdability")
1759: Object[][] holdabilityProvider() {
1760: return new Object[][] { new Object[] { ResultSet.HOLD_CURSORS_OVER_COMMIT } };
1761: }
1762:
1763: /**
1764: * @see java.sql.Connection#setHoldability(int)
1765: */
1766: @Test(dataProvider="int")
1767: public void setHoldability(int holdability) throws SQLException {
1768: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1769:
1770: this .connection1.setHoldability(holdability);
1771: this .connection2.setHoldability(holdability);
1772:
1773: this .replay();
1774:
1775: this .connection.setHoldability(holdability);
1776:
1777: this .verify();
1778: }
1779:
1780: /**
1781: * @see java.sql.Connection#setReadOnly(boolean)
1782: */
1783: @Test(dataProvider="boolean")
1784: public void setReadOnly(boolean readOnly) throws SQLException {
1785: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1786:
1787: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
1788: .andReturn(this .executor);
1789:
1790: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1791: this .balancer);
1792: EasyMock.expect(this .balancer.all())
1793: .andReturn(this .databaseSet);
1794:
1795: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1796:
1797: this .root.retain(this .databaseSet);
1798:
1799: this .connection1.setReadOnly(readOnly);
1800: this .connection2.setReadOnly(readOnly);
1801:
1802: this .replay();
1803:
1804: this .connection.setReadOnly(readOnly);
1805:
1806: this .verify();
1807: }
1808:
1809: /**
1810: * @see java.sql.Connection#setSavepoint()
1811: */
1812: @Test
1813: @SuppressWarnings("unchecked")
1814: public Savepoint setSavepoint() throws SQLException {
1815: Savepoint savepoint1 = EasyMock.createMock(Savepoint.class);
1816: Savepoint savepoint2 = EasyMock.createMock(Savepoint.class);
1817:
1818: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1819:
1820: EasyMock.expect(this .cluster.getTransactionalExecutor())
1821: .andReturn(this .executor);
1822:
1823: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1824: this .balancer);
1825: EasyMock.expect(this .balancer.all())
1826: .andReturn(this .databaseSet);
1827:
1828: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1829:
1830: this .root.retain(this .databaseSet);
1831:
1832: EasyMock.expect(this .connection1.setSavepoint()).andReturn(
1833: savepoint1);
1834: EasyMock.expect(this .connection2.setSavepoint()).andReturn(
1835: savepoint2);
1836:
1837: this .replay();
1838:
1839: Savepoint result = this .connection.setSavepoint();
1840:
1841: this .verify();
1842:
1843: assert Proxy.isProxyClass(result.getClass());
1844:
1845: SQLProxy proxy = SQLProxy.class.cast(Proxy
1846: .getInvocationHandler(result));
1847:
1848: assert proxy.getObject(this .database1) == savepoint1;
1849: assert proxy.getObject(this .database2) == savepoint2;
1850:
1851: return result;
1852: }
1853:
1854: /**
1855: * @see java.sql.Connection#setSavepoint(java.lang.String)
1856: */
1857: @Test(dataProvider="string")
1858: @SuppressWarnings("unchecked")
1859: public Savepoint setSavepoint(String name) throws SQLException {
1860: Savepoint savepoint1 = EasyMock.createMock(Savepoint.class);
1861: Savepoint savepoint2 = EasyMock.createMock(Savepoint.class);
1862:
1863: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1864:
1865: EasyMock.expect(this .cluster.getTransactionalExecutor())
1866: .andReturn(this .executor);
1867:
1868: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1869: this .balancer);
1870: EasyMock.expect(this .balancer.all())
1871: .andReturn(this .databaseSet);
1872:
1873: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1874:
1875: this .root.retain(this .databaseSet);
1876:
1877: EasyMock.expect(this .connection1.setSavepoint(name)).andReturn(
1878: savepoint1);
1879: EasyMock.expect(this .connection2.setSavepoint(name)).andReturn(
1880: savepoint2);
1881:
1882: this .replay();
1883:
1884: Savepoint result = this .connection.setSavepoint(name);
1885:
1886: this .verify();
1887:
1888: assert Proxy.isProxyClass(result.getClass());
1889:
1890: SQLProxy proxy = SQLProxy.class.cast(Proxy
1891: .getInvocationHandler(result));
1892:
1893: assert proxy.getObject(this .database1) == savepoint1;
1894: assert proxy.getObject(this .database2) == savepoint2;
1895:
1896: return result;
1897: }
1898:
1899: @DataProvider(name="isolation")
1900: Object[][] isolationProvider() {
1901: return new Object[][] { new Object[] { java.sql.Connection.TRANSACTION_NONE } };
1902: }
1903:
1904: /**
1905: * @see java.sql.Connection#setTransactionIsolation(int)
1906: */
1907: @Test(dataProvider="isolation")
1908: public void setTransactionIsolation(int level) throws SQLException {
1909: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1910:
1911: EasyMock.expect(this .cluster.getNonTransactionalExecutor())
1912: .andReturn(this .executor);
1913:
1914: EasyMock.expect(this .cluster.getBalancer()).andReturn(
1915: this .balancer);
1916: EasyMock.expect(this .balancer.all())
1917: .andReturn(this .databaseSet);
1918:
1919: EasyMock.expect(this .parent.getRoot()).andReturn(this .root);
1920:
1921: this .root.retain(this .databaseSet);
1922:
1923: this .connection1.setTransactionIsolation(level);
1924: this .connection2.setTransactionIsolation(level);
1925:
1926: this .replay();
1927:
1928: this .connection.setTransactionIsolation(level);
1929:
1930: this .verify();
1931: }
1932:
1933: @DataProvider(name="map")
1934: Object[][] mapProvider() {
1935: return new Object[][] { new Object[] { Collections.EMPTY_MAP } };
1936: }
1937:
1938: /**
1939: * @see java.sql.Connection#setTypeMap(java.util.Map)
1940: */
1941: @Test(dataProvider="map")
1942: public void setTypeMap(Map<String, Class<?>> map)
1943: throws SQLException {
1944: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1945:
1946: this .connection1.setTypeMap(map);
1947: this .connection2.setTypeMap(map);
1948:
1949: this .replay();
1950:
1951: this .connection.setTypeMap(map);
1952:
1953: this .verify();
1954: }
1955:
1956: @DataProvider(name="string-objects")
1957: Object[][] elementsProvider() {
1958: return new Object[][] { new Object[] { "", new Object[0] } };
1959: }
1960:
1961: /**
1962: * @see java.sql.Connection#createArrayOf(java.lang.String, java.lang.Object[])
1963: */
1964: @Test(dataProvider="string-objects")
1965: public Array createArrayOf(String typeName, Object[] elements)
1966: throws SQLException {
1967: Array array = EasyMock.createMock(Array.class);
1968:
1969: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1970:
1971: EasyMock.expect(
1972: this .connection1.createArrayOf(typeName, elements))
1973: .andReturn(array);
1974:
1975: this .replay();
1976:
1977: Array result = this .connection
1978: .createArrayOf(typeName, elements);
1979:
1980: this .verify();
1981:
1982: assert result == array;
1983:
1984: return result;
1985: }
1986:
1987: /**
1988: * @see java.sql.Connection#createBlob()
1989: */
1990: @Test
1991: public Blob createBlob() throws SQLException {
1992: Blob blob = EasyMock.createMock(Blob.class);
1993:
1994: EasyMock.expect(this .cluster.isActive()).andReturn(true);
1995:
1996: EasyMock.expect(this .connection1.createBlob()).andReturn(blob);
1997:
1998: this .replay();
1999:
2000: Blob result = this .connection.createBlob();
2001:
2002: this .verify();
2003:
2004: assert result == blob;
2005:
2006: return result;
2007: }
2008:
2009: /**
2010: * @see java.sql.Connection#createClob()
2011: */
2012: @Test
2013: public Clob createClob() throws SQLException {
2014: Clob clob = EasyMock.createMock(Clob.class);
2015:
2016: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2017:
2018: EasyMock.expect(this .connection1.createClob()).andReturn(clob);
2019:
2020: this .replay();
2021:
2022: Clob result = this .connection.createClob();
2023:
2024: this .verify();
2025:
2026: assert result == clob;
2027:
2028: return result;
2029: }
2030:
2031: /**
2032: * @see java.sql.Connection#createNClob()
2033: */
2034: @Test
2035: public NClob createNClob() throws SQLException {
2036: NClob clob = EasyMock.createMock(NClob.class);
2037:
2038: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2039:
2040: EasyMock.expect(this .connection1.createNClob()).andReturn(clob);
2041:
2042: this .replay();
2043:
2044: NClob result = this .connection.createNClob();
2045:
2046: this .verify();
2047:
2048: assert result == clob;
2049:
2050: return result;
2051: }
2052:
2053: /**
2054: * @see java.sql.Connection#createSQLXML()
2055: */
2056: @Test
2057: public SQLXML createSQLXML() throws SQLException {
2058: SQLXML xml = EasyMock.createMock(SQLXML.class);
2059:
2060: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2061:
2062: EasyMock.expect(this .connection1.createSQLXML()).andReturn(xml);
2063:
2064: this .replay();
2065:
2066: SQLXML result = this .connection.createSQLXML();
2067:
2068: this .verify();
2069:
2070: assert result == xml;
2071:
2072: return result;
2073: }
2074:
2075: /**
2076: * @see java.sql.Connection#createStruct(java.lang.String, java.lang.Object[])
2077: */
2078: @Test(dataProvider="string-objects")
2079: public Struct createStruct(String typeName, Object[] elements)
2080: throws SQLException {
2081: Struct struct = EasyMock.createMock(Struct.class);
2082:
2083: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2084:
2085: EasyMock.expect(
2086: this .connection1.createStruct(typeName, elements))
2087: .andReturn(struct);
2088:
2089: this .replay();
2090:
2091: Struct result = this .connection
2092: .createStruct(typeName, elements);
2093:
2094: this .verify();
2095:
2096: assert result == struct;
2097:
2098: return result;
2099: }
2100:
2101: /**
2102: * @see java.sql.Connection#getClientInfo()
2103: */
2104: @Test
2105: public Properties getClientInfo() throws SQLException {
2106: Properties properties = new Properties();
2107:
2108: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2109:
2110: EasyMock.expect(this .connection1.getClientInfo()).andReturn(
2111: properties);
2112:
2113: this .replay();
2114:
2115: Properties result = this .connection.getClientInfo();
2116:
2117: this .verify();
2118:
2119: assert result == properties;
2120:
2121: return result;
2122: }
2123:
2124: /**
2125: * @see java.sql.Connection#getClientInfo(java.lang.String)
2126: */
2127: @Test(dataProvider="string")
2128: public String getClientInfo(String property) throws SQLException {
2129: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2130:
2131: EasyMock.expect(this .connection1.getClientInfo(property))
2132: .andReturn("value");
2133:
2134: this .replay();
2135:
2136: String result = this .connection.getClientInfo(property);
2137:
2138: this .verify();
2139:
2140: assert result.equals("value");
2141:
2142: return result;
2143: }
2144:
2145: @DataProvider(name="int")
2146: Object[][] intProvider() {
2147: return new Object[][] { new Object[] { 1 } };
2148: }
2149:
2150: /**
2151: * @see java.sql.Connection#isValid(int)
2152: */
2153: @Test(dataProvider="int")
2154: public boolean isValid(int timeout) throws SQLException {
2155: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2156:
2157: EasyMock.expect(this .cluster.getBalancer()).andReturn(
2158: this .balancer);
2159: EasyMock.expect(this .balancer.next()).andReturn(this .database2);
2160:
2161: this .balancer.beforeInvocation(this .database2);
2162:
2163: EasyMock.expect(this .connection2.isValid(timeout)).andReturn(
2164: true);
2165:
2166: this .balancer.afterInvocation(this .database2);
2167:
2168: this .replay();
2169:
2170: boolean result = this .connection.isValid(timeout);
2171:
2172: this .verify();
2173:
2174: assert result;
2175:
2176: return result;
2177: }
2178:
2179: @DataProvider(name="properties")
2180: Object[][] propertiesProvider() {
2181: return new Object[][] { new Object[] { new Properties() } };
2182: }
2183:
2184: /**
2185: * @see java.sql.Connection#setClientInfo(java.util.Properties)
2186: */
2187: @Test(dataProvider="properties")
2188: public void setClientInfo(Properties properties)
2189: throws SQLClientInfoException {
2190: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2191:
2192: this .connection1.setClientInfo(properties);
2193: this .connection2.setClientInfo(properties);
2194:
2195: this .replay();
2196:
2197: this .connection.setClientInfo(properties);
2198:
2199: this .verify();
2200: }
2201:
2202: @DataProvider(name="string-string")
2203: Object[][] stringStringProvider() {
2204: return new Object[][] { new Object[] { "name", "value" } };
2205: }
2206:
2207: /**
2208: * @see java.sql.Connection#setClientInfo(java.lang.String, java.lang.String)
2209: */
2210: @Test(dataProvider="string-string")
2211: public void setClientInfo(String property, String value)
2212: throws SQLClientInfoException {
2213: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2214:
2215: this .connection1.setClientInfo(property, value);
2216: this .connection2.setClientInfo(property, value);
2217:
2218: this .replay();
2219:
2220: this .connection.setClientInfo(property, value);
2221:
2222: this .verify();
2223: }
2224:
2225: @DataProvider(name="class")
2226: Object[][] classProvider() {
2227: return new Object[][] { new Object[] { Connection.class } };
2228: }
2229:
2230: /**
2231: * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
2232: */
2233: @Test(dataProvider="class")
2234: public boolean isWrapperFor(Class<?> targetClass)
2235: throws SQLException {
2236: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2237:
2238: EasyMock.expect(this .connection1.isWrapperFor(targetClass))
2239: .andReturn(true);
2240:
2241: this .replay();
2242:
2243: boolean result = this .connection.isWrapperFor(targetClass);
2244:
2245: this .verify();
2246:
2247: assert result;
2248:
2249: return result;
2250: }
2251:
2252: /**
2253: * @see java.sql.Wrapper#unwrap(java.lang.Class)
2254: */
2255: @Test(dataProvider="class")
2256: public <T> T unwrap(Class<T> targetClass) throws SQLException {
2257: T object = EasyMock.createMock(targetClass);
2258:
2259: EasyMock.expect(this .cluster.isActive()).andReturn(true);
2260:
2261: EasyMock.expect(this .connection1.unwrap(targetClass))
2262: .andReturn(object);
2263:
2264: this .replay();
2265:
2266: T result = this .connection.unwrap(targetClass);
2267:
2268: this .verify();
2269:
2270: assert result == object;
2271:
2272: return result;
2273: }
2274:
2275: protected void extractIdentifiers(String sql) throws SQLException {
2276: EasyMock.expect(this .cluster.isSequenceDetectionEnabled())
2277: .andReturn(false);
2278: EasyMock
2279: .expect(this .cluster.isIdentityColumnDetectionEnabled())
2280: .andReturn(false);
2281:
2282: EasyMock.expect(this .cluster.getDatabaseMetaDataCache())
2283: .andReturn(this .metaData);
2284: EasyMock.expect(
2285: this .metaData.getDatabaseProperties(EasyMock
2286: .same(this .connection))).andReturn(
2287: this .databaseProperties);
2288: EasyMock.expect(
2289: this .databaseProperties.supportsSelectForUpdate())
2290: .andReturn(false);
2291: }
2292: }
|