You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2024/02/21 14:21:24 UTC

(logging-log4j-tools) 04/29: Simplify test resource access

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j-tools.git

commit 6e04662174a2d4e37a5b9bfe8f8d09dfa0c49c7f
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Tue Feb 13 10:50:53 2024 +0100

    Simplify test resource access
---
 .../org/apache/logging/log4j/docgen/TestUtils.java | 58 ----------------------
 .../docgen/internal/FreeMarkerGeneratorTest.java   |  9 ++--
 .../log4j/docgen/internal/SchemaGeneratorTest.java |  7 +--
 .../docgen/processor/AsciidocConverterTest.java    | 22 ++++----
 .../docgen/processor/DocGenProcessorTest.java      |  7 +--
 5 files changed, 16 insertions(+), 87 deletions(-)

diff --git a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/TestUtils.java b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/TestUtils.java
deleted file mode 100644
index 3bd9def..0000000
--- a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/TestUtils.java
+++ /dev/null
@@ -1,58 +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.docgen;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-
-public final class TestUtils {
-
-    private TestUtils() {}
-
-    public static Path resourcePath(final String name) {
-        final URI uri = resourceUri(name);
-        return Paths.get(uri);
-    }
-
-    public static URI resourceUri(final String name) {
-        final URL url = resourceUrl(name);
-        try {
-            return url.toURI();
-        } catch (URISyntaxException error) {
-            final String message = String.format("failed to obtain URI from URL `%s`", url);
-            throw new RuntimeException(message, error);
-        }
-    }
-
-    public static InputStream resourceStream(final String name) throws IOException {
-        final URL url = resourceUrl(name);
-        return url.openStream();
-    }
-
-    public static URL resourceUrl(final String name) {
-        final URL url = TestUtils.class.getResource(name);
-        assertThat(url).as("URL for resource `%s`", name).isNotNull();
-        return url;
-    }
-}
diff --git a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/internal/FreeMarkerGeneratorTest.java b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/internal/FreeMarkerGeneratorTest.java
index 7b36bca..1648607 100644
--- a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/internal/FreeMarkerGeneratorTest.java
+++ b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/internal/FreeMarkerGeneratorTest.java
@@ -16,8 +16,6 @@
  */
 package org.apache.logging.log4j.docgen.internal;
 
-import static org.apache.logging.log4j.docgen.TestUtils.resourcePath;
-import static org.apache.logging.log4j.docgen.TestUtils.resourceStream;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.nio.charset.StandardCharsets;
@@ -26,7 +24,6 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.stream.Stream;
 import org.apache.logging.log4j.docgen.PluginSet;
-import org.apache.logging.log4j.docgen.TestUtils;
 import org.apache.logging.log4j.docgen.freemarker.FreeMarkerGenerator;
 import org.apache.logging.log4j.docgen.freemarker.FreeMarkerGeneratorRequest;
 import org.apache.logging.log4j.docgen.io.stax.PluginBundleStaxReader;
