You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2018/10/16 14:53:26 UTC

logging-log4j2 git commit: Bullet-proof JDBC tests by always calling the SQL DDL to dropo a table even when a test fails.

Repository: logging-log4j2
Updated Branches:
  refs/heads/release-2.x baaa6cb78 -> fef702689


Bullet-proof JDBC tests by always calling the SQL DDL to dropo a table
even when a test fails.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/fef70268
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/fef70268
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/fef70268

Branch: refs/heads/release-2.x
Commit: fef702689cca3634040bdd8843f803fd621c0c84
Parents: baaa6cb
Author: Gary Gregory <ga...@gmail.com>
Authored: Tue Oct 16 08:53:23 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Tue Oct 16 08:53:23 2018 -0600

----------------------------------------------------------------------
 .../apache/logging/log4j/junit/JdbcRule.java    | 82 +++++++++++---------
 1 file changed, 44 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fef70268/log4j-core/src/test/java/org/apache/logging/log4j/junit/JdbcRule.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/junit/JdbcRule.java b/log4j-core/src/test/java/org/apache/logging/log4j/junit/JdbcRule.java
index 9354a5e..825248a 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/junit/JdbcRule.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/JdbcRule.java
@@ -18,7 +18,9 @@ package org.apache.logging.log4j.junit;
 
 import java.sql.Connection;
 import java.sql.Statement;
+import java.util.Objects;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.core.appender.db.jdbc.ConnectionSource;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
@@ -33,45 +35,49 @@ import org.junit.runner.Description;
  */
 public class JdbcRule implements TestRule {
 
-    private final ConnectionSource connectionSource;
-    private final String createTableStatement;
-    private final String dropTableStatement;
+	private final ConnectionSource connectionSource;
+	private final String createTableStatement;
+	private final String dropTableStatement;
 
-    /**
-     * Creates a JdbcRule using a {@link ConnectionSource} and a table creation statement.
-     *
-     * @param connectionSource     a source for obtaining a Connection
-     * @param createTableStatement a SQL DDL statement to set up a table for use in a JUnit test
-     * @param dropTableStatement   a SQL DDL statement to drop the created table
-     */
-    public JdbcRule(final ConnectionSource connectionSource, final String createTableStatement,
-                    final String dropTableStatement) {
-        this.dropTableStatement = dropTableStatement;
-        this.connectionSource = connectionSource;
-        this.createTableStatement = createTableStatement;
-    }
+	/**
+	 * Creates a JdbcRule using a {@link ConnectionSource} and a table creation statement.
+	 *
+	 * @param connectionSource a required source for obtaining a Connection.
+	 * @param createTableStatement an optional SQL DDL statement to create a table for use in a JUnit test.
+	 * @param dropTableStatement an optional SQL DDL statement to drop the created table.
+	 */
+	public JdbcRule(final ConnectionSource connectionSource, final String createTableStatement,
+			final String dropTableStatement) {
+		this.connectionSource = Objects.requireNonNull(connectionSource, "connectionSource");
+		this.createTableStatement = createTableStatement;
+		this.dropTableStatement = dropTableStatement;
+	}
 
-    public ConnectionSource getConnectionSource() {
-        return connectionSource;
-    }
+	public ConnectionSource getConnectionSource() {
+		return connectionSource;
+	}
 
-    @Override
-    public org.junit.runners.model.Statement apply(final org.junit.runners.model.Statement base,
-                                                   final Description description) {
-        return new org.junit.runners.model.Statement() {
-            @Override
-            public void evaluate() throws Throwable {
-                try (final Connection connection = connectionSource.getConnection()) {
-                    try (final Statement statement = connection.createStatement()) {
-                        statement.executeUpdate(createTableStatement);
-                    }
-                    base.evaluate();
-                    try (final Statement statement = connection.createStatement()) {
-                        statement.executeUpdate(dropTableStatement);
-                        statement.execute("SHUTDOWN");
-                    }
-                }
-            }
-        };
-    }
+	@Override
+	public org.junit.runners.model.Statement apply(final org.junit.runners.model.Statement base,
+			final Description description) {
+		return new org.junit.runners.model.Statement() {
+			@Override
+			public void evaluate() throws Throwable {
+				try (final Connection connection = connectionSource.getConnection();
+						final Statement statement = connection.createStatement()) {
+					try {
+						if (StringUtils.isNotEmpty(createTableStatement)) {
+							statement.executeUpdate(createTableStatement);
+						}
+						base.evaluate();
+					} finally {
+						if (StringUtils.isNotEmpty(dropTableStatement)) {
+							statement.executeUpdate(dropTableStatement);
+						}
+						statement.execute("SHUTDOWN");
+					}
+				}
+			}
+		};
+	}
 }