Merge pull request #50194 from zxuhan

* gh-50192-application-temp-path:
  Expose Path getters on ApplicationTemp

Closes gh-50194
This commit is contained in:
Stéphane Nicoll
2026-07-15 11:35:13 +02:00
4 changed files with 56 additions and 1 deletions
@@ -23,6 +23,7 @@ import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Enumeration;
@@ -163,6 +164,17 @@ public class ApplicationHome {
return this.source;
}
/**
* Returns the path of the underlying source used to find the home directory. This is
* usually the jar file or a directory. Can return {@code null} if the source cannot
* be determined.
* @return the underlying source path or {@code null}
* @since 4.2.0
*/
public @Nullable Path getSourcePath() {
return (this.source != null) ? this.source.toPath() : null;
}
/**
* Returns the application home directory.
* @return the home directory (never {@code null})
@@ -171,6 +183,15 @@ public class ApplicationHome {
return this.dir;
}
/**
* Returns the path of the application home directory.
* @return the home path (never {@code null})
* @since 4.2.0
*/
public Path getPath() {
return this.dir.toPath();
}
@Override
public String toString() {
return getDir().toString();
@@ -117,7 +117,12 @@ public class ApplicationTemp {
return createDirectory(getPath().resolve(subDir)).toFile();
}
private Path getPath() {
/**
* Return the path to be used for application specific temp files.
* @return the application temp path
* @since 4.2.0
*/
public Path getPath() {
if (this.path == null) {
this.pathLock.lock();
try {
@@ -57,6 +57,29 @@ class ApplicationHomeTests {
assertThat(applicationHome.getDir()).isEqualTo(app);
}
@Test
void getPathReturnsSamePathAsGetDir() throws Exception {
File app = new File(this.tempDir, "app");
ApplicationHome applicationHome = createApplicationHome(app);
assertThat(applicationHome.getPath()).isEqualTo(applicationHome.getDir().toPath());
}
@Test
void getSourcePathReturnsSamePathAsGetSource() throws Exception {
File app = new File(this.tempDir, "app");
ApplicationHome applicationHome = createApplicationHome(app);
File source = applicationHome.getSource();
assertThat(source).isNotNull();
assertThat(applicationHome.getSourcePath()).isEqualTo(source.toPath());
}
@Test
void getSourcePathReturnsNullWhenSourceIsNull() {
ApplicationHome applicationHome = new ApplicationHome();
assertThat(applicationHome.getSource()).isNull();
assertThat(applicationHome.getSourcePath()).isNull();
}
private ApplicationHome createApplicationHome(File location) throws Exception {
File examplePackage = new File(location, "com/example");
examplePackage.mkdirs();
@@ -79,6 +79,12 @@ class ApplicationTempTests {
assertThat(temp.getDir("abc")).isEqualTo(new File(temp.getDir(), "abc"));
}
@Test
void getPathReturnsSamePathAsGetDir() {
ApplicationTemp temp = new ApplicationTemp();
assertThat(temp.getPath()).isEqualTo(temp.getDir().toPath());
}
@Test
void posixPermissions() throws IOException {
ApplicationTemp temp = new ApplicationTemp();