001: package com.mockrunner.test.jdbc;
002:
003: import java.sql.Connection;
004: import java.sql.Driver;
005: import java.sql.DriverManager;
006: import java.sql.DriverPropertyInfo;
007: import java.sql.SQLException;
008: import java.util.ArrayList;
009: import java.util.Enumeration;
010: import java.util.Properties;
011:
012: import junit.framework.TestCase;
013:
014: import com.mockrunner.mock.jdbc.JDBCMockObjectFactory;
015: import com.mockrunner.mock.jdbc.MockConnection;
016: import com.mockrunner.mock.jdbc.MockDataSource;
017: import com.mockrunner.mock.jdbc.MockDriver;
018:
019: public class JDBCMockObjectFactoryTest extends TestCase {
020: public void testCreate() throws Exception {
021: JDBCMockObjectFactory factory = new JDBCMockObjectFactory();
022: assertTrue(factory.getMockDriver().connect("", null) == factory
023: .getMockDataSource().getConnection());
024: assertNotNull(DriverManager.getConnection("test"));
025: assertTrue(factory.getMockDriver().connect("", null) == DriverManager
026: .getConnection("test"));
027: Enumeration drivers = DriverManager.getDrivers();
028: drivers.nextElement();
029: assertFalse(drivers.hasMoreElements());
030: }
031:
032: public void testRestoreDivers() throws Exception {
033: DriverManager.registerDriver(new TestDriver());
034: DriverManager.registerDriver(new TestDriver());
035: ArrayList oldDrivers = new ArrayList();
036: Enumeration drivers = DriverManager.getDrivers();
037: while (drivers.hasMoreElements()) {
038: oldDrivers.add(drivers.nextElement());
039: }
040: JDBCMockObjectFactory factory = new JDBCMockObjectFactory();
041: new JDBCMockObjectFactory();
042: new JDBCMockObjectFactory();
043: drivers = DriverManager.getDrivers();
044: assertTrue(drivers.nextElement() instanceof MockDriver);
045: assertFalse(drivers.hasMoreElements());
046: factory.restoreDrivers();
047: drivers = DriverManager.getDrivers();
048: int numberDrivers = 0;
049: while (drivers.hasMoreElements()) {
050: numberDrivers++;
051: assertTrue(oldDrivers.contains(drivers.nextElement()));
052: }
053: factory.restoreDrivers();
054: factory.restoreDrivers();
055: factory.restoreDrivers();
056: int newNumberDrivers = 0;
057: drivers = DriverManager.getDrivers();
058: while (drivers.hasMoreElements()) {
059: newNumberDrivers++;
060: assertTrue(oldDrivers.contains(drivers.nextElement()));
061: }
062: assertTrue(numberDrivers == newNumberDrivers);
063: factory.registerMockDriver();
064: drivers = DriverManager.getDrivers();
065: numberDrivers = 0;
066: while (drivers.hasMoreElements()) {
067: numberDrivers++;
068: assertTrue(drivers.nextElement() instanceof MockDriver);
069: }
070: assertTrue(numberDrivers == 1);
071: factory.registerMockDriver();
072: factory.registerMockDriver();
073: factory.restoreDrivers();
074: factory.restoreDrivers();
075: drivers = DriverManager.getDrivers();
076: numberDrivers = 0;
077: while (drivers.hasMoreElements()) {
078: numberDrivers++;
079: assertTrue(oldDrivers.contains(drivers.nextElement()));
080: }
081: assertTrue(numberDrivers == newNumberDrivers);
082: }
083:
084: public void testOverrideCreate() {
085: JDBCMockObjectFactory factory = new TestJDBCMockObjectFactory();
086: assertNotSame(factory.getMockConnection().getClass(),
087: MockConnection.class);
088: assertNotSame(factory.getMockDataSource().getClass(),
089: MockDataSource.class);
090: assertNotSame(factory.getMockDriver().getClass(),
091: MockDriver.class);
092: }
093:
094: public static class TestDriver implements Driver {
095:
096: public boolean acceptsURL(String url) throws SQLException {
097: return false;
098: }
099:
100: public Connection connect(String url, Properties info)
101: throws SQLException {
102: return null;
103: }
104:
105: public int getMajorVersion() {
106: return 0;
107: }
108:
109: public int getMinorVersion() {
110: return 0;
111: }
112:
113: public DriverPropertyInfo[] getPropertyInfo(String url,
114: Properties info) throws SQLException {
115: return null;
116: }
117:
118: public boolean jdbcCompliant() {
119: return false;
120: }
121: }
122:
123: public static class TestJDBCMockObjectFactory extends
124: JDBCMockObjectFactory {
125: public MockConnection createMockConnection() {
126: return new MockConnection() {
127: };
128: }
129:
130: public MockDataSource createMockDataSource() {
131: return new MockDataSource() {
132: };
133: }
134:
135: public MockDriver createMockDriver() {
136: return new MockDriver() {
137: };
138: }
139: }
140: }
|