001: package com.mockrunner.test.jdbc;
002:
003: import java.sql.SQLException;
004: import java.sql.Statement;
005: import java.util.Arrays;
006: import java.util.Properties;
007:
008: import junit.framework.TestCase;
009:
010: import com.mockrunner.mock.jdbc.MockArray;
011: import com.mockrunner.mock.jdbc.MockBlob;
012: import com.mockrunner.mock.jdbc.MockClob;
013: import com.mockrunner.mock.jdbc.MockConnection;
014: import com.mockrunner.mock.jdbc.MockDatabaseMetaData;
015: import com.mockrunner.mock.jdbc.MockPreparedStatement;
016: import com.mockrunner.mock.jdbc.MockResultSet;
017: import com.mockrunner.mock.jdbc.MockSQLXML;
018: import com.mockrunner.mock.jdbc.MockStruct;
019:
020: public class MockConnectionTest extends TestCase {
021: private MockConnection connection;
022:
023: protected void setUp() throws Exception {
024: super .setUp();
025: connection = new MockConnection();
026: }
027:
028: protected void tearDown() throws Exception {
029: super .tearDown();
030: connection = null;
031: }
032:
033: public void testDatabaseMetaData() throws SQLException {
034: assertNotNull(connection.getMetaData());
035: assertTrue(connection.getMetaData() instanceof MockDatabaseMetaData);
036: assertSame(connection, connection.getMetaData().getConnection());
037: connection.setMetaData(null);
038: assertNull(connection.getMetaData());
039: connection.setMetaData(new MockDatabaseMetaData());
040: assertNotNull(connection.getMetaData());
041: assertTrue(connection.getMetaData() instanceof MockDatabaseMetaData);
042: assertSame(connection, connection.getMetaData().getConnection());
043: }
044:
045: public void testPrepareStatementAutoGeneratedKeys()
046: throws SQLException {
047: MockResultSet resultSet = new MockResultSet("testid");
048: try {
049: connection.prepareStatement("select", 50000);
050: } catch (SQLException exc) {
051: //should throw exception
052: }
053: MockPreparedStatement statement = (MockPreparedStatement) connection
054: .prepareStatement("select",
055: Statement.RETURN_GENERATED_KEYS);
056: connection.getPreparedStatementResultSetHandler()
057: .prepareGeneratedKeys("select", resultSet);
058: statement.execute();
059: assertEquals("testid", ((MockResultSet) statement
060: .getGeneratedKeys()).getId());
061: statement = (MockPreparedStatement) connection
062: .prepareStatement("select", Statement.NO_GENERATED_KEYS);
063: statement.execute();
064: assertTrue(((MockResultSet) statement.getGeneratedKeys())
065: .getId().indexOf("testid") < 0);
066: }
067:
068: public void testClientInfo() throws SQLException {
069: assertNull(connection.getClientInfo("name"));
070: assertTrue(connection.getClientInfo().isEmpty());
071: connection.getClientInfo().setProperty("name", "value");
072: assertNull(connection.getClientInfo("name"));
073: assertTrue(connection.getClientInfo().isEmpty());
074: connection.setClientInfo("name", "value");
075: assertEquals("value", connection.getClientInfo("name"));
076: assertEquals("value", connection.getClientInfo().getProperty(
077: "name"));
078: connection.setClientInfo(new Properties());
079: assertNull(connection.getClientInfo("name"));
080: assertTrue(connection.getClientInfo().isEmpty());
081: Properties properties = new Properties();
082: properties.setProperty("name", "value");
083: connection.setClientInfo(properties);
084: assertEquals(1, connection.getClientInfo().size());
085: assertEquals("value", connection.getClientInfo().getProperty(
086: "name"));
087: assertEquals("value", connection.getClientInfo("name"));
088: }
089:
090: public void testCreateTypes() throws SQLException {
091: MockBlob blob = (MockBlob) connection.createBlob();
092: assertEquals(0, blob.length());
093: MockClob clob = (MockClob) connection.createClob();
094: assertEquals(0, clob.length());
095: MockArray array = (MockArray) connection.createArrayOf("aName",
096: new Object[] { "1", "2", "3" });
097: assertEquals("aName", array.getBaseTypeName());
098: assertTrue(Arrays.equals(new Object[] { "1", "2", "3" },
099: (Object[]) array.getArray()));
100: MockStruct struct = (MockStruct) connection.createStruct(
101: "aName", new Object[] { "1", "2", "3" });
102: assertEquals("aName", struct.getSQLTypeName());
103: Object[] attributes = struct.getAttributes();
104: assertEquals("1", attributes[0]);
105: assertEquals("2", attributes[1]);
106: assertEquals("3", attributes[2]);
107: MockSQLXML sqlXML = (MockSQLXML) connection.createSQLXML();
108: assertNull(sqlXML.getContentAsString());
109: }
110: }
|