|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
22 | 22 | */
|
23 | 23 |
|
24 | 24 | /* @test
|
25 |
| - @bug 4842706 |
26 |
| - @summary Test some file operations with empty path |
| 25 | + * @bug 4842706 8024695 |
| 26 | + * @summary Test some file operations with empty path |
| 27 | + * @run junit EmptyPath |
27 | 28 | */
|
28 | 29 |
|
29 |
| -import java.io.*; |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileInputStream; |
| 32 | +import java.io.FileNotFoundException; |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.FileStore; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.HashSet; |
| 39 | +import java.util.Set; |
| 40 | + |
| 41 | +import org.junit.jupiter.api.BeforeAll; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | + |
| 44 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 45 | +import org.junit.jupiter.api.condition.EnabledOnOs; |
| 46 | +import org.junit.jupiter.api.condition.OS; |
| 47 | + |
| 48 | +import static org.junit.jupiter.api.Assertions.*; |
30 | 49 |
|
31 | 50 | public class EmptyPath {
|
32 |
| - public static void main(String [] args) throws Exception { |
33 |
| - File f = new File(""); |
34 |
| - f.mkdir(); |
| 51 | + private static final String EMPTY_STRING = ""; |
| 52 | + |
| 53 | + static File f; |
| 54 | + static Path p; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + public static void init() { |
| 58 | + f = new File(EMPTY_STRING); |
| 59 | + p = Path.of(EMPTY_STRING); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void canExecute() { |
| 64 | + assertTrue(f.canExecute()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void canRead() { |
| 69 | + assertTrue(f.canRead()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void canWrite() { |
| 74 | + assertTrue(f.canWrite()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void compareTo() { |
| 79 | + assertEquals(0, f.compareTo(p.toFile())); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void createNewFile() { |
| 84 | + assertThrows(IOException.class, () -> f.createNewFile()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void open() throws FileNotFoundException { |
| 89 | + assertThrows(FileNotFoundException.class, |
| 90 | + () -> new FileInputStream(f)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void delete() { |
| 95 | + assertFalse(f.delete()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void equals() { |
| 100 | + assertTrue(f.equals(p.toFile())); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void exists() { |
| 105 | + assertTrue(f.exists()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void getAbsolutePath() { |
| 110 | + System.out.println(p.toAbsolutePath().toString() + "\n" + |
| 111 | + f.getAbsolutePath()); |
| 112 | + assertEquals(p.toAbsolutePath().toString(), f.getAbsolutePath()); |
| 113 | + } |
| 114 | + |
| 115 | + private void checkSpace(long expected, long actual) { |
| 116 | + if (expected == 0) { |
| 117 | + assertEquals(0L, actual); |
| 118 | + } else { |
| 119 | + assertTrue(actual > 0); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void getFreeSpace() throws IOException { |
| 125 | + FileStore fs = Files.getFileStore(f.toPath()); |
| 126 | + checkSpace(fs.getUnallocatedSpace(), f.getFreeSpace()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void getName() { |
| 131 | + assertEquals(p.getFileName().toString(), f.getName()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void getParent() { |
| 136 | + assertNull(f.getParent()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void getPath() { |
| 141 | + assertEquals(p.toString(), f.getPath()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void getTotalSpace() throws IOException { |
| 146 | + FileStore fs = Files.getFileStore(f.toPath()); |
| 147 | + checkSpace(fs.getTotalSpace(), f.getTotalSpace()); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void getUsableSpace() throws IOException { |
| 152 | + FileStore fs = Files.getFileStore(f.toPath()); |
| 153 | + checkSpace(fs.getUsableSpace(), f.getUsableSpace()); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void isNotAbsolute() { |
| 158 | + assertFalse(f.isAbsolute()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void isAbsolute() { |
| 163 | + assertTrue(f.getAbsoluteFile().isAbsolute()); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void isDirectory() { |
| 168 | + assertTrue(f.isDirectory()); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void isFile() { |
| 173 | + assertFalse(f.isFile()); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void isHidden() { |
| 178 | + assertFalse(f.isHidden()); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void lastModified() { |
| 183 | + assertTrue(f.lastModified() > 0); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void length() throws IOException { |
| 188 | + assertEquals(Files.size(f.toPath()), f.length()); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void list() throws IOException { |
| 193 | + String[] files = f.list(); |
| 194 | + assertNotNull(files); |
| 195 | + Set<String> ioSet = new HashSet(Arrays.asList(files)); |
| 196 | + Set<String> nioSet = new HashSet(); |
| 197 | + Files.list(p).forEach((x) -> nioSet.add(x.toString())); |
| 198 | + assertEquals(nioSet, ioSet); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void mkdir() { |
| 203 | + assertFalse(f.mkdir()); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void setLastModified() { |
| 208 | + long t0 = f.lastModified(); |
| 209 | + long t = System.currentTimeMillis(); |
| 210 | + try { |
| 211 | + assertTrue(f.setLastModified(t)); |
| 212 | + assertEquals(t, f.lastModified()); |
| 213 | + assertTrue(f.setLastModified(t0)); |
| 214 | + assertEquals(t0, f.lastModified()); |
| 215 | + } finally { |
| 216 | + f.setLastModified(t0); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + // Note: Testing File.setExecutable is omitted because calling |
| 221 | + // File.setExecutable(false) makes it impossible to set the CWD to |
| 222 | + // executable again which makes subsequent tests fail |
| 223 | + |
| 224 | + @Test |
| 225 | + @DisabledOnOs({OS.WINDOWS}) |
| 226 | + public void setReadable() { |
| 227 | + assertTrue(f.canRead()); |
| 228 | + try { |
| 229 | + assertTrue(f.setReadable(false)); |
| 230 | + assertFalse(f.canRead()); |
| 231 | + assertTrue(f.setReadable(true)); |
| 232 | + assertTrue(f.canRead()); |
| 233 | + } finally { |
| 234 | + f.setReadable(true); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + @DisabledOnOs({OS.WINDOWS}) |
| 240 | + public void setReadOnly() { |
| 241 | + assertTrue(f.canExecute()); |
| 242 | + assertTrue(f.canRead()); |
| 243 | + assertTrue(f.canWrite()); |
35 | 244 | try {
|
36 |
| - f.createNewFile(); |
37 |
| - throw new RuntimeException("Expected exception not thrown"); |
38 |
| - } catch (IOException ioe) { |
39 |
| - // Correct result |
| 245 | + assertTrue(f.setReadOnly()); |
| 246 | + assertTrue(f.canRead()); |
| 247 | + assertFalse(f.canWrite()); |
| 248 | + assertTrue(f.setWritable(true, true)); |
| 249 | + assertTrue(f.canWrite()); |
| 250 | + } finally { |
| 251 | + f.setWritable(true, true); |
40 | 252 | }
|
| 253 | + } |
| 254 | + |
| 255 | + @Test |
| 256 | + @DisabledOnOs({OS.WINDOWS}) |
| 257 | + public void setWritable() { |
| 258 | + assertTrue(f.canWrite()); |
41 | 259 | try {
|
42 |
| - FileInputStream fis = new FileInputStream(f); |
43 |
| - fis.close(); |
44 |
| - throw new RuntimeException("Expected exception not thrown"); |
45 |
| - } catch (FileNotFoundException fnfe) { |
46 |
| - // Correct result |
| 260 | + assertTrue(f.setWritable(false, true)); |
| 261 | + assertFalse(f.canWrite()); |
| 262 | + assertTrue(f.setWritable(true, true)); |
| 263 | + assertTrue(f.canWrite()); |
| 264 | + } finally { |
| 265 | + f.setWritable(true, true); |
47 | 266 | }
|
48 | 267 | }
|
| 268 | + |
| 269 | + @Test |
| 270 | + public void toPath() { |
| 271 | + assertEquals(p, f.toPath()); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void toURI() { |
| 276 | + assertEquals(f.toPath().toUri(), f.toURI()); |
| 277 | + } |
49 | 278 | }
|
0 commit comments