package org.xbib.logging.log4j.test;

import org.apache.logging.log4j.Level;
import org.junit.jupiter.api.Test;
import org.xbib.logging.log4j.LevelTranslator;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class LevelTranslatorTest {

    private final LevelTranslator levelTranslator = LevelTranslator.getInstance();

    @Test
    public void testOff() {
        testLevel(Level.OFF, java.util.logging.Level.OFF);
    }

    @Test
    public void testFatal() {
        testLevel(Level.FATAL, org.xbib.logging.Level.FATAL);
    }

    @Test
    public void testError() {
        testLevel(Level.ERROR, org.xbib.logging.Level.ERROR);
        testLevel(Level.ERROR, java.util.logging.Level.SEVERE, org.xbib.logging.Level.ERROR);
    }

    @Test
    public void testWarn() {
        testLevel(Level.WARN, org.xbib.logging.Level.WARN);
        testLevel(Level.WARN, java.util.logging.Level.WARNING, org.xbib.logging.Level.WARN);
    }

    @Test
    public void testInfo() {
        testLevel(Level.INFO, org.xbib.logging.Level.INFO);
        testLevel(Level.INFO, java.util.logging.Level.INFO);
    }

    @Test
    public void testDebug() {
        testLevel(Level.DEBUG, org.xbib.logging.Level.DEBUG);
        testLevel(Level.DEBUG, java.util.logging.Level.FINE, org.xbib.logging.Level.DEBUG);
        testLevel(Level.DEBUG, java.util.logging.Level.CONFIG, org.xbib.logging.Level.DEBUG);
    }

    @Test
    public void testTrace() {
        testLevel(Level.TRACE, org.xbib.logging.Level.TRACE);
        testLevel(Level.TRACE, java.util.logging.Level.FINER, org.xbib.logging.Level.TRACE);
        testLevel(Level.TRACE, java.util.logging.Level.FINEST, org.xbib.logging.Level.TRACE);
    }

    @Test
    public void testAll() {
        testLevel(Level.ALL, java.util.logging.Level.ALL);
    }

    @Test
    public void testNull() {
        assertEquals(org.xbib.logging.Level.DEBUG, levelTranslator.translateLevel((Level) null),
                "Expected null log4j level to map to INFO");
        assertEquals(Level.DEBUG, levelTranslator.translateLevel((java.util.logging.Level) null),
                "Expected null JUL level to map to INFO");
        assertEquals(Level.DEBUG, levelTranslator.translateLevel(-1), "" +
                "Expected a -1 effective level to map to INFO");
    }

    private void testLevel(final Level log4jLevel, final java.util.logging.Level julLevel) {
        testLevel(log4jLevel, julLevel, julLevel);
    }

    private void testLevel(final Level log4jLevel, final java.util.logging.Level julLevel,
            final java.util.logging.Level expectedJulLevel) {
        assertEquals(log4jLevel, levelTranslator.translateLevel(julLevel),
                String.format("Expected log4j level %s to equal JUL level %s", log4jLevel, julLevel));
        assertEquals(expectedJulLevel, levelTranslator.translateLevel(log4jLevel),
                String.format("Expected JUL level %s to equal log4j level %s", julLevel, log4jLevel));
    }
}
