You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2022/01/12 13:01:54 UTC

[logging-log4j2] branch master updated: Add Source constructors for Path and URL; add unit test case class.

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

ggregory 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 fe562a1  Add Source constructors for Path and URL; add unit test case class.
fe562a1 is described below

commit fe562a1704f90711b8986826d95173d2c7ba0923
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jan 12 06:58:22 2022 -0500

    Add Source constructors for Path and URL; add unit test case class.
---
 .../apache/logging/log4j/core/util/SourceTest.java | 163 +++++++++++++++++++++
 .../org/apache/logging/log4j/core/util/Source.java | 152 +++++++++++++++----
 2 files changed, 287 insertions(+), 28 deletions(-)

diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/SourceTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/SourceTest.java
new file mode 100644
index 0000000..d0bc542
--- /dev/null
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/SourceTest.java
@@ -0,0 +1,163 @@
+/*
+ * 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.util;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link Source}.
+ */
+public class SourceTest {
+
+    @Test
+    public void testEqualityFile() {
+        assertEquals(new Source(new File("foo")), new Source(new File("foo")));
+        assertEquals(new Source(new File("foo")), new Source(new File("./foo")));
+        assertEquals(new Source(new File("foo.txt")), new Source(new File("./foo.txt")));
+    }
+
+    @Test
+    public void testEqualityPath() {
+        assertEquals(new Source(Paths.get("foo")), new Source(Paths.get("foo")));
+        assertEquals(new Source(Paths.get("foo")), new Source(Paths.get("./foo")));
+        assertEquals(new Source(Paths.get("foo.txt")), new Source(Paths.get("./foo.txt")));
+    }
+
+    @Test
+    @Disabled("File URI is broken.")
+    public void testEqualityURIFile() {
+        assertEquals(new Source(Paths.get("foo").toUri()), new Source(Paths.get("./foo").toUri()));
+    }
+
+    @Test
+    public void testEqualityURIHttp() {
+        assertEquals(new Source(URI.create("http://www.apache.org/index.html")), new Source(URI.create("http://www.apache.org/index.html")));
+        assertEquals(new Source(URI.create("http://www.apache.org/")), new Source(URI.create("http://www.apache.org////")));
+        assertEquals(new Source(URI.create("http://www.apache.org/")), new Source(URI.create("http://www.apache.org/./././.")));
+    }
+
+    public void testEqualityURLFile() throws MalformedURLException {
+        assertEquals(new Source(Paths.get("foo").toUri().toURL()), new Source(Paths.get("./foo").toUri().toURL()));
+    }
+
+    public void testEqualityURLHttp() throws MalformedURLException {
+        assertEquals(new Source(URI.create("http://www.apache.org/index.html").toURL()), new Source(URI.create("http://www.apache.org/index.html").toURL()));
+        assertEquals(new Source(URI.create("http://www.apache.org").toURL()), new Source(URI.create("http://www.apache.org////").toURL()));
+        assertEquals(new Source(URI.create("http://www.apache.org").toURL()), new Source(URI.create("http://www.apache.org/./././.").toURL()));
+    }
+
+    public void testEqualityURLHttps() throws MalformedURLException {
+        assertEquals(new Source(URI.create("https://www.apache.org/index.html").toURL()), new Source(URI.create("https://www.apache.org/index.html").toURL()));
+        assertEquals(new Source(URI.create("https://www.apache.org").toURL()), new Source(URI.create("https://www.apache.org////").toURL()));
+        assertEquals(new Source(URI.create("https://www.apache.org").toURL()), new Source(URI.create("https://www.apache.org/./././.").toURL()));
+    }
+
+    @Test
+    public void testFileConstructor() {
+        final Path path = Paths.get("foo");
+        final URI uri = path.toUri();
+        final File file = path.toFile();
+        final Source source = new Source(file);
+        assertEquals(file, source.getFile());
+        assertEquals(path, source.getFile().toPath());
+        assertEquals(path, source.getPath());
+        assertEquals(uri, source.getURI());
+    }
+
+    @Test
+    public void testPathStringConstructor() {
+        final Path path = Paths.get("foo");
+        final URI uri = path.toUri();
+        final File file = path.toFile();
+        final Source source = new Source(path);
+        assertEquals(file, source.getFile());
+        assertEquals(path, source.getFile().toPath());
+        assertEquals(path, source.getPath());
+        assertEquals(uri, source.getURI());
+    }
+
+    public void testPathURIFileConstructor() {
+        final Path path = Paths.get(URI.create("file:///C:/foo"));
+        final URI uri = path.toUri();
+        final File file = path.toFile();
+        final Source source = new Source(path);
+        assertEquals(file, source.getFile());
+        assertEquals(path, source.getFile().toPath());
+        assertEquals(path, source.getPath());
+        assertEquals(uri, source.getURI());
+    }
+
+    @Test
+    public void testURIConstructor() throws MalformedURLException {
+        final Path path = Paths.get("foo");
+        final URI uri = path.toUri();
+        final File file = path.toFile();
+        final Source source = new Source(uri);
+        assertEquals(file.getAbsoluteFile(), source.getFile());
+        assertEquals(uri.toString(), source.getLocation());
+        assertEquals(path.toAbsolutePath(), source.getPath());
+    }
+
+    @Test
+    public void testURIFileConstructor() throws MalformedURLException {
+        final URI uri = URI.create("file:///C:/foo");
+        final Path path = Paths.get(uri);
+        final File file = path.toFile();
+        final Source source = new Source(uri);
+        assertEquals(file.getAbsoluteFile(), source.getFile());
+        assertEquals(uri.toString(), source.getLocation());
+    }
+
+    @Test
+    public void testURIHttpConstructor() throws MalformedURLException {
+        final URI uri = URI.create("http://www.apache.org");
+        final Source source = new Source(uri);
+        assertEquals(null, source.getFile());
+        assertEquals(uri.toString(), source.getLocation());
+    }
+
+    @Test
+    public void testURIHttpsConstructor() throws MalformedURLException {
+        final URI uri = URI.create("https://www.apache.org");
+        final Source source = new Source(uri);
+        assertEquals(null, source.getFile());
+        assertEquals(uri.toString(), source.getLocation());
+    }
+
+    @Test
+    public void testURLConstructor() throws MalformedURLException {
+        final Path path = Paths.get("foo");
+        final File file = path.toFile();
+        final URI uri = path.toUri();
+        final URL url = uri.toURL();
+        final Source source = new Source(url);
+        assertEquals(file.getAbsoluteFile(), source.getFile());
+        assertEquals(url.toString(), source.getLocation());
+        assertEquals(path.toAbsolutePath(), source.getPath());
+    }
+}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Source.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Source.java
index 1b0e65f..7d095ff 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Source.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Source.java
@@ -17,17 +17,54 @@
 
 package org.apache.logging.log4j.core.util;
 
