16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 6244047
27 * @author Jim Gish
28 * @summary throw more precise IOException when pattern specifies invalid directory
29 *
30 * @run main/othervm CheckLockLocationTest
31 */
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.AccessDeniedException;
35 import java.nio.file.FileSystemException;
36 import java.nio.file.NoSuchFileException;
37 import java.util.logging.FileHandler;
38 public class CheckLockLocationTest {
39
40 private static final String NON_WRITABLE_DIR = "non-writable-dir";
41 private static final String NOT_A_DIR = "not-a-dir";
42 private static final String WRITABLE_DIR = "writable-dir";
43 private static final String NON_EXISTENT_DIR = "non-existent-dir";
44 private static boolean runNonWritableDirTest;
45
46 public static void main(String... args) throws IOException {
47 // we'll base all file creation attempts on the system temp directory,
48 // %t and also try specifying non-existent directories and plain files
49 // that should be directories, and non-writable directories,
50 // to exercise all code paths of checking the lock location
51 // Note that on platforms like Windows that don't support
52 // setWritable() on a directory, we'll skip the non-writable
53 // directory test if setWritable(false) returns false.
54 //
55 File writableDir = setup();
56 // we now have three files/directories to work with:
152 // Create a plain file which we will attempt to use as a directory
153 // (%t/not-a-dir)
154 File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
155 if (!createFile(notAdir, false)) {
156 throw new RuntimeException("Test setup failed: unable to a plain"
157 + " working file " + notAdir.getAbsolutePath() );
158 }
159 notAdir.deleteOnExit();
160
161 // Create a non-writable directory (%t/non-writable-dir)
162 File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
163 if (!createFile(nonWritableDir, true)) {
164 throw new RuntimeException("Test setup failed: unable to create"
165 + " a non-"
166 + "writable working directory "
167 + nonWritableDir.getAbsolutePath() );
168 }
169 nonWritableDir.deleteOnExit();
170
171 // make it non-writable
172 if (nonWritableDir.setWritable(false)) {
173 runNonWritableDirTest = true;
174 } else {
175 runNonWritableDirTest = false;
176 System.out.println( "Test Setup WARNING: unable to make"
177 + " working directory " + nonWritableDir.getAbsolutePath()
178 + " non-writable on platform " + System.getProperty("os.name"));
179
180 }
181
182 // make sure non-existent directory really doesn't exist
183 File nonExistentDir = new File(tmpOrHomeDir, NON_EXISTENT_DIR);
184 if (nonExistentDir.exists()) {
185 nonExistentDir.delete();
186 }
187 return writableDir;
188 }
189
190 /**
191 * @param newFile
192 * @return true if file already exists or creation succeeded
193 */
194 private static boolean createFile(File newFile, boolean makeDirectory) {
195 if (newFile.exists()) {
196 return true;
197 }
198 if (makeDirectory) {
199 return newFile.mkdir();
200 } else {
201 try {
202 return newFile.createNewFile();
203 } catch (IOException ioex) {
204 ioex.printStackTrace();
205 return false;
206 }
|
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 6244047
27 * @author Jim Gish
28 * @summary throw more precise IOException when pattern specifies invalid directory
29 *
30 * @run main/othervm CheckLockLocationTest
31 */
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.AccessDeniedException;
35 import java.nio.file.FileSystemException;
36 import java.nio.file.Files;
37 import java.nio.file.NoSuchFileException;
38 import java.nio.file.Path;
39 import java.util.logging.FileHandler;
40 public class CheckLockLocationTest {
41
42 private static final String NON_WRITABLE_DIR = "non-writable-dir";
43 private static final String NOT_A_DIR = "not-a-dir";
44 private static final String WRITABLE_DIR = "writable-dir";
45 private static final String NON_EXISTENT_DIR = "non-existent-dir";
46 private static boolean runNonWritableDirTest;
47
48 public static void main(String... args) throws IOException {
49 // we'll base all file creation attempts on the system temp directory,
50 // %t and also try specifying non-existent directories and plain files
51 // that should be directories, and non-writable directories,
52 // to exercise all code paths of checking the lock location
53 // Note that on platforms like Windows that don't support
54 // setWritable() on a directory, we'll skip the non-writable
55 // directory test if setWritable(false) returns false.
56 //
57 File writableDir = setup();
58 // we now have three files/directories to work with:
154 // Create a plain file which we will attempt to use as a directory
155 // (%t/not-a-dir)
156 File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
157 if (!createFile(notAdir, false)) {
158 throw new RuntimeException("Test setup failed: unable to a plain"
159 + " working file " + notAdir.getAbsolutePath() );
160 }
161 notAdir.deleteOnExit();
162
163 // Create a non-writable directory (%t/non-writable-dir)
164 File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
165 if (!createFile(nonWritableDir, true)) {
166 throw new RuntimeException("Test setup failed: unable to create"
167 + " a non-"
168 + "writable working directory "
169 + nonWritableDir.getAbsolutePath() );
170 }
171 nonWritableDir.deleteOnExit();
172
173 // make it non-writable
174 Path path = nonWritableDir.toPath();
175 final boolean nonWritable = nonWritableDir.setWritable(false);
176 final boolean isWritable = Files.isWritable(path);
177 if (nonWritable && !isWritable) {
178 runNonWritableDirTest = true;
179 System.out.println("Created non writable dir at: " + path.toString());
180 } else {
181 runNonWritableDirTest = false;
182 System.out.println( "Test Setup WARNING: unable to make"
183 + " working directory " + nonWritableDir.getAbsolutePath()
184 + " non-writable on platform " + System.getProperty("os.name"));
185
186 }
187
188 // make sure non-existent directory really doesn't exist
189 File nonExistentDir = new File(tmpOrHomeDir, NON_EXISTENT_DIR);
190 if (nonExistentDir.exists()) {
191 nonExistentDir.delete();
192 }
193 System.out.println("Setup completed - writableDir is: " + writableDir.getPath());
194 return writableDir;
195 }
196
197 /**
198 * @param newFile
199 * @return true if file already exists or creation succeeded
200 */
201 private static boolean createFile(File newFile, boolean makeDirectory) {
202 if (newFile.exists()) {
203 return true;
204 }
205 if (makeDirectory) {
206 return newFile.mkdir();
207 } else {
208 try {
209 return newFile.createNewFile();
210 } catch (IOException ioex) {
211 ioex.printStackTrace();
212 return false;
213 }
|