Author: fhanik
Date: Thu Jul 24 13:17:46 2014
New Revision: 1613111
URL: http://svn.apache.org/r1613111
Log:
Make tests pass by properly using the mock objects and not rely on a pre
existing table.
Modified:
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestSlowQueryReport.java
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestValidationQueryTimeout.java
Modified:
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestSlowQueryReport.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestSlowQueryReport.java?rev=1613111&r1=1613110&r2=1613111&view=diff
==============================================================================
---
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestSlowQueryReport.java
(original)
+++
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestSlowQueryReport.java
Thu Jul 24 13:17:46 2014
@@ -19,17 +19,25 @@ package org.apache.tomcat.jdbc.test;
import java.lang.management.ManagementFactory;
import java.sql.CallableStatement;
import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.DriverPropertyInfo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.sql.SQLTimeoutException;
import java.sql.Statement;
import java.util.Map;
+import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Logger;
import javax.management.AttributeChangeNotification;
import javax.management.Notification;
import javax.management.NotificationListener;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import org.apache.tomcat.jdbc.pool.ConnectionPool;
@@ -37,18 +45,34 @@ import org.apache.tomcat.jdbc.pool.inter
import org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx;
public class TestSlowQueryReport extends DefaultTestCase {
+ public static final String superSlowSql = "select count(1) from test where
val1 like 'ewq%eq' and val2 = 'ew%rre' and val3 = 'sda%da' and val4 =
'dad%ada'";
+ public static final String failedSql = "select 1 from non_existent";
+ @Before
+ public void setUp() throws SQLException {
+ DriverManager.registerDriver(new MockDriver());
+
+ // use our mock driver
+ this.datasource.setDriverClassName(MockDriver.class.getName());
+ this.datasource.setUrl(MockDriver.url);
+
+ // Required to trigger validation query's execution
+ this.datasource.setInitialSize(1);
+ this.datasource.setTestOnBorrow(true);
+ this.datasource.setValidationInterval(-1);
+ this.datasource.setValidationQuery("SELECT 1");
+ this.datasource.setMaxActive(1);
+
this.datasource.setJdbcInterceptors(SlowQueryReportJmx.class.getName()+"(threshold=50,notifyPool=false)");
+ }
@Test
public void testSlowSql() throws Exception {
int count = 3;
- this.init();
this.datasource.setMaxActive(1);
this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName()+"(threshold=50)");
Connection con = this.datasource.getConnection();
- String slowSql = "select count(1) from test where val1 like 'ewq%eq'
and val2 = 'ew%rre' and val3 = 'sda%da' and val4 = 'dad%ada'";
for (int i=0; i<count; i++) {
Statement st = con.createStatement();
- ResultSet rs = st.executeQuery(slowSql);
+ ResultSet rs = st.executeQuery(superSlowSql);
rs.close();
st.close();
}
@@ -60,7 +84,7 @@ public class TestSlowQueryReport extends
System.out.println("Stats:"+stats);
for (int i=0; i<count; i++) {
- PreparedStatement st = con.prepareStatement(slowSql);
+ PreparedStatement st = con.prepareStatement(superSlowSql);
ResultSet rs = st.executeQuery();
rs.close();
st.close();
@@ -68,7 +92,7 @@ public class TestSlowQueryReport extends
System.out.println("Stats:"+stats);
for (int i=0; i<count; i++) {
- CallableStatement st = con.prepareCall(slowSql);
+ CallableStatement st = con.prepareCall(superSlowSql);
ResultSet rs = st.executeQuery();
rs.close();
st.close();
@@ -84,14 +108,10 @@ public class TestSlowQueryReport extends
@Test
public void testSlowSqlJmx() throws Exception {
int count = 1;
- this.init();
- this.datasource.setMaxActive(1);
-
this.datasource.setJdbcInterceptors(SlowQueryReportJmx.class.getName()+"(threshold=50,notifyPool=false)");
Connection con = this.datasource.getConnection();
- String slowSql = "select count(1) from test where val1 like 'ewq%eq'";
for (int i=0; i<count; i++) {
Statement st = con.createStatement();
- ResultSet rs = st.executeQuery(slowSql);
+ ResultSet rs = st.executeQuery(superSlowSql);
rs.close();
st.close();
}
@@ -110,7 +130,7 @@ public class TestSlowQueryReport extends
null);
for (int i=0; i<count; i++) {
- PreparedStatement st = con.prepareStatement(slowSql);
+ PreparedStatement st = con.prepareStatement(superSlowSql);
ResultSet rs = st.executeQuery();
rs.close();
st.close();
@@ -118,7 +138,7 @@ public class TestSlowQueryReport extends
System.out.println("Stats:"+stats);
for (int i=0; i<count; i++) {
- CallableStatement st = con.prepareCall(slowSql);
+ CallableStatement st = con.prepareCall(superSlowSql);
ResultSet rs = st.executeQuery();
rs.close();
st.close();
@@ -134,9 +154,6 @@ public class TestSlowQueryReport extends
@Test
public void testFastSql() throws Exception {
int count = 3;
- this.init();
- this.datasource.setMaxActive(1);
- this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName());
Connection con = this.datasource.getConnection();
String fastSql = this.datasource.getValidationQuery();
for (int i=0; i<count; i++) {
@@ -157,21 +174,16 @@ public class TestSlowQueryReport extends
@Test
public void testFailedSql() throws Exception {
int count = 3;
- this.init();
- this.datasource.setMaxActive(1);
- this.datasource.setJdbcInterceptors(SlowQueryReport.class.getName());
Connection con = this.datasource.getConnection();
- String slowSql = "select 1 from non_existent";
for (int i=0; i<count; i++) {
Statement st = con.createStatement();
try {
- ResultSet rs = st.executeQuery(slowSql);
+ ResultSet rs = st.executeQuery(failedSql);
rs.close();
}catch (Exception x) {
// NO-OP
}
st.close();
-
}
Map<String,SlowQueryReport.QueryStats> map =
SlowQueryReport.getPoolStats(datasource.getPool().getName());
Assert.assertNotNull(map);
@@ -208,5 +220,118 @@ public class TestSlowQueryReport extends
}
}
+ /**
+ * Mock Driver, Connection and Statement implementations use to verify
setQueryTimeout was called.
+ */
+ public static class MockDriver implements java.sql.Driver {
+ public static final String url = "jdbc:tomcat:mock";
+
+ public MockDriver() {
+ }
+
+ @Override
+ public boolean acceptsURL(String url) throws SQLException {
+ return url!=null && url.equals(MockDriver.url);
+ }
+
+ @Override
+ public Connection connect(String url, Properties info) throws
SQLException {
+ return new MockConnection(info);
+ }
+
+ @Override
+ public int getMajorVersion() {
+ return 0;
+ }
+
+ @Override
+ public int getMinorVersion() {
+ return 0;
+ }
+
+ @Override
+ public DriverPropertyInfo[] getPropertyInfo(String url, Properties
info) throws SQLException {
+ return null;
+ }
+
+ @Override
+ public boolean jdbcCompliant() {
+ return false;
+ }
+
+ @Override
+ public Logger getParentLogger() throws SQLFeatureNotSupportedException
{
+ return null;
+ }
+ }
+
+ public static class MockConnection extends
org.apache.tomcat.jdbc.test.driver.Connection {
+ public MockConnection(Properties info) {
+ super(info);
+ }
+
+ @Override
+ public Statement createStatement() throws SQLException {
+ return new MockStatement(false);
+ }
+
+ @Override
+ public PreparedStatement prepareStatement(String sql) throws
SQLException {
+ return new MockStatement(sql.equals(superSlowSql));
+ }
+
+ @Override
+ public CallableStatement prepareCall(String sql) throws SQLException {
+ return new MockStatement(sql.equals(superSlowSql));
+ }
+ }
+
+ public static class MockStatement extends
org.apache.tomcat.jdbc.test.driver.Statement {
+ boolean slow = false;
+
+ public MockStatement(boolean slow) {
+ this.slow = slow;
+ }
+
+ @Override
+ public boolean execute(String sql) throws SQLException {
+ if (failedSql.equals(sql)) {
+ throw new SQLException("Invalid SQL:"+sql);
+ }
+ if (slow || superSlowSql.equals(sql)) {
+ try {
+ Thread.sleep(200);
+ }catch (Exception x) {
+ }
+ }
+ return super.execute(sql);
+ }
+
+ @Override
+ public ResultSet executeQuery(String sql) throws SQLException {
+ if (failedSql.equals(sql)) {
+ throw new SQLException("Invalid SQL:"+sql);
+ }
+ if (slow || superSlowSql.equals(sql)) {
+ try {
+ Thread.sleep(200);
+ }catch (Exception x) {
+ }
+ }
+ return super.executeQuery(sql);
+ }
+
+ @Override
+ public ResultSet executeQuery() throws SQLException {
+ if (slow) {
+ try {
+ Thread.sleep(200);
+ }catch (Exception x) {
+ }
+ }
+ return super.executeQuery();
+ }
+ }
+
}
Modified:
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestValidationQueryTimeout.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestValidationQueryTimeout.java?rev=1613111&r1=1613110&r2=1613111&view=diff
==============================================================================
---
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestValidationQueryTimeout.java
(original)
+++
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestValidationQueryTimeout.java
Thu Jul 24 13:17:46 2014
@@ -22,6 +22,7 @@ import java.sql.DriverPropertyInfo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
+import java.sql.SQLTimeoutException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Logger;
@@ -119,9 +120,6 @@ public class TestValidationQueryTimeout
@Test(expected=SQLException.class)
public void testValidationQueryTimeoutOnConnection() throws Exception {
// use our mock driver
- this.datasource.setDriverClassName("org.h2.Driver");
-
this.datasource.setUrl("jdbc:h2:~/.h2/test;QUERY_TIMEOUT=0;DB_CLOSE_ON_EXIT=FALSE");
-
// Required to trigger validation query's execution
this.datasource.setTestOnConnect(true);
this.datasource.setValidationInterval(-1);
@@ -151,8 +149,6 @@ public class TestValidationQueryTimeout
@Test
public void testLongValidationQueryTime() throws Exception {
// use our mock driver
- this.datasource.setDriverClassName("org.h2.Driver");
-
this.datasource.setUrl("jdbc:h2:~/.h2/test;QUERY_TIMEOUT=0;DB_CLOSE_ON_EXIT=FALSE");
Connection con = this.datasource.getConnection();
Statement stmt = null;
long start = 0, end = 0;
@@ -259,8 +255,20 @@ public class TestValidationQueryTimeout
@Override
public void setQueryTimeout(int seconds) throws SQLException {
super.setQueryTimeout(seconds);
- Assert.assertEquals(TIMEOUT, seconds);
isTimeoutSet = true;
}
+
+ @Override
+ public boolean execute(String sql) throws SQLException {
+ if (longQuery.equals(sql)) {
+ try {
+ Thread.sleep(getQueryTimeout() * 1000);
+ }catch (Exception x) {
+ }
+ throw new SQLTimeoutException();
+ } else {
+ return super.execute(sql);
+ }
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]