/**
 * 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.
 */

package org.apache.jena.jdbc.mem;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;


import org.apache.jena.jdbc.AbstractJenaDriverTests;
import org.apache.jena.jdbc.JenaDriver;
import org.apache.log4j.BasicConfigurator;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;

/**
 * Tests for the {@link MemDriver}
 *
 */
public class TestMemDriverWithLogging extends AbstractJenaDriverTests {

    private static ByteArrayOutputStream output;
    private static PrintStream orig;
    
    /**
     * Redirect stdout so as not to pollute the build output
     */
    @BeforeClass
    public static void setup() {
        // Redirect stdout
        orig = System.out;
        output = new ByteArrayOutputStream();
        System.setOut(new PrintStream(output));
    }
    
    /**
     * Resets logging configuration after these tests
     */
    @AfterClass
    public static void teardown() {
        BasicConfigurator.resetConfiguration();
        
        // Reset stdout
        System.setOut(orig);
        Assert.assertTrue(output.size() > 0);
    }

    @Override
    protected JenaDriver getDriver() {
        return new MemDriver();
    }

    @Override
    protected String getConnectionUrl() {
        return JenaDriver.DRIVER_PREFIX + MemDriver.MEM_DRIVER_PREFIX + MemDriver.PARAM_EMPTY + "=true&" + JenaDriver.PARAM_LOGGING + "=/jena-jdbc-log4j.properties";
    }

    @Override
    protected String getBadConnectionUrl() {
        return JenaDriver.DRIVER_PREFIX + MemDriver.MEM_DRIVER_PREFIX + MemDriver.PARAM_EMPTY + "=false";
    }
}

@@ -872,16 +872,17 @@
 utStream
+ 
 ;%0Aimport
@@ -901,18 +901,40 @@
 ntStream
+ 
 ;%0A
+import java.net.URL ;
 %0A%0Aimport
@@ -978,16 +978,17 @@
 verTests
+ 
 ;%0Aimport
@@ -1019,16 +1019,17 @@
 naDriver
+ 
 ;%0Aimport
@@ -1046,21 +1046,61 @@
 e.log4j.
-Basic
+LogManager ;%0Aimport org.apache.log4j.Property
 Configur
@@ -1103,16 +1103,17 @@
 igurator
+ 
 ;%0Aimport
@@ -1121,24 +1121,36 @@
 org.
-junit.AfterClass
+apache.log4j.helpers.Loader 
 ;%0Aim
@@ -1165,21 +1165,26 @@
 .junit.A
-ssert
+fterClass 
 ;%0Aimport
@@ -1205,16 +1205,17 @@
 oreClass
+ 
 ;%0A%0A/**%0A 
@@ -1418,110 +1418,24 @@
 ig;%0A
-    
 %0A    
-/**%0A     * Redirect stdout so as not to pollute the build output%0A     */%0A    @Before
+@After
 Class
-%0A   
  pub
@@ -1454,312 +1454,223 @@
 oid 
-setup() %7B%0A        // Redirect stdout%0A        orig = System.out;%0A        output = new ByteArrayOutputStream();%0A        System.setOut(new PrintStream(output));%0A    %7D%0A    %0A    /**%0A     * Resets logging configuration after these tests%0A     */%0A    @AfterClass%0A    public static void teardown() %7B%0A        Basic
+afterClassResetLogging() %7B%0A        // Unfortunately, this code %22knows%22 how log4j was set up in testing%0A        // Thsi is fragile.%0A        URL configURL = Loader.getResource(%22log4j.properties%22);%0A        new Property
 Conf
@@ -1681,22 +1681,21 @@
 ator
-.reset
+().do
 Configur
 atio
@@ -1694,16 +1694,20 @@
 igur
-ation();
+e(configURL,
 %0A   
@@ -1711,25 +1711,24 @@
         
-%0A
         
 // Reset
@@ -1723,96 +1723,71 @@
     
-// Reset stdout%0A        System.setOut(orig);%0A        Assert.assertTrue(output.size() %3E 0
+                               LogManager.getLoggerRepository()
 );%0A 
@@ -1783,32 +1783,36 @@
 itory());%0A    %7D%0A
+    
 %0A    @Override%0A 

Reset logging after tests.