1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.file;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.junit.jupiter.api.Assumptions.assumeFalse;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.net.URI;
34 import java.net.URISyntaxException;
35 import java.net.URL;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.DirectoryStream;
38 import java.nio.file.FileSystem;
39 import java.nio.file.FileSystems;
40 import java.nio.file.Files;
41 import java.nio.file.LinkOption;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.nio.file.attribute.DosFileAttributeView;
45 import java.nio.file.attribute.FileTime;
46 import java.nio.file.attribute.PosixFileAttributes;
47 import java.util.GregorianCalendar;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.Map;
51
52 import org.apache.commons.io.FileUtils;
53 import org.apache.commons.io.FilenameUtils;
54 import org.apache.commons.io.filefilter.NameFileFilter;
55 import org.apache.commons.io.test.TestUtils;
56 import org.apache.commons.lang3.ArrayUtils;
57 import org.apache.commons.lang3.StringUtils;
58 import org.apache.commons.lang3.SystemProperties;
59 import org.apache.commons.lang3.SystemUtils;
60 import org.junit.jupiter.api.Test;
61
62
63
64
65 class PathUtilsTest extends AbstractTempDirTest {
66
67 private static final String STRING_FIXTURE = "Hello World";
68
69 private static final byte[] BYTE_ARRAY_FIXTURE = STRING_FIXTURE.getBytes(StandardCharsets.UTF_8);
70
71 private static final String TEST_JAR_NAME = "test.jar";
72
73 private static final String TEST_JAR_PATH = "src/test/resources/org/apache/commons/io/test.jar";
74
75 private static final String PATH_FIXTURE = "NOTICE.txt";
76
77 private Path current() {
78 return PathUtils.current();
79 }
80
81 private Long getLastModifiedMillis(final Path file) throws IOException {
82 return Files.getLastModifiedTime(file).toMillis();
83 }
84
85 private Path getNonExistentPath() {
86 return Paths.get("/does not exist/for/certain");
87 }
88
89 private FileSystem openArchive(final Path p, final boolean createNew) throws IOException {
90 if (createNew) {
91 final Map<String, String> env = new HashMap<>();
92 env.put("create", "true");
93 final URI fileUri = p.toAbsolutePath().toUri();
94 final URI uri = URI.create("jar:" + fileUri.toASCIIString());
95 return FileSystems.newFileSystem(uri, env, null);
96 }
97 return FileSystems.newFileSystem(p, (ClassLoader) null);
98 }
99
100 private void setLastModifiedMillis(final Path file, final long millis) throws IOException {
101 Files.setLastModifiedTime(file, FileTime.fromMillis(millis));
102 }
103
104 @Test
105 void testCopyDirectoryForDifferentFilesystemsWithAbsolutePath() throws IOException {
106 final Path archivePath = Paths.get(TEST_JAR_PATH);
107 try (FileSystem archive = openArchive(archivePath, false)) {
108
109 Path sourceDir = archive.getPath("dir1");
110 PathUtils.copyDirectory(sourceDir, tempDirPath);
111 assertTrue(Files.exists(tempDirPath.resolve("f1")));
112
113
114 sourceDir = archive.getPath("/next");
115 PathUtils.copyDirectory(sourceDir, tempDirPath);
116 assertTrue(Files.exists(tempDirPath.resolve("dir")));
117 }
118 }
119
120 @Test
121 void testCopyDirectoryForDifferentFilesystemsWithAbsolutePathReverse() throws IOException {
122 try (FileSystem archive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
123
124 Path targetDir = archive.getPath("target");
125 Files.createDirectory(targetDir);
126 final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toAbsolutePath();
127 PathUtils.copyDirectory(sourceDir, targetDir);
128 assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
129
130
131 targetDir = archive.getPath("/");
132 PathUtils.copyDirectory(sourceDir, targetDir);
133 assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
134 }
135 }
136
137 @Test
138 void testCopyDirectoryForDifferentFilesystemsWithRelativePath() throws IOException {
139 final Path archivePath = Paths.get(TEST_JAR_PATH);
140 try (FileSystem archive = openArchive(archivePath, false);
141 FileSystem targetArchive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
142 final Path targetDir = targetArchive.getPath("targetDir");
143 Files.createDirectory(targetDir);
144
145 Path sourceDir = archive.getPath("next");
146 PathUtils.copyDirectory(sourceDir, targetDir);
147 assertTrue(Files.exists(targetDir.resolve("dir")));
148
149
150 sourceDir = archive.getPath("/dir1");
151 PathUtils.copyDirectory(sourceDir, targetDir);
152 assertTrue(Files.exists(targetDir.resolve("f1")));
153 }
154 }
155
156 @Test
157 void testCopyDirectoryForDifferentFilesystemsWithRelativePathReverse() throws IOException {
158 try (FileSystem archive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
159
160 Path targetDir = archive.getPath("target");
161 Files.createDirectory(targetDir);
162 final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2");
163 PathUtils.copyDirectory(sourceDir, targetDir);
164 assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
165
166
167 targetDir = archive.getPath("/");
168 PathUtils.copyDirectory(sourceDir, targetDir);
169 assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
170 }
171 }
172
173 @Test
174 void testCopyFile() throws IOException {
175 final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
176 final Path targetFile = PathUtils.copyFileToDirectory(sourceFile, tempDirPath);
177 assertTrue(Files.exists(targetFile));
178 assertEquals(Files.size(sourceFile), Files.size(targetFile));
179 }
180
181 @Test
182 void testCopyFileTwoFileSystem() throws IOException {
183 try (FileSystem archive = openArchive(Paths.get(TEST_JAR_PATH), false)) {
184 final Path sourceFile = archive.getPath("next/dir/test.log");
185 final Path targetFile = PathUtils.copyFileToDirectory(sourceFile, tempDirPath);
186 assertTrue(Files.exists(targetFile));
187 assertEquals(Files.size(sourceFile), Files.size(targetFile));
188 }
189 }
190
191 @Test
192 void testCopyURL() throws IOException {
193 final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
194 final URL url = new URL("file:///" + FilenameUtils.getPath(sourceFile.toAbsolutePath().toString()) + sourceFile.getFileName());
195 final Path targetFile = PathUtils.copyFileToDirectory(url, tempDirPath);
196 assertTrue(Files.exists(targetFile));
197 assertEquals(Files.size(sourceFile), Files.size(targetFile));
198 }
199
200 @Test
201 void testCreateDirectoriesAlreadyExists() throws IOException {
202 assertEquals(tempDirPath.getParent(), PathUtils.createParentDirectories(tempDirPath));
203 }
204
205 @SuppressWarnings("resource")
206 @Test
207 void testCreateDirectoriesForRoots() throws IOException {
208 for (final Path path : FileSystems.getDefault().getRootDirectories()) {
209 final Path parent = path.getParent();
210 assertNull(parent);
211 assertEquals(parent, PathUtils.createParentDirectories(path));
212 }
213 }
214
215 @Test
216 void testCreateDirectoriesForRootsLinkOptionNull() throws IOException {
217 for (final File f : File.listRoots()) {
218 final Path path = f.toPath();
219 assertEquals(path.getParent(), PathUtils.createParentDirectories(path, (LinkOption) null));
220 }
221 }
222
223 @Test
224 void testCreateDirectoriesNew() throws IOException {
225 assertEquals(tempDirPath, PathUtils.createParentDirectories(tempDirPath.resolve("child")));
226 }
227
228 @Test
229 void testCreateDirectoriesSymlink() throws IOException {
230 final Path symlinkedDir = createTempSymbolicLinkedRelativeDir(tempDirPath);
231 final String leafDirName = "child";
232 final Path newDirFollowed = PathUtils.createParentDirectories(symlinkedDir.resolve(leafDirName), PathUtils.NULL_LINK_OPTION);
233 assertEquals(Files.readSymbolicLink(symlinkedDir), newDirFollowed);
234 }
235
236 @Test
237 void testCreateDirectoriesSymlinkClashing() throws IOException {
238 final Path symlinkedDir = createTempSymbolicLinkedRelativeDir(tempDirPath);
239 assertEquals(symlinkedDir, PathUtils.createParentDirectories(symlinkedDir.resolve("child")));
240 }
241
242 @Test
243 void testGetBaseNamePathBaseCases() {
244 assertEquals("bar", PathUtils.getBaseName(Paths.get("a/b/c/bar.foo")));
245 assertEquals("foo", PathUtils.getBaseName(Paths.get("foo")));
246 assertEquals("", PathUtils.getBaseName(Paths.get("")));
247 assertEquals("", PathUtils.getBaseName(Paths.get(".")));
248 for (final File f : File.listRoots()) {
249 assertNull(PathUtils.getBaseName(f.toPath()));
250 }
251 if (SystemUtils.IS_OS_WINDOWS) {
252 assertNull(PathUtils.getBaseName(Paths.get("C:\\")));
253 }
254 }
255
256 @Test
257 void testGetBaseNamePathCornerCases() {
258 assertNull(PathUtils.getBaseName((Path) null));
259 assertEquals("foo", PathUtils.getBaseName(Paths.get("foo.")));
260 assertEquals("", PathUtils.getBaseName(Paths.get("bar/.foo")));
261 }
262
263 @Test
264 void testGetDosFileAttributeView() {
265
266 final DosFileAttributeView dosFileAttributeView = PathUtils.getDosFileAttributeView(current());
267 final Path path = Paths.get("this-file-does-not-exist-at.all");
268 assertFalse(Files.exists(path));
269 if (SystemUtils.IS_OS_MAC) {
270 assertNull(dosFileAttributeView);
271
272 assertNull(PathUtils.getDosFileAttributeView(path));
273 } else {
274 assertNotNull(dosFileAttributeView);
275
276 assertNotNull(PathUtils.getDosFileAttributeView(path));
277 }
278
279 assertThrows(NullPointerException.class, () -> PathUtils.getDosFileAttributeView(null));
280 }
281
282 @Test
283 void testGetExtension() {
284 assertNull(PathUtils.getExtension(null));
285 assertEquals("ext", PathUtils.getExtension(Paths.get("file.ext")));
286 assertEquals("", PathUtils.getExtension(Paths.get("README")));
287 assertEquals("com", PathUtils.getExtension(Paths.get("domain.dot.com")));
288 assertEquals("jpeg", PathUtils.getExtension(Paths.get("image.jpeg")));
289 assertEquals("", PathUtils.getExtension(Paths.get("a.b/c")));
290 assertEquals("txt", PathUtils.getExtension(Paths.get("a.b/c.txt")));
291 assertEquals("", PathUtils.getExtension(Paths.get("a/b/c")));
292 assertEquals("", PathUtils.getExtension(Paths.get("a.b\\c")));
293 assertEquals("txt", PathUtils.getExtension(Paths.get("a.b\\c.txt")));
294 assertEquals("", PathUtils.getExtension(Paths.get("a\\b\\c")));
295 assertEquals("", PathUtils.getExtension(Paths.get("C:\\temp\\foo.bar\\README")));
296 assertEquals("ext", PathUtils.getExtension(Paths.get("../filename.ext")));
297
298 if (File.separatorChar != '\\') {
299
300 assertEquals("txt", PathUtils.getExtension(Paths.get("foo.exe:bar.txt")));
301 }
302 }
303
304 @Test
305 void testGetFileName() {
306 assertNull(PathUtils.getFileName(null, null));
307 assertNull(PathUtils.getFileName(null, Path::toString));
308 assertNull(PathUtils.getFileName(Paths.get("/"), Path::toString));
309 assertNull(PathUtils.getFileName(Paths.get("/"), Path::toString));
310 assertEquals("", PathUtils.getFileName(Paths.get(""), Path::toString));
311 assertEquals("a", PathUtils.getFileName(Paths.get("a"), Path::toString));
312 assertEquals("a", PathUtils.getFileName(Paths.get("p", "a"), Path::toString));
313 }
314
315 @Test
316 void testGetFileNameString() {
317 assertNull(PathUtils.getFileNameString(Paths.get("/")));
318 assertEquals("", PathUtils.getFileNameString(Paths.get("")));
319 assertEquals("a", PathUtils.getFileNameString(Paths.get("a")));
320 assertEquals("a", PathUtils.getFileNameString(Paths.get("p", "a")));
321 }
322
323 @Test
324 void testGetLastModifiedFileTime_File_Present() throws IOException {
325 assertNotNull(PathUtils.getLastModifiedFileTime(current().toFile()));
326 }
327
328 @Test
329 void testGetLastModifiedFileTime_Path_Absent() throws IOException {
330 assertNull(PathUtils.getLastModifiedFileTime(getNonExistentPath()));
331 }
332
333 @Test
334 void testGetLastModifiedFileTime_Path_FileTime_Absent() throws IOException {
335 final FileTime fromMillis = FileTime.fromMillis(0);
336 assertEquals(fromMillis, PathUtils.getLastModifiedFileTime(getNonExistentPath(), fromMillis));
337 }
338
339 @Test
340 void testGetLastModifiedFileTime_Path_Present() throws IOException {
341 assertNotNull(PathUtils.getLastModifiedFileTime(current()));
342 }
343
344 @Test
345 void testGetLastModifiedFileTime_URI_Present() throws IOException {
346 assertNotNull(PathUtils.getLastModifiedFileTime(current().toUri()));
347 }
348
349 @Test
350 void testGetLastModifiedFileTime_URL_Present() throws IOException, URISyntaxException {
351 assertNotNull(PathUtils.getLastModifiedFileTime(current().toUri().toURL()));
352 }
353
354 @Test
355 void testGetPath() {
356 final String validKey = "user.dir";
357 final Path value = Paths.get(System.getProperty(validKey));
358 assertEquals(value, PathUtils.getPath(validKey, null));
359 assertEquals(value, PathUtils.getPath(validKey, validKey));
360 final String invalidKey = "this property key does not exist";
361 assertEquals(value, PathUtils.getPath(invalidKey, value.toString()));
362 assertNull(PathUtils.getPath(invalidKey, null));
363 assertEquals(value, PathUtils.getPath(null, value.toString()));
364 assertEquals(value, PathUtils.getPath("", value.toString()));
365 }
366
367 @Test
368 void testGetTempDirectory() {
369 final Path tempDirectory = Paths.get(SystemProperties.getJavaIoTmpdir());
370 assertEquals(tempDirectory, PathUtils.getTempDirectory());
371 }
372
373 @Test
374 void testIsDirectory() throws IOException {
375 assertFalse(PathUtils.isDirectory(null));
376
377 assertTrue(PathUtils.isDirectory(tempDirPath));
378 try (TempFile testFile1 = TempFile.create(tempDirPath, "prefix", null)) {
379 assertFalse(PathUtils.isDirectory(testFile1.get()));
380
381 Path ref = null;
382 try (TempDirectory tempDir = TempDirectory.create(getClass().getCanonicalName())) {
383 ref = tempDir.get();
384 assertTrue(PathUtils.isDirectory(tempDir.get()));
385 }
386 assertFalse(PathUtils.isDirectory(ref));
387 }
388 }
389
390 @Test
391 void testIsPosix() throws IOException {
392 boolean isPosix;
393 try {
394 Files.getPosixFilePermissions(current());
395 isPosix = true;
396 } catch (final UnsupportedOperationException e) {
397 isPosix = false;
398 }
399 assertEquals(isPosix, PathUtils.isPosix(current()));
400 }
401
402 @Test
403 void testIsPosixAbsentFile() {
404 assertFalse(PathUtils.isPosix(Paths.get("ImNotHereAtAllEver.never")));
405 assertFalse(PathUtils.isPosix(null));
406 }
407
408 @Test
409 void testIsRegularFile() throws IOException {
410 assertFalse(PathUtils.isRegularFile(null));
411
412 assertFalse(PathUtils.isRegularFile(tempDirPath));
413 try (TempFile testFile1 = TempFile.create(tempDirPath, "prefix", null)) {
414 assertTrue(PathUtils.isRegularFile(testFile1.get()));
415
416 Files.delete(testFile1.get());
417 assertFalse(PathUtils.isRegularFile(testFile1.get()));
418 }
419 }
420
421 @Test
422 void testNewDirectoryStream() throws Exception {
423 final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE);
424 try (DirectoryStream<Path> stream = PathUtils.newDirectoryStream(current(), pathFilter)) {
425 final Iterator<Path> iterator = stream.iterator();
426 final Path path = iterator.next();
427 assertEquals(PATH_FIXTURE, PathUtils.getFileNameString(path));
428 assertFalse(iterator.hasNext());
429 }
430 }
431
432 @Test
433 void testNewOutputStreamExistingFileAppendFalse() throws IOException {
434 testNewOutputStreamNewFile(false);
435 testNewOutputStreamNewFile(false);
436 }
437
438 @Test
439 void testNewOutputStreamExistingFileAppendTrue() throws IOException {
440 testNewOutputStreamNewFile(true);
441 final Path file = writeToNewOutputStream(true);
442 assertArrayEquals(ArrayUtils.addAll(BYTE_ARRAY_FIXTURE, BYTE_ARRAY_FIXTURE), Files.readAllBytes(file));
443 }
444
445 void testNewOutputStreamNewFile(final boolean append) throws IOException {
446 final Path file = writeToNewOutputStream(append);
447 assertArrayEquals(BYTE_ARRAY_FIXTURE, Files.readAllBytes(file));
448 }
449
450 @Test
451 void testNewOutputStreamNewFileAppendFalse() throws IOException {
452 testNewOutputStreamNewFile(false);
453 }
454
455 @Test
456 void testNewOutputStreamNewFileAppendTrue() throws IOException {
457 testNewOutputStreamNewFile(true);
458 }
459
460 @Test
461 void testNewOutputStreamNewFileInsideExistingSymlinkedDir() throws IOException {
462 final Path symlinkDir = createTempSymbolicLinkedRelativeDir(tempDirPath);
463 final Path file = symlinkDir.resolve("test.txt");
464 try (OutputStream outputStream = PathUtils.newOutputStream(file, new LinkOption[] {})) {
465
466 }
467 try (OutputStream outputStream = PathUtils.newOutputStream(file, null)) {
468
469 }
470 try (OutputStream outputStream = PathUtils.newOutputStream(file, true)) {
471
472 }
473 try (OutputStream outputStream = PathUtils.newOutputStream(file, false)) {
474
475 }
476 }
477
478 @Test
479 void testReadAttributesPosix() throws IOException {
480 boolean isPosix;
481 try {
482 Files.getPosixFilePermissions(current());
483 isPosix = true;
484 } catch (final UnsupportedOperationException e) {
485 isPosix = false;
486 }
487 assertEquals(isPosix, PathUtils.readAttributes(current(), PosixFileAttributes.class) != null);
488 }
489
490 @Test
491 void testReadStringEmptyFile() throws IOException {
492 final Path path = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin");
493 assertEquals(StringUtils.EMPTY, PathUtils.readString(path, StandardCharsets.UTF_8));
494 assertEquals(StringUtils.EMPTY, PathUtils.readString(path, null));
495 }
496
497 @Test
498 void testReadStringSimpleUtf8() throws IOException {
499 final Path path = Paths.get("src/test/resources/org/apache/commons/io/test-file-simple-utf8.bin");
500 final String expected = "ABC\r\n";
501 assertEquals(expected, PathUtils.readString(path, StandardCharsets.UTF_8));
502 assertEquals(expected, PathUtils.readString(path, null));
503 }
504
505 @Test
506 void testSetReadOnlyFile() throws IOException {
507 final Path resolved = tempDirPath.resolve("testSetReadOnlyFile.txt");
508
509 final boolean isPosix = PathUtils.isPosix(tempDirPath);
510
511
512 assumeFalse(SystemUtils.IS_OS_LINUX);
513
514 PathUtils.writeString(resolved, "test", StandardCharsets.UTF_8);
515 final boolean readable = Files.isReadable(resolved);
516 final boolean writable = Files.isWritable(resolved);
517 final boolean regularFile = Files.isRegularFile(resolved);
518 final boolean executable = Files.isExecutable(resolved);
519 final boolean hidden = Files.isHidden(resolved);
520 final boolean directory = Files.isDirectory(resolved);
521 final boolean symbolicLink = Files.isSymbolicLink(resolved);
522
523 assertTrue(readable);
524 assertTrue(writable);
525
526 PathUtils.setReadOnly(resolved, false);
527 assertTrue(Files.isReadable(resolved), "isReadable");
528 assertTrue(Files.isWritable(resolved), "isWritable");
529
530 PathUtils.setReadOnly(resolved, false);
531 assertTrue(Files.isReadable(resolved), "isReadable");
532 assertTrue(Files.isWritable(resolved), "isWritable");
533
534 assertEquals(regularFile, Files.isReadable(resolved));
535 assertEquals(executable, Files.isExecutable(resolved));
536 assertEquals(hidden, Files.isHidden(resolved));
537 assertEquals(directory, Files.isDirectory(resolved));
538 assertEquals(symbolicLink, Files.isSymbolicLink(resolved));
539
540 PathUtils.setReadOnly(resolved, true);
541 if (isPosix) {
542
543 assertFalse(Files.isReadable(resolved), "isReadable");
544 } else {
545 assertTrue(Files.isReadable(resolved), "isReadable");
546 }
547 assertFalse(Files.isWritable(resolved), "isWritable");
548 final DosFileAttributeView dosFileAttributeView = PathUtils.getDosFileAttributeView(resolved);
549 if (dosFileAttributeView != null) {
550 assertTrue(dosFileAttributeView.readAttributes().isReadOnly());
551 }
552 if (isPosix) {
553 assertFalse(Files.isReadable(resolved));
554 } else {
555 assertEquals(regularFile, Files.isReadable(resolved));
556 }
557 assertEquals(executable, Files.isExecutable(resolved));
558 assertEquals(hidden, Files.isHidden(resolved));
559 assertEquals(directory, Files.isDirectory(resolved));
560 assertEquals(symbolicLink, Files.isSymbolicLink(resolved));
561
562 PathUtils.setReadOnly(resolved, false);
563 PathUtils.deleteFile(resolved);
564 }
565
566 @Test
567 void testSetReadOnlyFileAbsent() {
568 assertThrows(IOException.class, () -> PathUtils.setReadOnly(Paths.get("does-not-exist-at-all-ever-never"), true));
569 }
570
571 @Test
572 void testTouch() throws IOException {
573 assertThrows(NullPointerException.class, () -> FileUtils.touch(null));
574
575 final Path file = managedTempDirPath.resolve("touch.txt");
576 Files.deleteIfExists(file);
577 assertFalse(Files.exists(file), "Bad test: test file still exists");
578 PathUtils.touch(file);
579 assertTrue(Files.exists(file), "touch() created file");
580 try (OutputStream out = Files.newOutputStream(file)) {
581 assertEquals(0, Files.size(file), "Created empty file.");
582 out.write(0);
583 }
584 assertEquals(1, Files.size(file), "Wrote one byte to file");
585 final long y2k = new GregorianCalendar(2000, 0, 1).getTime().getTime();
586 setLastModifiedMillis(file, y2k);
587 assertEquals(y2k, getLastModifiedMillis(file), "Bad test: set lastModified set incorrect value");
588 final long nowMillis = System.currentTimeMillis();
589 PathUtils.touch(file);
590 assertEquals(1, Files.size(file), "FileUtils.touch() didn't empty the file.");
591 assertNotEquals(y2k, getLastModifiedMillis(file), "FileUtils.touch() changed lastModified");
592 final int delta = 3000;
593 assertTrue(getLastModifiedMillis(file) >= nowMillis - delta, "FileUtils.touch() changed lastModified to more than now-3s");
594 assertTrue(getLastModifiedMillis(file) <= nowMillis + delta, "FileUtils.touch() changed lastModified to less than now+3s");
595 }
596
597 @Test
598 void testWriteStringToFile1() throws Exception {
599 final Path file = tempDirPath.resolve("write.txt");
600 PathUtils.writeString(file, "Hello \u1234", StandardCharsets.UTF_8);
601 final byte[] text = "Hello \u1234".getBytes(StandardCharsets.UTF_8);
602 TestUtils.assertEqualContent(text, file);
603 }
604
605
606
607
608 private Path writeToNewOutputStream(final boolean append) throws IOException {
609 final Path file = tempDirPath.resolve("test1.txt");
610 try (OutputStream os = PathUtils.newOutputStream(file, append)) {
611 os.write(BYTE_ARRAY_FIXTURE);
612 }
613 return file;
614 }
615
616 }