From 6f6c327d8eb870be90a23418ec9ab69ebdd72cb9 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 4 Aug 2022 12:38:23 -0500 Subject: [PATCH] Improve reliability of TempDirTest --- .../jetty/ee10/webapp/TempDirTest.java | 54 ++++++++++++------ .../eclipse/jetty/ee9/webapp/TempDirTest.java | 55 +++++++++++++------ 2 files changed, 75 insertions(+), 34 deletions(-) diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TempDirTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TempDirTest.java index 75bcc682c13..5e163334db8 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TempDirTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TempDirTest.java @@ -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 null value * so webappContent#tempDirectory is created under java.io.tmpdir @@ -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 diff --git a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TempDirTest.java b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TempDirTest.java index 87b4217e296..1843d872e77 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TempDirTest.java +++ b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TempDirTest.java @@ -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 null value * so webappContent#tempDirectory is created under java.io.tmpdir @@ -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