How to use create method of Drivers Package

Best Unobtainium_ruby code snippet using Drivers.create Github


Full Screen

...7import java.util.Iterator;8import java.util.Set;9import com.mockrunner.base.NestedApplicationException;10/**11 * Used to create all types of JDBC mock objects. 12 * Maintains the necessary dependencies between the mock objects.13 * If you use the mock objects returned by this14 * factory in your tests you can be sure that they are all15 * up to date.16 * Please note, that this class removes all drivers17 * from the JDBC <code>DriverManager</code> and registers18 * the {@link MockDriver}. All drivers are preserved and19 * can be restored with {@link #restoreDrivers}.20 */21public class JDBCMockObjectFactory22{23 private MockDataSource dataSource;24 private MockDriver driver;25 private MockConnection connection;26 private Set preservedDrivers;27 28 /**29 * Creates a new set of mock objects.30 */31 public JDBCMockObjectFactory()32 {33 dataSource = createMockDataSource();34 driver = createMockDriver();35 connection = createMockConnection();36 preservedDrivers = new HashSet();37 setUpDependencies();38 }39 private void setUpDependencies()40 {41 dataSource.setupConnection(connection);42 driver.setupConnection(connection);43 registerMockDriver();44 }45 private void deregisterDrivers()46 {47 try48 {49 Enumeration drivers = DriverManager.getDrivers();50 while(drivers.hasMoreElements())51 {52 DriverManager.deregisterDriver((Driver)drivers.nextElement());53 }54 }55 catch(SQLException exc)56 {57 throw new NestedApplicationException(exc);58 }59 }60 61 private void deregisterMockDrivers()62 {63 try64 {65 Enumeration drivers = DriverManager.getDrivers();66 while(drivers.hasMoreElements())67 {68 Driver currentDriver = (Driver)drivers.nextElement();69 if(currentDriver instanceof MockDriver)70 {71 DriverManager.deregisterDriver(currentDriver);72 }73 }74 }75 catch(SQLException exc)76 {77 throw new NestedApplicationException(exc);78 }79 }80 81 private void preserveDrivers()82 {83 Enumeration drivers = DriverManager.getDrivers();84 while(drivers.hasMoreElements())85 {86 Driver currentDriver = (Driver)drivers.nextElement();87 if(!(currentDriver instanceof MockDriver))88 {89 preservedDrivers.add(currentDriver);90 }91 }92 }93 94 /**95 * Removes all JDBC drivers from the <code>DriveManager</code> and96 * registers the mock driver. The removed drivers are preserved and97 * can be restored with {@link #restoreDrivers}.98 */99 public void registerMockDriver()100 {101 try102 {103 preserveDrivers();104 deregisterDrivers();105 DriverManager.registerDriver(driver);106 }107 catch(SQLException exc)108 {109 throw new NestedApplicationException(exc);110 }111 }112 113 /**114 * Since <code>JDBCMockObjectFactory</code> removes all the115 * drivers from the <code>DriveManager</code> (so the116 * {@link MockDriver} is guaranteed to be the only one)117 * you can use this method to restore the original drivers.118 * Automatically called by {@link com.mockrunner.base.BaseTestCase#tearDown}.119 */120 public void restoreDrivers()121 {122 deregisterMockDrivers();123 try124 {125 Iterator drivers = preservedDrivers.iterator();126 while(drivers.hasNext())127 {128 DriverManager.registerDriver((Driver);129 }130 }131 catch(SQLException exc)132 {133 throw new NestedApplicationException(exc);134 }135 preservedDrivers.clear();136 }137 138 /**139 * Creates the {@link com.mockrunner.mock.jdbc.MockConnection} using <code>new</code>.140 * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockConnection}.141 * @return the {@link com.mockrunner.mock.jdbc.MockConnection}142 */143 public MockConnection createMockConnection()144 {145 return new MockConnection();146 }147 /**148 * Creates the {@link com.mockrunner.mock.jdbc.MockDriver} using <code>new</code>.149 * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockDriver}.150 * @return the {@link com.mockrunner.mock.jdbc.MockDriver}151 */152 public MockDriver createMockDriver()153 {154 return new MockDriver();155 }156 /**157 * Creates the {@link com.mockrunner.mock.jdbc.MockDataSource} using <code>new</code>.158 * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockDataSource}.159 * @return the {@link com.mockrunner.mock.jdbc.MockDataSource}160 */161 public MockDataSource createMockDataSource()162 {163 return new MockDataSource();164 }165 /**166 * Returns the {@link com.mockrunner.mock.jdbc.MockDataSource}.167 * @return the {@link com.mockrunner.mock.jdbc.MockDataSource}168 */169 public MockDataSource getMockDataSource()170 {171 return dataSource;172 }173 174 /**175 * Returns the {@link com.mockrunner.mock.jdbc.MockDriver}....

Full Screen

Full Screen Github


Full Screen

...121 }122 123 public static class TestJDBCMockObjectFactory extends JDBCMockObjectFactory124 {125 public MockConnection createMockConnection()126 {127 return new MockConnection() {};128 }129 public MockDataSource createMockDataSource()130 {131 return new MockDataSource() {};132 }133 public MockDriver createMockDriver()134 {135 return new MockDriver() {};136 }137 }138}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Unobtainium_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?