Line 0
Link Here
|
|
|
1 |
package org.apache.tomcat.jdbc.test; |
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.DriverManager; |
5 |
import java.sql.DriverPropertyInfo; |
6 |
import java.sql.SQLException; |
7 |
import java.sql.SQLFeatureNotSupportedException; |
8 |
import java.sql.Statement; |
9 |
import java.util.Properties; |
10 |
import java.util.logging.Logger; |
11 |
|
12 |
import junit.framework.Assert; |
13 |
|
14 |
import org.apache.tomcat.jdbc.pool.interceptor.QueryTimeoutInterceptor; |
15 |
import org.junit.After; |
16 |
import org.junit.Before; |
17 |
import org.junit.Test; |
18 |
|
19 |
public class TestValidationQueryTimeout extends DefaultTestCase { |
20 |
|
21 |
private static int TIMEOUT = 10; |
22 |
private static boolean isTimeoutSet; |
23 |
|
24 |
@Before |
25 |
public void setUp() throws SQLException { |
26 |
DriverManager.registerDriver(new MockDriver()); |
27 |
|
28 |
// use our mock driver |
29 |
this.datasource.setDriverClassName(MockDriver.class.getName()); |
30 |
this.datasource.setUrl(MockDriver.url); |
31 |
|
32 |
// Required to trigger validation query's execution |
33 |
this.datasource.setTestOnBorrow(true); |
34 |
this.datasource.setValidationInterval(-1); |
35 |
this.datasource.setValidationQuery("SELECT 1"); |
36 |
this.datasource.setValidationQueryTimeout(TIMEOUT); |
37 |
this.datasource.setConnectionProperties("expectedTimeout=" + TIMEOUT); |
38 |
|
39 |
TIMEOUT = 10; |
40 |
isTimeoutSet = false; |
41 |
} |
42 |
|
43 |
@After |
44 |
public void tearDown() throws SQLException { |
45 |
DriverManager.deregisterDriver(new MockDriver()); |
46 |
} |
47 |
|
48 |
@Test |
49 |
public void testValidationQueryTimeoutEnabled() throws Exception { |
50 |
// because testOnBorrow is true, this triggers the validation query |
51 |
this.datasource.getConnection(); |
52 |
Assert.assertTrue(isTimeoutSet); |
53 |
} |
54 |
|
55 |
@Test |
56 |
public void testValidationQueryTimeoutDisabled() throws Exception { |
57 |
this.datasource.setValidationQueryTimeout(-1); |
58 |
|
59 |
// because testOnBorrow is true, this triggers the validation query |
60 |
this.datasource.getConnection(); |
61 |
Assert.assertFalse(isTimeoutSet); |
62 |
} |
63 |
|
64 |
@Test |
65 |
public void testValidationQueryTimeoutWithQueryTimeoutInterceptor() throws Exception { |
66 |
int interceptorTimeout = 30; |
67 |
this.datasource.setJdbcInterceptors( |
68 |
QueryTimeoutInterceptor.class.getName()+ |
69 |
"(queryTimeout="+ interceptorTimeout +")"); |
70 |
|
71 |
// because testOnBorrow is true, this triggers the validation query |
72 |
Connection con = this.datasource.getConnection(); |
73 |
Assert.assertTrue(isTimeoutSet); |
74 |
|
75 |
// increase the expected timeout to 30, which is what we set for the interceptor |
76 |
TIMEOUT = 30; |
77 |
|
78 |
// now create a statement, make sure the query timeout is set by the interceptor |
79 |
Statement st = con.createStatement(); |
80 |
Assert.assertEquals(interceptorTimeout, st.getQueryTimeout()); |
81 |
st.close(); |
82 |
st = con.prepareStatement(""); |
83 |
Assert.assertEquals(interceptorTimeout, st.getQueryTimeout()); |
84 |
st.close(); |
85 |
st = con.prepareCall(""); |
86 |
Assert.assertEquals(interceptorTimeout, st.getQueryTimeout()); |
87 |
st.close(); |
88 |
con.close(); |
89 |
|
90 |
// pull another connection and check it |
91 |
TIMEOUT = 10; |
92 |
isTimeoutSet = false; |
93 |
this.datasource.getConnection(); |
94 |
Assert.assertTrue(isTimeoutSet); |
95 |
} |
96 |
|
97 |
/** |
98 |
* Mock Driver, Connection and Statement implementations use to verify setQueryTimeout was called. |
99 |
*/ |
100 |
public static class MockDriver implements java.sql.Driver { |
101 |
public static final String url = "jdbc:tomcat:mock"; |
102 |
|
103 |
public MockDriver() { |
104 |
} |
105 |
|
106 |
@Override |
107 |
public boolean acceptsURL(String url) throws SQLException { |
108 |
return url!=null && url.equals(MockDriver.url); |
109 |
} |
110 |
|
111 |
@Override |
112 |
public Connection connect(String url, Properties info) throws SQLException { |
113 |
return new MockConnection(info); |
114 |
} |
115 |
|
116 |
@Override |
117 |
public int getMajorVersion() { |
118 |
return 0; |
119 |
} |
120 |
|
121 |
@Override |
122 |
public int getMinorVersion() { |
123 |
return 0; |
124 |
} |
125 |
|
126 |
@Override |
127 |
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { |
128 |
return null; |
129 |
} |
130 |
|
131 |
@Override |
132 |
public boolean jdbcCompliant() { |
133 |
return false; |
134 |
} |
135 |
|
136 |
@Override |
137 |
public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
138 |
return null; |
139 |
} |
140 |
} |
141 |
|
142 |
public static class MockConnection extends org.apache.tomcat.jdbc.test.driver.Connection { |
143 |
public MockConnection(Properties info) { |
144 |
super(info); |
145 |
} |
146 |
|
147 |
@Override |
148 |
public Statement createStatement() throws SQLException { |
149 |
return new MockStatement(); |
150 |
} |
151 |
} |
152 |
|
153 |
public static class MockStatement extends org.apache.tomcat.jdbc.test.driver.Statement { |
154 |
@Override |
155 |
public void setQueryTimeout(int seconds) throws SQLException { |
156 |
super.setQueryTimeout(seconds); |
157 |
Assert.assertEquals(TIMEOUT, seconds); |
158 |
isTimeoutSet = true; |
159 |
} |
160 |
} |
161 |
|
162 |
} |