You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2020/08/30 20:15:50 UTC
[logging-log4j2] branch master updated: [LOG4J2-2653] Migrate more
tests to JUnit 5
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new e32cdde [LOG4J2-2653] Migrate more tests to JUnit 5
e32cdde is described below
commit e32cddede58a7049544a0389f52ca0947243057e
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sun Aug 30 15:15:32 2020 -0500
[LOG4J2-2653] Migrate more tests to JUnit 5
Signed-off-by: Matt Sicker <bo...@gmail.com>
---
.../logging/log4j/junit/AbstractFileCleaner.java | 87 +++++++++++
.../logging/log4j/junit/CleanUpDirectories.java | 44 ++++++
.../apache/logging/log4j/junit/CleanUpFiles.java | 44 ++++++
.../logging/log4j/junit/DirectoryCleaner.java | 61 ++++++++
.../apache/logging/log4j/junit/FileCleaner.java | 43 ++++++
.../log4j/core/config/AbstractLog4j2_1100Test.java | 68 ---------
.../logging/log4j/core/config/AdvertiserTest.java | 29 ++--
.../core/config/AppenderControlArraySetTest.java | 8 +-
.../config/CompositeConfigurationMissingTest.java | 15 +-
.../log4j/core/config/ConfigurationSourceTest.java | 10 +-
.../log4j/core/config/ConfiguratorTest.java | 18 +--
.../log4j/core/config/CustomConfigurationTest.java | 54 +++----
.../logging/log4j/core/config/FileOutputTest.java | 37 ++---
.../log4j/core/config/JiraLog4j2_1100JsonTest.java | 31 ----
.../log4j/core/config/JiraLog4j2_1100XmlTest.java | 27 ----
.../core/config/JiraLog4j2_1100YamlBadTest.java | 33 ----
.../core/config/JiraLog4j2_1100YamlGoodTest.java | 31 ----
.../log4j/core/config/JiraLog4j2_2134Test.java | 23 +--
.../log4j/core/config/LoggerConfigTest.java | 57 +++----
.../log4j/core/config/LoggersPluginTest.java | 12 +-
.../log4j/core/config/MissingRootLoggerTest.java | 28 ++--
.../core/config/MultipleTriggeringPolicyTest.java | 84 +++++++++++
.../logging/log4j/core/config/PropertyTest.java | 39 ++---
.../core/config/ReconfigurationDeadlockTest.java | 19 +--
.../log4j/core/config/TestConfigurator.java | 167 ++++++++++-----------
.../log4j/core/config/TestConfiguratorError.java | 22 +--
26 files changed, 597 insertions(+), 494 deletions(-)
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/AbstractFileCleaner.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/AbstractFileCleaner.java
new file mode 100644
index 0000000..c5d2bc4
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/AbstractFileCleaner.java
@@ -0,0 +1,87 @@
+/*
+ * 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.junit;
+
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.fail;
+
+abstract class AbstractFileCleaner implements BeforeEachCallback, AfterEachCallback {
+ private static final int MAX_TRIES = Integer.getInteger("log4j2.junit.fileCleanerMaxTries", 10);
+
+ @Override
+ public void beforeEach(final ExtensionContext context) throws Exception {
+ clean(context);
+ }
+
+ @Override
+ public void afterEach(final ExtensionContext context) throws Exception {
+ clean(context);
+ }
+
+ private void clean(final ExtensionContext context) {
+ final Collection<Path> paths = getPathsForTest(context);
+ if (paths.isEmpty()) {
+ return;
+ }
+ final Map<Path, IOException> failures = new ConcurrentHashMap<>();
+ for (final Path path : paths) {
+ if (Files.exists(path)) {
+ for (int i = 0; i < MAX_TRIES; i++) {
+ try {
+ if (delete(path)) {
+ failures.remove(path);
+ break;
+ }
+ } catch (final IOException e) {
+ failures.put(path, e);
+ }
+ try {
+ TimeUnit.MILLISECONDS.sleep(200);
+ } catch (final InterruptedException ignored) {
+ failures.put(path, new InterruptedIOException());
+ Thread.currentThread().interrupt();
+ break;
+ }
+ }
+ }
+ }
+ if (!failures.isEmpty()) {
+ final String message = failures.entrySet().stream()
+ .map(e -> e.getKey() + " failed with " + e.getValue())
+ .collect(Collectors.joining(", "));
+ fail(message);
+ }
+ }
+
+ abstract Collection<Path> getPathsForTest(final ExtensionContext context);
+
+ abstract boolean delete(final Path path) throws IOException;
+}
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/CleanUpDirectories.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/CleanUpDirectories.java
new file mode 100644
index 0000000..3cdceda
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/CleanUpDirectories.java
@@ -0,0 +1,44 @@
+/*
+ * 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.junit;
+
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * JUnit extension to automatically clean up a list of directories and their contents before and after test execution.
+ * This will automatically retry deletion up to 10 times per file while pausing a short duration each time.
+ * This can be overridden with the system property {@code log4j2.junit.fileCleanerMaxTries}.
+ *
+ * @see CleanUpFiles
+ * @since 2.14.0
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+@Documented
+@Inherited
+@ExtendWith(DirectoryCleaner.class)
+public @interface CleanUpDirectories {
+ String[] value();
+}
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/CleanUpFiles.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/CleanUpFiles.java
new file mode 100644
index 0000000..b58d8f8
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/CleanUpFiles.java
@@ -0,0 +1,44 @@
+/*
+ * 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.junit;
+
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * JUnit extension to automatically clean up a list of files before and after test execution.
+ * This will automatically retry deletion up to 10 times per file while pausing a short duration each time.
+ * This can be overridden with the system property {@code log4j2.junit.fileCleanerMaxTries}.
+ *
+ * @see CleanUpDirectories
+ * @since 2.14.0
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+@Documented
+@Inherited
+@ExtendWith(FileCleaner.class)
+public @interface CleanUpFiles {
+ String[] value();
+}
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/DirectoryCleaner.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/DirectoryCleaner.java
new file mode 100644
index 0000000..efa971b
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/DirectoryCleaner.java
@@ -0,0 +1,61 @@
+/*
+ * 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.junit;
+
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.stream.Collectors;
+
+class DirectoryCleaner extends AbstractFileCleaner {
+ @Override
+ Collection<Path> getPathsForTest(final ExtensionContext context) {
+ final CleanUpDirectories cleanUpDirectories = context.getRequiredTestClass().getAnnotation(CleanUpDirectories.class);
+ return cleanUpDirectories == null ? Collections.emptySet() :
+ Arrays.stream(cleanUpDirectories.value()).map(Paths::get).collect(Collectors.toSet());
+ }
+
+ @Override
+ boolean delete(final Path path) throws IOException {
+ if (Files.exists(path) && Files.isDirectory(path)) {
+ Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
+ Files.deleteIfExists(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
+ Files.deleteIfExists(dir);
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ }
+ return true;
+ }
+}
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/FileCleaner.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/FileCleaner.java
new file mode 100644
index 0000000..64ae0df
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/FileCleaner.java
@@ -0,0 +1,43 @@
+/*
+ * 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.junit;
+
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.stream.Collectors;
+
+class FileCleaner extends AbstractFileCleaner {
+ @Override
+ Collection<Path> getPathsForTest(final ExtensionContext context) {
+ final CleanUpFiles cleanUpFiles = context.getRequiredTestClass().getAnnotation(CleanUpFiles.class);
+ return cleanUpFiles == null ? Collections.emptySet() :
+ Arrays.stream(cleanUpFiles.value()).map(Paths::get).collect(Collectors.toSet());
+ }
+
+ @Override
+ boolean delete(final Path path) throws IOException {
+ return Files.deleteIfExists(path);
+ }
+}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AbstractLog4j2_1100Test.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AbstractLog4j2_1100Test.java
deleted file mode 100644
index e5db9cc..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AbstractLog4j2_1100Test.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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.config;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-
-import org.apache.logging.log4j.core.appender.RollingFileAppender;
-import org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy;
-import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
-import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;
-import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
-import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.junit.Rule;
-import org.junit.Test;
-
-public abstract class AbstractLog4j2_1100Test {
-
- @Rule
- public LoggerContextRule context = new LoggerContextRule(getConfigurationResource());
-
- protected abstract String getConfigurationResource();
-
- @Test
- public void test() {
- final Configuration configuration = context.getConfiguration();
- assertNotNull(configuration);
- final RollingFileAppender appender = configuration.getAppender("File");
- assertNotNull(appender);
- final CompositeTriggeringPolicy compositeTriggeringPolicy = appender.getTriggeringPolicy();
- assertNotNull(compositeTriggeringPolicy);
- final TriggeringPolicy[] triggeringPolicies = compositeTriggeringPolicy.getTriggeringPolicies();
- SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = null;
- TimeBasedTriggeringPolicy timeBasedTriggeringPolicy = null;
- for (final TriggeringPolicy triggeringPolicy : triggeringPolicies) {
- if (triggeringPolicy instanceof TimeBasedTriggeringPolicy) {
- timeBasedTriggeringPolicy = (TimeBasedTriggeringPolicy) triggeringPolicy;
- assertEquals(7, timeBasedTriggeringPolicy.getInterval());
- }
- if (triggeringPolicy instanceof SizeBasedTriggeringPolicy) {
- sizeBasedTriggeringPolicy = (SizeBasedTriggeringPolicy) triggeringPolicy;
- assertEquals(100 * 1024 * 1024, sizeBasedTriggeringPolicy.getMaxFileSize());
- }
- }
- if (timeBasedTriggeringPolicy == null) {
- fail("Missing TimeBasedTriggeringPolicy");
- }
- if (sizeBasedTriggeringPolicy == null) {
- fail("Missing SizeBasedTriggeringPolicy");
- }
- }
-}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AdvertiserTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AdvertiserTest.java
index ad2ad7c..79de2a2 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AdvertiserTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AdvertiserTest.java
@@ -22,21 +22,20 @@ import java.util.Map;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
import org.apache.logging.log4j.status.StatusLogger;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
-/**
- *
- */
+@Tag("functional")
public class AdvertiserTest {
private static final String CONFIG = "log4j-advertiser.xml";
private static final String STATUS_LOG = "target/status.log";
- @BeforeClass
+ @BeforeAll
public static void setupClass() {
final File file = new File(STATUS_LOG);
file.delete();
@@ -53,7 +52,7 @@ public class AdvertiserTest {
}
}
- @AfterClass
+ @AfterAll
public static void cleanupClass() {
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
final LoggerContext ctx = LoggerContext.getContext();
@@ -85,10 +84,10 @@ public class AdvertiserTest {
foundSocket2 = true;
}
}
- assertTrue("Entries for File1 appender do not exist", foundFile1);
- assertTrue("Entries for File2 appender exist", !foundFile2);
- assertTrue("Entries for Socket1 appender do not exist", foundSocket1);
- assertTrue("Entries for Socket2 appender exist", !foundSocket2);
+ assertTrue(foundFile1, "Entries for File1 appender do not exist");
+ assertFalse(foundFile2, "Entries for File2 appender exist");
+ assertTrue(foundSocket1, "Entries for Socket1 appender do not exist");
+ assertFalse(foundSocket2, "Entries for Socket2 appender exist");
}
@Test
@@ -104,7 +103,7 @@ public class AdvertiserTest {
ctx.stop();
final Map<Object, Map<String, String>> entries = InMemoryAdvertiser.getAdvertisedEntries();
- assertTrue("Entries found: " + entries, entries.isEmpty());
+ assertTrue(entries.isEmpty(), "Entries found: " + entries);
//reconfigure for subsequent testing
ctx.start();
@@ -118,7 +117,7 @@ public class AdvertiserTest {
ctx.stop();
final Map<Object, Map<String, String>> entries = InMemoryAdvertiser.getAdvertisedEntries();
- assertTrue("Entries found: " + entries, entries.isEmpty());
+ assertTrue(entries.isEmpty(), "Entries found: " + entries);
ctx.start();
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AppenderControlArraySetTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AppenderControlArraySetTest.java
index ac5679a..e8469af 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AppenderControlArraySetTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/AppenderControlArraySetTest.java
@@ -19,12 +19,12 @@ package org.apache.logging.log4j.core.config;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.test.appender.FailOnceAppender;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
/**
* Tests the AppenderControlArraySet class..
@@ -34,7 +34,7 @@ public class AppenderControlArraySetTest {
@Test
public void testInitiallyEmpty() throws Exception {
assertTrue(new AppenderControlArraySet().isEmpty());
- assertTrue(new AppenderControlArraySet().get().length == 0);
+ assertEquals(0, new AppenderControlArraySet().get().length);
}
private AppenderControl createControl(final String name) {
@@ -174,7 +174,7 @@ public class AppenderControlArraySetTest {
public void testIsEmptyMeansZeroLengthArray() throws Exception {
final AppenderControlArraySet set = new AppenderControlArraySet();
assertTrue(set.isEmpty());
- assertTrue(set.get().length == 0);
+ assertEquals(0, set.get().length);
}
@Test
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationMissingTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationMissingTest.java
index 81693a4..81e6b80 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationMissingTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationMissingTest.java
@@ -19,15 +19,14 @@ package org.apache.logging.log4j.core.config;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.*;
public class CompositeConfigurationMissingTest {
- @BeforeClass
+ @BeforeAll
public static void beforeClass() {
System.setProperty("log4j2.configurationFile", "classpath:log4j-comp-logger-root.xml,log4j-does-not-exist.json");
}
@@ -37,12 +36,12 @@ public class CompositeConfigurationMissingTest {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
- assertNotNull("No configuration returned", config);
+ assertNotNull(config, "No configuration returned");
//Test for Root log level override
- assertEquals("Expected Root logger log level to be ERROR", Level.ERROR, config.getRootLogger().getLevel());
+ assertEquals(Level.ERROR, config.getRootLogger().getLevel(), "Expected Root logger log level to be ERROR");
//Test for no cat2 level override
final LoggerConfig cat2 = config.getLogger("cat2");
- assertEquals("Expected cat2 log level to be INFO", Level.DEBUG, cat2.getLevel());
+ assertEquals(Level.DEBUG, cat2.getLevel(), "Expected cat2 log level to be INFO");
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java
index 5ca42f2..c067b71 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java
@@ -17,17 +17,17 @@
package org.apache.logging.log4j.core.config;
+import org.junit.jupiter.api.Test;
+
import java.io.ByteArrayInputStream;
-import org.junit.Assert;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
public class ConfigurationSourceTest {
@Test
public void testJira_LOG4J2_2770_byteArray() throws Exception {
- ConfigurationSource configurationSource = new ConfigurationSource(
- new ByteArrayInputStream(new byte[] { 'a', 'b' }));
- Assert.assertNotNull(configurationSource.resetInputStream());
+ ConfigurationSource configurationSource = new ConfigurationSource(new ByteArrayInputStream(new byte[] { 'a', 'b' }));
+ assertNotNull(configurationSource.resetInputStream());
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
index 485c8cd..9fcfdf9 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
@@ -20,12 +20,12 @@ import java.io.File;
import java.net.URI;
import org.apache.logging.log4j.core.LoggerContext;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.*;
+@Tag("functional")
public class ConfiguratorTest {
@Test
@@ -46,9 +46,9 @@ public class ConfiguratorTest {
try (final LoggerContext loggerContext = Configurator.initialize(getClass().getName(), null, path)) {
assertNotNull(loggerContext.getConfiguration().getAppender("List"));
URI uri = loggerContext.getConfigLocation();
- assertNotNull("No configuration location returned", uri);
+ assertNotNull(uri, "No configuration location returned");
Configurator.reconfigure();
- assertEquals("Unexpected configuration location returned", uri, loggerContext.getConfigLocation());
+ assertEquals(uri, loggerContext.getConfigLocation(), "Unexpected configuration location returned");
}
}
@@ -58,16 +58,16 @@ public class ConfiguratorTest {
try (final LoggerContext loggerContext = Configurator.initialize(getClass().getName(), null, path)) {
assertNotNull(loggerContext.getConfiguration().getAppender("List"));
URI uri = loggerContext.getConfigLocation();
- assertNotNull("No configuration location returned", uri);
+ assertNotNull(uri, "No configuration location returned");
final URI location = new File("src/test/resources/log4j2-config.xml").toURI();
Configurator.reconfigure(location);
- assertEquals("Unexpected configuration location returned", location, loggerContext.getConfigLocation());
+ assertEquals(location, loggerContext.getConfigLocation(), "Unexpected configuration location returned");
}
}
private void testInitializeFromFilePath(final String path) {
try (final LoggerContext loggerContext = Configurator.initialize(getClass().getName(), null, path)) {
- Assert.assertNotNull(loggerContext.getConfiguration().getAppender("List"));
+ assertNotNull(loggerContext.getConfiguration().getAppender("List"));
}
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java
index 1eb193c..69f17ab 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java
@@ -16,9 +16,6 @@
*/
package org.apache.logging.log4j.core.config;
-import java.io.File;
-import java.io.Serializable;
-
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Layout;
@@ -26,47 +23,41 @@ import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.junit.CleanUpFiles;
+import org.apache.logging.log4j.junit.LoggerContextSource;
import org.apache.logging.log4j.status.StatusConsoleListener;
import org.apache.logging.log4j.status.StatusListener;
import org.apache.logging.log4j.status.StatusLogger;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
-import static org.apache.logging.log4j.hamcrest.FileMatchers.exists;
-import static org.apache.logging.log4j.hamcrest.FileMatchers.hasLength;
+import java.io.IOException;
+import java.io.Serializable;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-/**
- *
- */
+@CleanUpFiles("target/test.log")
public class CustomConfigurationTest {
- public static final String LOG_FILE = "target/test.log";
+ public static final Path LOG_FILE = Paths.get("target", "test.log");
- @BeforeClass
+ @BeforeAll
public static void before() {
System.setProperty("log4j.level", "info");
System.setProperty("log.level", "info");
}
- @Rule
- public LoggerContextRule init = new LoggerContextRule("log4j-props.xml");
-
- @Before
- public void setUp() throws Exception {
- new File(LOG_FILE).delete();
- }
-
@Test
- public void testConfig() {
+ @LoggerContextSource("log4j-props.xml")
+ public void testConfig(final LoggerContext ctx) throws IOException {
// don't bother using "error" since that's the default; try another level
- final LoggerContext ctx = this.init.getLoggerContext();
- ctx.reconfigure();
final Configuration config = ctx.getConfiguration();
assertThat(config, instanceOf(XmlConfiguration.class));
for (final StatusListener listener : StatusLogger.getLogger().getListeners()) {
@@ -81,7 +72,7 @@ public class CustomConfigurationTest {
.build();
// @formatter:off
final FileAppender appender = FileAppender.newBuilder()
- .setFileName(LOG_FILE)
+ .setFileName(LOG_FILE.toString())
.setAppend(false)
.setName("File")
.setIgnoreExceptions(false)
@@ -100,10 +91,9 @@ public class CustomConfigurationTest {
loggerConfig.addAppender(appender, null, null);
config.addLogger("org.apache.logging.log4j", loggerConfig);
ctx.updateLoggers();
- final Logger logger = ctx.getLogger(CustomConfigurationTest.class.getName());
+ final Logger logger = ctx.getLogger(CustomConfigurationTest.class);
logger.info("This is a test");
- final File file = new File(LOG_FILE);
- assertThat(file, exists());
- assertThat(file, hasLength(greaterThan(0L)));
+ assertTrue(Files.exists(LOG_FILE));
+ assertThat(Files.size(LOG_FILE), greaterThan(0L));
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java
index 4666978..37fbe82 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java
@@ -16,35 +16,26 @@
*/
package org.apache.logging.log4j.core.config;
-import java.io.File;
+import org.apache.logging.log4j.junit.CleanUpFiles;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.junit.jupiter.api.Test;
-import org.apache.logging.log4j.junit.CleanFiles;
-import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.RuleChain;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
-import static org.apache.logging.log4j.hamcrest.FileMatchers.exists;
-import static org.apache.logging.log4j.hamcrest.FileMatchers.hasLength;
-import static org.hamcrest.Matchers.greaterThan;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-/**
- *
- */
+@CleanUpFiles("target/status.log")
public class FileOutputTest {
- private static final String CONFIG = "classpath:log4j-filetest.xml";
- private static final String STATUS_LOG = "target/status.log";
-
- @Rule
- public RuleChain rules = RuleChain.outerRule(new CleanFiles(STATUS_LOG)).around(new LoggerContextRule(CONFIG));
-
@Test
- public void testConfig() {
- final File file = new File(STATUS_LOG);
- assertThat("Status output file does not exist", file, exists());
- assertThat("File is empty", file, hasLength(greaterThan(0L)));
+ @LoggerContextSource("classpath:log4j-filetest.xml")
+ public void testConfig() throws IOException {
+ final Path logFile = Paths.get("target", "status.log");
+ assertTrue(Files.exists(logFile), "Status output file does not exist");
+ assertTrue(Files.size(logFile) > 0, "File is empty");
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100JsonTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100JsonTest.java
deleted file mode 100644
index 69884ea..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100JsonTest.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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.config;
-
-import org.apache.logging.log4j.categories.Configurations;
-import org.junit.experimental.categories.Category;
-
-@Category(Configurations.Json.class)
-public class JiraLog4j2_1100JsonTest extends AbstractLog4j2_1100Test {
-
- @Override
- protected String getConfigurationResource() {
- return "LOG4J2-1100/log4j2.json";
- }
-
-}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100XmlTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100XmlTest.java
deleted file mode 100644
index 8e3aa9c..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100XmlTest.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * 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.config;
-
-public class JiraLog4j2_1100XmlTest extends AbstractLog4j2_1100Test {
-
- @Override
- protected String getConfigurationResource() {
- return "LOG4J2-1100/log4j2.xml";
- }
-
-}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100YamlBadTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100YamlBadTest.java
deleted file mode 100644
index ad2fefe..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100YamlBadTest.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * 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.config;
-
-import org.apache.logging.log4j.categories.Configurations;
-import org.junit.Ignore;
-import org.junit.experimental.categories.Category;
-
-@Ignore
-@Category(Configurations.Yaml.class)
-public class JiraLog4j2_1100YamlBadTest extends AbstractLog4j2_1100Test {
-
- @Override
- protected String getConfigurationResource() {
- return "LOG4J2-1100/log4j2-good.yaml";
- }
-
-}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100YamlGoodTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100YamlGoodTest.java
deleted file mode 100644
index 89eb4be..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_1100YamlGoodTest.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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.config;
-
-import org.apache.logging.log4j.categories.Configurations;
-import org.junit.experimental.categories.Category;
-
-@Category(Configurations.Yaml.class)
-public class JiraLog4j2_1100YamlGoodTest extends AbstractLog4j2_1100Test {
-
- @Override
- protected String getConfigurationResource() {
- return "LOG4J2-1100/log4j2-good.yaml";
- }
-
-}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_2134Test.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_2134Test.java
index 9b2cef3..e1d1799 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_2134Test.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_2134Test.java
@@ -24,14 +24,15 @@ import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.junit.Rule;
-import org.junit.Test;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Tag;
-public class JiraLog4j2_2134Test {
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
- @Rule
- public final LoggerContextRule loggerContextRule = new LoggerContextRule("src/test/resources/log4j2-2134.yml");
+@Tag("yaml")
+@LoggerContextSource("log4j2-2134.yml")
+public class JiraLog4j2_2134Test {
@Test
public void testRefresh() {
@@ -57,7 +58,7 @@ public class JiraLog4j2_2134Test {
ctx.stop();
ctx.start(config);
- log.error("Info message");
+ assertDoesNotThrow(() -> log.error("Info message"));
}
@Test
@@ -67,7 +68,7 @@ public class JiraLog4j2_2134Test {
final Configuration config = ctx.getConfiguration();
ctx.start(config);
- log.error("Info message");
+ assertDoesNotThrow(() -> log.error("Info message"));
}
@Test
@@ -77,7 +78,7 @@ public class JiraLog4j2_2134Test {
ctx.stop();
ctx.start();
- log.error("Info message");
+ assertDoesNotThrow(() -> log.error("Info message"));
}
@Test
@@ -88,7 +89,7 @@ public class JiraLog4j2_2134Test {
ctx.stop();
ctx.start(config);
- log.error("Info message");
+ assertDoesNotThrow(() -> log.error("Info message"));
}
@SuppressWarnings("deprecation")
@@ -134,6 +135,6 @@ public class JiraLog4j2_2134Test {
ctx.stop();
ctx.start(config);
- log.error("Info message");
+ assertDoesNotThrow(() -> log.error("Info message"));
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java
index 30de999..1676916 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/LoggerConfigTest.java
@@ -15,22 +15,15 @@ package org.apache.logging.log4j.core.config;/*
* limitations under the license.
*/
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent.Builder;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.List;
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.Marker;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.impl.Log4jLogEvent.Builder;
-import org.apache.logging.log4j.core.impl.LogEventFactory;
-import org.apache.logging.log4j.message.Message;
-import org.apache.logging.log4j.message.SimpleMessage;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for LoggerConfig.
@@ -45,7 +38,7 @@ public class LoggerConfigTest {
@SuppressWarnings({"deprecation"})
@Test
public void testPropertiesWithoutSubstitution() {
- assertNull("null propertiesList", createForProperties(null).getPropertyList());
+ assertNull(createForProperties(null).getPropertyList(), "null propertiesList");
final Property[] all = new Property[] {
Property.createProperty("key1", "value1"),
@@ -53,21 +46,16 @@ public class LoggerConfigTest {
};
final LoggerConfig loggerConfig = createForProperties(all);
final List<Property> list = loggerConfig.getPropertyList();
- assertEquals("map and list contents equal", new HashSet<>(list),
- new HashSet<>(loggerConfig.getPropertyList()));
+ assertEquals(new HashSet<>(list),
+ new HashSet<>(loggerConfig.getPropertyList()), "map and list contents equal");
final Object[] actualList = new Object[1];
- loggerConfig.setLogEventFactory(new LogEventFactory() {
- @Override
- public LogEvent createEvent(final String loggerName, final Marker marker, final String fqcn,
- final Level level, final Message data,
- final List<Property> properties, final Throwable t) {
- actualList[0] = properties;
- return new Builder().setTimeMillis(System.currentTimeMillis()).build();
- }
+ loggerConfig.setLogEventFactory((loggerName, marker, fqcn, level, data, properties, t) -> {
+ actualList[0] = properties;
+ return new Builder().setTimeMillis(System.currentTimeMillis()).build();
});
loggerConfig.log("name", "fqcn", null, Level.INFO, new SimpleMessage("msg"), null);
- assertSame("propertiesList passed in as is if no substitutions required", list, actualList[0]);
+ assertSame(list, actualList[0], "propertiesList passed in as is if no substitutions required");
}
@Test
@@ -78,29 +66,24 @@ public class LoggerConfigTest {
};
final LoggerConfig loggerConfig = createForProperties(all);
final List<Property> list = loggerConfig.getPropertyList();
- assertEquals("map and list contents equal", new HashSet<>(list),
- new HashSet<>(loggerConfig.getPropertyList()));
+ assertEquals(new HashSet<>(list),
+ new HashSet<>(loggerConfig.getPropertyList()), "map and list contents equal");
final Object[] actualListHolder = new Object[1];
- loggerConfig.setLogEventFactory(new LogEventFactory() {
- @Override
- public LogEvent createEvent(final String loggerName, final Marker marker, final String fqcn,
- final Level level, final Message data,
- final List<Property> properties, final Throwable t) {
- actualListHolder[0] = properties;
- return new Builder().setTimeMillis(System.currentTimeMillis()).build();
- }
+ loggerConfig.setLogEventFactory((loggerName, marker, fqcn, level, data, properties, t) -> {
+ actualListHolder[0] = properties;
+ return new Builder().setTimeMillis(System.currentTimeMillis()).build();
});
loggerConfig.log("name", "fqcn", null, Level.INFO, new SimpleMessage("msg"), null);
- assertNotSame("propertiesList with substitutions", list, actualListHolder[0]);
+ assertNotSame(list, actualListHolder[0], "propertiesList with substitutions");
@SuppressWarnings("unchecked")
final List<Property> actualList = (List<Property>) actualListHolder[0];
for (int i = 0; i < list.size(); i++) {
- assertEquals("name[" + i + "]", list.get(i).getName(), actualList.get(i).getName());
+ assertEquals(list.get(i).getName(), actualList.get(i).getName(), "name[" + i + "]");
final String value = list.get(i).getValue().replace("${sys:user.name}", System.getProperty("user.name"));
- assertEquals("value[" + i + "]", value, actualList.get(i).getValue());
+ assertEquals(value, actualList.get(i).getValue(), "value[" + i + "]");
}
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/LoggersPluginTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/LoggersPluginTest.java
index dd9e1f6..37d2307 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/LoggersPluginTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/LoggersPluginTest.java
@@ -20,22 +20,18 @@ package org.apache.logging.log4j.core.config;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.junit.LoggerContextSource;
import org.apache.logging.log4j.status.StatusData;
import org.apache.logging.log4j.status.StatusLogger;
-import org.junit.ClassRule;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
/**
* Tests LoggersPlugin.
*/
+@LoggerContextSource("multipleRootLoggersTest.xml")
public class LoggersPluginTest {
- private static final String CONFIG = "multipleRootLoggersTest.xml";
-
- @ClassRule
- public static LoggerContextRule context = new LoggerContextRule(CONFIG);
@Test
public void testEmptyAttribute() throws Exception {
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java
index ba711dd..67e4d3e 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java
@@ -22,43 +22,37 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.junit.jupiter.api.Test;
import static org.apache.logging.log4j.hamcrest.MapMatchers.hasSize;
+import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
-@RunWith(JUnit4.class)
+@LoggerContextSource("missingRootLogger.xml")
public class MissingRootLoggerTest {
- @Rule
- public LoggerContextRule context = new LoggerContextRule("missingRootLogger.xml");
-
@Test
- public void testMissingRootLogger() throws Exception {
- final LoggerContext ctx = context.getLoggerContext();
+ public void testMissingRootLogger(final LoggerContext ctx) throws Exception {
final Logger logger = ctx.getLogger("sample.Logger1");
- assertTrue("Logger should have the INFO level enabled", logger.isInfoEnabled());
- assertFalse("Logger should have the DEBUG level disabled", logger.isDebugEnabled());
+ assertTrue(logger.isInfoEnabled(), "Logger should have the INFO level enabled");
+ assertFalse(logger.isDebugEnabled(), "Logger should have the DEBUG level disabled");
final Configuration config = ctx.getConfiguration();
- assertNotNull("Config not null", config);
+ assertNotNull(config, "Config not null");
// final String MISSINGROOT = "MissingRootTest";
// assertTrue("Incorrect Configuration. Expected " + MISSINGROOT + " but found " + config.getName(),
// MISSINGROOT.equals(config.getName()));
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders not null", map);
+ assertNotNull(map, "Appenders not null");
assertThat("There should only be two appenders", map, hasSize(2));
assertThat(map, hasKey("List"));
assertThat(map, hasKey("DefaultConsole-2"));
final Map<String, LoggerConfig> loggerMap = config.getLoggers();
- assertNotNull("loggerMap not null", loggerMap);
+ assertNotNull(loggerMap, "loggerMap not null");
assertThat("There should only be one configured logger", loggerMap, hasSize(1));
// only the sample logger, no root logger in loggerMap!
assertThat("contains key=sample", loggerMap, hasKey("sample"));
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MultipleTriggeringPolicyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MultipleTriggeringPolicyTest.java
new file mode 100644
index 0000000..088495f
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MultipleTriggeringPolicyTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.config;
+
+import org.apache.logging.log4j.core.appender.RollingFileAppender;
+import org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy;
+import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
+import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;
+import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * Tests related to <a href="https://issues.apache.org/jira/browse/LOG4J2-1100">LOG4J2-1100</a>.
+ */
+class MultipleTriggeringPolicyTest {
+ @Test
+ @LoggerContextSource("LOG4J2-1100/log4j2.xml")
+ void xml(final Configuration configuration) {
+ assertBothTriggeringPoliciesConfigured(configuration);
+ }
+
+ @Test
+ @Tag("json")
+ @LoggerContextSource("LOG4J2-1100/log4j2.json")
+ void json(final Configuration configuration) {
+ assertBothTriggeringPoliciesConfigured(configuration);
+ }
+
+ @Test
+ @Tag("yaml")
+ @LoggerContextSource("LOG4J2-1100/log4j2-good.yaml")
+ void yaml(final Configuration configuration) {
+ assertBothTriggeringPoliciesConfigured(configuration);
+ }
+
+ @Test
+ @Tag("yaml")
+ @Disabled("LOG4J2-1100 demonstration")
+ @LoggerContextSource("LOG4J2-1100/log4j2-good.yaml")
+ void unsupportedYamlSyntax(final Configuration configuration) {
+ assertBothTriggeringPoliciesConfigured(configuration);
+ }
+
+ void assertBothTriggeringPoliciesConfigured(final Configuration configuration) {
+ final RollingFileAppender appender = configuration.getAppender("File");
+ assertNotNull(appender);
+ final CompositeTriggeringPolicy compositeTriggeringPolicy = appender.getTriggeringPolicy();
+ assertNotNull(compositeTriggeringPolicy);
+ final TriggeringPolicy[] triggeringPolicies = compositeTriggeringPolicy.getTriggeringPolicies();
+ assertEquals(2, triggeringPolicies.length);
+ final SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy;
+ final TimeBasedTriggeringPolicy timeBasedTriggeringPolicy;
+ if (triggeringPolicies[0] instanceof SizeBasedTriggeringPolicy) {
+ sizeBasedTriggeringPolicy = (SizeBasedTriggeringPolicy) triggeringPolicies[0];
+ timeBasedTriggeringPolicy = (TimeBasedTriggeringPolicy) triggeringPolicies[1];
+ } else {
+ sizeBasedTriggeringPolicy = (SizeBasedTriggeringPolicy) triggeringPolicies[1];
+ timeBasedTriggeringPolicy = (TimeBasedTriggeringPolicy) triggeringPolicies[0];
+ }
+ assertEquals(7, timeBasedTriggeringPolicy.getInterval());
+ assertEquals(100 * 1024 * 1024, sizeBasedTriggeringPolicy.getMaxFileSize());
+ }
+}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/PropertyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/PropertyTest.java
index 3a82ed2..e021185 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/PropertyTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/PropertyTest.java
@@ -15,40 +15,31 @@ package org.apache.logging.log4j.core.config;/*
* limitations under the license.
*/
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
import java.util.List;
import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.apache.logging.log4j.junit.Named;
import org.apache.logging.log4j.test.appender.ListAppender;
-import org.junit.ClassRule;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
/**
* Test for LOG4J2-1313
* <Property name="" value="" /> not working
*/
+@LoggerContextSource("configPropertyTest.xml")
public class PropertyTest {
- private static final String CONFIG = "configPropertyTest.xml";
-
- @ClassRule
- public static LoggerContextRule context = new LoggerContextRule(CONFIG);
@Test
- public void testEmptyAttribute() throws Exception {
+ public void testEmptyAttribute(@Named("List") final ListAppender app) throws Exception {
final org.apache.logging.log4j.Logger logger = LogManager.getLogger();
logger.info("msg");
- final ListAppender app = (ListAppender) context.getRequiredAppender("List");
- assertNotNull("No ListAppender", app);
-
final List<String> messages = app.getMessages();
- assertNotNull("No Messages", messages);
- assertEquals("message count" + messages, 1, messages.size());
+ assertNotNull(messages, "No Messages");
+ assertEquals(1, messages.size(), "message count" + messages);
//<Property name="emptyElementKey" />
//<Property name="emptyAttributeKey" value="" />
@@ -75,11 +66,11 @@ public class PropertyTest {
@Test
public void testIsValueNeedsLookup() {
- assertTrue("with ${ as value", Property.createProperty("", "${").isValueNeedsLookup());
- assertTrue("with ${ in value", Property.createProperty("", "blah${blah").isValueNeedsLookup());
- assertFalse("empty value", Property.createProperty("", "").isValueNeedsLookup());
- assertFalse("without ${ in value", Property.createProperty("", "blahblah").isValueNeedsLookup());
- assertFalse("without $ in value", Property.createProperty("", "blahb{sys:lah").isValueNeedsLookup());
- assertFalse("without { in value", Property.createProperty("", "blahb$sys:lah").isValueNeedsLookup());
+ assertTrue(Property.createProperty("", "${").isValueNeedsLookup(), "with ${ as value");
+ assertTrue(Property.createProperty("", "blah${blah").isValueNeedsLookup(), "with ${ in value");
+ assertFalse(Property.createProperty("", "").isValueNeedsLookup(), "empty value");
+ assertFalse(Property.createProperty("", "blahblah").isValueNeedsLookup(), "without ${ in value");
+ assertFalse(Property.createProperty("", "blahb{sys:lah").isValueNeedsLookup(), "without $ in value");
+ assertFalse(Property.createProperty("", "blahb$sys:lah").isValueNeedsLookup(), "without { in value");
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ReconfigurationDeadlockTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ReconfigurationDeadlockTest.java
index d1f962e..5a6d5a2 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ReconfigurationDeadlockTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ReconfigurationDeadlockTest.java
@@ -20,20 +20,15 @@ import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.junit.LoggerContextSource;
import org.apache.logging.log4j.message.ThreadDumpMessage;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
-/**
- *
- */
+@LoggerContextSource("reconfiguration-deadlock.xml")
public class ReconfigurationDeadlockTest {
- @Rule
- public LoggerContextRule init = new LoggerContextRule("reconfiguration-deadlock.xml");
private static final int THREAD_COUNT = 5;
private static final boolean[] finished = new boolean[THREAD_COUNT];
private static LoggerThread[] threads = new LoggerThread[THREAD_COUNT];
@@ -77,11 +72,11 @@ public class ReconfigurationDeadlockTest {
threads[i].interrupt();
}
}
- assertFalse("loggerThread didn't finish", stillWaiting);
+ assertFalse(stillWaiting, "loggerThread didn't finish");
}
- private class LoggerThread extends Thread {
+ private static class LoggerThread extends Thread {
private final Logger logger = LogManager.getRootLogger();
private final int index;
@@ -103,7 +98,7 @@ public class ReconfigurationDeadlockTest {
}
}
- private class Updater extends Thread {
+ private static class Updater extends Thread {
public volatile boolean shutdown = false;
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
index f0a820e..e6d8541 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
@@ -39,12 +39,12 @@ import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.apache.logging.log4j.core.filter.CompositeFilter;
import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.junit.After;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
import static org.apache.logging.log4j.hamcrest.MapMatchers.hasSize;
+import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasKey;
@@ -52,12 +52,9 @@ import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.theInstance;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
-/**
- *
- */
-@RunWith(JUnit4.class)
+@Tag("functional")
public class TestConfigurator {
private static final String CONFIG_NAME = "ConfigTest";
@@ -83,7 +80,7 @@ public class TestConfigurator {
"mmmmmmmmmm",
};
- @After
+ @AfterEach
public void cleanup() {
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
if (ctx != null) {
@@ -97,15 +94,15 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-config.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Unexpected Configuration.");
}
@Test
@@ -113,15 +110,15 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", null, new File("target/test-classes/log4j2-config.xml").toURI());
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Unexpected Configuration.");
}
@Test
@@ -132,15 +129,15 @@ public class TestConfigurator {
ctx = Configurator.initialize(null, source);
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Unexpected Configuration.");
}
@Test
@@ -151,15 +148,15 @@ public class TestConfigurator {
ctx = Configurator.initialize(null, source);
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Unexpected Configuration.");
}
@Test
@@ -167,15 +164,15 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "log4j2-config.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Unexpected Configuration.");
}
@Test
@@ -184,15 +181,15 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", null);
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Unexpected Configuration.");
}
@Test
@@ -200,15 +197,15 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "classpath:log4j2-config.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Incorrect Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Incorrect Configuration.");
}
@Test
@@ -216,15 +213,15 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "classloader:log4j2-config.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Incorrect Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Incorrect Configuration.");
}
@Test
@@ -232,28 +229,28 @@ public class TestConfigurator {
ctx = Configurator.initialize("-config", null);
LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Unexpected Configuration.");
}
@Test
public void testReconfiguration() throws Exception {
final File file = new File("target/test-classes/log4j2-config.xml");
- assertTrue("setLastModified should have succeeded.", file.setLastModified(System.currentTimeMillis() - 120000));
+ assertTrue(file.setLastModified(System.currentTimeMillis() - 120000), "setLastModified should have succeeded.");
ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-config.xml");
final Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("Wrong configuration", map, hasKey("List"));
@@ -262,7 +259,7 @@ public class TestConfigurator {
if (!file.setLastModified(System.currentTimeMillis())) {
Thread.sleep(500);
}
- assertTrue("setLastModified should have succeeded.", file.setLastModified(System.currentTimeMillis()));
+ assertTrue(file.setLastModified(System.currentTimeMillis()), "setLastModified should have succeeded.");
TimeUnit.SECONDS.sleep(config.getWatchManager().getIntervalSeconds()+1);
for (int i = 0; i < 17; ++i) {
logger.debug("Test message " + i);
@@ -277,7 +274,7 @@ public class TestConfigurator {
assertThat("Configuration not reset", newConfig, is(not(theInstance(config))));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
- assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+ assertEquals(NullConfiguration.NULL_NAME, config.getName(), "Unexpected Configuration.");
}
@@ -286,19 +283,19 @@ public class TestConfigurator {
ctx = Configurator.initialize("-config", null);
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals(CONFIG_NAME, config.getName(), "Incorrect Configuration.");
final Map<String, Appender> map = config.getAppenders();
- assertNotNull("Appenders map should not be null.", map);
+ assertNotNull(map, "Appenders map should not be null.");
assertThat(map, hasSize(greaterThan(0)));
assertThat("No ListAppender named List2", map, hasKey("List2"));
final Appender app = map.get("List2");
final Layout<? extends Serializable> layout = app.getLayout();
- assertNotNull("Appender List2 does not have a Layout", layout);
+ assertNotNull(layout, "Appender List2 does not have a Layout");
assertThat("Appender List2 is not configured with a PatternLayout", layout, instanceOf(PatternLayout.class));
final String pattern = ((PatternLayout) layout).getConversionPattern();
- assertNotNull("No conversion pattern for List2 PatternLayout", pattern);
- assertFalse("Environment variable was not substituted", pattern.startsWith("${env:PATH}"));
+ assertNotNull(pattern, "No conversion pattern for List2 PatternLayout");
+ assertFalse(pattern.startsWith("${env:PATH}"), "Environment variable was not substituted");
}
@Test
@@ -306,9 +303,9 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "bad/log4j-loggers.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
+ assertNotNull(config, "No configuration");
final String name = DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(config.hashCode());
- assertEquals("Unexpected Configuration.", name, config.getName());
+ assertEquals(name, config.getName(), "Unexpected Configuration.");
}
@Test
@@ -316,11 +313,11 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "bad/log4j-status.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Unexpected Configuration", "XMLConfigTest", config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals("XMLConfigTest", config.getName(), "Unexpected Configuration");
final LoggerConfig root = config.getLoggerConfig("");
- assertNotNull("No Root Logger", root);
- assertTrue("Expected error level, was " + root.getLevel(), Level.ERROR == root.getLevel());
+ assertNotNull(root, "No Root Logger");
+ assertSame(Level.ERROR, root.getLevel(), "Expected error level, was " + root.getLevel());
}
@Test
@@ -328,12 +325,12 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "bad/log4j-badfilterparam.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Unexpected Configuration", "XMLConfigTest", config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals("XMLConfigTest", config.getName(), "Unexpected Configuration");
final LoggerConfig lcfg = config.getLoggerConfig("org.apache.logging.log4j.test1");
- assertNotNull("No Logger", lcfg);
+ assertNotNull(lcfg, "No Logger");
final Filter filter = lcfg.getFilter();
- assertNull("Unexpected Filter", filter);
+ assertNull(filter, "Unexpected Filter");
}
@Test
@@ -341,14 +338,14 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "bad/log4j-nofilter.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Unexpected Configuration", "XMLConfigTest", config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals("XMLConfigTest", config.getName(), "Unexpected Configuration");
final LoggerConfig lcfg = config.getLoggerConfig("org.apache.logging.log4j.test1");
- assertNotNull("No Logger", lcfg);
+ assertNotNull(lcfg, "No Logger");
final Filter filter = lcfg.getFilter();
- assertNotNull("No Filter", filter);
+ assertNotNull(filter, "No Filter");
assertThat(filter, instanceOf(CompositeFilter.class));
- assertTrue("Unexpected filters", ((CompositeFilter) filter).isEmpty());
+ assertTrue(((CompositeFilter) filter).isEmpty(), "Unexpected filters");
}
@Test
@@ -356,8 +353,8 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "bad/log4j-badlayout.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Unexpected Configuration", "XMLConfigTest", config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals("XMLConfigTest", config.getName(), "Unexpected Configuration");
}
@Test
@@ -373,8 +370,8 @@ public class TestConfigurator {
ctx = Configurator.initialize("Test1", "bad/log4j-badfilename.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Unexpected Configuration", "XMLConfigTest", config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals("XMLConfigTest", config.getName(), "Unexpected Configuration");
assertThat(config.getAppenders(), hasSize(equalTo(2)));
}
@@ -398,8 +395,8 @@ public class TestConfigurator {
builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));
ctx = Configurator.initialize(builder.build());
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Unexpected Configuration", "BuilderTest", config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals("BuilderTest", config.getName(), "Unexpected Configuration");
assertThat(config.getAppenders(), hasSize(equalTo(1)));
}
@@ -438,8 +435,8 @@ public class TestConfigurator {
.add( builder.newAppenderRef( "rolling" ) ) );
final Configuration config = builder.build();
config.initialize();
- assertNotNull("No rolling file appender", config.getAppender("rolling"));
- assertEquals("Unexpected Configuration", "RollingBuilder", config.getName());
+ assertNotNull(config.getAppender("rolling"), "No rolling file appender");
+ assertEquals("RollingBuilder", config.getName(), "Unexpected Configuration");
// Initialize the new configuration
final LoggerContext ctx = Configurator.initialize( config );
Configurator.shutdown(ctx);
@@ -478,11 +475,11 @@ public class TestConfigurator {
builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));
ctx = Configurator.initialize(builder.build());
final Configuration config = ctx.getConfiguration();
- assertNotNull("No configuration", config);
- assertEquals("Unexpected Configuration", "BuilderTest", config.getName());
+ assertNotNull(config, "No configuration");
+ assertEquals("BuilderTest", config.getName(), "Unexpected Configuration");
assertThat(config.getAppenders(), hasSize(equalTo(1)));
- assertNotNull("Filter script not found", config.getScriptManager().getScript("filter.groovy"));
- assertNotNull("pattern selector script not found", config.getScriptManager().getScript("selectorScript"));
+ assertNotNull(config.getScriptManager().getScript("filter.groovy"), "Filter script not found");
+ assertNotNull(config.getScriptManager().getScript("selectorScript"), "pattern selector script not found");
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
index 5699f42..4c5021c 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
@@ -17,28 +17,22 @@
package org.apache.logging.log4j.core.config;
import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.junit.LoggerContextFactoryExtension;
import org.apache.logging.log4j.simple.SimpleLoggerContextFactory;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.assertNull;
-/**
- *
- */
public class TestConfiguratorError {
- private static final String FACTORY_PROPERTY_NAME = "log4j2.loggerContextFactory";
-
- @BeforeClass
- public static void beforeClass() {
- System.setProperty(FACTORY_PROPERTY_NAME, SimpleLoggerContextFactory.class.getName());
- }
+ @RegisterExtension
+ static final LoggerContextFactoryExtension extension = new LoggerContextFactoryExtension(new SimpleLoggerContextFactory());
@Test
public void testErrorNoClassLoader() throws Exception {
try (final LoggerContext ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-config.xml")) {
- assertNull("No LoggerContext should have been returned", ctx);
+ assertNull(ctx, "No LoggerContext should have been returned");
}
}
@@ -46,7 +40,7 @@ public class TestConfiguratorError {
public void testErrorNullClassLoader() throws Exception {
try (final LoggerContext ctx = Configurator.initialize("Test1", null,
"target/test-classes/log4j2-config.xml")) {
- assertNull("No LoggerContext should have been returned", ctx);
+ assertNull(ctx, "No LoggerContext should have been returned");
}
}
}