8334332: TestIOException.java fails if run by root

Reviewed-by: prappo
This commit is contained in:
SendaoYan 2024-06-18 10:24:43 +00:00 committed by Pavel Rappo
parent 614b99a8f8
commit 472b935b44

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, 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
@ -23,9 +23,9 @@
/*
* @test
* @bug 8164130
* @bug 8164130 8334332
* @summary test IOException handling
* @library ../../lib
* @library ../../lib /test/lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
* @build javadoc.tester.*
* @run main TestIOException
@ -39,6 +39,7 @@ import java.util.Locale;
import java.util.Map;
import javadoc.tester.JavadocTester;
import jtreg.SkippedException;
/**
* Tests IO Exception handling.
@ -61,16 +62,13 @@ public class TestIOException extends JavadocTester {
public void testReadOnlyDirectory() {
File outDir = new File("out1");
if (!outDir.mkdir()) {
throw error(outDir, "Cannot create directory");
throw skip(outDir, "Cannot create directory");
}
if (!outDir.setReadOnly()) {
if (skip(outDir)) {
return;
}
throw error(outDir, "could not set directory read-only");
throw skip(outDir, "could not set directory read-only");
}
if (outDir.canWrite()) {
throw error(outDir, "directory is writable");
throw skip(outDir, "directory is writable");
}
try {
@ -93,15 +91,15 @@ public class TestIOException extends JavadocTester {
public void testReadOnlyFile() throws Exception {
File outDir = new File("out2");
if (!outDir.mkdir()) {
throw error(outDir, "Cannot create directory");
throw skip(outDir, "Cannot create directory");
}
File index = new File(outDir, "index.html");
try (FileWriter fw = new FileWriter(index)) { }
if (!index.setReadOnly()) {
throw error(index, "could not set index read-only");
throw skip(index, "could not set index read-only");
}
if (index.canWrite()) {
throw error(index, "index is writable");
throw skip(index, "index is writable");
}
try {
@ -139,16 +137,13 @@ public class TestIOException extends JavadocTester {
File outDir = new File("out3");
File pkgOutDir = new File(outDir, "p");
if (!pkgOutDir.mkdirs()) {
throw error(pkgOutDir, "Cannot create directory");
throw skip(pkgOutDir, "Cannot create directory");
}
if (!pkgOutDir.setReadOnly()) {
if (skip(pkgOutDir)) {
return;
}
throw error(pkgOutDir, "could not set directory read-only");
throw skip(pkgOutDir, "could not set directory read-only");
}
if (pkgOutDir.canWrite()) {
throw error(pkgOutDir, "directory is writable");
throw skip(pkgOutDir, "directory is writable");
}
// run javadoc and check results
@ -192,16 +187,13 @@ public class TestIOException extends JavadocTester {
File pkgOutDir = new File(outDir, "p");
File docFilesOutDir = new File(pkgOutDir, "doc-files");
if (!docFilesOutDir.mkdirs()) {
throw error(docFilesOutDir, "Cannot create directory");
throw skip(docFilesOutDir, "Cannot create directory");
}
if (!docFilesOutDir.setReadOnly()) {
if (skip(docFilesOutDir)) {
return;
}
throw error(docFilesOutDir, "could not set directory read-only");
throw skip(docFilesOutDir, "could not set directory read-only");
}
if (docFilesOutDir.canWrite()) {
throw error(docFilesOutDir, "directory is writable");
throw skip(docFilesOutDir, "directory is writable");
}
try {
@ -219,10 +211,11 @@ public class TestIOException extends JavadocTester {
}
}
private Error error(File f, String message) {
private Error skip(File f, String message) {
out.print(System.getProperty("user.name"));
out.println(f + ": " + message);
showAllAttributes(f.toPath());
throw new Error(f + ": " + message);
throw new SkippedException(f + ": " + message);
}
private void showAllAttributes(Path p) {
@ -242,20 +235,5 @@ public class TestIOException extends JavadocTester {
out.println("Error accessing attributes " + attributes + ": " + t);
}
}
private boolean skip(File dir) {
if (isWindows()) {
showAllAttributes(dir.toPath());
out.println("Windows: cannot set directory read only:" + dir);
out.println("TEST CASE SKIPPED");
return true;
} else {
return false;
}
}
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase(Locale.US).startsWith("windows");
}
}