CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-logging-log4j--log4j-core-test

Comprehensive test suite and testing utilities for Apache Log4j core logging implementation

Pending
Overview
Eval results
Files

testing-utilities.mddocs/

Testing Utilities

Essential utilities for test infrastructure including port management, system property handling, logger context lifecycle management, compilation helpers, and performance testing tools.

Capabilities

Port Management

Utilities for finding available network ports for testing network components.

/**
 * Utility for finding available server ports for testing
 */
public final class AvailablePortFinder {
    
    /**
     * Minimum port number for IPv4
     */
    public static final int MIN_PORT_NUMBER = 1100;
    
    /**
     * Maximum port number for IPv4  
     */
    public static final int MAX_PORT_NUMBER = 65535;
    
    /**
     * Gets next available port starting at minimum port
     * @return Available port number
     */
    public static int getNextAvailable();
    
    /**
     * Gets next available port from specified starting port
     * @param fromPort Starting port to search from
     * @return Available port number
     */
    public static int getNextAvailable(int fromPort);
    
    /**
     * Checks if specific port is available
     * @param port Port number to check
     * @return true if port is available, false if in use
     */
    public static boolean available(int port);
}

/**
 * TestRule to discover available port and save in system property
 */
public class AvailablePortSystemPropertyTestRule extends SystemPropertyTestRule {
    
    /**
     * Creates rule that finds available port and sets system property
     * @param name System property name to set with port number
     * @return AvailablePortSystemPropertyTestRule instance
     */
    public static AvailablePortSystemPropertyTestRule create(String name);
}

Port Management Usage:

import org.apache.logging.log4j.core.test.AvailablePortFinder;

public class NetworkAppenderTest {
    
    @Test
    public void testNetworkAppender() {
        // Find available port for test server
        int port = AvailablePortFinder.getNextAvailable();
        
        // Start mock server on available port
        MockServer server = new MockServer(port);
        
        // Configure appender to use this port
        // Perform test...
    }
    
    @Test
    public void testSpecificPortRange() {
        // Find port starting from 8080
        int port = AvailablePortFinder.getNextAvailable(8080);
        assertTrue(port >= 8080);
        assertTrue(AvailablePortFinder.available(port));
    }
}

// Using with TestRule
public class PortRuleTest {
    
    @Rule
    public AvailablePortSystemPropertyTestRule portRule = 
        AvailablePortSystemPropertyTestRule.create("test.port");
    
    @Test
    public void testWithPortProperty() {
        String port = System.getProperty("test.port");
        assertNotNull(port);
        // Port is automatically available
    }
}

System Property Management

TestRules for managing system properties during test execution.

/**
 * JUnit TestRule to set and reset system properties during tests
 */
public class SystemPropertyTestRule implements TestRule {
    
    /**
     * Creates rule to set system property to specified value
     * @param name System property name
     * @param value System property value
     * @return SystemPropertyTestRule instance
     */
    public static SystemPropertyTestRule create(String name, String value);
    
    /**
     * Creates rule to set system property using value supplier
     * @param name System property name
     * @param valueSupplier Supplier providing property value
     * @return SystemPropertyTestRule instance
     */
    public static SystemPropertyTestRule create(String name, Supplier<String> valueSupplier);
    
    /**
     * Applies the rule to test execution
     * @param base The base statement
     * @param description Test description
     * @return Statement with property management
     */
    public Statement apply(Statement base, Description description);
    
    /**
     * Gets the property name managed by this rule
     * @return Property name
     */
    public String getName();
    
    /**
     * Gets the property value set by this rule
     * @return Property value
     */
    public String getValue();
    
    /**
     * Gets the value supplier for this rule
     * @return Value supplier
     */
    public Supplier<String> getValueSupplier();
}

System Property Usage:

import org.apache.logging.log4j.core.test.SystemPropertyTestRule;

public class SystemPropertyTest {
    
