More coverage tests and redundant code removal.

pull/192/head
Brett Wooldridge 10 years ago
parent b47d1dc105
commit 9bd925a885

@ -43,9 +43,7 @@ public final class DriverDataSource implements DataSource
driverProperties.put("password", driverProperties.getProperty("password", password));
}
if (DriverManager.getDriver(jdbcUrl) == null) {
throw new IllegalArgumentException("DriverManager was unable to load driver for URL " + jdbcUrl);
}
DriverManager.getDriver(jdbcUrl);
}
catch (SQLException e) {
throw new RuntimeException("Unable to get driver for JDBC URL " + jdbcUrl, e);

@ -26,38 +26,58 @@ import com.zaxxer.hikari.util.DriverDataSource;
public class JdbcDriverTest
{
private HikariDataSource ds;
private HikariDataSource ds;
@After
public void teardown()
{
ds.close();
}
@After
public void teardown()
{
if (ds != null) {
ds.close();
}
}
@Test
public void driverTest1() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
config.setJdbcUrl("jdbc:stub");
config.addDataSourceProperty("user", "bart");
config.addDataSourceProperty("password", "simpson");
@Test
public void driverTest1() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
config.setJdbcUrl("jdbc:stub");
config.addDataSourceProperty("user", "bart");
config.addDataSourceProperty("password", "simpson");
ds = new HikariDataSource(config);
ds = new HikariDataSource(config);
Assert.assertTrue(ds.isWrapperFor(DriverDataSource.class));
Assert.assertTrue(ds.isWrapperFor(DriverDataSource.class));
DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
Assert.assertNotNull(unwrap);
DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
Assert.assertNotNull(unwrap);
Connection connection = ds.getConnection();
connection.close();
Connection connection = ds.getConnection();
connection.close();
connection = ds.getConnection("foo", "bar");
connection.close();
connection = ds.getConnection("foo", "bar");
connection.close();
}
}
@Test
public void driverTest2() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
config.setJdbcUrl("jdbc:invalid");
try {
ds = new HikariDataSource(config);
}
catch (RuntimeException e) {
Assert.assertTrue(e.getMessage().contains("Unable to get driver"));
}
}
}

@ -50,6 +50,7 @@ public class TestConnections
config.setConnectionTestQuery("VALUES 1");
config.setConnectionInitSql("SELECT 1");
config.setRecordMetrics(true);
config.setReadOnly(true);
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(60));
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
@ -357,7 +358,7 @@ public class TestConnections
public void testGetWithUsername() throws Exception
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMinimumIdle(0);
config.setMaximumPoolSize(4);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
@ -366,9 +367,14 @@ public class TestConnections
Connection connection1 = ds.getConnection("foo", "bar");
connection1.close();
Connection connection2 = ds.getConnection("faz", "baf");
Connection connection2 = ds.getConnection("foo", "bar");
connection2.close();
Assert.assertSame(connection1.unwrap(Connection.class), connection2.unwrap(Connection.class));
Connection connection3 = ds.getConnection("faz", "baf");
connection3.close();
HashMap<Object,HikariPool> multiPool = TestElf.getMultiPool(ds);
Assert.assertTrue(multiPool.size() > 1);

