This is an automated email from the ASF dual-hosted git repository. remm pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push: new 1bccd6d977 Improve test and extensibility 1bccd6d977 is described below commit 1bccd6d977e313f59598269922fedd9df75b9e42 Author: remm <r...@apache.org> AuthorDate: Fri Nov 29 16:38:28 2024 +0100 Improve test and extensibility --- .../apache/catalina/valves/JDBCAccessLogValve.java | 59 +++++++++++++--------- .../catalina/valves/TestJDBCAccessLogValve.java | 52 ++++++++++--------- 2 files changed, 63 insertions(+), 48 deletions(-) diff --git a/java/org/apache/catalina/valves/JDBCAccessLogValve.java b/java/org/apache/catalina/valves/JDBCAccessLogValve.java index 6ece27a82f..abd8771bf8 100644 --- a/java/org/apache/catalina/valves/JDBCAccessLogValve.java +++ b/java/org/apache/catalina/valves/JDBCAccessLogValve.java @@ -100,7 +100,8 @@ import org.apache.tomcat.util.ExceptionUtils; * @author Peter Rossbach */ -public final class JDBCAccessLogValve extends ValveBase implements AccessLog { +public class JDBCAccessLogValve extends ValveBase implements AccessLog { + // ----------------------------------------------------------- Constructors @@ -156,47 +157,47 @@ public final class JDBCAccessLogValve extends ValveBase implements AccessLog { * * @since 6.0.15 */ - boolean useLongContentLength = false; + protected boolean useLongContentLength = false; /** * The connection username to use when trying to connect to the database. */ - String connectionName = null; + protected String connectionName = null; /** * The connection URL to use when trying to connect to the database. */ - String connectionPassword = null; + protected String connectionPassword = null; /** * Instance of the JDBC Driver class we use as a connection factory. */ - Driver driver = null; + protected Driver driver = null; - private String driverName; - private String connectionURL; - private String tableName; - private String remoteHostField; - private String userField; - private String timestampField; - private String virtualHostField; - private String methodField; - private String queryField; - private String statusField; - private String bytesField; - private String refererField; - private String userAgentField; - private String pattern; - private boolean resolveHosts; + protected String driverName; + protected String connectionURL; + protected String tableName; + protected String remoteHostField; + protected String userField; + protected String timestampField; + protected String virtualHostField; + protected String methodField; + protected String queryField; + protected String statusField; + protected String bytesField; + protected String refererField; + protected String userAgentField; + protected String pattern; + protected boolean resolveHosts; - private Connection conn; - private PreparedStatement ps; + protected Connection conn; + protected PreparedStatement ps; - private long currentTimeMillis; + protected long currentTimeMillis; /** * Should this valve set request attributes for IP address, hostname, protocol and port used for the request. @@ -204,7 +205,7 @@ public final class JDBCAccessLogValve extends ValveBase implements AccessLog { * * @see #setRequestAttributesEnabled(boolean) */ - boolean requestAttributesEnabled = true; + protected boolean requestAttributesEnabled = true; // ------------------------------------------------------------- Properties @@ -558,6 +559,7 @@ public final class JDBCAccessLogValve extends ValveBase implements AccessLog { } conn = driver.connect(connectionURL, props); conn.setAutoCommit(true); + prepare(); String logPattern = pattern; if (logPattern.equals("common")) { ps = conn.prepareStatement( @@ -571,6 +573,15 @@ public final class JDBCAccessLogValve extends ValveBase implements AccessLog { } } + + /** + * Prepare tables for processing. Used by subclasses for testing. + * @throws SQLException if an exception occurs + */ + protected void prepare() throws SQLException { + } + + /** * Close the specified database connection. */ diff --git a/test/org/apache/catalina/valves/TestJDBCAccessLogValve.java b/test/org/apache/catalina/valves/TestJDBCAccessLogValve.java index 621f972c38..acca447510 100644 --- a/test/org/apache/catalina/valves/TestJDBCAccessLogValve.java +++ b/test/org/apache/catalina/valves/TestJDBCAccessLogValve.java @@ -16,9 +16,9 @@ */ package org.apache.catalina.valves; -import java.sql.Connection; -import java.sql.DriverManager; + import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; @@ -71,21 +71,14 @@ public class TestJDBCAccessLogValve extends TomcatBaseTest { @Test public void testValve() throws Exception { - String connectionURL = "jdbc:derby:" + getTemporaryDirectory().getAbsolutePath() + "/" + logPattern; - - Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); - try (Connection connection = DriverManager.getConnection(connectionURL + ";create=true"); - Statement statement = connection.createStatement()) { - statement.execute(SCHEMA); - } - - Tomcat tomcat = getTomcatInstance(); + Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = getProgrammaticRootContext(); - JDBCAccessLogValve accessLogValve = new JDBCAccessLogValve(); + CustomJDBCAccessLogValve accessLogValve = new CustomJDBCAccessLogValve(); accessLogValve.setDriverName("org.apache.derby.jdbc.EmbeddedDriver"); - accessLogValve.setConnectionURL(connectionURL); + accessLogValve.setConnectionURL("jdbc:derby:" + getTemporaryDirectory().getAbsolutePath() + + "/accesslog-" + logPattern + ";create=true"); accessLogValve.setPattern(logPattern); ctx.getParent().getPipeline().addValve(accessLogValve); @@ -99,20 +92,31 @@ public class TestJDBCAccessLogValve extends TomcatBaseTest { rc = getUrl("http://localhost:" + getPort() + "/test2?foo=bar", result, null); Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rc); + accessLogValve.verify(); + tomcat.stop(); - try (Connection connection = DriverManager.getConnection(connectionURL); - Statement statement = connection.createStatement()) { - statement.execute("SELECT * FROM access"); - ResultSet rs = statement.getResultSet(); - Assert.assertTrue(rs.next()); - Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rs.getInt("status")); - Assert.assertEquals("/test1", rs.getString("query")); - Assert.assertTrue(rs.next()); - Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rs.getInt("status")); - Assert.assertEquals("/test2", rs.getString("query")); + } + + protected class CustomJDBCAccessLogValve extends JDBCAccessLogValve { + @Override + protected void prepare() throws SQLException { + try (Statement statement = conn.createStatement()) { + statement.execute(SCHEMA); + } } + public void verify() throws Exception { + try (Statement statement = conn.createStatement()) { + statement.execute("SELECT * FROM access"); + ResultSet rs = statement.getResultSet(); + Assert.assertTrue(rs.next()); + Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rs.getInt("status")); + Assert.assertEquals("/test1", rs.getString("query")); + Assert.assertTrue(rs.next()); + Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rs.getInt("status")); + Assert.assertEquals("/test2", rs.getString("query")); + } + } } - } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org