/*
 * 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.logging.log4j.core.appender;

import java.io.IOException;
import java.net.SocketException;

import org.apache.logging.log4j.core.net.Facility;
import org.apache.logging.log4j.core.net.mock.MockSyslogServerFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 *
 */
public class SyslogAppenderTest extends SyslogAppenderTestBase {

    public SyslogAppenderTest() {
        root = ctx.getLogger("SyslogAppenderTest");
    }

    @Before
    public void setUp() {
        sentMessages.clear();
    }

    @After
    public void teardown() {
        removeAppenders();
        if (syslogServer != null) {
            syslogServer.shutdown();
        }
    }

    @Test
    public void testTCPAppender() throws Exception {
        initTCPTestEnvironment(null);

        sendAndCheckLegacyBSDMessage("This is a test message");
        sendAndCheckLegacyBSDMessage("This is a test message 2");
    }

    @Test
    public void testDefaultAppender() throws Exception {
        initTCPTestEnvironment(null);

        sendAndCheckLegacyBSDMessage("This is a test message");
        sendAndCheckLegacyBSDMessage("This is a test message 2");
    }

    @Test
    public void testTCPStructuredAppender() throws Exception {
        initTCPTestEnvironment("RFC5424");

        sendAndCheckStructuredMessage();
    }

    @Test
    public void testUDPAppender() throws Exception {
        initUDPTestEnvironment("bsd");

        sendAndCheckLegacyBSDMessage("This is a test message");
        root.removeAppender(appender);
        appender.stop();
    }

    @Test
    public void testUDPStructuredAppender() throws Exception {
        initUDPTestEnvironment("RFC5424");

        sendAndCheckStructuredMessage();
        root.removeAppender(appender);
        appender.stop();
    }

    protected void initUDPTestEnvironment(final String messageFormat) throws SocketException {
        syslogServer = MockSyslogServerFactory.createUDPSyslogServer(1, PORTNUM);
        syslogServer.start();
        initAppender("udp", messageFormat);
    }

    protected void initTCPTestEnvironment(final String messageFormat) throws IOException {
        syslogServer = MockSyslogServerFactory.createTCPSyslogServer(1, PORTNUM);
        syslogServer.start();
        initAppender("tcp", messageFormat);
    }

    protected void initAppender(final String transportFormat, final String messageFormat) {
        appender = createAppender(transportFormat, messageFormat);
        appender.start();
        initRootLogger(appender);
    }

    private SyslogAppender createAppender(final String protocol, final String format) {
        return SyslogAppender.createAppender("localhost", PORTNUM, protocol, null, 0, -1, true, "Test", true,
            false, Facility.LOCAL0, "Audit", 18060, true, "RequestContext", null, null, includeNewLine, null,
            "TestApp", "Test", null, "ipAddress,loginId", null, format, null, null, null, null, null, false);
    }
}

@@ -946,24 +946,24 @@
 ore.net.
-Facility
+Protocol
 ;%0Aimport
@@ -1027,16 +1027,67 @@
 actory;%0A
+import org.apache.logging.log4j.util.EnglishEnums;%0A
 import o
@@ -3505,329 +3505,687 @@
     
-return SyslogAppender.createAppender(%22localhost%22, PORTNUM, protocol, null, 0, -1, true, %22Test%22, true,%0A            false, Facility.LOCAL0, %22Audit%22, 18060, true, %22RequestContext%22, null, null, includeNewLine, null,%0A            %22TestApp%22, %22Test%22, null, %22ipAddress,loginId%22, null, format, null, null, null, null, null, false);
+final boolean newLine = includeNewLine;%0A        // @formatter:off%0A        return SyslogAppender.newSyslogAppenderBuilder()%0A                .withPort(PORTNUM)%0A                .withProtocol(EnglishEnums.valueOf(Protocol.class, protocol))%0A                .withReconnectDelayMillis(-1)%0A                .withName(%22TestApp%22)%0A                .withIgnoreExceptions(false)%0A                .setId(%22Audit%22)%0A                .setEnterpriseNumber(18060)%0A                .setMdcId(%22RequestContext%22)%0A                .setNewLine(newLine)%0A                .setAppName(%22TestApp%22)%0A                .setMsgId(%22Test%22)%0A                .setFormat(format)%0A                .build();%0A        // @formatter:on
 %0A   

Replace use of deprecated method.