org.slf4j.plus.ExceptionLoggerFactoryTests.java Source code

Java tutorial

Introduction

Here is the source code for org.slf4j.plus.ExceptionLoggerFactoryTests.java

Source

/*
 * Copyright 2015-2017 the original author or authors.
 *
 * Licensed 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.slf4j.plus;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.lang.reflect.Field;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.CustomLoggerFactory;
import org.slf4j.Logger;
import org.springframework.util.ReflectionUtils;

/**
 * {@link ExceptionLoggerFactory} tests
 */
public class ExceptionLoggerFactoryTests {

    /**
     * {@link Before}
     */
    @Before
    public void before() {

        ExceptionLoggerFactory.setLoggerClass(ExceptionLogger.class);
    }

    /**
     * {@link ExceptionLoggerFactory#getLogger()}
     */
    @Test
    public void getLoggerByAuto() {

        assertThat(ExceptionLoggerFactory.getLogger().getName(), equalTo(this.getClass().getName()));
        assertThat(ExceptionLoggerFactory.getLogger(5).getName(), equalTo(this.getClass().getName()));
    }

    /**
     * {@link ExceptionLoggerFactory#getLogger(Class)}
     */
    @Test
    public void getLoggerByClass() {

        assertThat(ExceptionLoggerFactory.getLogger(Integer.class).getName(), equalTo(Integer.class.getName()));
        assertThat(ExceptionLoggerFactory.getLogger(Integer.class, 5).getName(), equalTo(Integer.class.getName()));
    }

    /**
     * {@link ExceptionLoggerFactory#getLogger(String)}
     */
    @Test
    public void getLoggerByName() {

        assertThat(ExceptionLoggerFactory.getLogger(Long.class).getName(), equalTo(Long.class.getName()));
        assertThat(ExceptionLoggerFactory.getLogger(Long.class, 5).getName(), equalTo(Long.class.getName()));
    }

    /**
     * {@link ExceptionLoggerFactory#getLogger(Logger, int)}
     */
    @Test
    public void getLogger() {

        Logger delegate = CustomLoggerFactory.getLogger();

        {
            ExceptionLogger logger = ExceptionLoggerFactory.getLogger(delegate, 1);

            assertThat(logger.getClass(), equalTo(ExceptionLogger.class));
            assertThat(logger.getName(), equalTo(delegate.getName()));
            assertThat(this.getMaxFrameLength(logger), equalTo(1));
        }

        ExceptionLoggerFactory.setLoggerClass(CustomExceptionLogger.class);

        {
            ExceptionLogger logger = ExceptionLoggerFactory.getLogger(delegate, 2);

            assertThat(logger.getClass(), equalTo(CustomExceptionLogger.class));
            assertThat(logger.getName(), equalTo(delegate.getName()));
            assertThat(this.getMaxFrameLength(logger), equalTo(2));
        }
    }

    /**
     * Get max frame length
     * 
     * @param logger {@link ExceptionLogger}
     * @return max frame length
     */
    private int getMaxFrameLength(ExceptionLogger logger) {

        Field field = ReflectionUtils.findField(ExceptionLogger.class, "maxFrameLength");
        ReflectionUtils.makeAccessible(field);

        return (int) ReflectionUtils.getField(field, logger);
    }

    /**
     * Custom {@link ExceptionLogger}
     */
    private static class CustomExceptionLogger extends ExceptionLogger {

        /**
         * Constructor
         * 
         * @param delegate {@link Logger}
         * @param maxFrameLength max frame length
         */
        protected CustomExceptionLogger(Logger delegate, int maxFrameLength) {

            super(delegate, maxFrameLength);
        }
    }
}