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 2014/09/03 18:29:39 UTC

git commit: Tests JDBC logging at the DriverManager level.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 7bb1ad473 -> 76fbbf812


Tests JDBC logging at the DriverManager level.

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

Branch: refs/heads/master
Commit: 76fbbf8121c9943c21154f2c5d0ca9862e20e81c
Parents: 7bb1ad4
Author: Gary Gregory <ga...@gmail.com>
Authored: Wed Sep 3 12:29:35 2014 -0400
Committer: Gary Gregory <ga...@gmail.com>
Committed: Wed Sep 3 12:29:35 2014 -0400

----------------------------------------------------------------------
 .../log4j/io/LoggerPrintWriterJdbcH2Test.java   | 62 ++++++++++++++++++++
 .../resources/log4j2-jdbc-driver-manager.xml    | 20 +++++++
 2 files changed, 82 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/76fbbf81/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/LoggerPrintWriterJdbcH2Test.java
----------------------------------------------------------------------
diff --git a/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/LoggerPrintWriterJdbcH2Test.java b/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/LoggerPrintWriterJdbcH2Test.java
new file mode 100644
index 0000000..46af2f7
--- /dev/null
+++ b/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/LoggerPrintWriterJdbcH2Test.java
@@ -0,0 +1,62 @@
+package org.apache.logging.log4j.io;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.apache.logging.log4j.spi.ExtendedLogger;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.apache.logging.log4j.util.Strings;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+
+public class LoggerPrintWriterJdbcH2Test {
+    @ClassRule
+    public static InitialLoggerContext context = new InitialLoggerContext("log4j2-jdbc-driver-manager.xml");
+
+    private static final String PASSWORD = Strings.EMPTY;
+
+    private static final String USER_ID = "sa";
+
+    private ListAppender listAppender;
+
+    private ListAppender getListAppender() {
+        return listAppender;
+    }
+
+    protected Connection newConnection() throws SQLException {
+        return DriverManager.getConnection("jdbc:h2:mem:Log4j", USER_ID, PASSWORD);
+    }
+
+    private void setListAppender(ListAppender listAppender) {
+        this.listAppender = listAppender;
+    }
+
+    @SuppressWarnings("resource")
+    public void setLogWriter(PrintWriter printWriter) {
+        DriverManager.setLogWriter(printWriter);
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        this.setListAppender(context.getListAppender("List").clear());
+    }
+
+    @Test
+    public void testDriverManager_setLogWriter() throws SQLException {
+        Assert.assertEquals(0, this.getListAppender().getMessages().size());
+        this.setLogWriter(new LoggerPrintWriter((ExtendedLogger) LogManager.getLogger(), Level.ALL));
+        try {
+            this.newConnection().close();
+        } finally {
+            this.setLogWriter(null);
+        }
+        Assert.assertTrue(this.getListAppender().getMessages().size() > 0);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/76fbbf81/log4j-iostreams/src/test/resources/log4j2-jdbc-driver-manager.xml
----------------------------------------------------------------------
diff --git a/log4j-iostreams/src/test/resources/log4j2-jdbc-driver-manager.xml b/log4j-iostreams/src/test/resources/log4j2-jdbc-driver-manager.xml
new file mode 100644
index 0000000..2c59243
--- /dev/null
+++ b/log4j-iostreams/src/test/resources/log4j2-jdbc-driver-manager.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more ~ contributor license agreements. See the NOTICE 
+  file distributed with ~ this work for additional information regarding copyright ownership. ~ The ASF licenses this file 
+  to You under the Apache License, Version 2.0 ~ (the "License"); you may not use this file except in compliance with ~ the 
+  License. You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable 
+  law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES 
+  OR CONDITIONS OF ANY KIND, either express or implied. ~ See the License for the specific language governing permissions and 
+  ~ limitations under the License. -->
+<Configuration name="UnitTest" status="info" packages="org.apache.logging.log4j.test">
+  <Appenders>
+    <List name="List">
+      <PatternLayout pattern="%level %m%n" />
+    </List>
+  </Appenders>
+  <Loggers>
+    <Root level="all">
+      <AppenderRef ref="List" />
+    </Root>
+  </Loggers>
+</Configuration>
\ No newline at end of file