/*
 * 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.log4j.net;

import static org.apache.log4j.TestConstants.TEST_INPUT_PREFIX;
import static org.junit.Assert.assertEquals;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.xml.DOMConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SocketAppenderTest {

    /* JUnit's setUp and tearDown */

    @Before
    public void setUp() {
        DOMConfigurator.configure(TEST_INPUT_PREFIX + "xml/SocketAppenderTestConfig.xml");
        // DOMConfigurator.configure(TEST_INPUT_PREFIX +
        // "xml/socketAppenderForChainsaw.xml");

        logger = Logger.getLogger(SocketAppenderTest.class);
        secondary = (LastOnlyAppender) Logger.getLogger("org.apache.log4j.net.SocketAppenderTestDummy")
                .getAppender("lastOnly");
    }

    @After
    public void tearDown() {
    }

    /* Tests */

    @Test
    public void testFallbackErrorHandlerWhenStarting() {
        String msg = "testFallbackErrorHandlerWhenStarting";
        logger.debug(msg);

        // above debug log will fail and shoul be redirected to secondary appender
        assertEquals("SocketAppender with FallbackErrorHandler", msg, secondary.getLastMessage());
    }

    /* Fields */

    private static Logger logger;
    private static LastOnlyAppender secondary;

    /* Inner classes */

    /**
     * Inner-class For debugging purposes only Saves last LoggerEvent
     */
    static public class LastOnlyAppender extends AppenderSkeleton {
        protected void append(LoggingEvent event) {
            this.lastEvent = event;
        }

        public boolean requiresLayout() {
            return false;
        }

        public void close() {
            this.closed = true;
        }

        /**
         * @return last appended LoggingEvent's message
         */
        public String getLastMessage() {
            if (this.lastEvent != null)
                return this.lastEvent.getMessage().toString();
            else
                return "";
        }

        private LoggingEvent lastEvent;
    }

    ;

}