| 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 com.zaxxer.hikari.pool.TestElf.getPool; |
| 21 |
import static org.junit.Assert.assertNotNull; |
| 22 |
import static org.junit.Assert.assertSame; |
| 23 |
import static org.junit.Assert.assertTrue; |
| 24 |
import static org.junit.Assert.fail; |
| 25 |
|
| 26 |
import java.sql.Connection; |
| 27 |
import java.sql.PreparedStatement; |
| 28 |
import java.sql.ResultSet; |
| 29 |
import java.sql.SQLException; |
| 30 |
import java.sql.Statement; |
| 31 |
|
| 32 |
import org.junit.After; |
| 33 |
import org.junit.Before; |
| 34 |
import org.junit.Test; |
| 35 |
|
| 36 |
import com.zaxxer.hikari.HikariConfig; |
| 37 |
import com.zaxxer.hikari.HikariDataSource; |
| 38 |
|
| 39 |
public class ExceptionTest |
| 40 |
{ |
| 41 |
private HikariDataSource ds; |
| 42 |
|
| 43 |
@Before |
| 44 |
public void setup() |
| 45 |
{ |
| 46 |
HikariConfig config = newHikariConfig(); |
| 47 |
config.setMinimumIdle(1); |
| 48 |
config.setMaximumPoolSize(2); |
| 49 |
config.setConnectionTestQuery("VALUES 1"); |
| 50 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 51 |
|
| 52 |
ds = new HikariDataSource(config); |
| 53 |
} |
| 54 |
|
| 55 |
@After |
| 56 |
public void teardown() |
| 57 |
{ |
| 58 |
ds.close(); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void testException1() throws SQLException |
| 63 |
{ |
| 64 |
try (Connection connection = ds.getConnection()) { |
| 65 |
assertNotNull(connection); |
| 66 |
|
| 67 |
PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?"); |
| 68 |
assertNotNull(statement); |
| 69 |
|
| 70 |
ResultSet resultSet = statement.executeQuery(); |
| 71 |
assertNotNull(resultSet); |
| 72 |
|
| 73 |
try { |
| 74 |
statement.getMaxFieldSize(); |
| 75 |
fail(); |
| 76 |
} |
| 77 |
catch (Exception e) { |
| 78 |
assertSame(SQLException.class, e.getClass()); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
HikariPool pool = getPool(ds); |
| 83 |
assertTrue("Total (3) connections not as expected", pool.getTotalConnections() >= 0); |
| 84 |
assertTrue("Idle (3) connections not as expected", pool.getIdleConnections() >= 0); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void testUseAfterStatementClose() throws SQLException |
| 89 |
{ |
| 90 |
Connection connection = ds.getConnection(); |
| 91 |
assertNotNull(connection); |
| 92 |
|
| 93 |
try (Statement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?")) { |
| 94 |
statement.close(); |
| 95 |
statement.getMoreResults(); |
| 96 |
|
| 97 |
fail(); |
| 98 |
} |
| 99 |
catch (SQLException e) { |
| 100 |
assertSame("Connection is closed", e.getMessage()); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void testUseAfterClose() throws SQLException |
| 106 |
{ |
| 107 |
try (Connection connection = ds.getConnection()) { |
| 108 |
assertNotNull(connection); |
| 109 |
connection.close(); |
| 110 |
|
| 111 |
try (Statement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?")) { |
| 112 |
fail(); |
| 113 |
} |
| 114 |
catch (SQLException e) { |
| 115 |
assertSame("Connection is closed", e.getMessage()); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |