Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetty 12 - Add tests in util/resource for alternate FileSystem implementations #9149

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions jetty-core/jetty-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@
public final class URIUtil
{
private static final Logger LOG = LoggerFactory.getLogger(URIUtil.class);
private static final Index<String> KNOWN_SCHEMES = new Index.Builder<String>()
.caseSensitive(false)
.with("file:")
.with("jrt:")
.with("jar:")
.build();

// From https://www.rfc-editor.org/rfc/rfc3986
private static final String UNRESERVED = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.ProviderNotFoundException;
import java.util.List;
Expand Down Expand Up @@ -48,10 +49,23 @@ class ResourceFactoryInternals
CURRENT_WORKING_DIR = Path.of(System.getProperty("user.dir"));

// The default resource factories
RESOURCE_FACTORIES.put("jar", new MountedPathResourceFactory());
MountedPathResourceFactory mountedPathResourceFactory = new MountedPathResourceFactory();
RESOURCE_FACTORIES.put("jar", mountedPathResourceFactory);
PathResourceFactory pathResourceFactory = new PathResourceFactory();
RESOURCE_FACTORIES.put("file", pathResourceFactory);
RESOURCE_FACTORIES.put("jrt", pathResourceFactory);

/* Best effort attempt to detect that an alternate FileSystem type that is in use.
* We don't attempt to look up a Class, as not all runtimes and environments have the classes anymore
* (eg: they were compiled into native code)
* The build.properties is present in the jetty-util jar, so it's reasonably safe to look for that
* as a resource
*/
URL url = ResourceFactoryInternals.class.getResource("/org/eclipse/jetty/version/build.properties");
joakime marked this conversation as resolved.
Show resolved Hide resolved
if ((url != null) && !RESOURCE_FACTORIES.contains(url.getProtocol()))
{
RESOURCE_FACTORIES.put(url.getProtocol(), mountedPathResourceFactory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using a new MountedPathResourceFactory instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real reason to, the implementation wouldn't change with each new protocol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An argument could be made that there should be a test here, to see if the URL points to a mounted FileSystem or not, and adding the appropriate ResourceFactory based on results.

Or using some variation of the mountIfNeeded() logic always here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If adding some logic to figure out if mounting is needed or not doesn't take too much, let's add it. Otherwise let's get these changes make it in and eventually open an enhancement bug.

}
}

static ResourceFactory ROOT = new CompositeResourceFactory()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.util.resource;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.util.Optional;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test to ensure that Alternate FileSystem providers work as expected.
*
* <p>
* Uses the <a href="https://github.com/google/jimfs">google/jimfs</a> In-Memory FileSystem provider
* to have a FileSystem based on scheme `jimfs` (with an authority)
* </p>
*/
public class AlternateFileSystemResourceTest
{
private static final Logger LOG = LoggerFactory.getLogger(AlternateFileSystemResourceTest.class);
private FileSystem jimfs;
private URI fsBaseURI;

@BeforeEach
public void initInMemoryFileSystem(TestInfo testInfo)
{
Optional<Method> testMethod = testInfo.getTestMethod();
if (testMethod.isPresent())
{
String testMethodName = testMethod.get().getName();
// Create a jimfs that has the testMethodName as its authority
// eg: jimfs://<test-method-name>/
jimfs = Jimfs.newFileSystem(testMethodName, Configuration.unix());
}
else
{
// Let jimfs establish a unique name on its own
// eg: jimfs://a3cc0bda-1238-4847-864f-22fae7614146/
jimfs = Jimfs.newFileSystem(Configuration.unix());
}
fsBaseURI = jimfs.getPath("/").toUri();

ResourceFactory.registerResourceFactory(fsBaseURI.getScheme(), new MountedPathResourceFactory());
lorban marked this conversation as resolved.
Show resolved Hide resolved
}

@AfterEach
public void closeInMemoryFileSystem()
{
IO.close(jimfs);
}

@Test
public void testNewResource() throws IOException
{
// Create some content to reference
Files.writeString(jimfs.getPath("/foo.txt"), "Hello Foo", StandardCharsets.UTF_8);

// Reference it via Resource object
Resource resource = ResourceFactory.root().newResource(fsBaseURI.resolve("/foo.txt"));
assertTrue(Resources.isReadable(resource));

LOG.info("resource = {}", resource);

try (InputStream in = resource.newInputStream())
{
String contents = IO.toString(in, StandardCharsets.UTF_8);
assertThat(contents, is("Hello Foo"));
}
}

@Test
public void testNavigateResource() throws IOException
{
// Create some content to reference
Files.createDirectories(jimfs.getPath("/zed"));
Files.writeString(jimfs.getPath("/zed/bar.txt"), "Hello Bar", StandardCharsets.UTF_8);

// Reference it via Resource object
Resource resourceRoot = ResourceFactory.root().newResource(fsBaseURI.resolve("/"));
assertTrue(Resources.isDirectory(resourceRoot));

Resource resourceZedDir = resourceRoot.resolve("zed");
assertTrue(Resources.isDirectory(resourceZedDir));

Resource resourceBarText = resourceZedDir.resolve("bar.txt");
LOG.info("resource = {}", resourceBarText);

try (InputStream in = resourceBarText.newInputStream())
{
String contents = IO.toString(in, StandardCharsets.UTF_8);
assertThat(contents, is("Hello Bar"));
}
}
}