/*
 * Copyright (c) 2011-Present VMware, Inc. or its affiliates, All Rights Reserved.
 *
 * 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
 *
 *       https://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 reactor.netty.transport.logging;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import java.nio.charset.Charset;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.DefaultByteBufHolder;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class ReactorNettyLoggingHandlerTest {

	private LoggingHandler defaultCharsetReactorNettyLoggingHandler;

	private Appender<ILoggingEvent> mockedAppender;
	private ArgumentCaptor<LoggingEvent> loggingEventArgumentCaptor;

	@BeforeEach
	@SuppressWarnings("unchecked")
	public void setUp() {
		defaultCharsetReactorNettyLoggingHandler =
			new ReactorNettyLoggingHandler(
				ReactorNettyLoggingHandlerTest.class.getName(),
				LogLevel.DEBUG,
				Charset.defaultCharset());

		mockedAppender = (Appender<ILoggingEvent>) Mockito.mock(Appender.class);
		loggingEventArgumentCaptor = ArgumentCaptor.forClass(LoggingEvent.class);
		Mockito.when(mockedAppender.getName()).thenReturn("MOCK");
		Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
		root.addAppender(mockedAppender);
	}

	@Test
	public void shouldLogByteBuf() {
		final ByteBuf byteBuf = Unpooled.copiedBuffer("TEST", Charset.defaultCharset());

		sendMessage(byteBuf, "[id: 0xembedded, L:embedded - R:embedded] READ: 4B TEST");
	}

	@Test
	public void shouldLogByteBufHolder() {
		final ByteBufHolder byteBufHolder =
			new DefaultByteBufHolder(Unpooled.copiedBuffer("TEST", Charset.defaultCharset()));

		sendMessage(byteBufHolder, "[id: 0xembedded, L:embedded - R:embedded] READ: 4B TEST");
	}

	@Test
	public void shouldLogObject() {
		sendMessage("TEST", "[id: 0xembedded, L:embedded - R:embedded] READ: TEST");
	}

	private void sendMessage(Object input, String expectedResult) {
		final EmbeddedChannel channel = new EmbeddedChannel(defaultCharsetReactorNettyLoggingHandler);
		channel.writeInbound(input);

		Mockito.verify(mockedAppender, Mockito.times(4)).doAppend(loggingEventArgumentCaptor.capture());

		final LoggingEvent relevantLog = loggingEventArgumentCaptor.getAllValues().get(2);
		assertThat(relevantLog.getMessage()).isEqualTo(expectedResult);
	}

	@Test
	public void shouldThrowUnsupportedOperationExceptionWhenByteBufFormatIsCalled() {
		assertThatExceptionOfType(UnsupportedOperationException.class)
				.isThrownBy(() -> defaultCharsetReactorNettyLoggingHandler.byteBufFormat());
	}

}
@@ -852,16 +852,56 @@
 arset;%0A%0A
+import org.junit.jupiter.api.AfterEach;%0A
 import o
@@ -1758,76 +1758,233 @@
 or;%0A
-%0A%09@BeforeEach%0A%09@SuppressWarnings(%22unchecked%22)%0A%09public void setUp() %7B
+%09private Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);%0A%0A%09@BeforeEach%0A%09@SuppressWarnings(%22unchecked%22)%0A%09public void setUp() %7B%0A%09%09mockedAppender = (Appender%3CILoggingEvent%3E) Mockito.mock(Appender.class);
 %0A%09%09d
@@ -2168,83 +2168,8 @@
 );%0A%0A
-%09%09mockedAppender = (Appender%3CILoggingEvent%3E) Mockito.mock(Appender.class);%0A
 %09%09lo
@@ -2307,88 +2307,90 @@
 ;%0A%09%09
-Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
+root.addAppender(mockedAppender);%0A%09%7D%0A%0A%09@AfterEach%0A%09public void tearDown()%7B
 %0A%09%09root.
 addA
@@ -2377,35 +2377,38 @@
 rDown()%7B%0A%09%09root.
-add
+detach
 Appender(mockedA

Fix #1422 Remove mocked appender from root logger after each test in ReactorNettyLoggingHandlerTest