From 57ae67a08a64eb21a73df0487448b7d09d2f3fc1 Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Mon, 27 Oct 2014 17:35:10 +0900 Subject: [PATCH] Improve test coverage. --- .../com/zaxxer/hikari/HikariDataSource.java | 12 +- .../com/zaxxer/hikari/TestConnections.java | 5 + .../zaxxer/hikari/mocks/StubDataSource.java | 211 +++++++++--------- .../com/zaxxer/hikari/HikariDataSource.java | 12 +- .../com/zaxxer/hikari/TestConnections.java | 5 + .../zaxxer/hikari/mocks/StubDataSource.java | 211 +++++++++--------- 6 files changed, 240 insertions(+), 216 deletions(-) diff --git a/hikaricp-java6/src/main/java/com/zaxxer/hikari/HikariDataSource.java b/hikaricp-java6/src/main/java/com/zaxxer/hikari/HikariDataSource.java index 5e7e9078..544fe795 100644 --- a/hikaricp-java6/src/main/java/com/zaxxer/hikari/HikariDataSource.java +++ b/hikaricp-java6/src/main/java/com/zaxxer/hikari/HikariDataSource.java @@ -47,7 +47,6 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea private final HashMap multiPool; private volatile boolean isShutdown; - private int loginTimeout; private final HikariPool fastPathPool; private volatile HikariPool pool; @@ -152,14 +151,21 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea @Override public void setLoginTimeout(int seconds) throws SQLException { - this.loginTimeout = seconds; + for (HikariPool hikariPool : multiPool.values()) { + hikariPool.getDataSource().setLoginTimeout(seconds); + } } /** {@inheritDoc} */ @Override public int getLoginTimeout() throws SQLException { - return loginTimeout; + HikariPool hikariPool = multiPool.values().iterator().next(); + if (hikariPool != null) { + return hikariPool.getDataSource().getLoginTimeout(); + } + + return 0; } /** {@inheritDoc} */ diff --git a/hikaricp-java6/src/test/java/com/zaxxer/hikari/TestConnections.java b/hikaricp-java6/src/test/java/com/zaxxer/hikari/TestConnections.java index 15cbf69a..28b16fe8 100644 --- a/hikaricp-java6/src/test/java/com/zaxxer/hikari/TestConnections.java +++ b/hikaricp-java6/src/test/java/com/zaxxer/hikari/TestConnections.java @@ -57,6 +57,8 @@ public class TestConnections config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); HikariDataSource ds = new HikariDataSource(config); + ds.setLoginTimeout(10); + Assert.assertSame(10, ds.getLoginTimeout()); try { Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); @@ -385,6 +387,9 @@ public class TestConnections Connection connection = ds.getConnection(); connection.close(); + + PoolUtilities.quietlySleep(1001L); + connection = ds.getConnection(); } finally { StubConnection.oldDriver = false; diff --git a/hikaricp-java6/src/test/java/com/zaxxer/hikari/mocks/StubDataSource.java b/hikaricp-java6/src/test/java/com/zaxxer/hikari/mocks/StubDataSource.java index d5675c9a..e5225197 100644 --- a/hikaricp-java6/src/test/java/com/zaxxer/hikari/mocks/StubDataSource.java +++ b/hikaricp-java6/src/test/java/com/zaxxer/hikari/mocks/StubDataSource.java @@ -30,109 +30,110 @@ import javax.sql.DataSource; */ public class StubDataSource implements DataSource { - private String user; - private String password; - private PrintWriter logWriter; - private SQLException throwException; - - 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; - } - - public void setURL(String url) - { - // we don't care - } - - /** {@inheritDoc} */ - @Override - public PrintWriter getLogWriter() throws SQLException - { - return logWriter; - } - - /** {@inheritDoc} */ - @Override - public void setLogWriter(PrintWriter out) throws SQLException - { - this.logWriter = out; - } - - /** {@inheritDoc} */ - @Override - public void setLoginTimeout(int seconds) throws SQLException - { - } - - /** {@inheritDoc} */ - @Override - public int getLoginTimeout() throws SQLException - { - return 0; - } - - /** {@inheritDoc} */ - public Logger getParentLogger() throws SQLFeatureNotSupportedException - { - return null; - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override - public T unwrap(Class iface) throws SQLException - { - if (iface.isInstance(this)) { - return (T) this; - } - - throw new SQLException("Wrapped DataSource is not an instance of " + iface); - } - - /** {@inheritDoc} */ - @Override - public boolean isWrapperFor(Class iface) throws SQLException - { - return false; - } - - /** {@inheritDoc} */ - @Override - public Connection getConnection() throws SQLException - { - if (throwException != null) - { - throw throwException; - } - - return new StubConnection(); - } - - /** {@inheritDoc} */ - @Override - public Connection getConnection(String username, String password) throws SQLException - { - return new StubConnection(); - } - - public void setThrowException(SQLException e) - { - this.throwException = e; - } + private String user; + private String password; + private PrintWriter logWriter; + private SQLException throwException; + private int loginTimeout; + + 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; + } + + public void setURL(String url) + { + // we don't care + } + + /** {@inheritDoc} */ + @Override + public PrintWriter getLogWriter() throws SQLException + { + return logWriter; + } + + /** {@inheritDoc} */ + @Override + public void setLogWriter(PrintWriter out) throws SQLException + { + this.logWriter = out; + } + + /** {@inheritDoc} */ + @Override + public void setLoginTimeout(int seconds) throws SQLException + { + this.loginTimeout = seconds; + } + + /** {@inheritDoc} */ + @Override + public int getLoginTimeout() throws SQLException + { + return loginTimeout; + } + + /** {@inheritDoc} */ + public Logger getParentLogger() throws SQLFeatureNotSupportedException + { + return null; + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override + public T unwrap(Class iface) throws SQLException + { + if (iface.isInstance(this)) { + return (T) this; + } + + throw new SQLException("Wrapped DataSource is not an instance of " + iface); + } + + /** {@inheritDoc} */ + @Override + public boolean isWrapperFor(Class iface) throws SQLException + { + return false; + } + + /** {@inheritDoc} */ + @Override + public Connection getConnection() throws SQLException + { + if (throwException != null) { + throw throwException; + } + + return new StubConnection(); + } + + /** {@inheritDoc} */ + @Override + public Connection getConnection(String username, String password) throws SQLException + { + return new StubConnection(); + } + + public void setThrowException(SQLException e) + { + this.throwException = e; + } } diff --git a/hikaricp/src/main/java/com/zaxxer/hikari/HikariDataSource.java b/hikaricp/src/main/java/com/zaxxer/hikari/HikariDataSource.java index 5e7e9078..544fe795 100644 --- a/hikaricp/src/main/java/com/zaxxer/hikari/HikariDataSource.java +++ b/hikaricp/src/main/java/com/zaxxer/hikari/HikariDataSource.java @@ -47,7 +47,6 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea private final HashMap multiPool; private volatile boolean isShutdown; - private int loginTimeout; private final HikariPool fastPathPool; private volatile HikariPool pool; @@ -152,14 +151,21 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea @Override public void setLoginTimeout(int seconds) throws SQLException { - this.loginTimeout = seconds; + for (HikariPool hikariPool : multiPool.values()) { + hikariPool.getDataSource().setLoginTimeout(seconds); + } } /** {@inheritDoc} */ @Override public int getLoginTimeout() throws SQLException { - return loginTimeout; + HikariPool hikariPool = multiPool.values().iterator().next(); + if (hikariPool != null) { + return hikariPool.getDataSource().getLoginTimeout(); + } + + return 0; } /** {@inheritDoc} */ diff --git a/hikaricp/src/test/java/com/zaxxer/hikari/TestConnections.java b/hikaricp/src/test/java/com/zaxxer/hikari/TestConnections.java index 15cbf69a..28b16fe8 100644 --- a/hikaricp/src/test/java/com/zaxxer/hikari/TestConnections.java +++ b/hikaricp/src/test/java/com/zaxxer/hikari/TestConnections.java @@ -57,6 +57,8 @@ public class TestConnections config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); HikariDataSource ds = new HikariDataSource(config); + ds.setLoginTimeout(10); + Assert.assertSame(10, ds.getLoginTimeout()); try { Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); @@ -385,6 +387,9 @@ public class TestConnections Connection connection = ds.getConnection(); connection.close(); + + PoolUtilities.quietlySleep(1001L); + connection = ds.getConnection(); } finally { StubConnection.oldDriver = false; diff --git a/hikaricp/src/test/java/com/zaxxer/hikari/mocks/StubDataSource.java b/hikaricp/src/test/java/com/zaxxer/hikari/mocks/StubDataSource.java index d5675c9a..e5225197 100644 --- a/hikaricp/src/test/java/com/zaxxer/hikari/mocks/StubDataSource.java +++ b/hikaricp/src/test/java/com/zaxxer/hikari/mocks/StubDataSource.java @@ -30,109 +30,110 @@ import javax.sql.DataSource; */ public class StubDataSource implements DataSource { - private String user; - private String password; - private PrintWriter logWriter; - private SQLException throwException; - - 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; - } - - public void setURL(String url) - { - // we don't care - } - - /** {@inheritDoc} */ - @Override - public PrintWriter getLogWriter() throws SQLException - { - return logWriter; - } - - /** {@inheritDoc} */ - @Override - public void setLogWriter(PrintWriter out) throws SQLException - { - this.logWriter = out; - } - - /** {@inheritDoc} */ - @Override - public void setLoginTimeout(int seconds) throws SQLException - { - } - - /** {@inheritDoc} */ - @Override - public int getLoginTimeout() throws SQLException - { - return 0; - } - - /** {@inheritDoc} */ - public Logger getParentLogger() throws SQLFeatureNotSupportedException - { - return null; - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override - public T unwrap(Class iface) throws SQLException - { - if (iface.isInstance(this)) { - return (T) this; - } - - throw new SQLException("Wrapped DataSource is not an instance of " + iface); - } - - /** {@inheritDoc} */ - @Override - public boolean isWrapperFor(Class iface) throws SQLException - { - return false; - } - - /** {@inheritDoc} */ - @Override - public Connection getConnection() throws SQLException - { - if (throwException != null) - { - throw throwException; - } - - return new StubConnection(); - } - - /** {@inheritDoc} */ - @Override - public Connection getConnection(String username, String password) throws SQLException - { - return new StubConnection(); - } - - public void setThrowException(SQLException e) - { - this.throwException = e; - } + private String user; + private String password; + private PrintWriter logWriter; + private SQLException throwException; + private int loginTimeout; + + 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; + } + + public void setURL(String url) + { + // we don't care + } + + /** {@inheritDoc} */ + @Override + public PrintWriter getLogWriter() throws SQLException + { + return logWriter; + } + + /** {@inheritDoc} */ + @Override + public void setLogWriter(PrintWriter out) throws SQLException + { + this.logWriter = out; + } + + /** {@inheritDoc} */ + @Override + public void setLoginTimeout(int seconds) throws SQLException + { + this.loginTimeout = seconds; + } + + /** {@inheritDoc} */ + @Override + public int getLoginTimeout() throws SQLException + { + return loginTimeout; + } + + /** {@inheritDoc} */ + public Logger getParentLogger() throws SQLFeatureNotSupportedException + { + return null; + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override + public T unwrap(Class iface) throws SQLException + { + if (iface.isInstance(this)) { + return (T) this; + } + + throw new SQLException("Wrapped DataSource is not an instance of " + iface); + } + + /** {@inheritDoc} */ + @Override + public boolean isWrapperFor(Class iface) throws SQLException + { + return false; + } + + /** {@inheritDoc} */ + @Override + public Connection getConnection() throws SQLException + { + if (throwException != null) { + throw throwException; + } + + return new StubConnection(); + } + + /** {@inheritDoc} */ + @Override + public Connection getConnection(String username, String password) throws SQLException + { + return new StubConnection(); + } + + public void setThrowException(SQLException e) + { + this.throwException = e; + } }