@@ -37,9 +34,9 @@ public class FreeMarkerGeneratorTest {
     @Test
     void generatePluginDocumentation() throws Exception {
         final PluginBundleStaxReader reader = new PluginBundleStaxReader();
-        final PluginSet set = reader.read(resourceStream("/META-INF/log4j/plugins-sample.xml"));
-        final Path templateDir = TestUtils.resourcePath("/templates");
-        final Path expectedDirectory = resourcePath("/expected/freemarker");
+        final PluginSet set = reader.read("src/test/resources/META-INF/log4j/plugins-sample.xml");
+        final Path templateDir = Paths.get("src/test/resources/templates");
+        final Path expectedDirectory = Paths.get("src/test/resources/expected/freemarker");
         final Path outputDirectory = Paths.get("target/test-site/freemarker");
 
         final FreeMarkerGenerator generator = new DefaultFreeMarkerGenerator();
diff --git a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/internal/SchemaGeneratorTest.java b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/internal/SchemaGeneratorTest.java
index 70c0c1a..149e0a7 100644
--- a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/internal/SchemaGeneratorTest.java
+++ b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/internal/SchemaGeneratorTest.java
@@ -16,12 +16,9 @@
  */
 package org.apache.logging.log4j.docgen.internal;
 
-import static org.apache.logging.log4j.docgen.TestUtils.resourceStream;
-import static org.apache.logging.log4j.docgen.TestUtils.resourceUrl;
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 
 import java.io.IOException;
-import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -41,7 +38,7 @@ public class SchemaGeneratorTest {
     void schemaGeneration() {
         final Source xmlSchema =
                 Input.fromURI("https://www.w3.org/2001/XMLSchema.xsd").build();
-        final URL expectedSchema = resourceUrl("/expected/xsd/log4j-config.xsd");
+        final Path expectedSchema = Paths.get("src/test/resources/expected/xsd/log4j-config.xsd");
         final Path actualSchema = assertDoesNotThrow(SchemaGeneratorTest::generateSchema);
         XmlAssert.assertThat(actualSchema)
                 .isValidAgainst(xmlSchema)
@@ -53,7 +50,7 @@ public class SchemaGeneratorTest {
 
     private static Path generateSchema() throws XMLStreamException, IOException {
         final PluginBundleStaxReader reader = new PluginBundleStaxReader();
-        final PluginSet set = reader.read(resourceStream("/META-INF/log4j/plugins-sample.xml"));
+        final PluginSet set = reader.read("src/test/resources/META-INF/log4j/plugins-sample.xml");
         final SchemaGenerator generator = new DefaultSchemaGenerator();
         final SchemaGeneratorRequest request = new SchemaGeneratorRequest();
         final Path outputDirectory = Paths.get("target/test-site/xsd");
diff --git a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/AsciidocConverterTest.java b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/AsciidocConverterTest.java
index d4191a9..76af48d 100644
--- a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/AsciidocConverterTest.java
+++ b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/AsciidocConverterTest.java
@@ -17,7 +17,6 @@
 package org.apache.logging.log4j.docgen.processor;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.apache.logging.log4j.docgen.TestUtils.resourcePath;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.IOException;
@@ -28,7 +27,6 @@ import java.nio.file.Paths;
 import java.util.List;
 import java.util.Locale;
 import java.util.Set;
-import java.util.stream.Collectors;
 import javax.lang.model.SourceVersion;
 import javax.lang.model.element.Element;
 import javax.tools.Diagnostic;
@@ -48,31 +46,29 @@ import org.junit.jupiter.api.Test;
 
 public class AsciidocConverterTest {
 
-    private static final Path LICENSE_PATH = resourcePath("/templates/license.ftl");
+    private static final Path LICENSE_PATH = Paths.get("src/test/resources/templates/license.ftl");
 
     @Test
     void convertToAsciidoc() throws Exception {
         final DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
-        final DiagnosticCollector<JavaFileObject> ds = new DiagnosticCollector<>();
+        final DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
         final StandardJavaFileManager fileManager = tool.getStandardFileManager(null, Locale.ROOT, UTF_8);
 
-        final Path basePath = Paths.get(System.getProperty("basedir", "."));
-        final Path sourcePath = resourcePath("/processor/asciidoc/example/JavadocExample.java");
+        final Path sourcePath = Paths.get("src/test/resources/processor/asciidoc/example/JavadocExample.java");
         final Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjects(sourcePath);
 
+        final Path basePath = Paths.get(System.getProperty("basedir", "."));
         final Path destPath = basePath.resolve("target/test-site");
         Files.createDirectories(destPath);
         fileManager.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Set.of(destPath));
 
-        final DocumentationTask task = tool.getTask(null, fileManager, ds, TestDoclet.class, null, sources);
+        final DocumentationTask task =
+                tool.getTask(null, fileManager, diagnosticCollector, TestDoclet.class, null, sources);
         task.call();
 
-        final List<String> warnings = ds.getDiagnostics().stream()
-                .filter(d -> d.getKind() != Diagnostic.Kind.NOTE)
-                .map(d -> d.getMessage(Locale.ROOT))
-                .collect(Collectors.toList());
-        assertThat(warnings).isEmpty();
-        final Path expectedPath = resourcePath("/expected/processor/JavadocExample.adoc");
+        List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticCollector.getDiagnostics();
+        assertThat(diagnostics).noneMatch(diagnostic -> diagnostic.getKind() != Diagnostic.Kind.NOTE);
+        final Path expectedPath = Paths.get("src/test/resources/expected/processor/JavadocExample.adoc");
         assertThat(destPath.resolve("processor/JavadocExample.adoc")).hasSameTextualContentAs(expectedPath, UTF_8);
     }
 
diff --git a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/DocGenProcessorTest.java b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/DocGenProcessorTest.java
index 5b1db38..e2b6af3 100644
--- a/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/DocGenProcessorTest.java
+++ b/log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/DocGenProcessorTest.java
@@ -16,12 +16,9 @@
  */
 package org.apache.logging.log4j.docgen.processor;
 
-import static org.apache.logging.log4j.docgen.TestUtils.resourcePath;
-import static org.apache.logging.log4j.docgen.TestUtils.resourceUrl;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 
-import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -55,7 +52,7 @@ public class DocGenProcessorTest {
     void descriptorGenerationSucceeds(final String version) {
         final Path basePath = Paths.get(System.getProperty("basedir", "."));
         final Path schema = basePath.resolve("target/generated-site/resources/xsd/plugins-0.1.0.xsd");
-        final URL expected = resourceUrl("/expected/processor/META-INF/log4j/plugins.xml");
+        final Path expected = Paths.get("src/test/resources/expected/processor/META-INF/log4j/plugins.xml");
         final Path actual = assertDoesNotThrow(() -> generateDescriptor(version));
         XmlAssert.assertThat(actual)
                 .isValidAgainst(schema)
@@ -72,7 +69,7 @@ public class DocGenProcessorTest {
                 compiler.getStandardFileManager(null, Locale.ROOT, StandardCharsets.UTF_8);
 
         final Path basePath = Paths.get(System.getProperty("basedir", "."));
-        final Path sourcePath = resourcePath("/processor/" + version);
+        final Path sourcePath = Paths.get("src/test/resources/processor/" + version);
         final Iterable<? extends JavaFileObject> sources;
         try (final Stream<Path> files = Files.walk(sourcePath)) {
             sources = fileManager.getJavaFileObjects(