Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate log4j-core-test to JUnit 5 #3061

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions log4j-core-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,6 @@
<scope>test</scope>
</dependency>

<!-- JUnit, naturally -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>

<!-- Used for Kafka appender -->
<dependency>
<groupId>org.apache.kafka</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.rules.ExternalResource;

/**
* This class should not perform logging using Log4j to avoid accidentally
* loading or re-loading Log4j configurations.
*/
public abstract class AbstractExternalFileCleaner extends ExternalResource {
public abstract class AbstractExternalFileCleaner extends ExternalResource
implements BeforeEachCallback, AfterEachCallback {

protected static final String CLEANER_MARKER = "CLEANER";

Expand Down Expand Up @@ -91,13 +95,23 @@ public AbstractExternalFileCleaner(
}
}

@Override
public void afterEach(ExtensionContext context) {
after();
}

@Override
protected void after() {
if (cleanAfter()) {
this.clean();
}
}

@Override
public void beforeEach(ExtensionContext context) {
before();
}

@Override
protected void before() {
if (cleanBefore()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.logging.log4j.core.test.junit;

import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.status.StatusLogger;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

public class CleanFoldersRuleExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {

private final String CONFIG;
private final String ClassName;
private final ClassLoader ClassNameLoader;
private LoggerContext context;
private CleanFolders cleanFolders;

public CleanFoldersRuleExtension(
final String DIR, final String CONFIG, final String ClassName, final ClassLoader ClassNameLoader) {
this.CONFIG = CONFIG;
this.ClassName = ClassName;
this.ClassNameLoader = ClassNameLoader;
this.cleanFolders = new CleanFolders(DIR);
}

public CleanFoldersRuleExtension(
final String DIR,
final String CONFIG,
final String ClassName,
final ClassLoader ClassNameLoader,
final boolean before,
final boolean after,
final int maxTries) {
this.CONFIG = CONFIG;
this.ClassName = ClassName;
this.ClassNameLoader = ClassNameLoader;
this.cleanFolders = new CleanFolders(before, after, maxTries, DIR);
}

@Override
public void beforeEach(final ExtensionContext ctx) throws Exception {
this.cleanFolders.beforeEach(ctx);
this.context = Configurator.initialize(ClassName, ClassNameLoader, CONFIG);
}

@Override
public void afterEach(final ExtensionContext ctx) throws Exception {
if (this.context != null) {
Configurator.shutdown(this.context, 10, TimeUnit.SECONDS);
StatusLogger.getLogger().reset();
}
this.cleanFolders.afterEach(ctx);
}

@Override
public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
throws ParameterResolutionException {
// Check if the parameter is of type LoggerContext
return parameterContext.getParameter().getType().equals(LoggerContext.class);
}

@Override
public Object resolveParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
throws ParameterResolutionException {
// Return the LoggerContext instance
return this.context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.appender.db.jdbc.ConnectionSource;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.rules.TestRule;
import org.junit.runner.Description;

Expand All @@ -35,12 +38,15 @@
* @since 2.8
*/
@SuppressFBWarnings("SQL_INJECTION_JDBC")
public class JdbcRule implements TestRule {
public class JdbcRule implements TestRule, BeforeEachCallback, AfterEachCallback {

private final ConnectionSource connectionSource;
private final String createTableStatement;
private final String dropTableStatement;

private Connection connection;
private Statement statement;

/**
* Creates a JdbcRule using a {@link ConnectionSource} and a table creation statement.
*
Expand All @@ -63,24 +69,45 @@ public org.junit.runners.model.Statement apply(
return new org.junit.runners.model.Statement() {
@Override
public void evaluate() throws Throwable {
try (final Connection connection = getConnection();
final Statement statement = connection.createStatement()) {
try {
if (StringUtils.isNotEmpty(createTableStatement)) {
statement.executeUpdate(createTableStatement);
}
base.evaluate();
} finally {
if (StringUtils.isNotEmpty(dropTableStatement)) {
statement.executeUpdate(dropTableStatement);
}
statement.execute("SHUTDOWN");
}
try {
setupConnection();
base.evaluate();
} finally {
closeConnection();
}
}
};
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
setupConnection();
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
closeConnection();
}

void setupConnection() throws SQLException {
connection = getConnection();
statement = connection.createStatement();

if (StringUtils.isNotEmpty(createTableStatement)) {
statement.executeUpdate(createTableStatement);
}
}

void closeConnection() throws SQLException {
if (StringUtils.isNotEmpty(dropTableStatement)) {
statement.executeUpdate(dropTableStatement);
}
statement.execute("SHUTDOWN");

statement.close();
connection.close();
}

public Connection getConnection() throws SQLException {
return connectionSource.getConnection();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.logging.log4j.core.test.junit;

import java.util.Collections;
import java.util.Map;
import javax.naming.Context;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;

/**
* JUnit Extension to create a mock {@link Context} and bind an object to a name.
*
*/
public class JndiExtension implements BeforeEachCallback {

private final Map<String, Object> initialBindings;

public JndiExtension(final String name, final Object value) {
this.initialBindings = Collections.singletonMap(name, value);
}

public JndiExtension(final Map<String, Object> initialBindings) {
this.initialBindings = initialBindings;
}

public void beforeEach(ExtensionContext ctx) throws Exception {
final SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
for (final Map.Entry<String, Object> entry : initialBindings.entrySet()) {
builder.bind(entry.getKey(), entry.getValue());
}
}
}
Loading
Loading