org.springframework.boot.logging.java.JavaLoggerSystemTests.java Source code

Java tutorial

Introduction

Here is the source code for org.springframework.boot.logging.java.JavaLoggerSystemTests.java

Source

/*
 * Copyright 2012-2013 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.springframework.boot.logging.java;

import java.io.IOException;

import org.apache.commons.logging.impl.Jdk14Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.logging.LogLevel;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * Tests for {@link JavaLoggingSystem}.
 * 
 * @author Dave Syer
 */
public class JavaLoggerSystemTests {

    private JavaLoggingSystem loggingSystem = new JavaLoggingSystem(getClass().getClassLoader());

    @Rule
    public OutputCapture output = new OutputCapture();

    private Jdk14Logger logger;

    @Before
    public void init() throws SecurityException, IOException {
        this.logger = new Jdk14Logger(getClass().getName());
    }

    @After
    public void clear() {
        System.clearProperty("LOG_FILE");
        System.clearProperty("LOG_PATH");
        System.clearProperty("PID");
    }

    @Test
    public void testCustomFormatter() throws Exception {
        this.loggingSystem.initialize();
        this.logger.info("Hello world");
        String output = this.output.toString().trim();
        assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
        assertTrue("Wrong output:\n" + output, output.contains("???? INFO ["));
    }

    @Test
    public void testSystemPropertyInitializesFormat() throws Exception {
        System.setProperty("PID", "1234");
        this.loggingSystem.initialize(
                "classpath:" + ClassUtils.addResourcePathToPackagePath(getClass(), "logging.properties"));
        this.logger.info("Hello world");
        this.logger.info("Hello world");
        String output = this.output.toString().trim();
        assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
        assertTrue("Wrong output:\n" + output, output.contains("1234 INFO ["));
    }

    @Test
    public void testNonDefaultConfigLocation() throws Exception {
        this.loggingSystem.initialize("classpath:logging-nondefault.properties");
        this.logger.info("Hello world");
        String output = this.output.toString().trim();
        assertTrue("Wrong output:\n" + output, output.contains("INFO: Hello"));
    }

    @Test(expected = IllegalStateException.class)
    public void testNonexistentConfigLocation() throws Exception {
        this.loggingSystem.initialize("classpath:logging-nonexistent.properties");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNullConfigLocation() throws Exception {
        this.loggingSystem.initialize(null);
    }

    @Test
    public void setLevel() throws Exception {
        this.loggingSystem.initialize();
        this.logger.debug("Hello");
        this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
        this.logger.debug("Hello");
        assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello"), equalTo(1));
    }

}