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 2022/02/04 04:29:08 UTC
[logging-log4j2] branch master updated: Use more descriptive assertion impls
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 cff5eb9 Use more descriptive assertion impls
cff5eb9 is described below
commit cff5eb9c8dffe9dd75e9eb0f8afb43e83f0c2649
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Thu Feb 3 22:28:47 2022 -0600
Use more descriptive assertion impls
---
.../plugins/util/PluginManagerPackagesTest.java | 108 +++++++++------------
1 file changed, 46 insertions(+), 62 deletions(-)
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/util/PluginManagerPackagesTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/util/PluginManagerPackagesTest.java
index 86eb727..99b110f 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/util/PluginManagerPackagesTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/util/PluginManagerPackagesTest.java
@@ -16,39 +16,35 @@
*/
package org.apache.logging.log4j.core.config.plugins.util;
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Locale;
-
-import javax.tools.Diagnostic;
-import javax.tools.DiagnosticCollector;
-import javax.tools.JavaCompiler;
-import javax.tools.JavaFileObject;
-import javax.tools.StandardJavaFileManager;
-import javax.tools.ToolProvider;
-
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.Configurator;
-import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.core.test.appender.ListAppender;
+import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import javax.tools.Diagnostic;
+import javax.tools.DiagnosticCollector;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
public class PluginManagerPackagesTest {
- private static final String TEST_SOURCE = "target/test-classes/customPlugin/FixedStringLayout.java";
- private static Configuration config;
- private static ListAppender listAppender;
+ private static final Path TEST_SOURCE = Path.of("target", "test-classes", "customPlugin", "FixedStringLayout.java");
private static LoggerContext ctx;
@AfterAll
@@ -61,16 +57,9 @@ public class PluginManagerPackagesTest {
}
@AfterAll
- public static void afterClass() {
- File file = new File(TEST_SOURCE);
- File parent = file.getParentFile();
- if (file.exists()) {
- file.delete();
- }
- file = new File(parent, "FixedStringLayout.class");
- if (file.exists()) {
- file.delete();
- }
+ public static void afterClass() throws Exception {
+ Files.deleteIfExists(TEST_SOURCE);
+ Files.deleteIfExists(TEST_SOURCE.resolveSibling("FixedStringLayout.class"));
}
@Test
@@ -79,11 +68,10 @@ public class PluginManagerPackagesTest {
// To ensure our custom plugin is NOT included in the log4j plugin metadata file,
// we make sure the class does not exist until after the build is finished.
// So we don't create the custom plugin class until this test is run.
- final File orig = new File(TEST_SOURCE + ".source");
- final File f = new File(orig.getParentFile(), "FixedStringLayout.java");
- assertTrue(orig.renameTo(f), "renamed source file failed");
- compile(f);
- assertTrue(f.renameTo(orig), "reverted source file failed");
+ final Path orig = TEST_SOURCE.resolveSibling("FixedStringLayout.java.source");
+ final Path source = Files.move(orig, TEST_SOURCE);
+ compile(source);
+ Files.move(source, orig);
// load the compiled class
Class.forName("customplugin.FixedStringLayout");
@@ -91,45 +79,41 @@ public class PluginManagerPackagesTest {
// now that the custom plugin class exists, we load the config
// with the packages element pointing to our custom plugin
ctx = Configurator.initialize("Test1", "customplugin/log4j2-741.xml");
- config = ctx.getConfiguration();
- listAppender = config.getAppender("List");
+ Configuration config = ctx.getConfiguration();
+ ListAppender listAppender = config.getAppender("List");
final Logger logger = LogManager.getLogger(PluginManagerPackagesTest.class);
logger.info("this message is ignored");
- final List<String> messages = listAppender.getMessages();
- assertEquals(1, messages.size(), messages.toString());
- assertEquals("abc123XYZ", messages.get(0));
+ assertThat(listAppender.getMessages()).hasSize(1).hasSameElementsAs(List.of("abc123XYZ"));
}
- static void compile(final File f) throws IOException {
+ static void compile(final Path path) {
// set up compiler
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
final List<String> errors = new ArrayList<>();
- try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
- final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(
- Collections.singletonList(f));
- String classPath = System.getProperty("jdk.module.path");
- List<String> options = new ArrayList<>();
- if (Strings.isNotBlank(classPath)) {
- options.add("-classpath");
- options.add(classPath);
- }
- options.add("-proc:none");
- // compile generated source
- compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits).call();
+ assertDoesNotThrow(() -> {
+ try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
+ final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromPaths(List.of(path));
+ String classPath = System.getProperty("jdk.module.path");
+ List<String> options = new ArrayList<>();
+ if (Strings.isNotBlank(classPath)) {
+ options.add("-classpath");
+ options.add(classPath);
+ }
+ options.add("-proc:none");
+ // compile generated source
+ compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits).call();
- // check we don't have any compilation errors
- for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
- if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
- errors.add(String.format("Compile error: %s%n", diagnostic.getMessage(Locale.getDefault())));
+ // check we don't have any compilation errors
+ for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
+ if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
+ errors.add(String.format("Compile error: %s%n", diagnostic.getMessage(Locale.getDefault())));
+ }
}
}
- } catch (RuntimeException ex) {
- ex.printStackTrace();
- fail(ex.getMessage());
- }
- assertTrue(errors.isEmpty(), errors.toString());
+ });
+ assertThat(errors).isEmpty();
}
}