@ -16,9 +16,13 @@
package com.zaxxer.hikari;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.SimpleLogger;
import com.zaxxer.hikari.pool.HikariPool;
/**
@ -68,4 +72,17 @@ public final class TestElf
throw new RuntimeException(e);
}
}
public static void setSlf4jTargetStream(Class<?> clazz, PrintStream stream)
{
SimpleLogger simpleLogger = (SimpleLogger) LoggerFactory.getLogger(clazz);
try {
Field field = SimpleLogger.class.getDeclaredField("TARGET_STREAM");
field.setAccessible(true);
field.set(simpleLogger, stream);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}

@ -15,14 +15,30 @@
*/
package com.zaxxer.hikari;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.Assert;
import org.junit.Test;
import com.zaxxer.hikari.mocks.StubDataSource;
/**
* @author Brett Wooldridge
*/
public class TestValidation
{
@Test
public void validateLoadProperties()
{
System.setProperty("hikaricp.configurationFile", "src/test/resources/propfile1.properties");
HikariConfig config = new HikariConfig();
System.clearProperty("hikaricp.configurationFile");
Assert.assertEquals(5, config.getMinimumIdle());
}
@Test
public void validateInvalidCustomizer()
{
@ -34,6 +50,36 @@ public class TestValidation
Assert.assertNull(config.getConnectionCustomizerClassName());
}
@Test
public void validateValidCustomizer()
{
try {
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setConnectionCustomizerClassName("com.zaxxer.hikari.TestValidation$TestCustomizer");
config.validate();
Assert.assertNotNull(config.getConnectionCustomizer());
}
catch (Exception e) {
Assert.fail();
}
}
@Test
public void validateValidCustomizer2()
{
try {
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setConnectionCustomizer(new TestCustomizer());
config.validate();
Assert.assertNotNull(config.getConnectionCustomizer());
}
catch (Exception e) {
Assert.fail();
}
}
@Test
public void validateMissingProperties()
{
@ -169,6 +215,26 @@ public class TestValidation
}
}
@Test
public void validateBothDSAndDSName()
{
try {
HikariConfig config = new HikariConfig();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(HikariConfig.class, ps);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setDataSource(new StubDataSource());
config.validate();
Assert.assertTrue(new String(baos.toByteArray()).contains("both dataSource"));
}
catch (IllegalStateException ise) {
Assert.assertTrue(ise.getMessage().contains("both dataSource"));
}
}
@Test
public void validateMissingTest()
{
@ -200,4 +266,26 @@ public class TestValidation
// pass
}
}
@Test
public void validateZeroConnectionTimeout()
{
try {
HikariConfig config = new HikariConfig();
config.setConnectionTimeout(0);
config.validate();
Assert.assertEquals(Integer.MAX_VALUE, config.getConnectionTimeout());
}
catch (IllegalArgumentException ise) {
// pass
}
}
public static class TestCustomizer implements IConnectionCustomizer
{
@Override
public void customize(Connection connection) throws SQLException
{
}
}
}

@ -57,7 +57,7 @@ public class StubDriver implements Driver
@Override
public boolean acceptsURL(String url) throws SQLException
{
return true;
return "jdbc:stub".equals(url);
}
/** {@inheritDoc} */

@ -43,9 +43,7 @@ public final class DriverDataSource implements DataSource
driverProperties.put("password", driverProperties.getProperty("password", password));
}
if (DriverManager.getDriver(jdbcUrl) == null) {
throw new IllegalArgumentException("DriverManager was unable to load driver for URL " + jdbcUrl);
}
DriverManager.getDriver(jdbcUrl);
}
catch (SQLException e) {
throw new RuntimeException("Unable to get driver for JDBC URL " + jdbcUrl, e);

@ -26,38 +26,58 @@ import com.zaxxer.hikari.util.DriverDataSource;
public class JdbcDriverTest
{
private HikariDataSource ds;
private HikariDataSource ds;
@After
public void teardown()
{
ds.close();
}
@After
public void teardown()
{
if (ds != null) {
ds.close();
}
}
@Test
public void driverTest1() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
config.setJdbcUrl("jdbc:stub");
config.addDataSourceProperty("user", "bart");
config.addDataSourceProperty("password", "simpson");
@Test
public void driverTest1() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
config.setJdbcUrl("jdbc:stub");
config.addDataSourceProperty("user", "bart");
config.addDataSourceProperty("password", "simpson");
ds = new HikariDataSource(config);
ds = new HikariDataSource(config);
Assert.assertTrue(ds.isWrapperFor(DriverDataSource.class));
Assert.assertTrue(ds.isWrapperFor(DriverDataSource.class));
DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
Assert.assertNotNull(unwrap);
DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
Assert.assertNotNull(unwrap);
Connection connection = ds.getConnection();
connection.close();
Connection connection = ds.getConnection();
connection.close();
connection = ds.getConnection("foo", "bar");
connection.close();
connection = ds.getConnection("foo", "bar");
connection.close();
}
}
@Test
public void driverTest2() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
config.setJdbcUrl("jdbc:invalid");
try {
ds = new HikariDataSource(config);
}
catch (RuntimeException e) {
Assert.assertTrue(e.getMessage().contains("Unable to get driver"));
}
}
}

