001: package com.mockrunner.mock.jdbc;
002:
003: import java.sql.Driver;
004: import java.sql.DriverManager;
005: import java.sql.SQLException;
006: import java.util.Enumeration;
007: import java.util.HashSet;
008: import java.util.Iterator;
009: import java.util.Set;
010:
011: import com.mockrunner.base.NestedApplicationException;
012:
013: /**
014: * Used to create all types of JDBC mock objects.
015: * Maintains the necessary dependencies between the mock objects.
016: * If you use the mock objects returned by this
017: * factory in your tests you can be sure that they are all
018: * up to date.
019: * Please note, that this class removes all drivers
020: * from the JDBC <code>DriverManager</code> and registers
021: * the {@link MockDriver}. All drivers are preserved and
022: * can be restored with {@link #restoreDrivers}.
023: */
024: public class JDBCMockObjectFactory {
025: private MockDataSource dataSource;
026: private MockDriver driver;
027: private MockConnection connection;
028: private Set preservedDrivers;
029:
030: /**
031: * Creates a new set of mock objects.
032: */
033: public JDBCMockObjectFactory() {
034: dataSource = createMockDataSource();
035: driver = createMockDriver();
036: connection = createMockConnection();
037: preservedDrivers = new HashSet();
038: setUpDependencies();
039: }
040:
041: private void setUpDependencies() {
042: dataSource.setupConnection(connection);
043: driver.setupConnection(connection);
044: registerMockDriver();
045: }
046:
047: private void deregisterDrivers() {
048: try {
049: Enumeration drivers = DriverManager.getDrivers();
050: while (drivers.hasMoreElements()) {
051: DriverManager.deregisterDriver((Driver) drivers
052: .nextElement());
053: }
054: } catch (SQLException exc) {
055: throw new NestedApplicationException(exc);
056: }
057: }
058:
059: private void deregisterMockDrivers() {
060: try {
061: Enumeration drivers = DriverManager.getDrivers();
062: while (drivers.hasMoreElements()) {
063: Driver currentDriver = (Driver) drivers.nextElement();
064: if (currentDriver instanceof MockDriver) {
065: DriverManager.deregisterDriver(currentDriver);
066: }
067: }
068: } catch (SQLException exc) {
069: throw new NestedApplicationException(exc);
070: }
071: }
072:
073: private void preserveDrivers() {
074: Enumeration drivers = DriverManager.getDrivers();
075: while (drivers.hasMoreElements()) {
076: Driver currentDriver = (Driver) drivers.nextElement();
077: if (!(currentDriver instanceof MockDriver)) {
078: preservedDrivers.add(currentDriver);
079: }
080: }
081: }
082:
083: /**
084: * Removes all JDBC drivers from the <code>DriveManager</code> and
085: * registers the mock driver. The removed drivers are preserved and
086: * can be restored with {@link #restoreDrivers}.
087: */
088: public void registerMockDriver() {
089: try {
090: preserveDrivers();
091: deregisterDrivers();
092: DriverManager.registerDriver(driver);
093: } catch (SQLException exc) {
094: throw new NestedApplicationException(exc);
095: }
096: }
097:
098: /**
099: * Since <code>JDBCMockObjectFactory</code> removes all the
100: * drivers from the <code>DriveManager</code> (so the
101: * {@link MockDriver} is guaranteed to be the only one)
102: * you can use this method to restore the original drivers.
103: * Automatically called by {@link com.mockrunner.base.BaseTestCase#tearDown}.
104: */
105: public void restoreDrivers() {
106: deregisterMockDrivers();
107: try {
108: Iterator drivers = preservedDrivers.iterator();
109: while (drivers.hasNext()) {
110: DriverManager.registerDriver((Driver) drivers.next());
111: }
112: } catch (SQLException exc) {
113: throw new NestedApplicationException(exc);
114: }
115: preservedDrivers.clear();
116: }
117:
118: /**
119: * Creates the {@link com.mockrunner.mock.jdbc.MockConnection} using <code>new</code>.
120: * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockConnection}.
121: * @return the {@link com.mockrunner.mock.jdbc.MockConnection}
122: */
123: public MockConnection createMockConnection() {
124: return new MockConnection();
125: }
126:
127: /**
128: * Creates the {@link com.mockrunner.mock.jdbc.MockDriver} using <code>new</code>.
129: * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockDriver}.
130: * @return the {@link com.mockrunner.mock.jdbc.MockDriver}
131: */
132: public MockDriver createMockDriver() {
133: return new MockDriver();
134: }
135:
136: /**
137: * Creates the {@link com.mockrunner.mock.jdbc.MockDataSource} using <code>new</code>.
138: * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockDataSource}.
139: * @return the {@link com.mockrunner.mock.jdbc.MockDataSource}
140: */
141: public MockDataSource createMockDataSource() {
142: return new MockDataSource();
143: }
144:
145: /**
146: * Returns the {@link com.mockrunner.mock.jdbc.MockDataSource}.
147: * @return the {@link com.mockrunner.mock.jdbc.MockDataSource}
148: */
149: public MockDataSource getMockDataSource() {
150: return dataSource;
151: }
152:
153: /**
154: * Returns the {@link com.mockrunner.mock.jdbc.MockDriver}.
155: * @return the {@link com.mockrunner.mock.jdbc.MockDriver}
156: */
157: public MockDriver getMockDriver() {
158: return driver;
159: }
160:
161: /**
162: * Returns the {@link com.mockrunner.mock.jdbc.MockConnection}.
163: * @return the {@link com.mockrunner.mock.jdbc.MockConnection}
164: */
165: public MockConnection getMockConnection() {
166: return connection;
167: }
168: }
|