Implemented ground for a mockable jdbc driver for unit test purposes.
mvn eclipse:eclipse -DdownloadSources mvn test mvn package
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package org.jaydebeapi.mockdriver;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.DriverPropertyInfo;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class MockDriver implements Driver {
|
||||
|
||||
static {
|
||||
MockDriver driver = new MockDriver();
|
||||
try {
|
||||
DriverManager.registerDriver(driver);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection connect(String url, Properties info) throws SQLException {
|
||||
if (!url.startsWith("jdbc:jaydebeapi://")) {
|
||||
return null;
|
||||
}
|
||||
return Mockito.mock(Connection.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptsURL(String url) throws SQLException {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
|
||||
throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMajorVersion() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinorVersion() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean jdbcCompliant() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jaydebeapi.mockdriver.MockDriver
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.jaydebeapi.mockdriver;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MockDriverTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
Connection connection = DriverManager
|
||||
.getConnection("jdbc:jaydebeapi://dummyurl");
|
||||
assertThat(connection, notNullValue());
|
||||
}
|
||||
|
||||
@Test(expected = SQLException.class)
|
||||
public void testWrongUrl() throws Exception {
|
||||
DriverManager.getConnection("jdbc:unkown://dummyurl", "user",
|
||||
"password");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user