https://issues.apache.org/jira/browse/AMQ-3623 - Add DefaultTestAppender utility abstract class. patch applied with thanks

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1211533 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2011-12-07 16:56:50 +00:00
parent 2f72b71829
commit 8bab1c1fb2
3 changed files with 109 additions and 62 deletions

View File

@ -24,6 +24,7 @@ import junit.framework.TestCase;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.TransportConnection;
import org.apache.activemq.transport.TransportDisposedIOException;
import org.apache.activemq.util.DefaultTestAppender;
import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
@ -35,21 +36,9 @@ import org.apache.log4j.spi.LoggingEvent;
public class AMQ2902Test extends TestCase {
final AtomicBoolean gotExceptionInLog = new AtomicBoolean(Boolean.FALSE);
final AtomicBoolean failedToFindMDC = new AtomicBoolean(Boolean.FALSE);
Appender appender = new Appender() {
public void addFilter(Filter newFilter) {
}
public Filter getFilter() {
return null;
}
public void clearFilters() {
}
public void close() {
}
Appender appender = new DefaultTestAppender() {
@Override
public void doAppend(LoggingEvent event) {
if (event.getThrowableInformation() != null
&& event.getThrowableInformation().getThrowable() instanceof TransportDisposedIOException) {
@ -60,31 +49,6 @@ public class AMQ2902Test extends TestCase {
}
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 {

View File

@ -22,6 +22,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.jms.Connection;
import javax.jms.Destination;
@ -35,10 +36,12 @@ import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.util.DefaultTestAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
@ -74,12 +77,24 @@ public class AMQ3567Test {
@Test
public void runTest() throws Exception {
produceSingleMessage();
File file = File.createTempFile("whatever", null);
FileOutputStream fos = new FileOutputStream(file);
org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger("org.apache.activemq.util.ServiceSupport");
Layout layout = new PatternLayout("%d | %-5p | %c - %m%n");
WriterAppender appender = new WriterAppender(layout, fos);
log4jLogger.addAppender(appender);
final AtomicBoolean failed = new AtomicBoolean(false);
log4jLogger.addAppender(new DefaultTestAppender() {
@Override
public void doAppend(LoggingEvent event) {
if (event.getThrowableInformation() != null) {
if (event.getThrowableInformation().getThrowable() instanceof InterruptedException) {
InterruptedException ie = (InterruptedException)event.getThrowableInformation().getThrowable();
if (ie.getMessage().startsWith("Could not stop service:")) {
logger.info("Received an interrupted exception : ", ie);
failed.set(true);
}
}
}
}
});
Level level = log4jLogger.getLevel();
log4jLogger.setLevel(Level.DEBUG);
@ -88,27 +103,12 @@ public class AMQ3567Test {
try {
stopConsumer();
stopBroker();
log4jLogger.removeAppender(appender);
fos.close();
read = new BufferedReader(new FileReader(file));
String line;
while ((line = read.readLine()) != null) {
if (line.indexOf("InterruptedException") > -1) {
if (line.indexOf("Could not stop service:") > -1) {
logger.info("Received an interrupted exception {}", line);
fail("An Interrupt exception was generated");
}
}
if (failed.get()) {
fail("An Interrupt exception was generated");
}
} finally {
log4jLogger.setLevel(level);
if (read != null)
read.close();
file.deleteOnExit();
}
}

View File

@ -0,0 +1,83 @@
/**
* 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.util;
import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.spi.Filter;
public abstract class DefaultTestAppender implements Appender {
String name = this.getClass().getSimpleName();
@Override
public void addFilter(Filter newFilter) {
}
@Override
public Filter getFilter() {
return null;
}
@Override
public void clearFilters() {
}
@Override
public void close() {
}
@Override
public String getName() {
return name;
}
@Override
public void setErrorHandler(ErrorHandler errorHandler) {
}
@Override
public ErrorHandler getErrorHandler() {
return null;
}
@Override
public void setLayout(Layout layout) {
}
@Override
public Layout getLayout() {
return null;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public boolean requiresLayout() {
return false;
}
}