Comprehensive test suite and testing utilities for Apache Log4j core logging implementation
npx @tessl/cli install tessl/maven-org-apache-logging-log4j--log4j-core-test@2.25.0Apache Log4j Core Test Library is a comprehensive testing infrastructure for Apache Log4j core implementation. It provides essential test utilities, mock servers, JUnit integration, custom appenders, and testing helpers that enable thorough testing of Log4j-based applications and extensions.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core-test</artifactId>
<version>2.25.1</version>
<scope>test</scope>
</dependency>// Test appenders
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.appender.BlockingAppender;
// JUnit 4 integration
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
// JUnit 5 integration
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.Named;
// Utility classes
import org.apache.logging.log4j.core.test.AvailablePortFinder;
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
// Common Log4j classes used with test library
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogEvent;import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogEvent;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.junit.Rule;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class MyLoggerTest {
@Rule
public LoggerContextRule context = new LoggerContextRule("log4j2-test.xml");
@Test
public void testLogging() {
// Get the ListAppender configured in log4j2-test.xml
ListAppender listAppender = context.getListAppender("TestAppender");
// Perform logging operations
Logger logger = context.getLogger();
logger.info("Test message");
// Verify logged events
List<LogEvent> events = listAppender.getEvents();
assertEquals(1, events.size());
assertEquals("Test message", events.get(0).getMessage().getFormattedMessage());
}
}The Log4j Core Test Library is organized around several key components:
Core test appenders for capturing and analyzing log events, including the primary ListAppender for event collection and specialized appenders for testing failure scenarios and threading behavior.
public class ListAppender extends AbstractAppender {
public List<LogEvent> getEvents();
public List<String> getMessages();
public List<byte[]> getData();
public ListAppender clear();
public static ListAppender getListAppender(String name);
}
public class BlockingAppender extends AbstractAppender {
public static BlockingAppender createAppender(String name);
public volatile boolean running;
}Comprehensive JUnit 4 and JUnit 5 integration providing TestRules and Extensions for LoggerContext management, configuration loading, and test lifecycle integration.
public class LoggerContextRule implements TestRule, LoggerContextAccessor {
public <T extends Appender> T getAppender(String name);
public <T extends Appender> T getRequiredAppender(String name, Class<T> cls);
public ListAppender getListAppender(String name);
public Logger getLogger();
public Configuration getConfiguration();
public LoggerContext getLoggerContext();
public void reconfigure();
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LoggerContextSource {
String value();
ReconfigurationPolicy reconfigure() default ReconfigurationPolicy.NEVER;
long timeout() default 0;
TimeUnit unit() default TimeUnit.SECONDS;
}Mock SMTP and Syslog servers for testing network appenders, supporting TCP, UDP, and TLS protocols with message capture and verification capabilities.
public class SimpleSmtpServer implements Runnable {
public static SimpleSmtpServer start();
public static SimpleSmtpServer start(int port);
public synchronized boolean isStopped();
public synchronized void stop();
public synchronized Iterator<SmtpMessage> getReceivedEmail();
public synchronized int getReceivedEmailSize();
}
public abstract class MockSyslogServer extends Thread {
public abstract int getLocalPort();
public void shutdown();
public int getNumberOfReceivedMessages();
public List<String> getMessageList();
}Essential utilities for test infrastructure including port management, system property handling, logger context lifecycle management, and compilation helpers.
public final class AvailablePortFinder {
public static int getNextAvailable();
public static int getNextAvailable(int fromPort);
public static boolean available(int port);
public static final int MIN_PORT_NUMBER = 1100;
public static final int MAX_PORT_NUMBER = 65535;
}
public class SystemPropertyTestRule implements TestRule {
public static SystemPropertyTestRule create(String name, String value);
public Statement apply(Statement base, Description description);
public String getName();
public String getValue();
}
public class CoreLoggerContexts {
public static void stopLoggerContext();
public static void stopLoggerContext(boolean currentContext);
public static void stopLoggerContext(boolean currentContext, File checkFilePresence);
}Custom Hamcrest matchers for file-based assertions and Map testing, providing fluent assertion APIs for common testing scenarios.
public final class FileMatchers {
public static Matcher<File> exists();
public static Matcher<File> hasLength(Matcher<Long> lengthMatcher);
public static Matcher<File> isEmpty();
public static Matcher<File> lastModified(Matcher<Long> lastModifiedMatcher);
public static Matcher<File> hasFiles();
public static Matcher<File> hasName(Matcher<String> nameMatcher);
}JUnit test category interfaces for organizing and running specific types of tests, including performance tests, async logger tests, and component-specific test groups.
public interface AsyncLoggers {}
public interface PerformanceTests {}
public interface Appenders {}
public interface Configurations {}
public interface Layouts {}
public interface Scripts {}public enum ReconfigurationPolicy {
NEVER, BEFORE_EACH, AFTER_EACH
}
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Named {
String value();
}
public class TestMarkers {
public static final Marker LIFE_CYCLE;
public static final Marker TEST;
public static final Marker TEST_RULE;
public static final Marker TEST_RULE_LIFE_CYCLE;
}