| 1 |
/* |
| 2 |
* Copyright (C) 2013 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 com.zaxxer.hikari.pool.TestElf.getPool; |
| 21 |
import static org.junit.Assert.assertFalse; |
| 22 |
import static org.junit.Assert.assertNotNull; |
| 23 |
import static org.junit.Assert.assertSame; |
| 24 |
import static org.junit.Assert.assertTrue; |
| 25 |
|
| 26 |
import java.sql.Connection; |
| 27 |
import java.sql.SQLException; |
| 28 |
|
| 29 |
import org.junit.Test; |
| 30 |
|
| 31 |
import com.zaxxer.hikari.HikariConfig; |
| 32 |
import com.zaxxer.hikari.HikariDataSource; |
| 33 |
import com.zaxxer.hikari.mocks.StubConnection; |
| 34 |
import com.zaxxer.hikari.mocks.StubDataSource; |
| 35 |
|
| 36 |
/** |
| 37 |
* @author Brett Wooldridge |
| 38 |
*/ |
| 39 |
public class UnwrapTest |
| 40 |
{ |
| 41 |
@Test |
| 42 |
public void testUnwrapConnection() throws SQLException |
| 43 |
{ |
| 44 |
HikariConfig config = newHikariConfig(); |
| 45 |
config.setMinimumIdle(1); |
| 46 |
config.setMaximumPoolSize(1); |
| 47 |
config.setInitializationFailTimeout(0); |
| 48 |
config.setConnectionTestQuery("VALUES 1"); |
| 49 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 50 |
|
| 51 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 52 |
ds.getConnection().close(); |
| 53 |
assertSame("Idle connections not as expected", 1, getPool(ds).getIdleConnections()); |
| 54 |
|
| 55 |
Connection connection = ds.getConnection(); |
| 56 |
assertNotNull(connection); |
| 57 |
|
| 58 |
StubConnection unwrapped = connection.unwrap(StubConnection.class); |
| 59 |
assertTrue("unwrapped connection is not instance of StubConnection: " + unwrapped, (unwrapped != null && unwrapped instanceof StubConnection)); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void testUnwrapDataSource() throws SQLException |
| 65 |
{ |
| 66 |
HikariConfig config = newHikariConfig(); |
| 67 |
config.setMinimumIdle(1); |
| 68 |
config.setMaximumPoolSize(1); |
| 69 |
config.setInitializationFailTimeout(0); |
| 70 |
config.setConnectionTestQuery("VALUES 1"); |
| 71 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 72 |
|
| 73 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 74 |
StubDataSource unwrap = ds.unwrap(StubDataSource.class); |
| 75 |
assertNotNull(unwrap); |
| 76 |
assertTrue(unwrap instanceof StubDataSource); |
| 77 |
|
| 78 |
assertTrue(ds.isWrapperFor(HikariDataSource.class)); |
| 79 |
assertTrue(ds.unwrap(HikariDataSource.class) instanceof HikariDataSource); |
| 80 |
|
| 81 |
assertFalse(ds.isWrapperFor(getClass())); |
| 82 |
try { |
| 83 |
ds.unwrap(getClass()); |
| 84 |
} |
| 85 |
catch (SQLException e) { |
| 86 |
assertTrue(e.getMessage().contains("Wrapped DataSource")); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |