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;
    }

    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);
    }
}

@@ -31,16 +31,51 @@
 4j.io;%0A%0A
+import java.io.OutputStreamWriter;%0A
 import j
@@ -485,16 +485,52 @@
 trings;%0A
+import org.h2.jdbcx.JdbcDataSource;%0A
 import o
@@ -599,16 +599,41 @@
 ssRule;%0A
+import org.junit.Ignore;%0A
 import o
@@ -816,16 +816,79 @@
 xml%22);%0A%0A
+    private static final String H2_URL = %22jdbc:h2:mem:Log4j%22;%0A%0A
     priv
@@ -1027,16 +1027,171 @@
 ender;%0A%0A
+    private LoggerPrintWriter createLoggerPrintWriter() %7B%0A        return new LoggerPrintWriter((ExtendedLogger) LogManager.getLogger(), Level.ALL);%0A    %7D%0A%0A
     priv
@@ -1369,35 +1369,22 @@
 nection(
-%22jdbc:h2:mem:Log4j%22
+H2_URL
 , USER_I
@@ -1519,120 +1519,8 @@
  %7D%0A%0A
-    public void setLogWriter(PrintWriter printWriter) %7B%0A        DriverManager.setLogWriter(printWriter);%0A    %7D%0A%0A
     
@@ -1649,20 +1649,213 @@
     
-%7D%0A%0A    @Test
+    Assert.assertEquals(0, this.getListAppender().getMessages().size());%0A    %7D%0A%0A    @Test%0A    @Ignore(%22DataSource#setLogWriter() has no effect in H2, it uses its own internal logging and an SLF4J bridge.%22)
 %0A   
@@ -1872,28 +1872,25 @@
 id testD
-riverManager
+ataSource
 _setLogW
@@ -1927,39 +1927,521 @@
         
-Assert.assertEquals(0, 
+JdbcDataSource dataSource = new JdbcDataSource();%0A        dataSource.setUrl(H2_URL);%0A        dataSource.setUser(USER_ID);%0A        dataSource.setPassword(PASSWORD);%0A        dataSource.setLogWriter(createLoggerPrintWriter());%0A        // dataSource.setLogWriter(new PrintWriter(new OutputStreamWriter(System.out)));%0A        Connection conn = dataSource.getConnection();%0A        try %7B%0A            conn.prepareCall(%22select 1%22);%0A        %7D finally %7B%0A            conn.close();%0A        %7D%0A        Assert.assertTrue(
 this.get
@@ -2479,87 +2479,191 @@
 ze()
+ %3E 0
 );%0A    
+%7D%0A%0A
     
-this.setLogWriter(new LoggerPrintWriter((ExtendedLogger) Log
+@Test%0A    public void testDriverManager_setLogWriter() throws SQLException %7B%0A        DriverManager.setLogWriter(createLoggerPrintWriter());%0A        // Driver
 Manager.
 getL
@@ -2658,38 +2658,79 @@
 Manager.
-g
+s
 etLog
-ger(), Level.ALL
+Writer(new PrintWriter(new OutputStreamWriter(System.out)
 ));%0A    
@@ -2746,24 +2746,42 @@
 %0A           
+ Connection conn =
  this.newCon
@@ -2789,16 +2789,113 @@
 ection()
+;%0A            try %7B%0A                conn.rollback();%0A            %7D finally %7B%0A                conn
 .close()
@@ -2888,32 +2888,46 @@
   conn.close();%0A
+            %7D%0A
         %7D finall
@@ -2938,28 +2938,37 @@
             
-this
+DriverManager
 .setLogWrite

Add an @Ignore'd test.