Cleanup tests in jetty-util Resource (#8586)

+ No more @Disabled tests
+ Merge JrtResourceTest into ResourceTest
+ Merge ClassPathResourceTest into ResourceTest
+ Rename JarResourceTest to MountedPathResourceTest
This commit is contained in:
Joakim Erdfelt 2022-09-15 07:40:05 -05:00 committed by GitHub
parent b6224aea30
commit 8e98467c39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 184 additions and 220 deletions

View File

@ -1,134 +0,0 @@
//
// ========================================================================
// 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.nio.file.Files;
import java.nio.file.Path;
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 static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
public class ClassPathResourceTest
{
private ResourceFactory.Closeable resourceFactory;
@BeforeEach
public void beforeEach()
{
assertThat(FileSystemPool.INSTANCE.mounts(), empty());
resourceFactory = ResourceFactory.closeable();
}
@AfterEach
public void afterEach()
{
IO.close(resourceFactory);
assertThat(FileSystemPool.INSTANCE.mounts(), empty());
}
/**
* Test a class path resource for existence.
*/
@Test
public void testClassPathResourceClassRelative()
{
final String classPathName = "Resource.class";
Resource resource = resourceFactory.newClassPathResource(classPathName);
// A class path cannot be a directory
assertFalse(resource.isDirectory(), "Class path cannot be a directory.");
// A class path must exist
assertTrue(resource.exists(), "Class path resource does not exist.");
}
/**
* Test a class path resource for existence.
*/
@Test
public void testClassPathResourceClassAbsolute()
{
final String classPathName = "/org/eclipse/jetty/util/resource/Resource.class";
Resource resource = resourceFactory.newClassPathResource(classPathName);
// A class path cannot be a directory
assertFalse(resource.isDirectory(), "Class path cannot be a directory.");
// A class path must exist
assertTrue(resource.exists(), "Class path resource does not exist.");
}
/**
* Test a class path resource for directories.
*
* @throws Exception failed test
*/
@Test
public void testClassPathResourceDirectory() throws Exception
{
// If the test runs in the module-path, resource "/" cannot be found.
assumeFalse(Resource.class.getModule().isNamed());
final String classPathName = "/";
Resource resource = resourceFactory.newClassPathResource(classPathName);
// A class path must be a directory
assertTrue(resource.isDirectory(), "Class path must be a directory.");
assertTrue(Files.isDirectory(resource.getPath()), "Class path returned file must be a directory.");
// A class path must exist
assertTrue(resource.exists(), "Class path resource does not exist.");
}
/**
* Test a class path resource for a file.
*
* @throws Exception failed test
*/
@Test
public void testClassPathResourceFile() throws Exception
{
final String fileName = "resource.txt";
final String classPathName = "/" + fileName;
// Will locate a resource in the class path
Resource resource = resourceFactory.newClassPathResource(classPathName);
// A class path cannot be a directory
assertFalse(resource.isDirectory(), "Class path must be a directory.");
assertTrue(resource != null);
Path path = resource.getPath();
assertEquals(fileName, path.getFileName().toString(), "File name from class path is not equal.");
assertTrue(Files.isRegularFile(path), "File returned from class path should be a regular file.");
// A class path must exist
assertTrue(resource.exists(), "Class path resource does not exist.");
}
}

View File

@ -44,7 +44,6 @@ import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
@ -246,7 +245,6 @@ public class FileSystemResourceTest
}
@Test
@Disabled("Will be fixed in PR #8436")
public void testAccessUniCodeFile() throws Exception
{
Path dir = workDir.getEmptyPathDir();
@ -263,30 +261,34 @@ public class FileSystemResourceTest
Resource base = ResourceFactory.root().newResource(subdir);
// Cannot use decoded Unicode
assertThrows(IllegalArgumentException.class, () -> base.resolve("swedish-å.txt"));
assertThrows(IllegalArgumentException.class, () -> base.resolve("swedish-ä.txt"));
assertThrows(IllegalArgumentException.class, () -> base.resolve("swedish-ö.txt"));
// Use decoded Unicode
Resource refD1 = base.resolve("swedish-å.txt");
Resource refD2 = base.resolve("swedish-ä.txt");
Resource refD3 = base.resolve("swedish-ö.txt");
assertTrue(refD1.exists(), "Ref D1 exists");
assertTrue(refD2.exists(), "Ref D2 exists");
assertTrue(refD3.exists(), "Ref D3 exists");
// Use encoded Unicode
Resource refA1 = base.resolve("swedish-%C3%A5.txt"); // swedish-å.txt
Resource refA2 = base.resolve("swedish-%C3%A4.txt"); // swedish-ä.txt
Resource refO1 = base.resolve("swedish-%C3%B6.txt"); // swedish-ö.txt
Resource refE1 = base.resolve("swedish-%C3%A5.txt"); // swedish-å.txt
Resource refE2 = base.resolve("swedish-%C3%A4.txt"); // swedish-ä.txt
Resource refE3 = base.resolve("swedish-%C3%B6.txt"); // swedish-ö.txt
assertThat("Ref A1 exists", refA1.exists(), is(true));
assertThat("Ref A2 exists", refA2.exists(), is(true));
assertThat("Ref O1 exists", refO1.exists(), is(true));
assertTrue(refE1.exists(), "Ref E1 exists");
assertTrue(refE2.exists(), "Ref E2 exists");
assertTrue(refE3.exists(), "Ref E3 exists");
if (LINUX.isCurrentOs())
{
assertThat("Ref A1 alias", refA1.isAlias(), is(false));
assertThat("Ref A2 alias", refA2.isAlias(), is(false));
assertThat("Ref O1 alias", refO1.isAlias(), is(false));
assertThat("Ref A1 alias", refE1.isAlias(), is(false));
assertThat("Ref A2 alias", refE2.isAlias(), is(false));
assertThat("Ref O1 alias", refE3.isAlias(), is(false));
}
assertThat("Ref A1 contents", toString(refA1), is("hi a-with-circle"));
assertThat("Ref A2 contents", toString(refA2), is("hi a-with-two-dots"));
assertThat("Ref O1 contents", toString(refO1), is("hi o-with-two-dots"));
assertThat("Ref A1 contents", toString(refE1), is("hi a-with-circle"));
assertThat("Ref A2 contents", toString(refE2), is("hi a-with-two-dots"));
assertThat("Ref O1 contents", toString(refE3), is("hi o-with-two-dots"));
}
/**

View File

@ -1,59 +0,0 @@
//
// ========================================================================
// 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 org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
public class JrtResourceTest
{
@BeforeEach
public void beforeEach()
{
assertThat(FileSystemPool.INSTANCE.mounts(), empty());
}
@AfterEach
public void afterEach()
{
assertThat(FileSystemPool.INSTANCE.mounts(), empty());
}
@Test
public void testResourceModule()
throws Exception
{
Resource resource = ResourceFactory.root().newResource("jrt:/java.base");
assertThat(resource.exists(), is(true));
assertThat(resource.isDirectory(), is(true));
assertThat(resource.length(), is(0L));
}
@Test
public void testResourceAllModules()
throws Exception
{
Resource resource = ResourceFactory.root().newResource("jrt:/");
assertThat(resource.exists(), is(true));
assertThat(resource.isDirectory(), is(true));
assertThat(resource.length(), is(0L));
}
}

View File

@ -43,7 +43,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(WorkDirExtension.class)
public class JarResourceTest
public class MountedPathResourceTest
{
public WorkDir workDir;

View File

@ -14,11 +14,14 @@
package org.eclipse.jetty.util.resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Stream;
@ -27,11 +30,9 @@ import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.URIUtil;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
@ -43,11 +44,14 @@ import org.junit.jupiter.params.provider.MethodSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@ExtendWith(WorkDirExtension.class)
@ -373,12 +377,163 @@ public class ResourceTest
}
@Test
@Disabled
public void testDotAlias()
public void testDotAliasDirExists(WorkDir workDir) throws IOException
{
Resource resource = resourceFactory.newResource("/foo/bar");
Resource same = resource.resolve(".");
assertNotNull(same);
assertTrue(same.isAlias());
Path dir = workDir.getEmptyPathDir().resolve("foo/bar");
FS.ensureDirExists(dir);
Resource resource = resourceFactory.newResource(dir);
Resource dot = resource.resolve(".");
assertNotNull(dot);
assertTrue(dot.exists());
assertTrue(dot.isAlias(), "Reference to '.' is an alias to itself");
assertTrue(Files.isSameFile(dot.getPath(), Paths.get(dot.getAlias())));
}
@Test
public void testDotAliasDirDoesNotExist(WorkDir workDir)
{
Path dir = workDir.getEmptyPathDir().resolve("foo/bar");
// at this point we have a directory reference that does not exist
Resource resource = resourceFactory.newResource(dir);
Resource dot = resource.resolve(".");
assertNotNull(dot);
assertFalse(dot.exists());
assertFalse(dot.isAlias(), "Reference to '.' is not an alias as directory doesn't exist");
}
@Test
public void testDotAliasFileExists(WorkDir workDir) throws IOException
{
Path dir = workDir.getEmptyPathDir().resolve("foo");
FS.ensureDirExists(dir);
Path file = dir.resolve("bar.txt");
FS.touch(file);
Resource resource = resourceFactory.newResource(file);
Resource dot = resource.resolve(".");
assertNotNull(dot);
assertTrue(dot.exists());
assertTrue(dot.isAlias(), "Reference to '.' is an alias to itself");
assertTrue(Files.isSameFile(dot.getPath(), Paths.get(dot.getAlias())));
}
@Test
public void testDotAliasFileDoesNotExists(WorkDir workDir) throws IOException
{
Path dir = workDir.getEmptyPathDir().resolve("foo");
FS.ensureDirExists(dir);
Path file = dir.resolve("bar.txt");
// at this point we have a file reference that does not exist
assertFalse(Files.exists(file));
Resource resource = resourceFactory.newResource(file);
Resource dot = resource.resolve(".");
assertNotNull(dot);
assertFalse(dot.exists());
assertFalse(dot.isAlias(), "Reference to '.' is not an alias as file doesn't exist");
}
@Test
public void testJrtResourceModule()
{
Resource resource = ResourceFactory.root().newResource("jrt:/java.base");
assertThat(resource.exists(), is(true));
assertThat(resource.isDirectory(), is(true));
assertThat(resource.length(), is(0L));
}
@Test
public void testJrtResourceAllModules()
{
Resource resource = ResourceFactory.root().newResource("jrt:/");
assertThat(resource.exists(), is(true));
assertThat(resource.isDirectory(), is(true));
assertThat(resource.length(), is(0L));
}
/**
* Test a class path resource for existence.
*/
@Test
public void testClassPathResourceClassRelative()
{
final String classPathName = "Resource.class";
Resource resource = resourceFactory.newClassPathResource(classPathName);
// A class path cannot be a directory
assertFalse(resource.isDirectory(), "Class path cannot be a directory.");
// A class path must exist
assertTrue(resource.exists(), "Class path resource does not exist.");
}
/**
* Test a class path resource for existence.
*/
@Test
public void testClassPathResourceClassAbsolute()
{
final String classPathName = "/org/eclipse/jetty/util/resource/Resource.class";
Resource resource = resourceFactory.newClassPathResource(classPathName);
// A class path cannot be a directory
assertFalse(resource.isDirectory(), "Class path cannot be a directory.");
// A class path must exist
assertTrue(resource.exists(), "Class path resource does not exist.");
}
/**
* Test a class path resource for directories.
*
* @throws Exception failed test
*/
@Test
public void testClassPathResourceDirectory() throws Exception
{
// If the test runs in the module-path, resource "/" cannot be found.
assumeFalse(Resource.class.getModule().isNamed());
final String classPathName = "/";
Resource resource = resourceFactory.newClassPathResource(classPathName);
// A class path must be a directory
assertTrue(resource.isDirectory(), "Class path must be a directory.");
assertTrue(Files.isDirectory(resource.getPath()), "Class path returned file must be a directory.");
// A class path must exist
assertTrue(resource.exists(), "Class path resource does not exist.");
}
/**
* Test a class path resource for a file.
*
* @throws Exception failed test
*/
@Test
public void testClassPathResourceFile() throws Exception
{
final String fileName = "resource.txt";
final String classPathName = "/" + fileName;
// Will locate a resource in the class path
Resource resource = resourceFactory.newClassPathResource(classPathName);
// A class path cannot be a directory
assertFalse(resource.isDirectory(), "Class path must be a directory.");
assertNotNull(resource);
Path path = resource.getPath();
assertEquals(fileName, path.getFileName().toString(), "File name from class path is not equal.");
assertTrue(Files.isRegularFile(path), "File returned from class path should be a regular file.");
// A class path must exist
assertTrue(resource.exists(), "Class path resource does not exist.");
}
}