Improve code coverage.

pull/192/head
Brett Wooldridge 11 years ago
parent c2334a96cd
commit 017044a5cf

@ -0,0 +1,125 @@
/*
* Copyright (C) 2013 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaxxer.hikari;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.spi.LocationAwareLogger;
import com.zaxxer.hikari.pool.HikariPool;
import com.zaxxer.hikari.util.LeakTask;
import com.zaxxer.hikari.util.PoolUtilities;
/**
* @author Brett Wooldridge
*/
public class MiscTest
{
@Test
public void testLogWriter() throws SQLException
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(4);
config.setPoolName("test");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
TestElf.setConfigUnitTest(true);
final HikariDataSource ds = new HikariDataSource(config);
try {
PrintWriter writer = new PrintWriter(System.out);
ds.setLogWriter(writer);
Assert.assertSame(writer, ds.getLogWriter());
Assert.assertEquals("test", config.getPoolName());
}
finally
{
TestElf.setConfigUnitTest(false);
ds.close();
}
}
@Test
public void testInvalidIsolation()
{
try {
PoolUtilities.getTransactionIsolation("INVALID");
Assert.fail();
}
catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
}
@Test
public void testCreateInstance()
{
try {
PoolUtilities.createInstance("invalid", null);
Assert.fail();
}
catch (RuntimeException e) {
Assert.assertTrue(e.getCause() instanceof ClassNotFoundException);
}
}
@Test
public void testLeakDetection() throws SQLException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(LeakTask.class, ps);
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(4);
config.setPoolName("test");
config.setThreadFactory(Executors.defaultThreadFactory());
config.setMetricRegistry(null);
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(1));
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
TestElf.setConfigUnitTest(true);
final HikariDataSource ds = new HikariDataSource(config);
try {
TestElf.setSlf4jLogLevel(HikariPool.class, LocationAwareLogger.DEBUG_INT);
TestElf.getPool(ds).logPoolState();
Connection connection = ds.getConnection();
PoolUtilities.quietlySleep(TimeUnit.SECONDS.toMillis(4));
connection.close();
PoolUtilities.quietlySleep(TimeUnit.SECONDS.toMillis(1));
ps.close();
String s = new String(baos.toByteArray());
Assert.assertNotNull("Exception string was null", s);
Assert.assertTrue("Expected exception to contain 'Connection leak detection' but contains *" + s + "*", s.contains("Connection leak detection"));
}
finally
{
TestElf.setConfigUnitTest(false);
ds.close();
}
}
}

@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
@ -159,6 +160,35 @@ public class TestValidation
}
}
@Test
public void validateIdleTimeoutTooSmall()
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(HikariConfig.class, ps);
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setIdleTimeout(TimeUnit.SECONDS.toMillis(25));
config.validate();
Assert.assertTrue(new String(baos.toByteArray()).contains("less than 30000ms"));
}
@Test
public void validateIdleTimeoutExceedsLifetime()
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(HikariConfig.class, ps);
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2));
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(3));
config.validate();
Assert.assertTrue(new String(baos.toByteArray()).contains("greater than maxLifetime"));
}
@Test
public void validateInvalidMinIdle()
{

@ -21,6 +21,7 @@ import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
@ -95,6 +96,8 @@ public class MiscTest
config.setMinimumIdle(0);
config.setMaximumPoolSize(4);
config.setPoolName("test");
config.setThreadFactory(Executors.defaultThreadFactory());
config.setMetricRegistry(null);
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(1));
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
TestElf.setConfigUnitTest(true);

@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
@ -159,6 +160,35 @@ public class TestValidation
}
}
@Test
public void validateIdleTimeoutTooSmall()
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(HikariConfig.class, ps);
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setIdleTimeout(TimeUnit.SECONDS.toMillis(25));
config.validate();
Assert.assertTrue(new String(baos.toByteArray()).contains("less than 30000ms"));
}
@Test
public void validateIdleTimeoutExceedsLifetime()
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(HikariConfig.class, ps);
HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2));
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(3));
config.validate();
Assert.assertTrue(new String(baos.toByteArray()).contains("greater than maxLifetime"));
}
@Test
public void validateInvalidMinIdle()
{

Loading…
Cancel
Save