/**
 * 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.activemq.bugs;

import java.util.concurrent.atomic.AtomicBoolean;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import junit.framework.TestCase;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.TransportConnection;
import org.apache.activemq.transport.TransportDisposedIOException;
import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

public class AMQ2902Test extends TestCase {
    final AtomicBoolean gotExceptionInLog = new AtomicBoolean(Boolean.FALSE);

    Appender appender = new Appender() {
        public void addFilter(Filter newFilter) {
        }

        public Filter getFilter() {
            return null;
        }

        public void clearFilters() {
        }

        public void close() {
        }

        public void doAppend(LoggingEvent event) {
            if (event.getThrowableInformation() != null
                    && event.getThrowableInformation().getThrowable() instanceof TransportDisposedIOException) {
                gotExceptionInLog.set(Boolean.TRUE);
            }
            return;
        }

        public String getName() {
            return "AMQ2902TestAppender";
        }

        public void setErrorHandler(ErrorHandler errorHandler) {
        }

        public ErrorHandler getErrorHandler() {
            return null;
        }

        public void setLayout(Layout layout) {
        }

        public Layout getLayout() {
            return null;
        }

        public void setName(String name) {
        }

        public boolean requiresLayout() {
            return false;
        }
    };

    public void testNoExceptionOnClosewithStartStop() throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                "vm://localhost?broker.persistent=false");
        Connection connection = connectionFactory.createConnection();
        connection.start();
        connection.stop();
        connection.close();
    }

    public void testNoExceptionOnClose() throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                "vm://localhost?broker.persistent=false");
        Connection connection = connectionFactory.createConnection();
        connection.close();
    }

    public void setUp() throws Exception {
        gotExceptionInLog.set(Boolean.FALSE);
        Logger.getRootLogger().addAppender(appender);
        Logger.getLogger(TransportConnection.class.getName() + ".Transport").setLevel(Level.DEBUG);
    }

    public void tearDown() throws Exception {
        Logger.getRootLogger().removeAppender(appender);
        assertFalse("got unexpected ex in log on graceful close", gotExceptionInLog.get());
    }
}

@@ -1558,16 +1558,92 @@
 .FALSE);
+%0A    final AtomicBoolean failedToFindMDC = new AtomicBoolean(Boolean.FALSE);
 %0A%0A    Ap
@@ -2182,24 +2182,148 @@
           %7D%0A
+            if (event.getMDC(%22activemq.broker%22) == null) %7B%0A                failedToFindMDC.set(Boolean.TRUE);%0A            %7D%0A
             
@@ -3619,24 +3619,68 @@
 ean.FALSE);%0A
+        failedToFindMDC.set(Boolean.FALSE);%0A
         Logg
@@ -3713,32 +3713,32 @@
 nder(appender);%0A
-
         Logger.g
@@ -3821,16 +3821,101 @@
 DEBUG);%0A
+        Logger.getLogger(TransportConnection.class.getName()).setLevel(Level.DEBUG);%0A
     %7D%0A%0A 
@@ -4008,32 +4008,32 @@
 nder(appender);%0A
-
         assertFa
@@ -4108,16 +4108,76 @@
 get());%0A
+        assertFalse(%22MDC is there%22, failedToFindMDC.get());%0A
     %7D%0A%7D%0A

add quick test variant to validate presence of mdc between threads