test/jdk/java/io/FileOutputStream/FileOpenTest.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     1
/*
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     4
 *
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     8
 *
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    13
 * accompanied this code).
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    14
 *
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    18
 *
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    21
 * questions.
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    22
 */
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    23
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    24
/*
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    25
 * @test
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    26
 * @bug 6364894
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    27
 * @requires (os.family == "windows")
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    28
 * @library /test/lib
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    29
 * @build jdk.test.lib.Asserts
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    30
 * @run main FileOpenTest
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    31
 * @summary Test to ensure that opening of hidden Vs non-hidden,
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    32
 *          read/write Vs read-only files for writing works as expected.
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    33
 */
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    34
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    35
import java.io.File;
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    36
import java.io.IOException;
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    37
import java.io.FileOutputStream;
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    38
import java.nio.file.Files;
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    39
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    40
import static jdk.test.lib.Asserts.assertTrue;
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    41
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    42
public class FileOpenTest {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    43
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    44
    private static File tmpFile;
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    45
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    46
    public static void main(String args[]) throws Exception {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    47
        try {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    48
            tmpFile = File.createTempFile("FileOpenTest", "suffix");
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    49
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    50
            // Opening Writable Normal File..
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    51
            test(true);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    52
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    53
            // Opening Writable Hidden File..
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    54
            Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    55
            test(false);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    56
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    57
            // Opening Read-Only Hidden File..
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    58
            Files.setAttribute(tmpFile.toPath(), "dos:hidden", false);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    59
            tmpFile.setReadOnly();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    60
            test(false);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    61
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    62
            // Opening Read-Only Normal File..
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    63
            Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    64
            test(false);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    65
        } finally {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    66
            tmpFile.delete();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    67
        }
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    68
    }
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    69
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    70
    private static void test(boolean writable) throws Exception {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    71
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    72
        try (FileOutputStream fs = new FileOutputStream(tmpFile)) {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    73
            fs.write(1);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    74
            assertTrue(writable, "Able to open READ-ONLY file for WRITING!");
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    75
            assertTrue(tmpFile.canWrite(), "Able to open READ-ONLY file for WRITING!");
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    76
        } catch(IOException e) {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    77
            assertTrue(!writable, "Unable to open non-READ-ONLY file for WRITING!");
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    78
            System.out.println("Caught the Exception as expected");
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    79
            e.printStackTrace(System.out);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    80
        }
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    81
    }
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents:
diff changeset
    82
}