-import org.apache.logging.log4j.core.config.ConfigurationSource;
-
 import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Objects;
 
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+
 /**
  * Represents the source for the logging configuration as an immutable object.
  */
 public class Source {
 
+    private static String normalize(final File file) {
+        try {
+            return file.getCanonicalFile().getAbsolutePath();
+        } catch (final IOException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
+    private static File toFile(final Path path) {
+        try {
+            return Objects.requireNonNull(path, "path").toFile();
+        } catch (final UnsupportedOperationException e) {
+            return null;
+        }
+    }
+
+    private static File toFile(final URI uri) {
+        try {
+            return toFile(Paths.get(Objects.requireNonNull(uri, "uri")));
+        } catch (final Exception e) {
+            return null;
+        }
+    }
+
+    private static URI toURI(final URL url) {
+        try {
+            return Objects.requireNonNull(url, "url").toURI();
+        } catch (final URISyntaxException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
     private final File file;
     private final URI uri;
     private final String location;
@@ -50,9 +87,33 @@ public class Source {
      * @param file the file where the input stream originated
      */
     public Source(final File file) {
-        this.file = Objects.requireNonNull(file, "file is null");
-        this.location = file.getAbsolutePath();
-        this.uri = null;
+        this.file = Objects.requireNonNull(file, "file");
+        this.location = normalize(file);
+        this.uri = file.toURI();
+    }
+
+    /**
+     * Constructs a new {@code Source} from the specified Path.
+     *
+     * @param path the Path where the input stream originated
+     */
+    public Source(final Path path) {
+        final Path normPath = Objects.requireNonNull(path, "path").normalize();
+        this.file = toFile(normPath);
+        this.uri = normPath.toUri();
+        this.location = normPath.toString();
+    }
+
+    /**
+     * Constructs a new {@code Source} from the specified URI.
+     *
+     * @param uri the URI where the input stream originated
+     */
+    public Source(final URI uri) {
+        final URI normUri = Objects.requireNonNull(uri, "uri").normalize();
+        this.uri = normUri;
+        this.location = normUri.toString();
+        this.file = toFile(normUri);
     }
 
     /**
@@ -60,11 +121,36 @@ public class Source {
      *
      * @param uri the URI where the input stream originated
      * @param lastModified Not used.
+     * @deprecated Use {@link Source#Source(URI)}.
      */
+    @Deprecated
     public Source(final URI uri, final long lastModified) {
-        this.uri = Objects.requireNonNull(uri, "URI is null");
+        this(uri);
+    }
+
+    /**
+     * Constructs a new {@code Source} from the specified URL.
+     *
+     * @param url the URL where the input stream originated
+     * @throws IllegalArgumentException if this URL is not formatted strictly according to to RFC2396 and cannot be
+     *         converted to a URI.
+     */
+    public Source(final URL url) {
+        this.uri = toURI(url);
         this.location = uri.toString();
-        this.file = null;
+        this.file = toFile(uri);
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof Source)) {
+            return false;
+        }
+        final Source source = (Source) o;
+        return Objects.equals(location, source.location);
     }
 
     /**
@@ -78,6 +164,25 @@ public class Source {
     }
 
     /**
+     * Gets a string describing the configuration source file or URI, or {@code null} if this configuration source
+     * has neither a file nor an URI.
+     *
+     * @return a string describing the configuration source file or URI, or {@code null}
+     */
+    public String getLocation() {
+        return location;
+    }
+
+    /**
+     * Gets this source as a Path.
+     *
+     * @return this source as a Path.
+     */
+    public Path getPath() {
+        return file != null ? file.toPath() : uri != null ? Paths.get(uri) : Paths.get(location);
+    }
+
+    /**
      * Gets the configuration source URI, or {@code null} if this configuration source is based on a file or has
      * neither a file nor an URI.
      *
@@ -88,34 +193,25 @@ public class Source {
     }
 
     /**
-     * Gets a string describing the configuration source file or URI, or {@code null} if this configuration source
-     * has neither a file nor an URI.
+     * Gets the configuration source URL.
      *
-     * @return a string describing the configuration source file or URI, or {@code null}
+     * @return the configuration source URI, or {@code null}
      */
-    public String getLocation() {
-        return location;
-    }
-
-    @Override
-    public String toString() {
-        return location;
+    public URL getURL() {
+        try {
+            return uri.toURL();
+        } catch (final MalformedURLException e) {
+            throw new IllegalStateException(e);
+        }
     }
 
     @Override
-    public boolean equals(final Object o) {
-        if (this == o) {
-            return true;
-        }
-        if (!(o instanceof Source)) {
-            return false;
-        }
-        final Source source = (Source) o;
-        return Objects.equals(location, source.location);
+    public int hashCode() {
+        return 31 + Objects.hashCode(location);
     }
 
     @Override
-    public int hashCode() {
-        return Objects.hash(location);
+    public String toString() {
+        return location;
     }
 }