| 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 com.zaxxer.hikari.HikariConfig; |
| 20 |
import com.zaxxer.hikari.HikariDataSource; |
| 21 |
import com.zaxxer.hikari.util.ConcurrentBag; |
| 22 |
import org.apache.logging.log4j.Level; |
| 23 |
import org.apache.logging.log4j.core.Appender; |
| 24 |
import org.apache.logging.log4j.core.LogEvent; |
| 25 |
import org.apache.logging.log4j.core.Logger; |
| 26 |
import org.apache.logging.log4j.core.appender.AbstractAppender; |
| 27 |
import org.apache.logging.log4j.core.layout.CsvLogEventLayout; |
| 28 |
import org.apache.logging.slf4j.Log4jLogger; |
| 29 |
import org.slf4j.LoggerFactory; |
| 30 |
|
| 31 |
import java.io.DataInputStream; |
| 32 |
import java.io.IOException; |
| 33 |
import java.io.PrintStream; |
| 34 |
import java.lang.reflect.Field; |
| 35 |
import java.net.URL; |
| 36 |
import java.sql.Connection; |
| 37 |
|
| 38 |
/** |
| 39 |
* Utility methods for testing. |
| 40 |
* |
| 41 |
* @author Brett Wooldridge |
| 42 |
*/ |
| 43 |
public final class TestElf |
| 44 |
{ |
| 45 |
private TestElf() { |
| 46 |
// default constructor |
| 47 |
} |
| 48 |
|
| 49 |
public static boolean isJava9() { |
| 50 |
return System.getProperty("java.version").startsWith("9"); |
| 51 |
} |
| 52 |
|
| 53 |
public static HikariPool getPool(final HikariDataSource ds) |
| 54 |
{ |
| 55 |
try { |
| 56 |
Field field = ds.getClass().getDeclaredField("pool"); |
| 57 |
field.setAccessible(true); |
| 58 |
return (HikariPool) field.get(ds); |
| 59 |
} |
| 60 |
catch (Exception e) { |
| 61 |
throw new RuntimeException(e); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
static ConcurrentBag<?> getConcurrentBag(final HikariDataSource ds) |
| 66 |
{ |
| 67 |
try { |
| 68 |
Field field = HikariPool.class.getDeclaredField("connectionBag"); |
| 69 |
field.setAccessible(true); |
| 70 |
return (ConcurrentBag<?>) field.get(getPool(ds)); |
| 71 |
} |
| 72 |
catch (Exception e) { |
| 73 |
throw new RuntimeException(e); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public static HikariConfig getUnsealedConfig(final HikariDataSource ds) |
| 78 |
{ |
| 79 |
try { |
| 80 |
HikariPool pool = getPool(ds); |
| 81 |
Field configField = PoolBase.class.getDeclaredField("config"); |
| 82 |
configField.setAccessible(true); |
| 83 |
HikariConfig config = (HikariConfig) configField.get(pool); |
| 84 |
|
| 85 |
Field field = HikariConfig.class.getDeclaredField("sealed"); |
| 86 |
field.setAccessible(true); |
| 87 |
field.setBoolean(config, false); |
| 88 |
return config; |
| 89 |
} |
| 90 |
catch (Exception e) { |
| 91 |
throw new RuntimeException(e); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static boolean getConnectionCommitDirtyState(final Connection connection) |
| 96 |
{ |
| 97 |
try { |
| 98 |
Field field = ProxyConnection.class.getDeclaredField("isCommitStateDirty"); |
| 99 |
field.setAccessible(true); |
| 100 |
return field.getBoolean(connection); |
| 101 |
} |
| 102 |
catch (Exception e) { |
| 103 |
throw new RuntimeException(e); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
static void setConfigUnitTest(final boolean unitTest) |
| 108 |
{ |
| 109 |
try { |
| 110 |
Field field = HikariConfig.class.getDeclaredField("unitTest"); |
| 111 |
field.setAccessible(true); |
| 112 |
field.setBoolean(null, unitTest); |
| 113 |
} |
| 114 |
catch (Exception e) { |
| 115 |
throw new RuntimeException(e); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
static void setSlf4jTargetStream(final Class<?> clazz, final PrintStream stream) |
| 120 |
{ |
| 121 |
try { |
| 122 |
Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz); |
| 123 |
|
| 124 |
Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger"); |
| 125 |
field.setAccessible(true); |
| 126 |
|
| 127 |
Logger logger = (Logger) field.get(log4Jlogger); |
| 128 |
if (logger.getAppenders().containsKey("string")) { |
| 129 |
Appender appender = logger.getAppenders().get("string"); |
| 130 |
logger.removeAppender(appender); |
| 131 |
} |
| 132 |
|
| 133 |
logger.addAppender(new StringAppender("string", stream)); |
| 134 |
} |
| 135 |
catch (Exception e) { |
| 136 |
throw new RuntimeException(e); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
static void setSlf4jLogLevel(final Class<?> clazz, final Level logLevel) |
| 141 |
{ |
| 142 |
try { |
| 143 |
Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz); |
| 144 |
|
| 145 |
Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger"); |
| 146 |
field.setAccessible(true); |
| 147 |
|
| 148 |
Logger logger = (Logger) field.get(log4Jlogger); |
| 149 |
logger.setLevel(logLevel); |
| 150 |
} |
| 151 |
catch (Exception e) { |
| 152 |
throw new RuntimeException(e); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public static HikariConfig newHikariConfig() |
| 157 |
{ |
| 158 |
final StackTraceElement callerStackTrace = Thread.currentThread().getStackTrace()[2]; |
| 159 |
|
| 160 |
String poolName = callerStackTrace.getMethodName(); |
| 161 |
if ("setup".equals(poolName)) { |
| 162 |
poolName = callerStackTrace.getClassName(); |
| 163 |
} |
| 164 |
|
| 165 |
final HikariConfig config = new HikariConfig(); |
| 166 |
config.setPoolName(poolName); |
| 167 |
return config; |
| 168 |
} |
| 169 |
|
| 170 |
static HikariDataSource newHikariDataSource() |
| 171 |
{ |
| 172 |
final StackTraceElement callerStackTrace = Thread.currentThread().getStackTrace()[2]; |
| 173 |
|
| 174 |
String poolName = callerStackTrace.getMethodName(); |
| 175 |
if ("setup".equals(poolName)) { |
| 176 |
poolName = callerStackTrace.getClassName(); |
| 177 |
} |
| 178 |
|
| 179 |
final HikariDataSource ds = new HikariDataSource(); |
| 180 |
ds.setPoolName(poolName); |
| 181 |
return ds; |
| 182 |
} |
| 183 |
|
| 184 |
private static class StringAppender extends AbstractAppender |
| 185 |
{ |
| 186 |
private PrintStream stream; |
| 187 |
|
| 188 |
StringAppender(final String name, final PrintStream stream) |
| 189 |
{ |
| 190 |
super(name, null, CsvLogEventLayout.createDefaultLayout()); |
| 191 |
this.stream = stream; |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void append(final LogEvent event) |
| 196 |
{ |
| 197 |
stream.println(event.getMessage().getFormattedMessage()); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
public static class FauxWebClassLoader extends ClassLoader |
| 202 |
{ |
| 203 |
static final byte[] classBytes = new byte[16_000]; |
| 204 |
|
| 205 |
@Override |
| 206 |
public Class<?> loadClass(final String name) throws ClassNotFoundException |
| 207 |
{ |
| 208 |
if (name.startsWith("java") || name.startsWith("org")) { |
| 209 |
return super.loadClass(name, true); |
| 210 |
} |
| 211 |
|
| 212 |
final String resourceName = "/" + name.replace('.', '/') + ".class"; |
| 213 |
final URL resource = this.getClass().getResource(resourceName); |
| 214 |
try (DataInputStream is = new DataInputStream(resource.openStream())) { |
| 215 |
int read = 0; |
| 216 |
while (read < classBytes.length) { |
| 217 |
final int rc = is.read(classBytes, read, classBytes.length - read); |
| 218 |
if (rc == -1) { |
| 219 |
break; |
| 220 |
} |
| 221 |
read += rc; |
| 222 |
} |
| 223 |
|
| 224 |
return defineClass(name, classBytes, 0, read); |
| 225 |
} |
| 226 |
catch (IOException e) { |
| 227 |
throw new ClassNotFoundException(name); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |