| 1 |
/* |
| 2 |
* Copyright (C) 2013, 2014 Brett Wooldridge |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.zaxxer.hikari.pool; |
| 18 |
|
| 19 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 20 |
import static org.junit.Assert.assertEquals; |
| 21 |
import static org.junit.Assert.assertSame; |
| 22 |
import static org.junit.Assert.assertTrue; |
| 23 |
import static org.junit.Assert.fail; |
| 24 |
|
| 25 |
import java.io.ByteArrayOutputStream; |
| 26 |
import java.io.PrintWriter; |
| 27 |
import java.util.Properties; |
| 28 |
import java.util.Set; |
| 29 |
|
| 30 |
import javax.sql.DataSource; |
| 31 |
|
| 32 |
import org.junit.Test; |
| 33 |
|
| 34 |
import com.zaxxer.hikari.HikariConfig; |
| 35 |
import com.zaxxer.hikari.util.PropertyElf; |
| 36 |
|
| 37 |
public class TestPropertySetter |
| 38 |
{ |
| 39 |
@Test |
| 40 |
public void testProperty1() throws Exception |
| 41 |
{ |
| 42 |
Properties propfile1 = new Properties(); |
| 43 |
propfile1.load(TestPropertySetter.class.getResourceAsStream("/propfile1.properties")); |
| 44 |
HikariConfig config = new HikariConfig(propfile1); |
| 45 |
config.validate(); |
| 46 |
|
| 47 |
assertEquals(5, config.getMinimumIdle()); |
| 48 |
assertEquals("SELECT 1", config.getConnectionTestQuery()); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void testProperty2() throws Exception |
| 53 |
{ |
| 54 |
Properties propfile2 = new Properties(); |
| 55 |
propfile2.load(TestPropertySetter.class.getResourceAsStream("/propfile2.properties")); |
| 56 |
HikariConfig config = new HikariConfig(propfile2); |
| 57 |
config.validate(); |
| 58 |
|
| 59 |
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); |
| 60 |
DataSource dataSource = (DataSource) clazz.newInstance(); |
| 61 |
PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties()); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void testObjectProperty() throws Exception |
| 66 |
{ |
| 67 |
HikariConfig config = newHikariConfig(); |
| 68 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 69 |
PrintWriter writer = new PrintWriter(new ByteArrayOutputStream()); |
| 70 |
config.addDataSourceProperty("logWriter", writer); |
| 71 |
|
| 72 |
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); |
| 73 |
DataSource dataSource = (DataSource) clazz.newInstance(); |
| 74 |
PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties()); |
| 75 |
|
| 76 |
assertSame(PrintWriter.class, dataSource.getLogWriter().getClass()); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void testPropertyUpperCase() throws Exception |
| 81 |
{ |
| 82 |
Properties propfile3 = new Properties(); |
| 83 |
propfile3.load(TestPropertySetter.class.getResourceAsStream("/propfile3.properties")); |
| 84 |
HikariConfig config = new HikariConfig(propfile3); |
| 85 |
config.validate(); |
| 86 |
|
| 87 |
Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); |
| 88 |
DataSource dataSource = (DataSource) clazz.newInstance(); |
| 89 |
PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties()); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void testGetPropertyNames() throws Exception |
| 94 |
{ |
| 95 |
Set<String> propertyNames = PropertyElf.getPropertyNames(HikariConfig.class); |
| 96 |
assertTrue(propertyNames.contains("dataSourceClassName")); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void testSetNonExistantPropertyName() throws Exception |
| 101 |
{ |
| 102 |
try { |
| 103 |
Properties props = new Properties(); |
| 104 |
props.put("what", "happened"); |
| 105 |
PropertyElf.setTargetFromProperties(new HikariConfig(), props); |
| 106 |
fail(); |
| 107 |
} |
| 108 |
catch (RuntimeException e) { |
| 109 |
} |
| 110 |
} |
| 111 |
} |