    @Rule
    public SystemPropertyTestRule logLevelRule = 
        SystemPropertyTestRule.create("log4j2.level", "DEBUG");
    
    @Rule  
    public SystemPropertyTestRule tempDirRule =
        SystemPropertyTestRule.create("test.temp.dir", () -> {
            return Files.createTempDirectory("log4j-test").toString();
        });
    
    @Test
    public void testWithSystemProperties() {
        assertEquals("DEBUG", System.getProperty("log4j2.level"));
        assertTrue(System.getProperty("test.temp.dir").startsWith("/tmp"));
        // Properties automatically restored after test
    }
}

Logger Context Management

Utilities for managing LoggerContext lifecycle during testing.

/**
 * Logger context lifecycle management utilities
 */
public class CoreLoggerContexts {
    
    /**
     * Stops the current LoggerContext
     */
    public static void stopLoggerContext();
    
    /**
     * Stops LoggerContext with option to use current context
     * @param currentContext Whether to stop current context or default
     */
    public static void stopLoggerContext(boolean currentContext);
    
    /**
     * Stops LoggerContext and verifies file presence
     * @param currentContext Whether to stop current context
     * @param checkFilePresence File to check for presence after stop
     */
    public static void stopLoggerContext(boolean currentContext, File checkFilePresence);
    
    /**
     * Stops LoggerContext with file presence check
     * @param checkFilePresence File to check for presence after stop
     */
    public static void stopLoggerContext(File checkFilePresence);
}

Compilation Utilities

Java source code compilation utilities for testing dynamic code scenarios.

/**
 * Java source code compilation utilities for testing
 */
public class Compiler {
    
    /**
     * Compiles single Java source file
     * @param source Source file to compile
     * @param compilerOptions Additional compiler options
     * @throws RuntimeException if compilation fails
     */
    public static void compile(File source, String... compilerOptions);
    
    /**
     * Compiles multiple Java source files
     * @param sources Iterable of source files to compile
     * @param compilerOptions Additional compiler options  
     * @throws RuntimeException if compilation fails
     */
    public static void compile(Iterable<? extends File> sources, String... compilerOptions);
}

Compilation Usage:

import org.apache.logging.log4j.core.test.Compiler;

public class DynamicCompilationTest {
    
    @Test
    public void testDynamicPluginCompilation() throws IOException {
        // Create temporary Java source file
        File sourceFile = Files.createTempFile("TestPlugin", ".java").toFile();
        
        String sourceCode = """
            package test;
            @Plugin(name = "TestPlugin", category = "Core")
            public class TestPlugin extends AbstractAppender {
                // Plugin implementation
            }
            """;
        
        Files.write(sourceFile.toPath(), sourceCode.getBytes());
        
        // Compile the source
        Compiler.compile(sourceFile, "-cp", System.getProperty("java.class.path"));
        
        // Verify compilation produced .class file
        File classFile = new File(sourceFile.getParent(), "TestPlugin.class");
        assertTrue(classFile.exists());
    }
}

Performance Testing Utilities

Utilities for GC-free logging performance testing and profiler integration.

/**
 * GC-free logging performance testing utilities
 */
public enum GcFreeLoggingTestUtil {
    INSTANCE;
    
    /**
     * Executes logging with specified configuration and test class
     * @param configurationFile Configuration file path
     * @param testClass Test class to execute
     */
    public static void executeLogging(String configurationFile, Class<?> testClass);
    
    /**
     * Runs test class with GC monitoring
     * @param cls Test class to run
     */
    public static void runTest(Class<?> cls);
    
    /**
     * CharSequence implementation for GC-free testing
     */
    public static class MyCharSeq implements CharSequence {
        
        public MyCharSeq(String value);
        
        public int length();
        public char charAt(int index);
        public CharSequence subSequence(int start, int end);
        public String toString();
    }
}

/**
 * YourKit Java Profiler helper for performance testing
 */
public final class Profiler {
    
    /**
     * Checks if YourKit profiler is active
     * @return true if profiler is active and available
     */
    public static boolean isActive();
    
