| 1 |
/* |
| 2 |
* Copyright (C) 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 |
package com.zaxxer.hikari.pool; |
| 17 |
|
| 18 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 19 |
import static com.zaxxer.hikari.pool.TestElf.setSlf4jTargetStream; |
| 20 |
import static org.junit.Assert.assertEquals; |
| 21 |
import static org.junit.Assert.assertTrue; |
| 22 |
import static org.junit.Assert.fail; |
| 23 |
|
| 24 |
import java.io.ByteArrayOutputStream; |
| 25 |
import java.io.PrintStream; |
| 26 |
import java.util.concurrent.TimeUnit; |
| 27 |
|
| 28 |
import org.junit.Test; |
| 29 |
|
| 30 |
import com.zaxxer.hikari.HikariConfig; |
| 31 |
|
| 32 |
/** |
| 33 |
* @author Brett Wooldridge |
| 34 |
*/ |
| 35 |
public class TestValidation |
| 36 |
{ |
| 37 |
@Test |
| 38 |
public void validateLoadProperties() |
| 39 |
{ |
| 40 |
System.setProperty("hikaricp.configurationFile", "/propfile1.properties"); |
| 41 |
HikariConfig config = newHikariConfig(); |
| 42 |
System.clearProperty("hikaricp.configurationFile"); |
| 43 |
assertEquals(5, config.getMinimumIdle()); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void validateMissingProperties() |
| 48 |
{ |
| 49 |
try { |
| 50 |
HikariConfig config = new HikariConfig("missing"); |
| 51 |
config.validate(); |
| 52 |
} |
| 53 |
catch (IllegalArgumentException ise) { |
| 54 |
assertTrue(ise.getMessage().contains("property file")); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void validateMissingDS() |
| 60 |
{ |
| 61 |
try { |
| 62 |
HikariConfig config = newHikariConfig(); |
| 63 |
config.validate(); |
| 64 |
fail(); |
| 65 |
} |
| 66 |
catch (IllegalArgumentException ise) { |
| 67 |
assertTrue(ise.getMessage().contains("dataSource or dataSourceClassName or jdbcUrl is required.")); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void validateMissingUrl() |
| 73 |
{ |
| 74 |
try { |
| 75 |
HikariConfig config = newHikariConfig(); |
| 76 |
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver"); |
| 77 |
config.validate(); |
| 78 |
fail(); |
| 79 |
} |
| 80 |
catch (IllegalArgumentException ise) { |
| 81 |
assertTrue(ise.getMessage().contains("jdbcUrl is required with driverClassName")); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void validateDriverAndUrl() |
| 87 |
{ |
| 88 |
try { |
| 89 |
HikariConfig config = newHikariConfig(); |
| 90 |
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver"); |
| 91 |
config.setJdbcUrl("jdbc:stub"); |
| 92 |
config.validate(); |
| 93 |
} |
| 94 |
catch (Throwable t) { |
| 95 |
fail(t.getMessage()); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void validateBadDriver() |
| 101 |
{ |
| 102 |
try { |
| 103 |
HikariConfig config = newHikariConfig(); |
| 104 |
config.setDriverClassName("invalid"); |
| 105 |
config.validate(); |
| 106 |
fail(); |
| 107 |
} |
| 108 |
catch (RuntimeException ise) { |
| 109 |
assertTrue(ise.getMessage().startsWith("Failed to load driver class invalid ")); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void validateInvalidConnectionTimeout() |
| 115 |
{ |
| 116 |
try { |
| 117 |
HikariConfig config = newHikariConfig(); |
| 118 |
config.setConnectionTimeout(10L); |
| 119 |
fail(); |
| 120 |
} |
| 121 |
catch (IllegalArgumentException ise) { |
| 122 |
assertTrue(ise.getMessage().contains("connectionTimeout cannot be less than 250ms")); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void validateInvalidValidationTimeout() |
| 128 |
{ |
| 129 |
try { |
| 130 |
HikariConfig config = newHikariConfig(); |
| 131 |
config.setValidationTimeout(10L); |
| 132 |
fail(); |
| 133 |
} |
| 134 |
catch (IllegalArgumentException ise) { |
| 135 |
assertTrue(ise.getMessage().contains("validationTimeout cannot be less than 250ms")); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void validateInvalidIdleTimeout() |
| 141 |
{ |
| 142 |
try { |
| 143 |
HikariConfig config = newHikariConfig(); |
| 144 |
config.setIdleTimeout(-1L); |
| 145 |
fail("negative idle timeout accepted"); |
| 146 |
} |
| 147 |
catch (IllegalArgumentException ise) { |
| 148 |
assertTrue(ise.getMessage().contains("idleTimeout cannot be negative")); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
@Test |
| 153 |
public void validateIdleTimeoutTooSmall() |
| 154 |
{ |
| 155 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 156 |
PrintStream ps = new PrintStream(baos, true); |
| 157 |
setSlf4jTargetStream(HikariConfig.class, ps); |
| 158 |
|
| 159 |
HikariConfig config = newHikariConfig(); |
| 160 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 161 |
config.setIdleTimeout(TimeUnit.SECONDS.toMillis(5)); |
| 162 |
config.validate(); |
| 163 |
assertTrue(new String(baos.toByteArray()).contains("less than 10000ms")); |
| 164 |
} |
| 165 |
|
| 166 |
@Test |
| 167 |
public void validateIdleTimeoutExceedsLifetime() |
| 168 |
{ |
| 169 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 170 |
PrintStream ps = new PrintStream(baos, true); |
| 171 |
setSlf4jTargetStream(HikariConfig.class, ps); |
| 172 |
|
| 173 |
HikariConfig config = newHikariConfig(); |
| 174 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 175 |
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2)); |
| 176 |
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(3)); |
| 177 |
config.validate(); |
| 178 |
|
| 179 |
String s = new String(baos.toByteArray()); |
| 180 |
assertTrue("idleTimeout is close to or more than maxLifetime, disabling it." + s + "*", s.contains("is close to or more than maxLifetime")); |
| 181 |
} |
| 182 |
|
| 183 |
@Test |
| 184 |
public void validateInvalidMinIdle() |
| 185 |
{ |
| 186 |
try { |
| 187 |
HikariConfig config = newHikariConfig(); |
| 188 |
config.setMinimumIdle(-1); |
| 189 |
fail(); |
| 190 |
} |
| 191 |
catch (IllegalArgumentException ise) { |
| 192 |
assertTrue(ise.getMessage().contains("minimumIdle cannot be negative")); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void validateInvalidMaxPoolSize() |
| 198 |
{ |
| 199 |
try { |
| 200 |
HikariConfig config = newHikariConfig(); |
| 201 |
config.setMaximumPoolSize(0); |
| 202 |
fail(); |
| 203 |
} |
| 204 |
catch (IllegalArgumentException ise) { |
| 205 |
assertTrue(ise.getMessage().contains("maxPoolSize cannot be less than 1")); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
@Test |
| 210 |
public void validateInvalidLifetime() |
| 211 |
{ |
| 212 |
try { |
| 213 |
HikariConfig config = newHikariConfig(); |
| 214 |
config.setConnectionTimeout(Integer.MAX_VALUE); |
| 215 |
config.setIdleTimeout(1000L); |
| 216 |
config.setMaxLifetime(-1L); |
| 217 |
config.setLeakDetectionThreshold(1000L); |
| 218 |
config.validate(); |
| 219 |
fail(); |
| 220 |
} |
| 221 |
catch (IllegalArgumentException ise) { |
| 222 |
// pass |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
@Test |
| 227 |
public void validateInvalidLeakDetection() |
| 228 |
{ |
| 229 |
try { |
| 230 |
HikariConfig config = newHikariConfig(); |
| 231 |
config.setLeakDetectionThreshold(1000L); |
| 232 |
config.validate(); |
| 233 |
fail(); |
| 234 |
} |
| 235 |
catch (IllegalArgumentException ise) { |
| 236 |
// pass |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
@Test |
| 241 |
public void validateZeroConnectionTimeout() |
| 242 |
{ |
| 243 |
try { |
| 244 |
HikariConfig config = newHikariConfig(); |
| 245 |
config.setConnectionTimeout(0); |
| 246 |
config.validate(); |
| 247 |
assertEquals(Integer.MAX_VALUE, config.getConnectionTimeout()); |
| 248 |
} |
| 249 |
catch (IllegalArgumentException ise) { |
| 250 |
// pass |
| 251 |
} |
| 252 |
} |
| 253 |
} |