// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;

import com.yahoo.jdisc.test.TestDriver;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogEntry;
import org.osgi.service.log.LogReaderService;

import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;


/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
 */
public class OsgiLogServiceIntegrationTest {

    @Test
    @SuppressWarnings("unchecked")
    public void requireThatAllSupportedLogFrameworksAreConfigured() throws Exception {
        // need to explicitly set log level of root logger since integration suite now provides a logger config file,
        // which disables that setLevel() call of the OsgiLogManager.
        Logger.getLogger("").setLevel(Level.INFO);

        long now = System.currentTimeMillis();
        TestDriver driver = TestDriver.newApplicationBundleInstance("app-h-log.jar", false);
        BundleContext ctx = driver.osgiFramework().bundleContext();
        ServiceReference<?> ref = ctx.getServiceReference(LogReaderService.class.getName());
        LogReaderService reader = (LogReaderService)ctx.getService(ref);
        Enumeration<LogEntry> log = (Enumeration<LogEntry>)reader.getLog();

        assertEntry(Level.INFO, "[jdk14] hello world", null, now, log);
        assertEntry(Level.INFO, "[slf4j] hello world", null, now, log);
        assertEntry(Level.INFO, "[log4j] hello world", null, now, log);
        assertEntry(Level.INFO, "[jcl] hello world", null, now, log);

        assertTrue(driver.close());
    }

    private static void assertEntry(Level expectedLevel, String expectedMessage, Throwable expectedException,
                                    long expectedTimeGE, Enumeration<LogEntry> log)
    {
        assertTrue(log.hasMoreElements());
        LogEntry entry = log.nextElement();
        assertNotNull(entry);
        System.err.println("log entry: "+entry.getMessage()+" bundle="+entry.getBundle());
        assertEquals(expectedMessage, entry.getMessage());
        assertNull(entry.getBundle());
        assertNotNull(entry.getServiceReference());
        assertEquals(OsgiLogHandler.toServiceLevel(expectedLevel), entry.getLevel());
        assertEquals(expectedException, entry.getException());
        assertTrue(expectedTimeGE <= entry.getTime());
    }
}

@@ -393,27 +393,78 @@
 va.util.
-Enumeration
+ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List
 ;%0Aimport
@@ -778,16 +778,35 @@
 ult%3C/a%3E%0A
+ * @author bjorncs%0A
  */%0Apubl
@@ -1601,27 +1601,25 @@
         
-Enumeration
+ArrayList
 %3CLogEntr
@@ -1628,84 +1628,129 @@
  log
- = (Enumeration%3CLogEntry%3E)reader.getLog();%0A%0A        assertEntry(Level.INFO, 
+Entries = Collections.list(reader.getLog());%0A        assertTrue(logEntries.size() %3E= 4);%0A%0A        assertLogContainsEntry(
 %22%5Bjd
@@ -1764,38 +1764,39 @@
 llo world%22, 
-null, now, log
+logEntries, now
 );%0A        a
@@ -1800,34 +1800,33 @@
   assert
-Entry(Level.INFO, 
+LogContainsEntry(
 %22%5Bslf4j%5D
@@ -1836,38 +1836,39 @@
 llo world%22, 
-null, now, log
+logEntries, now
 );%0A        a
@@ -1872,34 +1872,33 @@
   assert
-Entry(Level.INFO, 
+LogContainsEntry(
 %22%5Blog4j%5D
@@ -1912,30 +1912,31 @@
 world%22, 
-null, now, log
+logEntries, now
 );%0A     
@@ -1948,26 +1948,25 @@
 sert
-Entry(Level.INFO, 
+LogContainsEntry(
 %22%5Bjc
@@ -1986,22 +1986,23 @@
 d%22, 
-null, now, log
+logEntries, now
 );%0A%0A
@@ -2078,445 +2078,331 @@
 sert
-Entry(Level expectedLevel, String expectedMessage, Throwable expectedException,%0A                                    long expectedTimeGE, Enumeration%3CLogEntry%3E log)%0A    %7B%0A        assertTrue(log.hasMoreElements());%0A        LogEntry entry = log.nextElement();%0A        assertNotNull(entry);%0A        System.err.println(%22log entry: %22+entry.getMessage()+%22 bundle=%22+entry.getBundle());%0A        assertEquals(expectedMessage, entry.get
+LogContainsEntry(String expectedMessage, List%3CLogEntry%3E logEntries, long expectedTimeGE)%0A    %7B%0A        LogEntry entry = logEntries.stream().filter(e -%3E e.getMessage().equals(expectedMessage)).findFirst()%0A                .orElseThrow(() -%3E new AssertionError(%22Could not find log entry with messsage: %22 + expected
 Message
-(
 ));%0A
+%0A
     
@@ -2543,21 +2543,18 @@
 vel(
-expected
 Level
+.INFO
 ), e
@@ -2589,34 +2589,13 @@
 sert
-Equals(expectedException, 
+Null(
 entr

Rewrite unstable unit test