From 9ce9dc0465435a859dd2045970710f7a9e4bff6f Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Fri, 10 Oct 2014 18:31:45 +0900 Subject: [PATCH] More tests... still need to be merged into the Java 6 branch. --- .../java/com/zaxxer/hikari/MiscTests.java | 49 +++++++++++++++++++ .../com/zaxxer/hikari/TestConnections.java | 29 +++++++++++ .../test/java/com/zaxxer/hikari/TestElf.java | 45 ++++++++++------- 3 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 hikaricp/src/test/java/com/zaxxer/hikari/MiscTests.java diff --git a/hikaricp/src/test/java/com/zaxxer/hikari/MiscTests.java b/hikaricp/src/test/java/com/zaxxer/hikari/MiscTests.java new file mode 100644 index 00000000..f2539c85 --- /dev/null +++ b/hikaricp/src/test/java/com/zaxxer/hikari/MiscTests.java @@ -0,0 +1,49 @@ +/* + * 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.PrintWriter; +import java.sql.SQLException; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Brett Wooldridge + */ +public class MiscTests +{ + @Test + public void testLogWriter() throws SQLException + { + HikariConfig config = new HikariConfig(); + config.setMinimumIdle(1); + config.setMaximumPoolSize(4); + config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); + + final HikariDataSource ds = new HikariDataSource(config); + try { + PrintWriter writer = new PrintWriter(System.out); + ds.setLogWriter(writer); + Assert.assertSame(writer, ds.getLogWriter()); + } + finally + { + ds.close(); + } + } +} diff --git a/hikaricp/src/test/java/com/zaxxer/hikari/TestConnections.java b/hikaricp/src/test/java/com/zaxxer/hikari/TestConnections.java index 5c8255c2..2a3a7d4e 100644 --- a/hikaricp/src/test/java/com/zaxxer/hikari/TestConnections.java +++ b/hikaricp/src/test/java/com/zaxxer/hikari/TestConnections.java @@ -20,12 +20,14 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.HashMap; import java.util.concurrent.TimeUnit; import org.junit.Assert; import org.junit.Test; import com.zaxxer.hikari.mocks.StubConnection; +import com.zaxxer.hikari.pool.HikariPool; /** * System property testProxy can be one of: @@ -322,4 +324,31 @@ public class TestConnections ds.close(); } } + + @Test + public void testGetWithUsername() throws Exception + { + HikariConfig config = new HikariConfig(); + config.setMinimumIdle(1); + config.setMaximumPoolSize(4); + config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); + + final HikariDataSource ds = new HikariDataSource(config); + try { + Connection connection1 = ds.getConnection("foo", "bar"); + connection1.close(); + + Connection connection2 = ds.getConnection("faz", "baf"); + connection2.close(); + + HashMap multiPool = TestElf.getMultiPool(ds); + Assert.assertTrue(multiPool.size() > 1); + + Object[] array = multiPool.keySet().toArray(); + Assert.assertNotEquals(array[0], array[1]); + } + finally { + ds.close(); + } + } } diff --git a/hikaricp/src/test/java/com/zaxxer/hikari/TestElf.java b/hikaricp/src/test/java/com/zaxxer/hikari/TestElf.java index 1b2357c1..7ecf5a5e 100644 --- a/hikaricp/src/test/java/com/zaxxer/hikari/TestElf.java +++ b/hikaricp/src/test/java/com/zaxxer/hikari/TestElf.java @@ -17,6 +17,7 @@ package com.zaxxer.hikari; import java.lang.reflect.Field; +import java.util.HashMap; import com.zaxxer.hikari.pool.HikariPool; @@ -27,22 +28,32 @@ import com.zaxxer.hikari.pool.HikariPool; */ public final class TestElf { - private TestElf() - { - // default constructor - } + private TestElf() { + // default constructor + } - public static HikariPool getPool(HikariDataSource ds) - { - try - { - Field field = ds.getClass().getDeclaredField("pool"); - field.setAccessible(true); - return (HikariPool) field.get(ds); - } - catch (Exception e) - { - throw new RuntimeException(e); - } - } + public static HikariPool getPool(HikariDataSource ds) + { + try { + Field field = ds.getClass().getDeclaredField("pool"); + field.setAccessible(true); + return (HikariPool) field.get(ds); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + + @SuppressWarnings("unchecked") + public static HashMap getMultiPool(HikariDataSource ds) + { + try { + Field field = ds.getClass().getDeclaredField("multiPool"); + field.setAccessible(true); + return (HashMap) field.get(ds); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } }