Path: blob/master/test/jdk/java/io/File/SetReadOnly.java
41149 views
/*1* Copyright (c) 1998, 2012, 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/* @test24@bug 4091757 4939819 672884225@summary Basic test for setReadOnly method26@build SetReadOnly Util27@run main SetReadOnly28*/2930import java.io.*;313233public class SetReadOnly {3435public static void main(String[] args) throws Exception {36if (Util.isPrivileged()) {37System.out.println("Unable to test file permissions when running with privileges");38return;39}4041File d = new File(System.getProperty("test.dir", "."));42File f = new File(d, "x.SetReadOnly");4344if (f.exists()) {45if (!f.delete())46throw new Exception("Can't delete test file " + f);47}48if (f.setReadOnly())49throw new Exception("Succeeded on non-existent file: " + f);5051OutputStream o = new FileOutputStream(f);52o.write('x');53o.close();54if (!f.setReadOnly())55throw new Exception(f + ": Failed on file");56if (f.canWrite())57throw new Exception(f + ": File is writeable");5859f = new File(d, "x.SetReadOnly.dir");60if (f.exists()) {61if (!f.delete())62throw new Exception("Can't delete test directory " + f);63}64if (!f.mkdir())65throw new Exception(f + ": Cannot create directory");66if (f.setReadOnly() && f.canWrite())67throw new Exception(f + ": Directory is writeable");68if (!f.delete())69throw new Exception(f + ": Cannot delete directory");7071}7273}747576