Fixes and tests for property file-based configuration.

pull/6/head
Brett Wooldridge 11 years ago
parent db58400173
commit 8589d58e22

@ -25,6 +25,8 @@ import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.util.PropertyBeanSetter;
public final class HikariConfig implements HikariConfigMBean
{
private static final long CONNECTION_TIMEOUT = 5000L;
@ -98,6 +100,7 @@ public final class HikariConfig implements HikariConfigMBean
FileInputStream fis = new FileInputStream(propFile);
Properties props = new Properties();
props.load(fis);
PropertyBeanSetter.setTargetFromProperties(this, props);
}
catch (IOException io)
{
@ -105,7 +108,7 @@ public final class HikariConfig implements HikariConfigMBean
}
}
public void addDataSourceProperty(String propertyName, String value)
public void addDataSourceProperty(String propertyName, Object value)
{
driverProperties.put(propertyName, value);
}

@ -24,6 +24,8 @@ import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.HikariConfig;
/**
*
* @author Brett Wooldridge
@ -42,27 +44,54 @@ public final class PropertyBeanSetter
for (Object propKey : properties.keySet())
{
String propName = propKey.toString();
String propValue = properties.get(propKey).toString();
if (target instanceof HikariConfig && propName.startsWith("dataSource."))
{
HikariConfig config = (HikariConfig) target;
config.addDataSourceProperty(propName.substring("dataSource.".length()), propValue);
}
else
{
setProperty(target, propName, propValue);
}
}
}
private static void setProperty(Object target, String propName, String propValue)
{
String capitalized = "set" + propName.substring(0, 1).toUpperCase() + propName.substring(1);
try
{
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propName, target.getClass(), null, capitalized);
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod == null)
Class<?> paramClass = writeMethod.getParameterTypes()[0];
if (paramClass == int.class)
{
LOGGER.error("Property setter {}() is does not exist on target class {}", capitalized, target.getClass());
continue;
writeMethod.invoke(target, Integer.parseInt(propValue));
}
else if (paramClass == long.class)
{
writeMethod.invoke(target, Long.parseLong(propValue));
}
else if (paramClass == boolean.class)
{
writeMethod.invoke(target, Boolean.parseBoolean(propValue));
}
else if (paramClass == String.class)
{
writeMethod.invoke(target, propValue);
}
writeMethod.invoke(target, properties.get(propKey));
}
catch (IntrospectionException e)
{
LOGGER.error("Property {} is does not exist on target class {}", propName, target.getClass());
throw new RuntimeException(e);
}
catch (Exception e)
{
LOGGER.error("Exception setting property {} on target class {}", propName, target.getClass(), e);
}
throw new RuntimeException(e);
}
}
}

@ -0,0 +1,38 @@
package com.zaxxer.hikari;
import java.io.File;
import javax.sql.DataSource;
import org.junit.Assert;
import org.junit.Test;
import com.zaxxer.hikari.util.PropertyBeanSetter;
public class TestPropertySetter
{
@Test
public void testProperty1()
{
File file = new File("src/test/resources/propfile1.properties");
HikariConfig config = new HikariConfig(file.getPath());
config.validate();
Assert.assertEquals(1000L, config.getAcquireRetryDelay());
Assert.assertFalse(config.isAutoCommit());
Assert.assertEquals(3, config.getAcquireIncrement());
Assert.assertEquals("SELECT 1", config.getConnectionTestQuery());
}
@Test
public void testProperty2() throws Exception
{
File file = new File("src/test/resources/propfile2.properties");
HikariConfig config = new HikariConfig(file.getPath());
config.validate();
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName());
DataSource dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, config.getDataSourceProperties());
}
}

@ -30,6 +30,29 @@ import javax.sql.DataSource;
*/
public class StubDataSource implements DataSource
{
private String user;
private String password;
public String getUser()
{
return user;
}
public void setUser(String user)
{
this.user = user;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
/** {@inheritDoc} */
public PrintWriter getLogWriter() throws SQLException
{

@ -0,0 +1,4 @@
acquireIncrement=3
acquireRetryDelay=1000
connectionTestQuery=SELECT 1
autoCommit=false

@ -0,0 +1,7 @@
acquireIncrement=3
acquireRetryDelay=1000
connectionTestQuery=SELECT 1
autoCommit=false
dataSourceClassName=com.zaxxer.hikari.mocks.StubDataSource
dataSource.user=test
dataSource.password=test
Loading…
Cancel
Save