    /**
     * Starts CPU profiling if profiler is available
     * @throws RuntimeException if profiler not available
     */
    public static void start();
    
    /**
     * Stops profiling and captures snapshot
     * @throws RuntimeException if profiler not available or not started
     */
    public static void stop();
}

Rule Chain Factory

Utility for building JUnit RuleChains with multiple rules.

/**
 * Utility for building JUnit RuleChains
 */
public class RuleChainFactory {
    
    /**
     * Creates RuleChain from multiple TestRules
     * @param testRules TestRules to chain together
     * @return RuleChain containing all rules
     */
    public static RuleChain create(TestRule... testRules);
}

Test Markers

Predefined markers for test logging and categorization.

/**
 * Predefined markers for testing
 */
public class TestMarkers {
    
    /**
     * Marker for lifecycle-related test logging
     */
    public static final Marker LIFE_CYCLE;
    
    /**
     * General test marker
     */
    public static final Marker TEST;
    
    /**
     * Marker for TestRule-related logging
     */
    public static final Marker TEST_RULE;
    
    /**
     * Marker for TestRule lifecycle logging
     */
    public static final Marker TEST_RULE_LIFE_CYCLE;
}

Configuration Factory

Simple configuration factory for testing scenarios.

/**
 * Simple configuration factory for testing
 */
public class BasicConfigurationFactory extends ConfigurationFactory {
    
    /**
     * Gets configuration for LoggerContext with URI
     * @param loggerContext The logger context
     * @param name Configuration name
     * @param configLocation Configuration URI
     * @return Configuration instance
     */
    public Configuration getConfiguration(LoggerContext loggerContext, String name, URI configLocation);
    
    /**
     * Gets supported configuration types
     * @return Array of supported file extensions
     */
    public String[] getSupportedTypes();
    
    /**
     * Gets configuration from ConfigurationSource
     * @param loggerContext The logger context
     * @param source Configuration source
     * @return Configuration instance
     */
    public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source);
    
    /**
     * Simple configuration implementation for testing
     */
    public static class BasicConfiguration extends AbstractConfiguration {
        
        /**
         * Creates basic configuration with default console appender
         * @param loggerContext The logger context
         * @param name Configuration name
         */
        public BasicConfiguration(LoggerContext loggerContext, String name);
    }
}

Extended Levels

Custom logging levels for testing scenarios.

/**
 * Custom logging levels for testing
 * Plugin annotation: @Plugin(name = "ExtendedLevel", category = Level.CATEGORY)
 */
@Plugin(name = "ExtendedLevel", category = Level.CATEGORY)
public class ExtendedLevels {
    
    /**
     * NOTE level (350) - between INFO (400) and WARN (300)
     */
    public static final Level NOTE = Level.forName("NOTE", 350);
    
    /**
     * DETAIL level (450) - between DEBUG (500) and INFO (400) 
     */
    public static final Level DETAIL = Level.forName("DETAIL", 450);
}

Usage Patterns

Resource Management Pattern

@Rule
public RuleChain rules = RuleChainFactory.create(
    SystemPropertyTestRule.create("test.port", "8080"),
    AvailablePortSystemPropertyTestRule.create("available.port"),
    CleanFiles.cleanFiles("test.log")
);

Performance Testing Pattern

@Test
public void testGcFreeLogging() {
    if (Profiler.isActive()) {
        Profiler.start();
    }
    
    GcFreeLoggingTestUtil.executeLogging("gc-free-config.xml", this.getClass());
    
    if (Profiler.isActive()) {
        Profiler.stop();
    }
}

Context Lifecycle Pattern

@After
public void cleanup() {
    CoreLoggerContexts.stopLoggerContext();
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-logging-log4j--log4j-core-test

docs

hamcrest-matchers.md

index.md

junit-integration.md

mock-servers.md

test-appenders.md

test-categories.md

testing-utilities.md

tile.json