Skip to content
Open
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
76 changes: 42 additions & 34 deletions test/jdk/java/io/File/createTempFile/TargetDirectory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,7 +25,13 @@
* @test
* @bug 4847239
* @summary Verify directory parameter behavior in File.createTempFile(String,String,File)
* @library /test/lib
* @run junit TargetDirectory
*/
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.condition.DisabledIf;

import java.io.File;
import java.io.IOException;
Expand All @@ -40,24 +46,36 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jdk.test.lib.Platform;

public class TargetDirectory {
public static void main(String[] args) throws Exception {
// Target directory exists and is writable
Path dir = Path.of("target");
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class TargetDirectory {

@TempDir(cleanup = CleanupMode.ALWAYS)
Path tempDir;

@Test
void testWritableDirectory() throws Exception {
Path dir = tempDir.resolve("target");
File target = Files.createDirectory(dir).toFile();
File tmp = File.createTempFile("passes", null, target);
if (!Files.exists(tmp.toPath())) {
throw new RuntimeException("Temp file not created");
}
tmp.delete();
assertTrue(Files.exists(tmp.toPath()), "Temp file not created");
}

@Test
@DisabledIf("jdk.test.lib.Platform#isRoot")
Copy link
Contributor

Choose a reason for hiding this comment

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

This will silently skip the test.
Jtreg has a feature for keeping track of Skipped tests and reporting they were skipped.
Throwing SkippedException would enable that reporting.
Take a look at a similar test: test/jdk/java/io/File/SetAccess.java

Copy link
Member

Choose a reason for hiding this comment

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

This will silently skip the test.

I ran the test as root and as my normal user. For the normal user one gets

STARTED    TargetDirectory::testReadOnlyDirectory 'testReadOnlyDirectory()'

whereas for root

SKIPPED    TargetDirectory::testReadOnlyDirectory 'testReadOnlyDirectory()' @DisabledIf("jdk.test.lib.Platform#isRoot") evaluated to true

which looked okay to me.

void testReadOnlyDirectory() throws Exception {
Path dir = tempDir.resolve("target");
File target = Files.createDirectory(dir).toFile();

// Make target directory read-only
// Make 'target' read-only
if (Files.getFileStore(dir).supportsFileAttributeView("posix")) {
PosixFileAttributeView view =
Files.getFileAttributeView(dir, PosixFileAttributeView.class);
Set<PosixFilePermission> perms = new HashSet<>();
perms.add(PosixFilePermission.valueOf("OWNER_READ"));
perms.add(PosixFilePermission.OWNER_READ);
view.setPermissions(perms);
} else if (Files.getFileStore(dir).supportsFileAttributeView("acl")) {
AclFileAttributeView view = Files.getFileAttributeView(dir,
Expand All @@ -76,30 +94,20 @@ public static void main(String[] args) throws Exception {
throw new RuntimeException("Required attribute view not supported");
}

// Target directory exists but is read-only
try {
File.createTempFile("readonly", null, target);
throw new RuntimeException("Exception not thrown for read-only target directory");
} catch (IOException expected) {
} finally {
target.delete();
}
assertThrows(IOException.class,
() -> File.createTempFile("readonly", null, target));
}

// Target directory does not exist
try {
File.createTempFile("nonexistent", null, new File("void"));
throw new RuntimeException("Exception not thrown for non-existent target directory");
} catch (IOException expected) {
}
@Test
void testNonExistentDirectory() {
assertThrows(IOException.class,
() -> File.createTempFile("nonexistent", null, new File(tempDir.toFile(), "void")));
}

// Target is a file, not a directory
target = Files.createFile(Path.of("file")).toFile();
try {
File.createTempFile("file", null, target);
throw new RuntimeException("Exception not thrown for file target");
} catch (IOException expected) {
} finally {
target.delete();
}
@Test
void testTargetIsFile() throws Exception {
File target = Files.createFile(tempDir.resolve("file")).toFile();
assertThrows(IOException.class,
() -> File.createTempFile("file", null, target));
}
}