org.apache.tomee.util.SimpleTomEEFormatterTest.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.tomee.util.SimpleTomEEFormatterTest.java

Source

/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.tomee.util;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.junit.Test;

import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import static org.junit.Assert.assertEquals;

public class SimpleTomEEFormatterTest {
    private static final String LINE_SEPARATOR_KEY = "line.separator";

    @Test
    public void formatNullThrown() throws Exception {
        final String previousLineSeparatorProperty = System.getProperty(LINE_SEPARATOR_KEY);
        try {
            final String lineSeparatorValue = "\n";
            final String logMessage = "An example log record";
            final Level level = Level.FINEST;

            System.setProperty(LINE_SEPARATOR_KEY, lineSeparatorValue);
            final LogRecord logRecordInput = new LogRecord(level, logMessage);
            logRecordInput.setThrown(null);

            final Formatter formatter = new SimpleTomEEFormatter();
            final String actualFormatOutput = formatter.format(logRecordInput);

            final StringBuilder expectedFormatOutputSb = new StringBuilder(level.getLocalizedName());
            expectedFormatOutputSb.append(" - ").append(logMessage).append("\n");
            final String expectedFormatOutput = expectedFormatOutputSb.toString();

            assertEquals(expectedFormatOutput, actualFormatOutput);
        } finally {
            System.setProperty(LINE_SEPARATOR_KEY, previousLineSeparatorProperty);
        }
    }

    @Test
    public void formatNotNullThrown() throws Exception {
        final String previousLineSeparatorProperty = System.getProperty(LINE_SEPARATOR_KEY);

        try {
            final String lineSeparatorValue = "\n";
            final String logMessage = "An example log record";
            final Level level = Level.CONFIG;
            final String exceptionMessage = "An example exception";
            final Throwable thrown = new Exception(exceptionMessage);

            System.setProperty(LINE_SEPARATOR_KEY, lineSeparatorValue);
            final LogRecord logRecordInput = new LogRecord(level, logMessage);
            logRecordInput.setThrown(thrown);

            final Formatter formatter = new SimpleTomEEFormatter();
            final String actualFormatOutput = formatter.format(logRecordInput);

            final StringBuilder expectedFormatOutputSb = new StringBuilder(level.getLocalizedName());
            expectedFormatOutputSb.append(" - ").append(logMessage).append(lineSeparatorValue);
            expectedFormatOutputSb.append(ExceptionUtils.getStackTrace(thrown));

            final String expectedFormatOutput = expectedFormatOutputSb.toString();

            assertEquals(expectedFormatOutput, actualFormatOutput);
        } finally {
            System.setProperty(LINE_SEPARATOR_KEY, previousLineSeparatorProperty);
        }
    }

}