Skip to content

Commit

Permalink
Add Source constructors for Path and URL; add unit test case class.
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jan 12, 2022
1 parent 61b02d8 commit 6118187
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 27 deletions.
150 changes: 123 additions & 27 deletions log4j-core/src/main/java/org/apache/logging/log4j/core/util/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
package org.apache.logging.log4j.core.util;

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;
Expand All @@ -28,6 +34,37 @@
*/
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;
Expand All @@ -37,7 +74,7 @@ public class Source {
*
* @param source The ConfigurationSource.
*/
public Source(ConfigurationSource source) {
public Source(final ConfigurationSource source) {
this.file = source.getFile();
this.uri = source.getURI();
this.location = source.getLocation();
Expand All @@ -50,21 +87,70 @@ public Source(ConfigurationSource 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);
}

/**
* Constructs a new {@code Source} from the specified URI.
*
* @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);
}

/**
Expand All @@ -77,6 +163,25 @@ public File getFile() {
return file;
}

/**
* 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.
Expand All @@ -88,34 +193,25 @@ public URI getURI() {
}

/**
* 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;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
public URL getURL() {
try {
return uri.toURL();
} catch (final MalformedURLException e) {
throw new IllegalStateException(e);
}
if (!(o instanceof Source)) {
return false;
}
Source source = (Source) o;
return Objects.equals(location, source.location);
}

@Override
public int hashCode() {
return 31 + Objects.hashCode(location);
}

@Override
public String toString() {
return location;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 6118187

Please sign in to comment.