Merge pull request #9593 from shapirov103/jdbc-loading
[BAEL-4123] What is the purpose of 'Class.forName(“MY_JDBC_DRIVER”)'?
This commit is contained in:
commit
3265a5def1
|
@ -0,0 +1,72 @@
|
||||||
|
package com.baeldung.jdbc;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.sql.Driver;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class JdbcDriverLoadingUnitTest {
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws SQLException {
|
||||||
|
if (!org.postgresql.Driver.isRegistered()) {
|
||||||
|
org.postgresql.Driver.register();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Driver is registered automatically with the service provider mechanism.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void givenJdbcDriverWithSP_whenGettingDriver_thenDriverIsReturned() throws SQLException {
|
||||||
|
Driver driver = getDriver();
|
||||||
|
assertNotNull("Expected initialized postgres driver", driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Driver is registered automatically, then deregistered.
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
@Test(expected = SQLException.class)
|
||||||
|
public void givenRegisteredJdbcDriver_whenDeregister_thenNoDriverIsReturned() throws SQLException {
|
||||||
|
Driver driver = getDriver();
|
||||||
|
DriverManager.deregisterDriver(driver);
|
||||||
|
driver = getDriver();
|
||||||
|
assertNull(driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Driver is automatically by the JVM, then deregistered, then registered again.
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void givenNoRegisteredDriver_whenClassForName_thenDriverIsReturned() throws SQLException, ClassNotFoundException {
|
||||||
|
deregisterDriver();
|
||||||
|
|
||||||
|
Driver driver = null;
|
||||||
|
try {
|
||||||
|
driver = getDriver();
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
assertNull("Must fail on attempt to get a deregistered driver", driver);
|
||||||
|
|
||||||
|
org.postgresql.Driver.register(); // This is invoked as part of Class.forName() logic
|
||||||
|
|
||||||
|
driver = getDriver();
|
||||||
|
assertNotNull("Driver must register as part of class loading", driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deregisterDriver() throws SQLException {
|
||||||
|
org.postgresql.Driver.deregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Driver getDriver() throws SQLException {
|
||||||
|
return DriverManager.getDriver("jdbc:postgresql:");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue