Path: blob/master/test/jdk/java/io/FileOutputStream/FileOpenTest.java
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 636489426* @requires (os.family == "windows")27* @library /test/lib28* @build jdk.test.lib.Asserts29* @run main FileOpenTest30* @summary Test to ensure that opening of hidden Vs non-hidden,31* read/write Vs read-only files for writing works as expected.32*/3334import java.io.File;35import java.io.IOException;36import java.io.FileOutputStream;37import java.nio.file.Files;3839import static jdk.test.lib.Asserts.assertTrue;4041public class FileOpenTest {4243private static File tmpFile;4445public static void main(String args[]) throws Exception {46try {47tmpFile = File.createTempFile("FileOpenTest", "suffix");4849// Opening Writable Normal File..50test(true);5152// Opening Writable Hidden File..53Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);54test(false);5556// Opening Read-Only Hidden File..57Files.setAttribute(tmpFile.toPath(), "dos:hidden", false);58tmpFile.setReadOnly();59test(false);6061// Opening Read-Only Normal File..62Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);63test(false);64} finally {65tmpFile.delete();66}67}6869private static void test(boolean writable) throws Exception {7071try (FileOutputStream fs = new FileOutputStream(tmpFile)) {72fs.write(1);73assertTrue(writable, "Able to open READ-ONLY file for WRITING!");74assertTrue(tmpFile.canWrite(), "Able to open READ-ONLY file for WRITING!");75} catch(IOException e) {76assertTrue(!writable, "Unable to open non-READ-ONLY file for WRITING!");77System.out.println("Caught the Exception as expected");78e.printStackTrace(System.out);79}80}81}828384