@ -37,6 +37,7 @@ public class MiscTest
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(4);
config.setPoolName("test");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
TestElf.setConfigUnitTest();
@ -45,6 +46,7 @@ public class MiscTest
PrintWriter writer = new PrintWriter(System.out);
ds.setLogWriter(writer);
Assert.assertSame(writer, ds.getLogWriter());
Assert.assertEquals("test", config.getPoolName());
}
finally
{

@ -50,6 +50,7 @@ public class TestConnections
config.setConnectionTestQuery("VALUES 1");
config.setConnectionInitSql("SELECT 1");
config.setRecordMetrics(true);
config.setReadOnly(true);
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(60));
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
@ -357,7 +358,7 @@ public class TestConnections
public void testGetWithUsername() throws Exception
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMinimumIdle(0);
config.setMaximumPoolSize(4);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
@ -366,9 +367,14 @@ public class TestConnections
Connection connection1 = ds.getConnection("foo", "bar");
connection1.close();
Connection connection2 = ds.getConnection("faz", "baf");
Connection connection2 = ds.getConnection("foo", "bar");
connection2.close();
Assert.assertSame(connection1.unwrap(Connection.class), connection2.unwrap(Connection.class));
Connection connection3 = ds.getConnection("faz", "baf");
connection3.close();
HashMap<Object,HikariPool> multiPool = TestElf.getMultiPool(ds);
Assert.assertTrue(multiPool.size() > 1);

@ -16,9 +16,13 @@
package com.zaxxer.hikari;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.SimpleLogger;
import com.zaxxer.hikari.pool.HikariPool;
/**
@ -68,4 +72,17 @@ public final class TestElf
throw new RuntimeException(e);
}
}
public static void setSlf4jTargetStream(Class<?> clazz, PrintStream stream)
{
SimpleLogger simpleLogger = (SimpleLogger) LoggerFactory.getLogger(clazz);
try {
Field field = SimpleLogger.class.getDeclaredField("TARGET_STREAM");
field.setAccessible(true);
field.set(simpleLogger, stream);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}

@ -15,14 +15,30 @@
*/
package com.zaxxer.hikari;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.Assert;
import org.junit.Test;
import com.zaxxer.hikari.mocks.StubDataSource;
/**
* @author Brett Wooldridge
*/
public class TestValidation
{
@Test
public void validateLoadProperties()
{
System.setProperty("hikaricp.configurationFile", "src/test/resources/propfile1.properties");
HikariConfig config = new HikariConfig();
System.clearProperty("hikaricp.configurationFile");
Assert.assertEquals(5, config.getMinimumIdle());
}
@Test
public void validateInvalidCustomizer()
{
@ -34,6 +50,36 @@ public class TestValidation
Assert.assertNull(config.getConnectionCustomizerClassName());
}
@Test
public void validateValidCustomizer()
{
try {
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setConnectionCustomizerClassName("com.zaxxer.hikari.TestValidation$TestCustomizer");
config.validate();
Assert.assertNotNull(config.getConnectionCustomizer());
}
catch (Exception e) {
Assert.fail();
}
}
@Test
public void validateValidCustomizer2()
{
try {
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setConnectionCustomizer(new TestCustomizer());
config.validate();
Assert.assertNotNull(config.getConnectionCustomizer());
}
catch (Exception e) {
Assert.fail();
}
}
@Test
public void validateMissingProperties()
{
@ -169,6 +215,26 @@ public class TestValidation
}
}
@Test
public void validateBothDSAndDSName()
{
try {
HikariConfig config = new HikariConfig();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(HikariConfig.class, ps);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setDataSource(new StubDataSource());
config.validate();
Assert.assertTrue(new String(baos.toByteArray()).contains("both dataSource"));
}
catch (IllegalStateException ise) {
Assert.assertTrue(ise.getMessage().contains("both dataSource"));
}
}
@Test
public void validateMissingTest()
{
@ -200,4 +266,26 @@ public class TestValidation
// pass
}
}
@Test
public void validateZeroConnectionTimeout()
{
try {
HikariConfig config = new HikariConfig();
config.setConnectionTimeout(0);
config.validate();
Assert.assertEquals(Integer.MAX_VALUE, config.getConnectionTimeout());
}
catch (IllegalArgumentException ise) {
// pass
}
}
public static class TestCustomizer implements IConnectionCustomizer
{
@Override
public void customize(Connection connection) throws SQLException
{
}
}
}

@ -57,7 +57,7 @@ public class StubDriver implements Driver
@Override
public boolean acceptsURL(String url) throws SQLException
{
return true;
return "jdbc:stub".equals(url);
}
/** {@inheritDoc} */

Loading…
Cancel
Save