| 1 |
package com.zaxxer.hikari.pool; |
| 2 |
|
| 3 |
import com.zaxxer.hikari.mocks.StubConnection; |
| 4 |
import com.zaxxer.hikari.pool.TestElf.FauxWebClassLoader; |
| 5 |
import com.zaxxer.hikari.util.JavassistProxyFactory; |
| 6 |
import org.junit.Assert; |
| 7 |
import org.junit.Test; |
| 8 |
|
| 9 |
import java.lang.reflect.Method; |
| 10 |
import java.nio.file.Files; |
| 11 |
import java.nio.file.Path; |
| 12 |
import java.nio.file.Paths; |
| 13 |
import java.sql.Connection; |
| 14 |
import java.sql.Statement; |
| 15 |
import java.util.stream.Stream; |
| 16 |
|
| 17 |
public class TestJavassistCodegen { |
| 18 |
@Test |
| 19 |
public void testCodegen() throws Exception { |
| 20 |
String tmp = System.getProperty("java.io.tmpdir"); |
| 21 |
JavassistProxyFactory.main(tmp + (tmp.endsWith("/") ? "" : "/")); |
| 22 |
|
| 23 |
Path base = Paths.get(tmp, "target/classes/com/zaxxer/hikari/pool".split("/")); |
| 24 |
Assert.assertTrue("", Files.isRegularFile(base.resolve("HikariProxyConnection.class"))); |
| 25 |
Assert.assertTrue("", Files.isRegularFile(base.resolve("HikariProxyStatement.class"))); |
| 26 |
Assert.assertTrue("", Files.isRegularFile(base.resolve("HikariProxyCallableStatement.class"))); |
| 27 |
Assert.assertTrue("", Files.isRegularFile(base.resolve("HikariProxyPreparedStatement.class"))); |
| 28 |
Assert.assertTrue("", Files.isRegularFile(base.resolve("HikariProxyResultSet.class"))); |
| 29 |
Assert.assertTrue("", Files.isRegularFile(base.resolve("ProxyFactory.class"))); |
| 30 |
|
| 31 |
FauxWebClassLoader fauxClassLoader = new FauxWebClassLoader(); |
| 32 |
Class<?> proxyFactoryClass = fauxClassLoader.loadClass("com.zaxxer.hikari.pool.ProxyFactory"); |
| 33 |
|
| 34 |
Connection connection = new StubConnection(); |
| 35 |
|
| 36 |
Class<?> fastListClass = fauxClassLoader.loadClass("com.zaxxer.hikari.util.FastList"); |
| 37 |
Object fastList = fastListClass.getConstructor(Class.class).newInstance(Statement.class); |
| 38 |
|
| 39 |
Object proxyConnection = getMethod(proxyFactoryClass, "getProxyConnection") |
| 40 |
.invoke(null, |
| 41 |
null /*poolEntry*/, |
| 42 |
connection, |
| 43 |
fastList, |
| 44 |
null /*leakTask*/, |
| 45 |
0L /*now*/, |
| 46 |
Boolean.FALSE /*isReadOnly*/, |
| 47 |
Boolean.FALSE /*isAutoCommit*/); |
| 48 |
Assert.assertNotNull(proxyConnection); |
| 49 |
|
| 50 |
Object proxyStatement = getMethod(proxyConnection.getClass(), "createStatement", 0) |
| 51 |
.invoke(proxyConnection); |
| 52 |
Assert.assertNotNull(proxyStatement); |
| 53 |
} |
| 54 |
|
| 55 |
private Method getMethod(Class<?> clazz, String methodName, Integer... parameterCount) |
| 56 |
{ |
| 57 |
return Stream.of(clazz.getDeclaredMethods()) |
| 58 |
.filter(method -> method.getName().equals(methodName)) |
| 59 |
.filter(method -> (parameterCount.length == 0 || parameterCount[0] == method.getParameterCount())) |
| 60 |
.peek(method -> method.setAccessible(true)) |
| 61 |
.findFirst() |
| 62 |
.orElseThrow(RuntimeException::new); |
| 63 |
} |
| 64 |
} |