0001: package com.mockrunner.test.jdbc;
0002:
0003: import java.sql.ResultSet;
0004: import java.sql.Savepoint;
0005: import java.sql.Statement;
0006: import java.sql.Types;
0007: import java.util.ArrayList;
0008: import java.util.Arrays;
0009: import java.util.HashMap;
0010: import java.util.List;
0011: import java.util.Map;
0012:
0013: import junit.framework.TestCase;
0014:
0015: import com.mockrunner.base.VerifyFailedException;
0016: import com.mockrunner.jdbc.JDBCTestModule;
0017: import com.mockrunner.jdbc.ParameterSets;
0018: import com.mockrunner.mock.jdbc.JDBCMockObjectFactory;
0019: import com.mockrunner.mock.jdbc.MockBlob;
0020: import com.mockrunner.mock.jdbc.MockCallableStatement;
0021: import com.mockrunner.mock.jdbc.MockClob;
0022: import com.mockrunner.mock.jdbc.MockPreparedStatement;
0023: import com.mockrunner.mock.jdbc.MockResultSet;
0024: import com.mockrunner.mock.jdbc.MockStatement;
0025:
0026: public class JDBCTestModuleTest extends TestCase {
0027: private JDBCMockObjectFactory mockfactory;
0028: private JDBCTestModule module;
0029:
0030: protected void setUp() throws Exception {
0031: super .setUp();
0032: mockfactory = new JDBCMockObjectFactory();
0033: module = new JDBCTestModule(mockfactory);
0034: }
0035:
0036: private void prepareCallableStatements() throws Exception {
0037: mockfactory.getMockConnection().prepareCall(
0038: "{call getData(?, ?, ?, ?)}");
0039: mockfactory.getMockConnection().prepareCall(
0040: "{call setData(?, ?, ?, ?)}");
0041: }
0042:
0043: private void preparePreparedStatements() throws Exception {
0044: mockfactory.getMockConnection().prepareStatement(
0045: "INSERT INTO TEST (COL1, COL2) VALUES(?, ?)");
0046: mockfactory.getMockConnection().prepareStatement(
0047: "insert into test (col1, col2, col3) values(?, ?, ?)");
0048: mockfactory.getMockConnection().prepareStatement(
0049: "update mytable set test = test + ? where id = ?",
0050: ResultSet.TYPE_SCROLL_INSENSITIVE,
0051: ResultSet.CONCUR_UPDATABLE,
0052: ResultSet.HOLD_CURSORS_OVER_COMMIT);
0053: }
0054:
0055: private void prepareStatements() throws Exception {
0056: mockfactory.getMockConnection().createStatement();
0057: mockfactory.getMockConnection().createStatement(
0058: ResultSet.TYPE_SCROLL_INSENSITIVE,
0059: ResultSet.CONCUR_UPDATABLE);
0060: }
0061:
0062: public void testGetStatements() throws Exception {
0063: List statements = module.getStatements();
0064: assertNotNull(statements);
0065: assertEquals(0, statements.size());
0066: assertNull(module.getStatement(1));
0067: module.verifyNumberStatements(0);
0068: prepareStatements();
0069: statements = module.getStatements();
0070: assertNotNull(statements);
0071: assertEquals(2, statements.size());
0072: assertNotNull(module.getStatement(0));
0073: assertNotNull(module.getStatement(1));
0074: module.verifyNumberStatements(2);
0075: }
0076:
0077: public void testGetPreparedStatementsByIndex() throws Exception {
0078: List statements = module.getPreparedStatements();
0079: assertNotNull(statements);
0080: assertEquals(0, statements.size());
0081: assertNull(module.getPreparedStatement(1));
0082: module.verifyNumberPreparedStatements(0);
0083: preparePreparedStatements();
0084: statements = module.getPreparedStatements();
0085: assertNotNull(statements);
0086: assertEquals(3, statements.size());
0087: module.verifyNumberPreparedStatements(3);
0088: }
0089:
0090: public void testGetPreparedStatementsBySQL() throws Exception {
0091: preparePreparedStatements();
0092: List statements = module.getPreparedStatements("insert");
0093: assertNotNull(statements);
0094: assertEquals(2, statements.size());
0095: MockPreparedStatement statement = module
0096: .getPreparedStatement("insert");
0097: assertEquals("INSERT INTO TEST (COL1, COL2) VALUES(?, ?)",
0098: statement.getSQL());
0099: module.verifyNumberPreparedStatements(1, "update");
0100: module.verifyNumberPreparedStatements(1, "UPDATE");
0101: module.verifyNumberPreparedStatements(2, "insert");
0102: module.verifyNumberPreparedStatements(3);
0103: module.verifyPreparedStatementPresent("update");
0104: module.verifyPreparedStatementNotPresent("select");
0105: module.setCaseSensitive(true);
0106: statements = module.getPreparedStatements("insert");
0107: assertNotNull(statements);
0108: assertEquals(1, statements.size());
0109: statement = module.getPreparedStatement("insert");
0110: assertEquals(
0111: "insert into test (col1, col2, col3) values(?, ?, ?)",
0112: statement.getSQL());
0113: module.verifyNumberPreparedStatements(1, "update");
0114: module.verifyNumberPreparedStatements(0, "UPDATE");
0115: module.verifyNumberPreparedStatements(1, "insert");
0116: module.verifyNumberPreparedStatements(1, "INSERT");
0117: module.verifyNumberPreparedStatements(3);
0118: module.setExactMatch(true);
0119: statements = module.getPreparedStatements("insert");
0120: assertNotNull(statements);
0121: assertEquals(0, statements.size());
0122: module.verifyNumberPreparedStatements(0, "update");
0123: module.verifyNumberPreparedStatements(0, "UPDATE");
0124: module.verifyNumberPreparedStatements(0, "insert");
0125: module.verifyNumberPreparedStatements(0, "INSERT");
0126: module.verifyPreparedStatementNotPresent("update");
0127: module
0128: .verifyPreparedStatementPresent("insert into test (col1, col2, col3) values(?, ?, ?)");
0129: }
0130:
0131: public void testGetPreparedStatementsBySQLRegEx() throws Exception {
0132: module.setUseRegularExpressions(true);
0133: preparePreparedStatements();
0134: List statements = module.getPreparedStatements("insert");
0135: assertNotNull(statements);
0136: assertEquals(0, statements.size());
0137: statements = module.getPreparedStatements("insert into.*");
0138: assertEquals(2, statements.size());
0139: module.verifyNumberPreparedStatements(0, "update");
0140: module.verifyNumberPreparedStatements(2, "insert (.*) test.*");
0141: module.verifyNumberPreparedStatements(2, "insert (.*) TEST.*");
0142: module.setCaseSensitive(true);
0143: module.verifyNumberPreparedStatements(0, "insert (.*) TEST.*");
0144: }
0145:
0146: public void testGetPreparedStatementObjects() throws Exception {
0147: preparePreparedStatements();
0148: MockPreparedStatement statement = module
0149: .getPreparedStatement("update");
0150: statement.setInt(1, 3);
0151: statement.setLong(2, 10000);
0152: statement.setNull(3, 1);
0153: assertEquals(new Integer(3), statement.getParameter(1));
0154: assertEquals(new Long(10000), statement.getParameter(2));
0155: assertNull(statement.getParameter(3));
0156: assertTrue(statement.getParameterMap().containsKey(
0157: new Integer(3)));
0158: module.verifyPreparedStatementParameterPresent(statement, 1);
0159: module.verifyPreparedStatementParameterPresent("update", 3);
0160: module.verifyPreparedStatementParameterNotPresent("update", 4);
0161: module.verifyPreparedStatementParameterNotPresent(0, 1);
0162: module.verifyPreparedStatementParameter(statement, 1,
0163: new Integer(3));
0164: module.verifyPreparedStatementParameter(2, 2, new Long(10000));
0165: module.verifyPreparedStatementParameter(statement, 3, null);
0166: statement = module
0167: .getPreparedStatement("INSERT INTO TEST (COL1, COL2) VALUES(?, ?)");
0168: statement.setString(1, "test1");
0169: statement.setString(2, "test2");
0170: statement.setBytes(3, new byte[] { 1, 2, 3 });
0171: statement.setBytes(4, new byte[] {});
0172: module.verifyPreparedStatementParameterPresent(statement, 2);
0173: module.verifyPreparedStatementParameterPresent(statement, 3);
0174: module.verifyPreparedStatementParameterPresent(statement, 4);
0175: module.verifyPreparedStatementParameterNotPresent(statement, 5);
0176: module.verifyPreparedStatementParameter(0, 3, new byte[] { 1,
0177: 2, 3 });
0178: module.verifyPreparedStatementParameter(0, 4, new byte[] {});
0179: }
0180:
0181: public void testGetCallableStatementsByIndex() throws Exception {
0182: module.verifyNumberCallableStatements(0);
0183: prepareCallableStatements();
0184: module.verifyNumberCallableStatements(2);
0185: List statements = module.getCallableStatements();
0186: assertEquals("{call getData(?, ?, ?, ?)}",
0187: ((MockCallableStatement) statements.get(0)).getSQL());
0188: assertEquals("{call setData(?, ?, ?, ?)}",
0189: ((MockCallableStatement) statements.get(1)).getSQL());
0190: }
0191:
0192: public void testGetCallableStatementsBySQL() throws Exception {
0193: prepareCallableStatements();
0194: List statements = module.getCallableStatements("call");
0195: assertTrue(statements.size() == 2);
0196: MockCallableStatement statement = module
0197: .getCallableStatement("CALL");
0198: assertEquals("{call getData(?, ?, ?, ?)}", statement.getSQL());
0199: module.setCaseSensitive(true);
0200: statement = module.getCallableStatement("CALL");
0201: assertNull(statement);
0202: module.setCaseSensitive(false);
0203: module.setExactMatch(true);
0204: statement = module.getCallableStatement("CALL");
0205: assertNull(statement);
0206: statements = module
0207: .getCallableStatements("{call setData(?, ?, ?, ?)}");
0208: assertTrue(statements.size() == 1);
0209: module.setExactMatch(false);
0210: module.verifyNumberCallableStatements(1, "call getData");
0211: module.verifyNumberCallableStatements(2, "call");
0212: module.verifyCallableStatementPresent("call setData");
0213: module.verifyCallableStatementNotPresent("call setXYZ");
0214: }
0215:
0216: public void testGetCallableStatementsBySQLRegEx() throws Exception {
0217: module.setUseRegularExpressions(true);
0218: prepareCallableStatements();
0219: List statements = module.getCallableStatements("call");
0220: assertTrue(statements.size() == 0);
0221: MockCallableStatement statement = module
0222: .getCallableStatement(".*CALL.*");
0223: assertEquals("{call getData(?, ?, ?, ?)}", statement.getSQL());
0224: module.verifyCallableStatementNotPresent("call setData");
0225: module.verifyCallableStatementPresent("{call setData.*}");
0226: }
0227:
0228: public void testGetCallableStatementObjects() throws Exception {
0229: prepareCallableStatements();
0230: MockCallableStatement statement = module
0231: .getCallableStatement("{call setData(?, ?, ?, ?)}");
0232: statement.setInt("xyz", 1);
0233: statement.setString("3", null);
0234: statement.setString(1, "xyz");
0235: Map namedParameter = statement.getNamedParameterMap();
0236: Map indexedParameter = statement.getIndexedParameterMap();
0237: assertTrue(namedParameter.size() == 2);
0238: assertEquals(new Integer(1), namedParameter.get("xyz"));
0239: assertNull(namedParameter.get("3"));
0240: assertTrue(indexedParameter.size() == 1);
0241: assertEquals("xyz", indexedParameter.get(new Integer(1)));
0242: module.verifyCallableStatementParameterPresent(1, 1);
0243: try {
0244: module.verifyCallableStatementParameterNotPresent(
0245: statement, "3");
0246: fail();
0247: } catch (VerifyFailedException exc) {
0248: //should throw Exception
0249: }
0250: module.verifyCallableStatementParameterNotPresent(1, 2);
0251: module.verifyCallableStatementParameterPresent(statement, "3");
0252: module.verifyCallableStatementParameterNotPresent(statement,
0253: "31");
0254: module.verifyCallableStatementParameter(
0255: "{call setData(?, ?, ?, ?)}", "xyz", new Integer(1));
0256: module.verifyCallableStatementParameter(1, 1, "xyz");
0257: module.verifyCallableStatementParameter(1, "3", null);
0258: try {
0259: module.verifyCallableStatementParameter(1, 1, "zzz");
0260: fail();
0261: } catch (VerifyFailedException exc) {
0262: //should throw Exception
0263: }
0264: try {
0265: module.verifyCallableStatementParameter(1, 5, null);
0266: fail();
0267: } catch (VerifyFailedException exc) {
0268: //should throw Exception
0269: }
0270: statement.setBytes(1, new byte[] { 1 });
0271: statement.setBlob(2, new MockBlob(new byte[] { 3, 4 }));
0272: statement.setClob(3, new MockClob("test"));
0273: module.verifyCallableStatementParameter(1, 1, new byte[] { 1 });
0274: module.verifyCallableStatementParameter(statement, 2,
0275: new MockBlob(new byte[] { 3, 4 }));
0276: module.verifyCallableStatementParameter(1, 3, new MockClob(
0277: "test"));
0278: try {
0279: module.verifyCallableStatementParameter(1, 1,
0280: new byte[] { 2 });
0281: fail();
0282: } catch (VerifyFailedException exc) {
0283: //should throw Exception
0284: }
0285: }
0286:
0287: public void testVerifyCallableStatementOutParameterRegistered()
0288: throws Exception {
0289: prepareCallableStatements();
0290: MockCallableStatement statement = module
0291: .getCallableStatement("{call getData(?, ?, ?, ?)}");
0292: statement.registerOutParameter(1, Types.DECIMAL);
0293: statement.registerOutParameter("test", Types.BLOB);
0294: statement.registerOutParameter("xyz", Types.BINARY);
0295: module.verifyCallableStatementOutParameterRegistered(statement,
0296: 1);
0297: module.verifyCallableStatementOutParameterRegistered(statement,
0298: "test");
0299: module.verifyCallableStatementOutParameterRegistered(statement,
0300: "xyz");
0301: try {
0302: module.verifyCallableStatementOutParameterRegistered(
0303: "{call setData(?, ?, ?, ?)}", "xyz");
0304: fail();
0305: } catch (VerifyFailedException exc) {
0306: //should throw Exception
0307: }
0308: try {
0309: module.verifyCallableStatementOutParameterRegistered(1,
0310: "test");
0311: fail();
0312: } catch (VerifyFailedException exc) {
0313: //should throw Exception
0314: }
0315: module.verifyCallableStatementOutParameterRegistered(0, "test");
0316: }
0317:
0318: public void testGetExecutedSQLStatements() throws Exception {
0319: prepareStatements();
0320: preparePreparedStatements();
0321: prepareCallableStatements();
0322: MockStatement statement = module.getStatement(0);
0323: statement.execute("select");
0324: statement.execute("UPDATE");
0325: MockPreparedStatement preparedStatement = module
0326: .getPreparedStatement("insert");
0327: preparedStatement.execute();
0328: MockCallableStatement callableStatement = module
0329: .getCallableStatement("call");
0330: callableStatement.executeUpdate();
0331: List sqlStatements = module.getExecutedSQLStatements();
0332: assertTrue(sqlStatements.size() == 4);
0333: assertTrue(sqlStatements.contains("select"));
0334: assertTrue(sqlStatements.contains("UPDATE"));
0335: assertTrue(sqlStatements
0336: .contains("INSERT INTO TEST (COL1, COL2) VALUES(?, ?)"));
0337: assertTrue(sqlStatements.contains("{call getData(?, ?, ?, ?)}"));
0338: module.verifySQLStatementExecuted("select");
0339: module.verifySQLStatementExecuted("update");
0340: module.verifySQLStatementExecuted("INSERT");
0341: module.verifySQLStatementExecuted("{call");
0342: module.verifySQLStatementNotExecuted("{call}");
0343: module.setCaseSensitive(true);
0344: module.verifySQLStatementExecuted("UPDATE");
0345: module.verifySQLStatementNotExecuted("update");
0346: module.setExactMatch(true);
0347: module.verifySQLStatementExecuted("{call getData(?, ?, ?, ?)}");
0348: module.verifySQLStatementNotExecuted("call");
0349: module.setCaseSensitive(false);
0350: module.verifySQLStatementExecuted("{CALL getData(?, ?, ?, ?)}");
0351: }
0352:
0353: public void testGetExecutedSQLStatementsRegEx() throws Exception {
0354: module.setUseRegularExpressions(true);
0355: prepareStatements();
0356: preparePreparedStatements();
0357: prepareCallableStatements();
0358: MockStatement statement = module.getStatement(0);
0359: statement.execute("select");
0360: statement.execute("UPDATE");
0361: MockPreparedStatement preparedStatement = module
0362: .getPreparedStatement("insert.*");
0363: preparedStatement.execute();
0364: MockCallableStatement callableStatement = module
0365: .getCallableStatement("{call.*");
0366: callableStatement.executeUpdate();
0367: module.verifySQLStatementExecuted("select");
0368: module.verifySQLStatementExecuted("update.*");
0369: module.verifySQLStatementExecuted("INSERT into .*");
0370: module.verifySQLStatementExecuted("{call.*");
0371: module.verifySQLStatementNotExecuted("{call}");
0372: module.setCaseSensitive(true);
0373: module.verifySQLStatementExecuted("UPDATE.*");
0374: module.verifySQLStatementNotExecuted("update");
0375: module.setExactMatch(true);
0376: module.verifySQLStatementNotExecuted("UPDATE.*");
0377: module.verifySQLStatementExecuted("UPDATE");
0378: }
0379:
0380: public void testGetReturnedResultSets() throws Exception {
0381: prepareStatements();
0382: preparePreparedStatements();
0383: prepareCallableStatements();
0384: MockResultSet resultSet1 = module
0385: .getStatementResultSetHandler().createResultSet("1");
0386: MockResultSet resultSet2 = module
0387: .getStatementResultSetHandler().createResultSet("2");
0388: MockResultSet resultSet3 = module
0389: .getStatementResultSetHandler().createResultSet("3");
0390: MockResultSet resultSet4 = module
0391: .getStatementResultSetHandler().createResultSet("4");
0392: MockResultSet resultSet5 = module
0393: .getStatementResultSetHandler().createResultSet("5");
0394: MockResultSet resultSet6 = module
0395: .getStatementResultSetHandler().createResultSet("6");
0396: MockResultSet resultSet7 = module
0397: .getStatementResultSetHandler().createResultSet("7");
0398: module.getStatementResultSetHandler().prepareGlobalResultSet(
0399: resultSet1);
0400: module.getStatementResultSetHandler().prepareResultSet(
0401: "select id", resultSet2);
0402: module.getStatementResultSetHandler().prepareResultSets(
0403: "select xyz",
0404: new MockResultSet[] { resultSet3, resultSet5 });
0405: module.getPreparedStatementResultSetHandler().prepareResultSet(
0406: "select name", resultSet4, new String[] { "test" });
0407: module.getCallableStatementResultSetHandler().prepareResultSet(
0408: "call set", resultSet5);
0409: module.getCallableStatementResultSetHandler()
0410: .prepareResultSets(
0411: "call set",
0412: new MockResultSet[] { resultSet6, resultSet7,
0413: resultSet1 }, new String[] { "xyz" });
0414: MockStatement statement = module.getStatement(0);
0415: statement.executeQuery("select name");
0416: statement.executeQuery("select id");
0417: statement.executeQuery("select xyz");
0418: List list = module.getReturnedResultSets();
0419: assertEquals(3, list.size());
0420: assertEquals("1", ((MockResultSet) list.get(0)).getId());
0421: assertEquals("2", ((MockResultSet) list.get(1)).getId());
0422: assertEquals("3", ((MockResultSet[]) list.get(2))[0].getId());
0423: assertEquals("5", ((MockResultSet[]) list.get(2))[1].getId());
0424: MockPreparedStatement preparedStatement = (MockPreparedStatement) mockfactory
0425: .getMockConnection().prepareStatement("SELECT NAME");
0426: preparedStatement.setString(1, "test");
0427: preparedStatement.executeQuery();
0428: list = module.getReturnedResultSets();
0429: assertEquals(4, list.size());
0430: assertEquals("4", ((MockResultSet) list.get(3)).getId());
0431: MockCallableStatement callableStatement = module
0432: .getCallableStatement("call set");
0433: callableStatement.executeQuery();
0434: list = module.getReturnedResultSets();
0435: assertEquals(5, list.size());
0436: assertEquals("5", ((MockResultSet) list.get(4)).getId());
0437: callableStatement.setString(1, "xyz");
0438: callableStatement.executeQuery();
0439: list = module.getReturnedResultSets();
0440: assertEquals(6, list.size());
0441: assertEquals("6", ((MockResultSet[]) list.get(5))[0].getId());
0442: assertEquals("7", ((MockResultSet[]) list.get(5))[1].getId());
0443: assertEquals("1", ((MockResultSet[]) list.get(5))[2].getId());
0444: list = module.getReturnedResultSets("1");
0445: assertEquals(2, list.size());
0446: MockResultSet returned1 = (MockResultSet) list.get(0);
0447: MockResultSet returned2 = (MockResultSet) list.get(1);
0448: assertEquals("1", returned1.getId());
0449: assertEquals("1", returned2.getId());
0450: assertNotSame(returned1, returned2);
0451: assertNotSame(returned1, resultSet1);
0452: MockResultSet returned = module.getReturnedResultSet("1");
0453: assertEquals("1", returned.getId());
0454: assertSame(returned1, returned);
0455: }
0456:
0457: public void testReturnedResultSetsClosed() throws Exception {
0458: prepareStatements();
0459: preparePreparedStatements();
0460: prepareCallableStatements();
0461: MockResultSet resultSet1 = module
0462: .getStatementResultSetHandler().createResultSet("1");
0463: MockResultSet resultSet2 = module
0464: .getStatementResultSetHandler().createResultSet("2");
0465: MockResultSet resultSet3 = module
0466: .getStatementResultSetHandler().createResultSet("3");
0467: MockResultSet resultSet4 = module
0468: .getStatementResultSetHandler().createResultSet("4");
0469: MockResultSet resultSet5 = module
0470: .getStatementResultSetHandler().createResultSet("5");
0471: module.getStatementResultSetHandler().prepareGlobalResultSet(
0472: resultSet1);
0473: module.getStatementResultSetHandler().prepareResultSet(
0474: "select id", resultSet2);
0475: module.getStatementResultSetHandler().prepareResultSet(
0476: "select xyz", resultSet3);
0477: module.getPreparedStatementResultSetHandler().prepareResultSet(
0478: "select name", resultSet4, new String[] { "test" });
0479: module.getCallableStatementResultSetHandler().prepareResultSet(
0480: "call set", resultSet5, new String[] { "xyz" });
0481: MockStatement statement = module.getStatement(0);
0482: statement.executeQuery("select name");
0483: statement.executeQuery("select id");
0484: List list = module.getReturnedResultSets();
0485: assertEquals(2, list.size());
0486: assertEquals("1", ((MockResultSet) list.get(0)).getId());
0487: assertEquals("2", ((MockResultSet) list.get(1)).getId());
0488: MockPreparedStatement preparedStatement = module
0489: .getPreparedStatement("insert");
0490: preparedStatement.execute();
0491: list = module.getReturnedResultSets();
0492: assertEquals(2, list.size());
0493: assertEquals("1", ((MockResultSet) list.get(0)).getId());
0494: assertEquals("2", ((MockResultSet) list.get(1)).getId());
0495: preparedStatement = (MockPreparedStatement) mockfactory
0496: .getMockConnection().prepareStatement("SELECT NAME");
0497: preparedStatement.setString(1, "test");
0498: preparedStatement.executeQuery();
0499: list = module.getReturnedResultSets();
0500: assertEquals(3, list.size());
0501: assertEquals("1", ((MockResultSet) list.get(0)).getId());
0502: assertEquals("2", ((MockResultSet) list.get(1)).getId());
0503: assertEquals("4", ((MockResultSet) list.get(2)).getId());
0504: MockCallableStatement callableStatement = module
0505: .getCallableStatement("call set");
0506: callableStatement.setString(1, "test");
0507: callableStatement.executeQuery();
0508: list = module.getReturnedResultSets();
0509: assertEquals(3, list.size());
0510: assertEquals("1", ((MockResultSet) list.get(0)).getId());
0511: assertEquals("2", ((MockResultSet) list.get(1)).getId());
0512: assertEquals("4", ((MockResultSet) list.get(2)).getId());
0513: callableStatement.setString(1, "xyz");
0514: callableStatement.executeQuery();
0515: list = module.getReturnedResultSets();
0516: assertEquals(4, list.size());
0517: assertEquals("1", ((MockResultSet) list.get(0)).getId());
0518: assertEquals("2", ((MockResultSet) list.get(1)).getId());
0519: assertEquals("4", ((MockResultSet) list.get(2)).getId());
0520: assertEquals("5", ((MockResultSet) list.get(3)).getId());
0521: ((MockResultSet) list.get(0)).close();
0522: module.verifyResultSetClosed("1");
0523: try {
0524: module.verifyResultSetClosed("2");
0525: fail();
0526: } catch (VerifyFailedException exc) {
0527: //should throw exception
0528: }
0529: try {
0530: module.verifyAllResultSetsClosed();
0531: fail();
0532: } catch (VerifyFailedException exc) {
0533: //should throw exception
0534: }
0535: ((MockResultSet) list.get(1)).close();
0536: ((MockResultSet) list.get(2)).close();
0537: ((MockResultSet) list.get(3)).close();
0538: module.verifyAllResultSetsClosed();
0539: }
0540:
0541: public void testMultipleReturnedResultSetsClosed() throws Exception {
0542: prepareStatements();
0543: preparePreparedStatements();
0544: prepareCallableStatements();
0545: MockResultSet resultSet1 = module
0546: .getStatementResultSetHandler().createResultSet("1");
0547: MockResultSet resultSet2 = module
0548: .getStatementResultSetHandler().createResultSet("2");
0549: MockResultSet resultSet3 = module
0550: .getStatementResultSetHandler().createResultSet("3");
0551: MockResultSet resultSet4 = module
0552: .getStatementResultSetHandler().createResultSet("4");
0553: MockResultSet resultSet5 = module
0554: .getStatementResultSetHandler().createResultSet("5");
0555: MockResultSet resultSet6 = module
0556: .getStatementResultSetHandler().createResultSet("6");
0557: MockResultSet resultSet7 = module
0558: .getStatementResultSetHandler().createResultSet("7");
0559: module.getStatementResultSetHandler().prepareGlobalResultSet(
0560: resultSet1);
0561: module.getStatementResultSetHandler().prepareResultSet(
0562: "select id", resultSet2);
0563: module.getStatementResultSetHandler().prepareResultSets(
0564: "select xyz",
0565: new MockResultSet[] { resultSet3, resultSet5 });
0566: module.getPreparedStatementResultSetHandler().prepareResultSet(
0567: "select name", resultSet4, new String[] { "test" });
0568: module.getCallableStatementResultSetHandler().prepareResultSet(
0569: "call set", resultSet5);
0570: module.getCallableStatementResultSetHandler()
0571: .prepareResultSets(
0572: "call set",
0573: new MockResultSet[] { resultSet6, resultSet7,
0574: resultSet1 }, new String[] { "xyz" });
0575: MockStatement statement = module.getStatement(0);
0576: statement.executeQuery("select name");
0577: statement.executeQuery("select id");
0578: statement.executeQuery("select xyz");
0579: MockPreparedStatement preparedStatement = (MockPreparedStatement) mockfactory
0580: .getMockConnection().prepareStatement("SELECT NAME");
0581: preparedStatement.setString(1, "test");
0582: preparedStatement.executeQuery();
0583: MockCallableStatement callableStatement = module
0584: .getCallableStatement("call set");
0585: callableStatement.executeQuery();
0586: callableStatement.setString(1, "xyz");
0587: callableStatement.executeQuery();
0588: try {
0589: module.verifyAllStatementsClosed();
0590: fail();
0591: } catch (Exception exc) {
0592: //should throw Exception
0593: }
0594: List list = module.getReturnedResultSets();
0595: for (int ii = 0; ii < list.size() - 1; ii++) {
0596: Object object = list.get(ii);
0597: if (object instanceof MockResultSet) {
0598: ((MockResultSet) object).close();
0599: } else {
0600: MockResultSet[] resultSets = (MockResultSet[]) object;
0601: for (int yy = 0; yy < resultSets.length; yy++) {
0602: resultSets[yy].close();
0603: }
0604: }
0605: }
0606: try {
0607: module.verifyAllStatementsClosed();
0608: fail();
0609: } catch (Exception exc) {
0610: //should throw Exception
0611: }
0612: MockResultSet[] resultSets = (MockResultSet[]) list.get(list
0613: .size() - 1);
0614: for (int ii = 0; ii < resultSets.length; ii++) {
0615: resultSets[ii].close();
0616: }
0617: module.verifyAllResultSetsClosed();
0618: }
0619:
0620: public void testStatementsClosed() throws Exception {
0621: prepareStatements();
0622: preparePreparedStatements();
0623: prepareCallableStatements();
0624: MockStatement statement = module.getStatement(0);
0625: MockPreparedStatement preparedStatement = module
0626: .getPreparedStatement("update");
0627: statement.close();
0628: preparedStatement.close();
0629: module.verifyStatementClosed(0);
0630: module.verifyPreparedStatementClosed("update");
0631: try {
0632: module.verifyAllStatementsClosed();
0633: fail();
0634: } catch (Exception exc) {
0635: //should throw Exception
0636: }
0637: List statements = new ArrayList();
0638: statements.addAll(module.getStatements());
0639: statements.addAll(module.getPreparedStatements());
0640: statements.addAll(module.getCallableStatements());
0641: for (int ii = 0; ii < statements.size(); ii++) {
0642: ((MockStatement) statements.get(ii)).close();
0643: }
0644: module.verifyAllStatementsClosed();
0645: mockfactory.getMockConnection().close();
0646: module.verifyConnectionClosed();
0647: }
0648:
0649: public void testSavepoints() throws Exception {
0650: mockfactory.getMockConnection().setSavepoint();
0651: mockfactory.getMockConnection().setSavepoint("test");
0652: Savepoint savepoint2 = mockfactory.getMockConnection()
0653: .setSavepoint("xyz");
0654: Savepoint savepoint3 = mockfactory.getMockConnection()
0655: .setSavepoint();
0656: module.verifySavepointNotReleased(0);
0657: module.verifySavepointNotReleased(1);
0658: module.verifySavepointNotReleased(2);
0659: module.verifySavepointNotReleased(3);
0660: module.verifySavepointNotRolledBack(0);
0661: module.verifySavepointNotRolledBack("test");
0662: module.verifySavepointNotRolledBack(2);
0663: module.verifySavepointNotRolledBack(3);
0664: mockfactory.getMockConnection().releaseSavepoint(savepoint2);
0665: mockfactory.getMockConnection().rollback(savepoint3);
0666: module.verifySavepointNotReleased(0);
0667: module.verifySavepointNotReleased(1);
0668: module.verifySavepointReleased("xyz");
0669: module.verifySavepointNotReleased(3);
0670: module.verifySavepointNotRolledBack(0);
0671: module.verifySavepointNotRolledBack(1);
0672: module.verifySavepointNotRolledBack("xyz");
0673: module.verifySavepointRolledBack(3);
0674: try {
0675: module.verifySavepointReleased("test");
0676: fail();
0677: } catch (Exception exc) {
0678: //should throw Exception
0679: }
0680: try {
0681: module.verifySavepointNotRolledBack(3);
0682: fail();
0683: } catch (Exception exc) {
0684: //should throw Exception
0685: }
0686: List savepoints = module.getSavepoints();
0687: int[] ids = new int[4];
0688: for (int ii = 0; ii < savepoints.size(); ii++) {
0689: ids[ii] += 1;
0690: }
0691: assertTrue(ids[0] == 1);
0692: assertTrue(ids[1] == 1);
0693: assertTrue(ids[2] == 1);
0694: assertTrue(ids[3] == 1);
0695: Savepoint savepoint = module.getSavepoint("xyz");
0696: assertTrue(savepoint == savepoint2);
0697: }
0698:
0699: public void testVerifyNumberCommitsAndRollbacks() throws Exception {
0700: try {
0701: module.verifyCommitted();
0702: fail();
0703: } catch (Exception exc) {
0704: //should throw Exception
0705: }
0706: try {
0707: module.verifyRolledBack();
0708: fail();
0709: } catch (Exception exc) {
0710: //should throw Exception
0711: }
0712: Savepoint savepoint = mockfactory.getMockConnection()
0713: .setSavepoint();
0714: mockfactory.getMockConnection().commit();
0715: mockfactory.getMockConnection().rollback();
0716: mockfactory.getMockConnection().rollback(savepoint);
0717: module.verifyCommitted();
0718: module.verifyRolledBack();
0719: module.verifyNumberCommits(1);
0720: module.verifyNumberRollbacks(2);
0721: }
0722:
0723: public void testVerifyResultSet() {
0724: MockResultSet resultSet1 = module
0725: .getStatementResultSetHandler().createResultSet("test");
0726: resultSet1.addRow(new Integer[] { new Integer(1),
0727: new Integer(2), new Integer(3) });
0728: resultSet1.addRow(new Integer[] { new Integer(4),
0729: new Integer(5), new Integer(6) });
0730: resultSet1.addRow(new Integer[] { new Integer(7),
0731: new Integer(8), new Integer(9) });
0732: module.getStatementResultSetHandler().addReturnedResultSet(
0733: resultSet1);
0734: MockResultSet resultSet2 = module
0735: .getStatementResultSetHandler().createResultSet("xyz");
0736: resultSet2.addColumn("column", new String[] { "1", "2", "3" });
0737: module.getStatementResultSetHandler().addReturnedResultSet(
0738: resultSet2);
0739: module.verifyResultSetRow("test", 2, new Integer[] {
0740: new Integer(4), new Integer(5), new Integer(6) });
0741: try {
0742: module.verifyResultSetRow(resultSet1, 3, new Integer[] {
0743: new Integer(4), new Integer(5), new Integer(6) });
0744: fail();
0745: } catch (VerifyFailedException exc) {
0746: //should throw exception
0747: }
0748: module.verifyResultSetColumn("test", 1, new Integer[] {
0749: new Integer(1), new Integer(4), new Integer(7) });
0750: module.verifyResultSetColumn(resultSet2, 1, new String[] { "1",
0751: "2", "3" });
0752: module.verifyResultSetColumn(resultSet2, "column",
0753: new String[] { "1", "2", "3" });
0754: module.verifyResultSetRow("xyz", 3, new String[] { "3" });
0755: try {
0756: module.verifyResultSetRow(resultSet2, 3, new String[] {
0757: "3", "4" });
0758: fail();
0759: } catch (VerifyFailedException exc) {
0760: //should throw exception
0761: }
0762: try {
0763: module.verifyResultSetColumn("xyz", "testColumn",
0764: new String[] { "1" });
0765: fail();
0766: } catch (VerifyFailedException exc) {
0767: //should throw exception
0768: }
0769: try {
0770: module.verifyResultSetColumn("xyz", 2, new String[] { "1",
0771: "2", "3" });
0772: fail();
0773: } catch (VerifyFailedException exc) {
0774: //should throw exception
0775: }
0776: try {
0777: module.verifyResultSetRow(resultSet2, 5, new String[] {
0778: "1", "2", "3" });
0779: fail();
0780: } catch (VerifyFailedException exc) {
0781: //should throw exception
0782: }
0783: try {
0784: module.verifyResultSetEquals(resultSet1, resultSet2);
0785: fail();
0786: } catch (VerifyFailedException exc) {
0787: //should throw exception
0788: }
0789: module.verifyResultSetEquals(resultSet1, resultSet1);
0790: module.verifyResultSetEquals(resultSet2, resultSet2);
0791: resultSet2 = module.getStatementResultSetHandler()
0792: .createResultSet("test2");
0793: resultSet2.addRow(new Integer[] { new Integer(1),
0794: new Integer(2), new Integer(3) });
0795: resultSet2.addRow(new Integer[] { new Integer(4),
0796: new Integer(5), new Integer(6) });
0797: resultSet2.addRow(new Integer[] { new Integer(7),
0798: new Integer(8), new Integer(9) });
0799: module.getStatementResultSetHandler().addReturnedResultSet(
0800: resultSet2);
0801: module.getStatementResultSetHandler().addReturnedResultSet(
0802: resultSet1);
0803: module.verifyResultSetEquals(resultSet1, resultSet2);
0804: module.verifyResultSetEquals("test", resultSet2);
0805: module.verifyResultSetEquals("test2", resultSet1);
0806: }
0807:
0808: public void testVerifyResultSetRowModified() throws Exception {
0809: MockResultSet resultSet = module.getStatementResultSetHandler()
0810: .createResultSet("test");
0811: resultSet.addRow(new Integer[] { new Integer(1),
0812: new Integer(2), new Integer(3) });
0813: resultSet.addRow(new Integer[] { new Integer(4),
0814: new Integer(5), new Integer(6) });
0815: resultSet.addRow(new Integer[] { new Integer(7),
0816: new Integer(8), new Integer(9) });
0817: module.getStatementResultSetHandler().prepareResultSet(
0818: "select", resultSet);
0819: Statement statement = mockfactory.getMockConnection()
0820: .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
0821: ResultSet.CONCUR_UPDATABLE);
0822: MockResultSet returnedResultSet = (MockResultSet) statement
0823: .executeQuery("select");
0824: resultSet = (MockResultSet) module
0825: .getStatementResultSetHandler().getReturnedResultSets()
0826: .get(0);
0827: module.verifyResultSetRowNotDeleted(resultSet, 1);
0828: module.verifyResultSetRowNotDeleted("test", 2);
0829: module.verifyResultSetRowNotInserted("test", 2);
0830: module.verifyResultSetRowNotUpdated(resultSet, 3);
0831: try {
0832: module.verifyResultSetRowUpdated(resultSet, 1);
0833: fail();
0834: } catch (VerifyFailedException exc) {
0835: //should throw exception
0836: }
0837: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0838: returnedResultSet.next();
0839: returnedResultSet.updateRow();
0840: module.verifyResultSetRowUpdated(resultSet, 1);
0841: returnedResultSet.next();
0842: returnedResultSet.deleteRow();
0843: module.verifyResultSetRowDeleted(resultSet, 2);
0844: returnedResultSet.next();
0845: returnedResultSet.moveToInsertRow();
0846: returnedResultSet.updateString(1, "test");
0847: returnedResultSet.insertRow();
0848: returnedResultSet.moveToCurrentRow();
0849: module.verifyResultSetRowInserted("test", 3);
0850: returnedResultSet.first();
0851: returnedResultSet.moveToInsertRow();
0852: returnedResultSet.updateString(1, "test");
0853: returnedResultSet.insertRow();
0854: returnedResultSet.moveToCurrentRow();
0855: module.verifyResultSetRowInserted("test", 1);
0856: module.verifyResultSetRowDeleted(resultSet, 3);
0857: module.verifyResultSetRowNotUpdated(resultSet, 4);
0858: }
0859:
0860: public void testGetExecutedSQLStatementParameter() throws Exception {
0861: prepareStatements();
0862: preparePreparedStatements();
0863: prepareCallableStatements();
0864: module.getPreparedStatement(0).setString(1, "test");
0865: module.getPreparedStatement(0).setShort(2, (short) 2);
0866: module.getPreparedStatement(1).setBytes(1, new byte[] { 1 });
0867: module.getCallableStatement(1).setBoolean("name", false);
0868: module.getStatement(0).execute("select mydata");
0869: module.getStatement(1).execute("select mydata");
0870: module.getPreparedStatement(0).execute();
0871: module.getPreparedStatement(1).execute();
0872: module.getPreparedStatement(2).execute();
0873: module.getCallableStatement(0).execute();
0874: module.getCallableStatement(1).execute();
0875: Map parameterMap = module.getExecutedSQLStatementParameterMap();
0876: assertEquals(5, parameterMap.size());
0877: Map preparedStatementMap1 = ((ParameterSets) parameterMap
0878: .get("INSERT INTO TEST (COL1, COL2) VALUES(?, ?)"))
0879: .getParameterSet(0);
0880: assertEquals(2, preparedStatementMap1.size());
0881: assertEquals("test", preparedStatementMap1.get(new Integer(1)));
0882: assertEquals(new Short((short) 2), preparedStatementMap1
0883: .get(new Integer(2)));
0884: Map preparedStatementMap2 = ((ParameterSets) parameterMap
0885: .get("insert into test (col1, col2, col3) values(?, ?, ?)"))
0886: .getParameterSet(0);
0887: assertEquals(1, preparedStatementMap2.size());
0888: assertTrue(Arrays.equals(new byte[] { 1 },
0889: (byte[]) preparedStatementMap2.get(new Integer(1))));
0890: Map preparedStatementMap3 = ((ParameterSets) parameterMap
0891: .get("update mytable set test = test + ? where id = ?"))
0892: .getParameterSet(0);
0893: assertEquals(0, preparedStatementMap3.size());
0894: Map callableStatementMap1 = (Map) ((ParameterSets) parameterMap
0895: .get("{call getData(?, ?, ?, ?)}")).getParameterSet(0);
0896: assertEquals(0, callableStatementMap1.size());
0897: Map callableStatementMap2 = (Map) ((ParameterSets) parameterMap
0898: .get("{call setData(?, ?, ?, ?)}")).getParameterSet(0);
0899: assertEquals(1, callableStatementMap2.size());
0900: assertEquals(Boolean.FALSE, callableStatementMap2.get("name"));
0901: }
0902:
0903: public void testGetExecutedSQLStatementParameterSets()
0904: throws Exception {
0905: preparePreparedStatements();
0906: prepareCallableStatements();
0907: module.getPreparedStatement(0).setString(1, "test");
0908: module.getPreparedStatement(0).setShort(2, (short) 2);
0909: module.getPreparedStatement(1).setBytes(1, new byte[] { 1 });
0910: module.getCallableStatement(1).setBoolean("name", false);
0911: module.getPreparedStatement(0).execute();
0912: module.getPreparedStatement(1).execute();
0913: module.getPreparedStatement(2).execute();
0914: module.getCallableStatement(0).execute();
0915: module.getCallableStatement(1).execute();
0916: module.getPreparedStatement(0).setString(1, "test1");
0917: module.getPreparedStatement(0).setShort(2, (short) 3);
0918: module.getPreparedStatement(0).execute();
0919: ParameterSets sets1 = module
0920: .getExecutedSQLStatementParameterSets("INSERT INTO TEST (COL1, COL2)");
0921: assertEquals(2, sets1.getNumberParameterSets());
0922: Map parameterSet1 = sets1.getParameterSet(0);
0923: assertEquals(2, parameterSet1.size());
0924: assertEquals("test", parameterSet1.get(new Integer(1)));
0925: assertEquals(new Short((short) 2), parameterSet1
0926: .get(new Integer(2)));
0927: Map parameterSet2 = sets1.getParameterSet(1);
0928: assertEquals(2, parameterSet2.size());
0929: assertEquals("test1", parameterSet2.get(new Integer(1)));
0930: assertEquals(new Short((short) 3), parameterSet2
0931: .get(new Integer(2)));
0932: module.setUseRegularExpressions(true);
0933: ParameterSets sets2 = module
0934: .getExecutedSQLStatementParameterSets("insert into test \\(col1, col2, col3\\) .*");
0935: assertEquals(1, sets2.getNumberParameterSets());
0936: parameterSet1 = sets2.getParameterSet(0);
0937: assertEquals(1, parameterSet1.size());
0938: assertTrue(Arrays.equals(new byte[] { 1 },
0939: (byte[]) parameterSet1.get(new Integer(1))));
0940: ParameterSets sets3 = module
0941: .getExecutedSQLStatementParameterSets("{call setData\\(\\?, \\?, \\?, \\?\\)}");
0942: assertEquals(1, sets3.getNumberParameterSets());
0943: parameterSet1 = sets3.getParameterSet(0);
0944: assertEquals(1, parameterSet1.size());
0945: assertEquals(Boolean.FALSE, parameterSet1.get("name"));
0946: ParameterSets sets4 = module
0947: .getExecutedSQLStatementParameterSets("{call getData\\(\\?, \\?, \\?, \\?\\)}");
0948: assertEquals(1, sets4.getNumberParameterSets());
0949: parameterSet1 = sets4.getParameterSet(0);
0950: assertEquals(0, parameterSet1.size());
0951: assertNull(module
0952: .getExecutedSQLStatementParameterSets("{call xyz"));
0953: }
0954:
0955: public void testSQLStatementParameterNoParameterSets()
0956: throws Exception {
0957: prepareStatements();
0958: module.getStatement(0).execute("test");
0959: try {
0960: module.verifySQLStatementParameterNumber("test", 0, 0);
0961: fail();
0962: } catch (VerifyFailedException exc) {
0963: //should throw exception
0964: }
0965: preparePreparedStatements();
0966: module.getPreparedStatement(0).execute();
0967: try {
0968: module.verifySQLStatementParameterNumber(
0969: "INSERT INTO TEST (COL1, COL2) VALUES(?,", 1, 0);
0970: fail();
0971: } catch (VerifyFailedException exc) {
0972: //should throw exception
0973: }
0974: try {
0975: module.verifySQLStatementParameter(
0976: "INSERT INTO TEST (COL1, COL2) VALUES(?,", 1,
0977: new HashMap());
0978: fail();
0979: } catch (VerifyFailedException exc) {
0980: //should throw exception
0981: }
0982: }
0983:
0984: public void testSQLStatementParameterNumber() throws Exception {
0985: preparePreparedStatements();
0986: prepareCallableStatements();
0987: module.getPreparedStatement(0).setString(1, "test");
0988: module.getPreparedStatement(0).setString(2, "test");
0989: module.getCallableStatement(0).setString("name", "test");
0990: module.getCallableStatement(1).setString(1, "test");
0991: module.getPreparedStatement(0).execute();
0992: module.getPreparedStatement(1).execute();
0993: module.getPreparedStatement(2).execute();
0994: module.getCallableStatement(0).execute();
0995: module.getCallableStatement(1).execute();
0996: module.verifySQLStatementParameterNumber(
0997: "INSERT INTO TEST (COL1, COL2) VALUES(?,", 0, 2);
0998: module.verifySQLStatementParameterNumber(
0999: "insert into test (col1, col2, col3) values(?, ?, ?)",
1000: 0, 0);
1001: module
1002: .verifySQLStatementParameterNumber(
1003: "update mytable set test = test + ? where id = ?",
1004: 0, 0);
1005: module.verifySQLStatementParameterNumber(
1006: "{call getData(?, ?, ?, ?)}", 0, 1);
1007: module.verifySQLStatementParameterNumber("{call setData(?, ",
1008: 0, 1);
1009: try {
1010: module.verifySQLStatementParameterNumber(
1011: "{call getData(?, ?, ?, ?)}", 0, 3);
1012: fail();
1013: } catch (VerifyFailedException exc) {
1014: //should throw exception
1015: }
1016: try {
1017: module
1018: .verifySQLStatementParameterNumber(
1019: "insert into test (col1, col2, col3) values(?, ?, ?)",
1020: 0, 1);
1021: fail();
1022: } catch (VerifyFailedException exc) {
1023: //should throw exception
1024: }
1025: try {
1026: module.verifySQLStatementParameterNumber("xyz", 0, 0);
1027: fail();
1028: } catch (VerifyFailedException exc) {
1029: //should throw exception
1030: }
1031: }
1032:
1033: public void testSQLStatementParameterPreparedStatement()
1034: throws Exception {
1035: preparePreparedStatements();
1036: module.getPreparedStatement(1).setString(1, "test1");
1037: module.getPreparedStatement(1).setInt(2, 3);
1038: module.getPreparedStatement(0).execute();
1039: module.getPreparedStatement(1).execute();
1040: module.getPreparedStatement(2).execute();
1041: Map emptyMap = new HashMap();
1042: Map okTestMap = new HashMap();
1043: okTestMap.put(new Integer(1), "test1");
1044: okTestMap.put(new Integer(2), new Integer(3));
1045: Map failureTestMap1 = new HashMap();
1046: failureTestMap1.put(new Integer(1), "test1");
1047: failureTestMap1.put(new Integer(2), new Integer(2));
1048: Map failureTestMap2 = new HashMap();
1049: failureTestMap2.put(new Integer(1), "test1");
1050: failureTestMap2.put(new Integer(2), new Integer(3));
1051: failureTestMap2.put(new Integer(3), new Integer(3));
1052: module.verifySQLStatementParameter(
1053: "update mytable set test = test", 0, emptyMap);
1054: try {
1055: module.setUseRegularExpressions(true);
1056: module.verifySQLStatementParameter(
1057: "update mytable set test = test", 0, emptyMap);
1058: fail();
1059: } catch (VerifyFailedException exc) {
1060: //should throw exception
1061: }
1062: module.verifySQLStatementParameter(
1063: "update mytable set test = test.*", 0, emptyMap);
1064: module.setUseRegularExpressions(false);
1065: module.verifySQLStatementParameter(
1066: "insert into test (col1, col2, col3)", 0, okTestMap);
1067: module.verifySQLStatementParameter(
1068: "insert into test (col1, col2, col3)", 0, 2,
1069: new Integer(3));
1070: try {
1071: module
1072: .verifySQLStatementParameter(
1073: "insert into test (col1, col2, col3) values(?, ?, ?)",
1074: 0, failureTestMap1);
1075: fail();
1076: } catch (VerifyFailedException exc) {
1077: //should throw exception
1078: }
1079: try {
1080: module
1081: .verifySQLStatementParameter(
1082: "insert into test (col1, col2, col3) values(?, ?, ?)",
1083: 0, 1, "test2");
1084: fail();
1085: } catch (VerifyFailedException exc) {
1086: //should throw exception
1087: }
1088: try {
1089: module
1090: .verifySQLStatementParameter(
1091: "insert into test (col1, col2, col3) values(?, ?, ?)",
1092: 0, failureTestMap2);
1093: fail();
1094: } catch (VerifyFailedException exc) {
1095: //should throw exception
1096: }
1097: try {
1098: module.verifySQLStatementParameter(
1099: "INSERT INTO TEST (COL1, COL2) VALUES(?, ?)", 0,
1100: okTestMap);
1101: fail();
1102: } catch (VerifyFailedException exc) {
1103: //should throw exception
1104: }
1105: }
1106:
1107: public void testSQLStatementNullParameterPreparedStatement()
1108: throws Exception {
1109: preparePreparedStatements();
1110: module.getPreparedStatement(0).setString(1, null);
1111: module.getPreparedStatement(0).execute();
1112: module.verifySQLStatementParameter(
1113: "INSERT INTO TEST (COL1, COL2) VALUES(?, ?)", 0, 1,
1114: null);
1115: try {
1116: module.verifySQLStatementParameter(
1117: "INSERT INTO TEST (COL1, COL2) VALUES(?, ?)", 0, 1,
1118: "test");
1119: fail();
1120: } catch (VerifyFailedException exc) {
1121: //should throw exception
1122: }
1123: }
1124:
1125: public void testSQLStatementParameterCallableStatement()
1126: throws Exception {
1127: prepareCallableStatements();
1128: module.getCallableStatement(0).setString(1, "test1");
1129: module.getCallableStatement(0).setBytes(2, new byte[] { 1 });
1130: module.getCallableStatement(0).setInt("name", 1);
1131: module.getCallableStatement(0).execute();
1132: module.getCallableStatement(1).execute();
1133: module.verifySQLStatementParameter("{call getData(?, ?", 0, 1,
1134: "test1");
1135: module.setUseRegularExpressions(true);
1136: module.verifySQLStatementParameter(".*getData\\(\\?, \\?.*", 0,
1137: 1, "test1");
1138: module.setUseRegularExpressions(false);
1139: try {
1140: module.verifySQLStatementParameter(
1141: ".*getData\\(\\?, \\?.*", 0, 1, "test1");
1142: fail();
1143: } catch (VerifyFailedException exc) {
1144: //should throw exception
1145: }
1146: module.verifySQLStatementParameter(
1147: "{call getData(?, ?, ?, ?)}", 0, 2, new byte[] { 1 });
1148: module
1149: .verifySQLStatementParameter(
1150: "{call getData(?, ?, ?, ?)}", 0, "name",
1151: new Integer(1));
1152: try {
1153: module.verifySQLStatementParameter(
1154: "{call getData(?, ?, ?, ?)}", 0, 2, new byte[] { 1,
1155: 2 });
1156: fail();
1157: } catch (VerifyFailedException exc) {
1158: //should throw exception
1159: }
1160: try {
1161: module.verifySQLStatementParameter(
1162: "{call setData(?, ?, ?, ?)}", 0, 1, "");
1163: fail();
1164: } catch (VerifyFailedException exc) {
1165: //should throw exception
1166: }
1167: try {
1168: module.verifySQLStatementParameter("select", 0, 1, "");
1169: fail();
1170: } catch (VerifyFailedException exc) {
1171: //should throw exception
1172: }
1173: module.setCaseSensitive(true);
1174: try {
1175: module.verifySQLStatementParameter("{CALL getData(?, ?", 0,
1176: 1, "test1");
1177: fail();
1178: } catch (VerifyFailedException exc) {
1179: //should throw exception
1180: }
1181: }
1182:
1183: public void testSQLStatementNullParameterCallableStatement()
1184: throws Exception {
1185: prepareCallableStatements();
1186: module.getCallableStatement(0).setString("1", null);
1187: module.getCallableStatement(0).execute();
1188: module.verifySQLStatementParameter(
1189: "{call getData(?, ?, ?, ?)}", 0, "1", null);
1190: try {
1191: module.verifySQLStatementParameter(
1192: "{call getData(?, ?, ?, ?)}", 0, "1", "test");
1193: fail();
1194: } catch (VerifyFailedException exc) {
1195: //should throw exception
1196: }
1197: }
1198:
1199: public void testSQLStatementParameterMultipleParameterSets()
1200: throws Exception {
1201: prepareCallableStatements();
1202: module.getCallableStatement(0).setString(1, "test1");
1203: module.getCallableStatement(0).execute();
1204: module.getCallableStatement(0).setString(1, "xyz");
1205: module.getCallableStatement(0).setBoolean("name", true);
1206: module.getCallableStatement(0).execute();
1207: module.getCallableStatement(0).execute();
1208: module.verifySQLStatementParameterNumber(
1209: "{call getData(?, ?, ?, ?)}", 0, 1);
1210: module.verifySQLStatementParameterNumber(
1211: "{call getData(?, ?, ?, ?)}", 1, 2);
1212: module.verifySQLStatementParameterNumber(
1213: "{call getData(?, ?, ?, ?)}", 2, 2);
1214: module.setUseRegularExpressions(true);
1215: module.verifySQLStatementParameterNumber(
1216: "{call getData\\(\\?, \\?, \\?, \\?\\)}", 2, 2);
1217: module.verifySQLStatementParameterNumber(".call getData.*}", 2,
1218: 2);
1219: module.setUseRegularExpressions(false);
1220: module.verifySQLStatementParameter(
1221: "{call getData(?, ?, ?, ?)}", 1, 1, "xyz");
1222: module.verifySQLStatementParameter(
1223: "{call getData(?, ?, ?, ?)}", 1, "name", Boolean.TRUE);
1224: HashMap testMap = new HashMap();
1225: testMap.put(new Integer(1), "test1");
1226: module.verifySQLStatementParameter(
1227: "{call getData(?, ?, ?, ?)}", 0, testMap);
1228: try {
1229: module.verifySQLStatementParameterNumber(
1230: "{call getData(?, ?, ?, ?)}", 2, 0);
1231: fail();
1232: } catch (VerifyFailedException exc) {
1233: //should throw exception
1234: }
1235: try {
1236: module.verifySQLStatementParameter(
1237: "{call getData(?, ?, ?, ?)}", 0, new HashMap());
1238: fail();
1239: } catch (VerifyFailedException exc) {
1240: //should throw exception
1241: }
1242: }
1243:
1244: public void testSQLStatementParameterPreparedStatementBatchParameterSets()
1245: throws Exception {
1246: MockPreparedStatement preparedStatement = (MockPreparedStatement) mockfactory
1247: .getMockConnection().prepareStatement(
1248: "insert into test");
1249: preparedStatement.setString(1, "test1");
1250: preparedStatement.setInt(2, 3);
1251: preparedStatement.addBatch();
1252: preparedStatement.setString(1, "test2");
1253: preparedStatement.setInt(2, 4);
1254: preparedStatement.addBatch();
1255: preparedStatement.executeBatch();
1256: module.verifySQLStatementParameter("insert into test", 0, 1,
1257: "test1");
1258: module.verifySQLStatementParameter("insert into test", 0, 2,
1259: new Integer(3));
1260: module.verifySQLStatementParameter("insert into test", 1, 1,
1261: "test2");
1262: module.verifySQLStatementParameter("insert into test", 1, 2,
1263: new Integer(4));
1264: Map testMap = new HashMap();
1265: testMap.put(new Integer(1), "test1");
1266: testMap.put(new Integer(2), new Integer(3));
1267: module.verifySQLStatementParameter("insert into test", 0,
1268: testMap);
1269: testMap = new HashMap();
1270: testMap.put(new Integer(1), "test2");
1271: testMap.put(new Integer(2), new Integer(4));
1272: module.verifySQLStatementParameter("insert into test", 1,
1273: testMap);
1274: }
1275:
1276: public void testSQLStatementParameterCallableStatementBatchParameterSets()
1277: throws Exception {
1278: MockCallableStatement callableStatement = (MockCallableStatement) mockfactory
1279: .getMockConnection().prepareCall("call getData");
1280: callableStatement.setString("xyz1", "test1");
1281: callableStatement.setLong(1, 3);
1282: callableStatement.addBatch();
1283: callableStatement.setString(1, "test2");
1284: callableStatement.setInt("xyz1", 4);
1285: callableStatement.setInt("xyz2", 7);
1286: callableStatement.addBatch();
1287: callableStatement.executeBatch();
1288: module.verifySQLStatementParameter("call getData", 0, "xyz1",
1289: "test1");
1290: module.verifySQLStatementParameter("call getData", 0, 1,
1291: new Long(3));
1292: module.verifySQLStatementParameter("call getData", 1, "xyz1",
1293: new Integer(4));
1294: module.verifySQLStatementParameter("call getData", 1, "xyz2",
1295: new Integer(7));
1296: module.verifySQLStatementParameter("call getData", 1, 1,
1297: "test2");
1298: Map testMap = new HashMap();
1299: testMap.put("xyz1", "test1");
1300: testMap.put(new Integer(1), new Long(3));
1301: module.verifySQLStatementParameter("call getData", 0, testMap);
1302: testMap = new HashMap();
1303: testMap.put("xyz1", new Integer(4));
1304: testMap.put("xyz2", new Integer(7));
1305: testMap.put(new Integer(1), "test2");
1306: module.verifySQLStatementParameter("call getData", 1, testMap);
1307: }
1308:
1309: public void testPreparedStatementsAndCallableStatementsSQLOrdered()
1310: throws Exception {
1311: preparePreparedStatements();
1312: MockPreparedStatement preparedStatement = module
1313: .getPreparedStatement("insert");
1314: assertSame(module.getPreparedStatementResultSetHandler()
1315: .getPreparedStatements().get(0), preparedStatement);
1316: prepareCallableStatements();
1317: MockPreparedStatement callableStatement = module
1318: .getCallableStatement("call");
1319: assertSame(module.getCallableStatementResultSetHandler()
1320: .getCallableStatements().get(0), callableStatement);
1321: }
1322: }
|