Remove debugging code and add coverage test for property setter.

pull/192/head
Brett Wooldridge 11 years ago
parent 57ae67a08a
commit da07a6c8b7

@ -397,13 +397,6 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
} }
} }
/** {@inheritDoc} */
@Override
public void dumpPoolState()
{
connectionBag.dumpState();
}
// *********************************************************************** // ***********************************************************************
// Private methods // Private methods
// *********************************************************************** // ***********************************************************************

@ -32,6 +32,4 @@ public interface HikariPoolMBean
int getThreadsAwaitingConnection(); int getThreadsAwaitingConnection();
void closeIdleConnections(); void closeIdleConnections();
void dumpPoolState();
} }

@ -3,6 +3,7 @@ package com.zaxxer.hikari;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -14,60 +15,73 @@ import com.zaxxer.hikari.util.PropertyBeanSetter;
public class TestPropertySetter public class TestPropertySetter
{ {
@Test @Test
public void testProperty1() public void testProperty1()
{ {
File file = new File("src/test/resources/propfile1.properties"); File file = new File("src/test/resources/propfile1.properties");
HikariConfig config = new HikariConfig(file.getPath()); HikariConfig config = new HikariConfig(file.getPath());
config.validate(); config.validate();
Assert.assertEquals(5, config.getMinimumIdle()); Assert.assertEquals(5, config.getMinimumIdle());
Assert.assertEquals("SELECT 1", config.getConnectionTestQuery()); Assert.assertEquals("SELECT 1", config.getConnectionTestQuery());
} }
@Test @Test
public void testProperty2() throws Exception public void testProperty2() throws Exception
{ {
File file = new File("src/test/resources/propfile2.properties"); File file = new File("src/test/resources/propfile2.properties");
HikariConfig config = new HikariConfig(file.getPath()); HikariConfig config = new HikariConfig(file.getPath());
config.validate(); config.validate();
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
DataSource dataSource = (DataSource) clazz.newInstance(); DataSource dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties()); PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties());
} }
@Test @Test
public void testObjectProperty() throws Exception public void testObjectProperty() throws Exception
{ {
HikariConfig config = new HikariConfig(); HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
PrintWriter writer = new PrintWriter(new ByteArrayOutputStream()); PrintWriter writer = new PrintWriter(new ByteArrayOutputStream());
config.addDataSourceProperty("logWriter", writer); config.addDataSourceProperty("logWriter", writer);
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
DataSource dataSource = (DataSource) clazz.newInstance(); DataSource dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties()); PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties());
Assert.assertSame(PrintWriter.class, dataSource.getLogWriter().getClass()); Assert.assertSame(PrintWriter.class, dataSource.getLogWriter().getClass());
} }
@Test @Test
public void testPropertyUpperCase() throws Exception public void testPropertyUpperCase() throws Exception
{ {
File file = new File("src/test/resources/propfile3.properties"); File file = new File("src/test/resources/propfile3.properties");
HikariConfig config = new HikariConfig(file.getPath()); HikariConfig config = new HikariConfig(file.getPath());
config.validate(); config.validate();
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
DataSource dataSource = (DataSource) clazz.newInstance(); DataSource dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties()); PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties());
} }
@Test @Test
public void testGetPropertyNames() throws Exception public void testGetPropertyNames() throws Exception
{ {
Set<String> propertyNames = PropertyBeanSetter.getPropertyNames(HikariConfig.class); Set<String> propertyNames = PropertyBeanSetter.getPropertyNames(HikariConfig.class);
Assert.assertTrue(propertyNames.contains("dataSourceClassName")); Assert.assertTrue(propertyNames.contains("dataSourceClassName"));
} }
@Test
public void testSetNonExistantPropertyName() throws Exception
{
try {
Properties props = new Properties();
props.put("what", "happened");
PropertyBeanSetter.setTargetFromProperties(new HikariConfig(), props);
Assert.fail();
}
catch (RuntimeException e) {
}
}
} }

