package io.github.classgraph.test.utils;

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.Test;

import io.github.classgraph.ClassGraph;
import nonapi.io.github.classgraph.utils.LogNode;

public class LogNodeTest {

  @Test
  public void testLogNodeLogging() {

    // set the System.err
    final ByteArrayOutputStream err = new ByteArrayOutputStream();
    System.setErr(new PrintStream(err));

    Logger rootLogger = Logger.getLogger(""); 
    ConsoleHandler errPrintStreamHandler = new ConsoleHandler();
    errPrintStreamHandler.setLevel(Level.INFO);
    rootLogger.addHandler(errPrintStreamHandler);

    try {
      final LogNode node = new LogNode();
      node.log("any logging message").log("child message").log("sub child message");
      node.log("another root");
      node.flush();

      final Logger log = Logger.getLogger(ClassGraph.class.getName());
      if (log.isLoggable(Level.INFO)) {
        final String systemErrMessages = new String(err.toByteArray());
        assertTrue(systemErrMessages.contains("any logging message"));
        assertTrue(systemErrMessages.contains("-- child message"));
        assertTrue(systemErrMessages.contains("---- sub child message"));
        assertTrue(systemErrMessages.contains("another root"));
        // System.out.println(systemErrMessages);
      } // else logging will not take place

    } finally {
      rootLogger.removeHandler(errPrintStreamHandler);
    }
  }

}

@@ -439,14 +439,133 @@
 ging
-() %7B%0A%0A
+ToSystemErr() %7B%0A%0A    ConsoleHandler errPrintStreamHandler = null;%0A    Logger rootLogger = Logger.getLogger(%22%22);%0A    try %7B%0A%0A  
     
@@ -586,16 +586,18 @@
 tem.err%0A
+  
     fina
@@ -659,16 +659,18 @@
 ();%0A    
+  
 System.s
@@ -707,69 +707,9 @@
     
-Logger rootLogger = Logger.getLogger(%22%22); %0A    ConsoleHandler
+ 
  err
@@ -747,24 +747,26 @@
 eHandler();%0A
+  
     errPrint
@@ -801,24 +801,26 @@
 .INFO);%0A    
+  
 rootLogger.a
@@ -854,26 +854,16 @@
 dler);%0A%0A
-    try %7B%0A
       fi
@@ -1660,16 +1660,78 @@
 ndler);%0A
+      // reset to System.err%0A      System.setErr(System.err);%0A
     %7D%0A  

reset to System.err after test