Improve reliability of TempDirTest

This commit is contained in:
Joakim Erdfelt 2022-08-04 12:38:23 -05:00
parent d97ab506f7
commit 6f6c327d8e
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
2 changed files with 75 additions and 34 deletions

View File

@ -16,41 +16,52 @@ package org.eclipse.jetty.ee10.webapp;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import jakarta.servlet.ServletContext;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.toolchain.test.FS;
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.resource.FileSystemPool;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(WorkDirExtension.class)
public class TempDirTest
{
public WorkDir workDir;
private Server server;
private WebAppContext webapp;
private File jettyBase;
private Path jettyBase;
@BeforeEach
public void before()
{
jettyBase = MavenTestingUtils.getTargetTestingDir(TempDirTest.class.getSimpleName());
jettyBase = workDir.getEmptyPathDir().resolve("base");
FS.ensureEmpty(jettyBase);
System.setProperty("jetty.base", jettyBase.toString());
}
public void setupServer()
@ -73,6 +84,12 @@ public class TempDirTest
server.stop();
}
@AfterEach
public void tearDown()
{
assertThat(FileSystemPool.INSTANCE.mounts(), empty());
}
/**
* ServletContext.TEMPDIR has <code>null</code> value
* so webappContent#tempDirectory is created under <code>java.io.tmpdir</code>
@ -84,7 +101,9 @@ public class TempDirTest
WebAppContext webAppContext = new WebAppContext();
webAppContext.setAttribute(ServletContext.TEMPDIR, null);
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().getParent(), is(System.getProperty("java.io.tmpdir")));
Path webappTempDir = webAppContext.getTempDirectory().toPath();
Path javaIoTmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
assertEquals(javaIoTmpDir, webappTempDir.getParent());
}
/**
@ -108,7 +127,8 @@ public class TempDirTest
public void attributeWithValidDirectory(String type) throws Exception
{
WebAppContext webAppContext = new WebAppContext();
Path tmpDir = Files.createTempDirectory("jetty_test");
Path tmpDir = workDir.getPath().resolve("temp");
FS.ensureDirExists(tmpDir);
switch (type)
{
case "File":
@ -127,7 +147,8 @@ public class TempDirTest
// Test we have correct value as the webapp temp directory.
WebInfConfiguration webInfConfiguration = new WebInfConfiguration();
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().toPath(), is(tmpDir));
Path webappTempDir = webAppContext.getTempDirectory().toPath();
assertEquals(tmpDir, webappTempDir);
}
/**
@ -138,9 +159,9 @@ public class TempDirTest
public void attributeWithNonExistentDirectory(String type) throws Exception
{
WebAppContext webAppContext = new WebAppContext();
Path tmpDir = Files.createTempDirectory("jetty_test").resolve("foo_test_tmp");
Files.deleteIfExists(tmpDir);
Path tmpDir = workDir.getPath().resolve("foo_does_not_exist");
assertFalse(Files.exists(tmpDir));
switch (type)
{
case "File":
@ -199,7 +220,8 @@ public class TempDirTest
public void baseTempDirAttributeWithValidDirectory(String type) throws Exception
{
WebAppContext webAppContext = new WebAppContext();
Path tmpDir = Files.createTempDirectory("jetty_test");
Path tmpDir = workDir.getPath().resolve("temp_test");
FS.ensureDirExists(tmpDir);
switch (type)
{
case "File":
@ -231,7 +253,7 @@ public class TempDirTest
public void baseTempDirAttributeWithNonExistentDirectory(String type) throws Exception
{
WebAppContext webAppContext = new WebAppContext();
Path tmpDir = Files.createTempDirectory("jetty_test").resolve("foo_test_tmp");
Path tmpDir = workDir.getPath().resolve("does_not_exists");
Files.deleteIfExists(tmpDir);
assertFalse(Files.exists(tmpDir));
switch (type)
@ -262,12 +284,10 @@ public class TempDirTest
@Test
public void jettyBaseWorkDoesNotExist() throws Exception
{
Path workDir = jettyBase.toPath().resolve("work");
FS.ensureDirExists(jettyBase);
Path workDir = jettyBase.resolve("work");
FS.ensureDeleted(workDir);
WebInfConfiguration webInfConfiguration = new WebInfConfiguration();
WebAppContext webAppContext = new WebAppContext();
System.setProperty("jetty.base", jettyBase.getAbsolutePath());
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().getParent(), is(System.getProperty("java.io.tmpdir")));
}
@ -279,12 +299,10 @@ public class TempDirTest
@Test
public void jettyBaseWorkExists() throws Exception
{
Path workDir = jettyBase.toPath().resolve("work");
FS.ensureDirExists(jettyBase);
Path workDir = jettyBase.resolve("work");
FS.ensureDirExists(workDir);
WebInfConfiguration webInfConfiguration = new WebInfConfiguration();
WebAppContext webAppContext = new WebAppContext();
System.setProperty("jetty.base", jettyBase.getAbsolutePath());
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().getParent(), is(workDir.toString()));
}
@ -327,7 +345,11 @@ public class TempDirTest
// Once server is stopped the WebApp temp should be deleted if persistTempDir is false.
server.stop();
tempDirectory = webapp.getTempDirectory();
assertThat(tempDirectory != null && tempDirectory.exists(), is(persistTempDir));
assertNotNull(tempDirectory, "Temp Directory");
if (persistTempDir)
{
assertTrue(tempDirectory.exists(), "Temp Directory should exist");
}
}
@ParameterizedTest

View File

@ -23,35 +23,45 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.toolchain.test.FS;
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.resource.FileSystemPool;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(WorkDirExtension.class)
public class TempDirTest
{
public WorkDir workDir;
private Server server;
private WebAppContext webapp;
private File jettyBase;
private Path jettyBase;
@BeforeEach
public void before()
{
jettyBase = MavenTestingUtils.getTargetTestingDir(TempDirTest.class.getSimpleName());
jettyBase = workDir.getEmptyPathDir().resolve("base");
FS.ensureEmpty(jettyBase);
System.setProperty("jetty.base", jettyBase.toString());
}
public void setupServer()
@ -74,6 +84,12 @@ public class TempDirTest
server.stop();
}
@AfterEach
public void tearDown()
{
assertThat(FileSystemPool.INSTANCE.mounts(), empty());
}
/**
* ServletContext.TEMPDIR has <code>null</code> value
* so webappContent#tempDirectory is created under <code>java.io.tmpdir</code>
@ -85,7 +101,9 @@ public class TempDirTest
WebAppContext webAppContext = new WebAppContext();
webAppContext.setAttribute(ServletContext.TEMPDIR, null);
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().getParent(), is(System.getProperty("java.io.tmpdir")));
Path webappTempDir = webAppContext.getTempDirectory().toPath();
Path javaIoTmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
assertEquals(javaIoTmpDir, webappTempDir.getParent());
}
/**
@ -109,7 +127,8 @@ public class TempDirTest
public void attributeWithValidDirectory(String type) throws Exception
{
WebAppContext webAppContext = new WebAppContext();
Path tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "jetty_test");
Path tmpDir = workDir.getPath().resolve("temp");
FS.ensureDirExists(tmpDir);
switch (type)
{
case "File":
@ -128,7 +147,8 @@ public class TempDirTest
// Test we have correct value as the webapp temp directory.
WebInfConfiguration webInfConfiguration = new WebInfConfiguration();
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().toPath(), is(tmpDir));
Path webappTempDir = webAppContext.getTempDirectory().toPath();
assertEquals(tmpDir, webappTempDir);
}
/**
@ -139,10 +159,9 @@ public class TempDirTest
public void attributeWithNonExistentDirectory(String type) throws Exception
{
WebAppContext webAppContext = new WebAppContext();
Path tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "jetty_test")
.resolve("foo_test_tmp");
Files.deleteIfExists(tmpDir);
Path tmpDir = workDir.getPath().resolve("foo_does_not_exist");
assertFalse(Files.exists(tmpDir));
switch (type)
{
case "File":
@ -201,7 +220,8 @@ public class TempDirTest
public void baseTempDirAttributeWithValidDirectory(String type) throws Exception
{
WebAppContext webAppContext = new WebAppContext();
Path tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "jetty_test");
Path tmpDir = workDir.getPath().resolve("temp_test");
FS.ensureDirExists(tmpDir);
switch (type)
{
case "File":
@ -233,8 +253,7 @@ public class TempDirTest
public void baseTempDirAttributeWithNonExistentDirectory(String type) throws Exception
{
WebAppContext webAppContext = new WebAppContext();
Path tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "jetty_test")
.resolve("foo_test_tmp");
Path tmpDir = workDir.getPath().resolve("does_not_exists");
Files.deleteIfExists(tmpDir);
assertFalse(Files.exists(tmpDir));
switch (type)
@ -265,12 +284,10 @@ public class TempDirTest
@Test
public void jettyBaseWorkDoesNotExist() throws Exception
{
Path workDir = jettyBase.toPath().resolve("work");
FS.ensureDirExists(jettyBase);
Path workDir = jettyBase.resolve("work");
FS.ensureDeleted(workDir);
WebInfConfiguration webInfConfiguration = new WebInfConfiguration();
WebAppContext webAppContext = new WebAppContext();
System.setProperty("jetty.base", jettyBase.getAbsolutePath());
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().getParent(), is(System.getProperty("java.io.tmpdir")));
}
@ -282,12 +299,10 @@ public class TempDirTest
@Test
public void jettyBaseWorkExists() throws Exception
{
Path workDir = jettyBase.toPath().resolve("work");
FS.ensureDirExists(jettyBase);
Path workDir = jettyBase.resolve("work");
FS.ensureDirExists(workDir);
WebInfConfiguration webInfConfiguration = new WebInfConfiguration();
WebAppContext webAppContext = new WebAppContext();
System.setProperty("jetty.base", jettyBase.getAbsolutePath());
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().getParent(), is(workDir.toString()));
}
@ -330,7 +345,11 @@ public class TempDirTest
// Once server is stopped the WebApp temp should be deleted if persistTempDir is false.
server.stop();
tempDirectory = webapp.getTempDirectory();
assertThat(tempDirectory != null && tempDirectory.exists(), is(persistTempDir));
assertNotNull(tempDirectory, "Temp Directory");
if (persistTempDir)
{
assertTrue(tempDirectory.exists(), "Temp Directory should exist");
}
}
@ParameterizedTest