@ -384,13 +384,6 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
connectionBag.values(STATE_NOT_IN_USE).stream().filter(p -> connectionBag.reserve(p)).forEach(bagEntry -> closeConnection(bagEntry)); connectionBag.values(STATE_NOT_IN_USE).stream().filter(p -> connectionBag.reserve(p)).forEach(bagEntry -> closeConnection(bagEntry));
} }
/** {@inheritDoc} */
@Override
public void dumpPoolState()
{
connectionBag.dumpState();
}
// *********************************************************************** // ***********************************************************************
// Private methods // Private methods
// *********************************************************************** // ***********************************************************************

@ -32,6 +32,4 @@ public interface HikariPoolMBean
int getThreadsAwaitingConnection(); int getThreadsAwaitingConnection();
void closeIdleConnections(); void closeIdleConnections();
void dumpPoolState();
} }

@ -3,6 +3,7 @@ package com.zaxxer.hikari;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -14,60 +15,73 @@ import com.zaxxer.hikari.util.PropertyBeanSetter;
public class TestPropertySetter public class TestPropertySetter
{ {
@Test @Test
public void testProperty1() public void testProperty1()
{ {
File file = new File("src/test/resources/propfile1.properties"); File file = new File("src/test/resources/propfile1.properties");
HikariConfig config = new HikariConfig(file.getPath()); HikariConfig config = new HikariConfig(file.getPath());
config.validate(); config.validate();
Assert.assertEquals(5, config.getMinimumIdle()); Assert.assertEquals(5, config.getMinimumIdle());
Assert.assertEquals("SELECT 1", config.getConnectionTestQuery()); Assert.assertEquals("SELECT 1", config.getConnectionTestQuery());
} }
@Test @Test
public void testProperty2() throws Exception public void testProperty2() throws Exception
{ {
File file = new File("src/test/resources/propfile2.properties"); File file = new File("src/test/resources/propfile2.properties");
HikariConfig config = new HikariConfig(file.getPath()); HikariConfig config = new HikariConfig(file.getPath());
config.validate(); config.validate();
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
DataSource dataSource = (DataSource) clazz.newInstance(); DataSource dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties()); PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties());
} }
@Test @Test
public void testObjectProperty() throws Exception public void testObjectProperty() throws Exception
{ {
HikariConfig config = new HikariConfig(); HikariConfig config = new HikariConfig();
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
PrintWriter writer = new PrintWriter(new ByteArrayOutputStream()); PrintWriter writer = new PrintWriter(new ByteArrayOutputStream());
config.addDataSourceProperty("logWriter", writer); config.addDataSourceProperty("logWriter", writer);
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
DataSource dataSource = (DataSource) clazz.newInstance(); DataSource dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties()); PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties());
Assert.assertSame(PrintWriter.class, dataSource.getLogWriter().getClass()); Assert.assertSame(PrintWriter.class, dataSource.getLogWriter().getClass());
} }
@Test @Test
public void testPropertyUpperCase() throws Exception public void testPropertyUpperCase() throws Exception
{ {
File file = new File("src/test/resources/propfile3.properties"); File file = new File("src/test/resources/propfile3.properties");
HikariConfig config = new HikariConfig(file.getPath()); HikariConfig config = new HikariConfig(file.getPath());
config.validate(); config.validate();
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
DataSource dataSource = (DataSource) clazz.newInstance(); DataSource dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties()); PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties());
} }
@Test @Test
public void testGetPropertyNames() throws Exception public void testGetPropertyNames() throws Exception
{ {
Set<String> propertyNames = PropertyBeanSetter.getPropertyNames(HikariConfig.class); Set<String> propertyNames = PropertyBeanSetter.getPropertyNames(HikariConfig.class);
Assert.assertTrue(propertyNames.contains("dataSourceClassName")); Assert.assertTrue(propertyNames.contains("dataSourceClassName"));
} }
@Test
public void testSetNonExistantPropertyName() throws Exception
{
try {
Properties props = new Properties();
props.put("what", "happened");
PropertyBeanSetter.setTargetFromProperties(new HikariConfig(), props);
Assert.fail();
}
catch (RuntimeException e) {
}
}
} }

Loading…
Cancel
Save