Debian Codegraph @

src/test/java/com/zaxxer/hikari/pool/MiscTest.java

raw
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 com.zaxxer.hikari.pool.TestElf.setConfigUnitTest;
22 import static com.zaxxer.hikari.pool.TestElf.setSlf4jLogLevel;
23 import static com.zaxxer.hikari.pool.TestElf.setSlf4jTargetStream;
24 import static com.zaxxer.hikari.util.UtilityElf.createInstance;
25 import static com.zaxxer.hikari.util.UtilityElf.getTransactionIsolation;
26 import static com.zaxxer.hikari.util.UtilityElf.quietlySleep;
27 import static java.util.concurrent.TimeUnit.SECONDS;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
32 import static org.junit.Assert.fail;
33
34 import java.io.ByteArrayOutputStream;
35 import java.io.PrintStream;
36 import java.io.PrintWriter;
37 import java.sql.Connection;
38 import java.sql.SQLException;
39 import java.util.concurrent.Executors;
40 import java.util.concurrent.TimeUnit;
41
42 import org.apache.logging.log4j.Level;
43 import org.junit.Test;
44
45 import com.zaxxer.hikari.HikariConfig;
46 import com.zaxxer.hikari.HikariDataSource;
47
48 /**
49 * @author Brett Wooldridge
50 */
51 public class MiscTest
52 {
53 @Test
54 public void testLogWriter() throws SQLException
55 {
56 HikariConfig config = newHikariConfig();
57 config.setMinimumIdle(0);
58 config.setMaximumPoolSize(4);
59 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
60 setConfigUnitTest(true);
61
62 try (HikariDataSource ds = new HikariDataSource(config)) {
63 PrintWriter writer = new PrintWriter(System.out);
64 ds.setLogWriter(writer);
65 assertSame(writer, ds.getLogWriter());
66 assertEquals("testLogWriter", config.getPoolName());
67 }
68 finally
69 {
70 setConfigUnitTest(false);
71 }
72 }
73
74 @Test
75 public void testInvalidIsolation()
76 {
77 try {
78 getTransactionIsolation("INVALID");
79 fail();
80 }
81 catch (Exception e) {
82 assertTrue(e instanceof IllegalArgumentException);
83 }
84 }
85
86 @Test
87 public void testCreateInstance()
88 {
89 try {
90 createInstance("invalid", null);
91 fail();
92 }
93 catch (RuntimeException e) {
94 assertTrue(e.getCause() instanceof ClassNotFoundException);
95 }
96 }
97
98 @Test
99 public void testLeakDetection() throws Exception
100 {
101 ByteArrayOutputStream baos = new ByteArrayOutputStream();
102 try (PrintStream ps = new PrintStream(baos, true)) {
103 setSlf4jTargetStream(Class.forName("com.zaxxer.hikari.pool.ProxyLeakTask"), ps);
104 setConfigUnitTest(true);
105
106 HikariConfig config = newHikariConfig();
107 config.setMinimumIdle(0);
108 config.setMaximumPoolSize(4);
109 config.setThreadFactory(Executors.defaultThreadFactory());
110 config.setMetricRegistry(null);
111 config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(1));
112 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
113
114 try (HikariDataSource ds = new HikariDataSource(config)) {
115 setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
116 getPool(ds).logPoolState();
117
118 try (Connection connection = ds.getConnection()) {
119 quietlySleep(SECONDS.toMillis(4));
120 connection.close();
121 quietlySleep(SECONDS.toMillis(1));
122 ps.close();
123 String s = new String(baos.toByteArray());
124 assertNotNull("Exception string was null", s);
125 assertTrue("Expected exception to contain 'Connection leak detection' but contains *" + s + "*", s.contains("Connection leak detection"));
126 }
127 }
128 finally
129 {
130 setConfigUnitTest(false);
131 setSlf4jLogLevel(HikariPool.class, Level.INFO);
132 }
133 }
134 }
135 }