Skip to content

Commit 7cd0c6c

Browse files
AlbaHerreriasRealtinhulkobajcoglanninetteadhikari
committed
Migrate log4j-core-test to JUnit 5
Co-Authored-By: Julia Krüger <juliakbln@gmail.com> Co-Authored-By: hulkoba <4191287+hulkoba@users.noreply.github.com> Co-Authored-By: James Coglan <9265+jcoglan@users.noreply.github.com> Co-Authored-By: Ninette Adhikari <13760198+ninetteadhikari@users.noreply.github.com>
1 parent 0f48f60 commit 7cd0c6c

File tree

122 files changed

+3104
-2570
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+3104
-2570
lines changed

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/AbstractExternalFileCleaner.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@
2929
import java.util.Map;
3030
import java.util.Set;
3131
import org.junit.Assert;
32+
import org.junit.jupiter.api.extension.AfterEachCallback;
33+
import org.junit.jupiter.api.extension.BeforeEachCallback;
34+
import org.junit.jupiter.api.extension.ExtensionContext;
3235
import org.junit.rules.ExternalResource;
3336

3437
/**
3538
* This class should not perform logging using Log4j to avoid accidentally
3639
* loading or re-loading Log4j configurations.
3740
*/
38-
public abstract class AbstractExternalFileCleaner extends ExternalResource {
41+
public abstract class AbstractExternalFileCleaner extends ExternalResource
42+
implements BeforeEachCallback, AfterEachCallback {
3943

4044
protected static final String CLEANER_MARKER = "CLEANER";
4145

@@ -91,13 +95,23 @@ public AbstractExternalFileCleaner(
9195
}
9296
}
9397

98+
@Override
99+
public void afterEach(ExtensionContext context) {
100+
after();
101+
}
102+
94103
@Override
95104
protected void after() {
96105
if (cleanAfter()) {
97106
this.clean();
98107
}
99108
}
100109

110+
@Override
111+
public void beforeEach(ExtensionContext context) {
112+
before();
113+
}
114+
101115
@Override
102116
protected void before() {
103117
if (cleanBefore()) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.test.junit;
18+
19+
import java.util.concurrent.TimeUnit;
20+
import org.apache.logging.log4j.core.LoggerContext;
21+
import org.apache.logging.log4j.core.config.Configurator;
22+
import org.apache.logging.log4j.status.StatusLogger;
23+
import org.junit.jupiter.api.extension.AfterEachCallback;
24+
import org.junit.jupiter.api.extension.BeforeEachCallback;
25+
import org.junit.jupiter.api.extension.ExtensionContext;
26+
import org.junit.jupiter.api.extension.ParameterContext;
27+
import org.junit.jupiter.api.extension.ParameterResolutionException;
28+
import org.junit.jupiter.api.extension.ParameterResolver;
29+
30+
public class CleanFoldersRuleExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
31+
32+
private final String CONFIG;
33+
private final String ClassName;
34+
private final ClassLoader ClassNameLoader;
35+
private LoggerContext context;
36+
private CleanFolders cleanFolders;
37+
38+
public CleanFoldersRuleExtension(
39+
final String DIR, final String CONFIG, final String ClassName, final ClassLoader ClassNameLoader) {
40+
this.CONFIG = CONFIG;
41+
this.ClassName = ClassName;
42+
this.ClassNameLoader = ClassNameLoader;
43+
this.cleanFolders = new CleanFolders(DIR);
44+
}
45+
46+
public CleanFoldersRuleExtension(
47+
final String DIR,
48+
final String CONFIG,
49+
final String ClassName,
50+
final ClassLoader ClassNameLoader,
51+
final boolean before,
52+
final boolean after,
53+
final int maxTries) {
54+
this.CONFIG = CONFIG;
55+
this.ClassName = ClassName;
56+
this.ClassNameLoader = ClassNameLoader;
57+
this.cleanFolders = new CleanFolders(before, after, maxTries, DIR);
58+
}
59+
60+
@Override
61+
public void beforeEach(final ExtensionContext ctx) throws Exception {
62+
this.cleanFolders.beforeEach(ctx);
63+
this.context = Configurator.initialize(ClassName, ClassNameLoader, CONFIG);
64+
}
65+
66+
@Override
67+
public void afterEach(final ExtensionContext ctx) throws Exception {
68+
if (this.context != null) {
69+
Configurator.shutdown(this.context, 10, TimeUnit.SECONDS);
70+
StatusLogger.getLogger().reset();
71+
}
72+
this.cleanFolders.afterEach(ctx);
73+
}
74+
75+
@Override
76+
public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
77+
throws ParameterResolutionException {
78+
// Check if the parameter is of type LoggerContext
79+
return parameterContext.getParameter().getType().equals(LoggerContext.class);
80+
}
81+
82+
@Override
83+
public Object resolveParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
84+
throws ParameterResolutionException {
85+
// Return the LoggerContext instance
86+
return this.context;
87+
}
88+
}

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/JdbcRule.java

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.util.Objects;
2424
import org.apache.commons.lang3.StringUtils;
2525
import org.apache.logging.log4j.core.appender.db.jdbc.ConnectionSource;
26+
import org.junit.jupiter.api.extension.AfterEachCallback;
27+
import org.junit.jupiter.api.extension.BeforeEachCallback;
28+
import org.junit.jupiter.api.extension.ExtensionContext;
2629
import org.junit.rules.TestRule;
2730
import org.junit.runner.Description;
2831

@@ -35,12 +38,15 @@
3538
* @since 2.8
3639
*/
3740
@SuppressFBWarnings("SQL_INJECTION_JDBC")
38-
public class JdbcRule implements TestRule {
41+
public class JdbcRule implements TestRule, BeforeEachCallback, AfterEachCallback {
3942

4043
private final ConnectionSource connectionSource;
4144
private final String createTableStatement;
4245
private final String dropTableStatement;
4346

47+
private Connection connection;
48+
private Statement statement;
49+
4450
/**
4551
* Creates a JdbcRule using a {@link ConnectionSource} and a table creation statement.
4652
*
@@ -63,24 +69,45 @@ public org.junit.runners.model.Statement apply(
6369
return new org.junit.runners.model.Statement() {
6470
@Override
6571
public void evaluate() throws Throwable {
66-
try (final Connection connection = getConnection();
67-
final Statement statement = connection.createStatement()) {
68-
try {
69-
if (StringUtils.isNotEmpty(createTableStatement)) {
70-
statement.executeUpdate(createTableStatement);
71-
}
72-
base.evaluate();
73-
} finally {
74-
if (StringUtils.isNotEmpty(dropTableStatement)) {
75-
statement.executeUpdate(dropTableStatement);
76-
}
77-
statement.execute("SHUTDOWN");
78-
}
72+
try {
73+
setupConnection();
74+
base.evaluate();
75+
} finally {
76+
closeConnection();
7977
}
8078
}
8179
};
8280
}
8381

82+
@Override
83+
public void beforeEach(ExtensionContext context) throws Exception {
84+
setupConnection();
85+
}
86+
87+
@Override
88+
public void afterEach(ExtensionContext context) throws Exception {
89+
closeConnection();
90+
}
91+
92+
void setupConnection() throws SQLException {
93+
connection = getConnection();
94+
statement = connection.createStatement();
95+
96+
if (StringUtils.isNotEmpty(createTableStatement)) {
97+
statement.executeUpdate(createTableStatement);
98+
}
99+
}
100+
101+
void closeConnection() throws SQLException {
102+
if (StringUtils.isNotEmpty(dropTableStatement)) {
103+
statement.executeUpdate(dropTableStatement);
104+
}
105+
statement.execute("SHUTDOWN");
106+
107+
statement.close();
108+
connection.close();
109+
}
110+
84111
public Connection getConnection() throws SQLException {
85112
return connectionSource.getConnection();
86113
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.test.junit;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
import javax.naming.Context;
22+
import org.junit.jupiter.api.extension.BeforeEachCallback;
23+
import org.junit.jupiter.api.extension.ExtensionContext;
24+
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
25+
26+
/**
27+
* JUnit Extension to create a mock {@link Context} and bind an object to a name.
28+
*
29+
*/
30+
public class JndiExtension implements BeforeEachCallback {
31+
32+
private final Map<String, Object> initialBindings;
33+
34+
public JndiExtension(final String name, final Object value) {
35+
this.initialBindings = Collections.singletonMap(name, value);
36+
}
37+
38+
public JndiExtension(final Map<String, Object> initialBindings) {
39+
this.initialBindings = initialBindings;
40+
}
41+
42+
public void beforeEach(ExtensionContext ctx) throws Exception {
43+
final SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
44+
for (final Map.Entry<String, Object> entry : initialBindings.entrySet()) {
45+
builder.bind(entry.getKey(), entry.getValue());
46+
}
47+
}
48+
}

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/JndiRule.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121
import javax.naming.Context;
22+
import javax.naming.NamingException;
23+
import org.junit.jupiter.api.extension.BeforeEachCallback;
24+
import org.junit.jupiter.api.extension.ExtensionContext;
2225
import org.junit.rules.TestRule;
2326
import org.junit.runner.Description;
2427
import org.junit.runners.model.Statement;
@@ -29,7 +32,7 @@
2932
*
3033
* @since 2.8
3134
*/
32-
public class JndiRule implements TestRule {
35+
public class JndiRule implements TestRule, BeforeEachCallback {
3336

3437
private final Map<String, Object> initialBindings;
3538

@@ -46,12 +49,21 @@ public Statement apply(final Statement base, final Description description) {
4649
return new Statement() {
4750
@Override
4851
public void evaluate() throws Throwable {
49-
final SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
50-
for (final Map.Entry<String, Object> entry : initialBindings.entrySet()) {
51-
builder.bind(entry.getKey(), entry.getValue());
52-
}
52+
before();
5353
base.evaluate();
5454
}
5555
};
5656
}
57+
58+
private void before() throws NamingException {
59+
final SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
60+
for (final Map.Entry<String, Object> entry : initialBindings.entrySet()) {
61+
builder.bind(entry.getKey(), entry.getValue());
62+
}
63+
}
64+
65+
@Override
66+
public void beforeEach(ExtensionContext context) throws Exception {
67+
before();
68+
}
5769
}

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/package-info.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
* @see org.junit.rules.TestRule
2121
*/
2222
@Export
23-
@Version("2.23.1")
23+
@Version("2.24.1")
24+
@BaselineIgnore("2.24.1")
2425
package org.apache.logging.log4j.core.test.junit;
2526

27+
import aQute.bnd.annotation.baseline.BaselineIgnore;
2628
import org.osgi.annotation.bundle.Export;
2729
import org.osgi.annotation.versioning.Version;

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/layout/Log4j2_1482_Test.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.core.test.layout;
1818

19+
import static org.junit.jupiter.api.Assertions.fail;
20+
1921
import java.io.File;
2022
import java.io.IOException;
2123
import java.nio.charset.Charset;
@@ -26,23 +28,28 @@
2628
import java.util.List;
2729
import org.apache.logging.log4j.core.LoggerContext;
2830
import org.apache.logging.log4j.core.config.Configurator;
29-
import org.apache.logging.log4j.core.test.categories.Layouts;
30-
import org.apache.logging.log4j.core.test.junit.CleanFolders;
31-
import org.junit.Assert;
32-
import org.junit.Rule;
33-
import org.junit.Test;
34-
import org.junit.experimental.categories.Category;
31+
import org.apache.logging.log4j.core.test.junit.CleanFoldersRuleExtension;
32+
import org.junit.jupiter.api.Tag;
33+
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.extension.RegisterExtension;
3535

3636
/**
3737
* Tests https://issues.apache.org/jira/browse/LOG4J2-1482
3838
*/
39-
@Category(Layouts.Csv.class)
39+
@Tag("Layouts.Csv")
4040
public abstract class Log4j2_1482_Test {
4141

4242
static final String CONFIG_LOCATION = "log4j2-1482.xml";
4343

4444
static final String FOLDER = "target/log4j2-1482";
4545

46+
@RegisterExtension
47+
private CleanFoldersRuleExtension cleanFolders = new CleanFoldersRuleExtension(
48+
FOLDER,
49+
CONFIG_LOCATION,
50+
Log4j2_1482_Test.class.getName(),
51+
this.getClass().getClassLoader());
52+
4653
private static final int LOOP_COUNT = 10;
4754

4855
static void assertFileContents(final int runNumber) throws IOException {
@@ -56,15 +63,11 @@ static void assertFileContents(final int runNumber) throws IOException {
5663
final File[] files = folder.toFile().listFiles();
5764
Arrays.sort(files);
5865
System.out.println("Run " + runNumber + ": " + Arrays.toString(files));
59-
Assert.fail(
60-
String.format("Run %,d, line %,d of %,d: \"%s\" in %s", runNumber, i++, size, string, lines));
66+
fail(String.format("Run %,d, line %,d of %,d: \"%s\" in %s", runNumber, i++, size, string, lines));
6167
}
6268
}
6369
}
6470

65-
@Rule
66-
public CleanFolders cleanFolders = new CleanFolders(FOLDER);
67-
6871
protected abstract void log(int runNumber);
6972

7073
private void loopingRun(final int loopCount) throws IOException {

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/layout/package-info.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
* limitations under the license.
1616
*/
1717
@Export
18-
@Version("2.20.1")
18+
@Version("2.24.1")
19+
@BaselineIgnore("2.24.1")
1920
package org.apache.logging.log4j.core.test.layout;
2021

22+
import aQute.bnd.annotation.baseline.BaselineIgnore;
2123
import org.osgi.annotation.bundle.Export;
2224
import org.osgi.annotation.versioning.Version;

0 commit comments

Comments
 (0)