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.junit.After; |
15 |
import org.junit.Before; |
16 |
import org.junit.Test; |
17 |
|
18 |
public class TestValidationQueryTimeout extends DefaultTestCase { |
19 |
|
20 |
private static final int TIMEOUT = 10; |
21 |
private static boolean isTimeoutSet; |
22 |
|
23 |
@Before |
24 |
public void setUp() throws SQLException { |
25 |
DriverManager.registerDriver(new MockDriver()); |
26 |
isTimeoutSet = false; |
27 |
} |
28 |
|
29 |
@After |
30 |
public void tearDown() throws SQLException { |
31 |
DriverManager.deregisterDriver(new MockDriver()); |
32 |
} |
33 |
|
34 |
@Test |
35 |
public void testValidationQueryTimeoutEnabled() throws Exception { |
36 |
// use our mock driver |
37 |
this.datasource.setDriverClassName(MockDriver.class.getName()); |
38 |
this.datasource.setUrl(MockDriver.url); |
39 |
|
40 |
// Required to trigger validation query's execution |
41 |
this.datasource.setTestOnBorrow(true); |
42 |
this.datasource.setValidationInterval(-1); |
43 |
this.datasource.setValidationQuery("SELECT 1"); |
44 |
this.datasource.setValidationQueryTimeout(TIMEOUT); |
45 |
|
46 |
// because testOnBorrow is true, this triggers the validation query |
47 |
this.datasource.getConnection(); |
48 |
Assert.assertTrue(isTimeoutSet); |
49 |
} |
50 |
|
51 |
@Test |
52 |
public void testValidationQueryTimeoutDisabled() throws Exception { |
53 |
// use our mock driver |
54 |
this.datasource.setDriverClassName(MockDriver.class.getName()); |
55 |
this.datasource.setUrl(MockDriver.url); |
56 |
|
57 |
// Required to trigger validation query's execution |
58 |
this.datasource.setTestOnBorrow(true); |
59 |
this.datasource.setValidationInterval(-1); |
60 |
this.datasource.setValidationQuery("SELECT 1"); |
61 |
this.datasource.setValidationQueryTimeout(-1); |
62 |
|
63 |
// because testOnBorrow is true, this triggers the validation query |
64 |
this.datasource.getConnection(); |
65 |
Assert.assertFalse(isTimeoutSet); |
66 |
} |
67 |
|
68 |
/** |
69 |
* Mock Driver, Connection and Statement implementations use to verify setQueryTimeout was called. |
70 |
*/ |
71 |
public static class MockDriver implements java.sql.Driver { |
72 |
public static final String url = "jdbc:tomcat:mock"; |
73 |
|
74 |
public MockDriver() { |
75 |
} |
76 |
|
77 |
@Override |
78 |
public boolean acceptsURL(String url) throws SQLException { |
79 |
return url!=null && url.equals(MockDriver.url); |
80 |
} |
81 |
|
82 |
@Override |
83 |
public Connection connect(String url, Properties info) throws SQLException { |
84 |
return new MockConnection(info); |
85 |
} |
86 |
|
87 |
@Override |
88 |
public int getMajorVersion() { |
89 |
return 0; |
90 |
} |
91 |
|
92 |
@Override |
93 |
public int getMinorVersion() { |
94 |
return 0; |
95 |
} |
96 |
|
97 |
@Override |
98 |
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { |
99 |
return null; |
100 |
} |
101 |
|
102 |
@Override |
103 |
public boolean jdbcCompliant() { |
104 |
return false; |
105 |
} |
106 |
|
107 |
@Override |
108 |
public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
109 |
return null; |
110 |
} |
111 |
} |
112 |
|
113 |
public static class MockConnection extends org.apache.tomcat.jdbc.test.driver.Connection { |
114 |
public MockConnection(Properties info) { |
115 |
super(info); |
116 |
} |
117 |
|
118 |
@Override |
119 |
public Statement createStatement() throws SQLException { |
120 |
return new MockStatement(); |
121 |
} |
122 |
} |
123 |
|
124 |
public static class MockStatement extends org.apache.tomcat.jdbc.test.driver.Statement { |
125 |
@Override |
126 |
public void setQueryTimeout(int seconds) throws SQLException { |
127 |
super.setQueryTimeout(seconds); |
128 |
Assert.assertEquals(TIMEOUT, seconds); |
129 |
isTimeoutSet = true; |
130 |
} |
131 |
} |
132 |
|
133 |
} |