Path: blob/master/test/jdk/java/nio/file/Files/SetLastModifiedTime.java
41153 views
/*1* Copyright (c) 2014, 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*/2223import java.io.File;24import java.nio.file.Files;25import java.nio.file.Path;26import java.nio.file.Paths;27import java.nio.file.attribute.FileTime;2829import org.testng.annotations.Test;30import org.testng.annotations.AfterClass;31import org.testng.annotations.BeforeClass;3233import static org.testng.Assert.assertEquals;34import static org.testng.Assert.assertTrue;35import static org.testng.Assert.assertFalse;3637/**38* @test39* @bug 4313887 8062949 819187240* @library ..41* @run testng SetLastModifiedTime42* @summary Unit test for Files.setLastModifiedTime43*/4445public class SetLastModifiedTime {4647static Path testDir;4849@BeforeClass50void createTestDirectory() throws Exception {51testDir = TestUtil.createTemporaryDirectory();52}5354@AfterClass55void removeTestDirectory() throws Exception {56TestUtil.removeAll(testDir);57}5859/**60* Exercise Files.setLastModifiedTime on the given file61*/62void test(Path path) throws Exception {63FileTime now = Files.getLastModifiedTime(path);64FileTime zero = FileTime.fromMillis(0L);6566Path result = Files.setLastModifiedTime(path, zero);67assertTrue(result == path);68assertEquals(Files.getLastModifiedTime(path), zero);6970result = Files.setLastModifiedTime(path, now);71assertTrue(result == path);72assertEquals(Files.getLastModifiedTime(path), now);73}7475@Test76public void testRegularFile() throws Exception {77Path file = Files.createFile(testDir.resolve("file"));78test(file);79}8081@Test82public void testDirectory() throws Exception {83Path dir = Files.createDirectory(testDir.resolve("dir"));84test(dir);85}8687@Test88public void testSymbolicLink() throws Exception {89if (TestUtil.supportsLinks(testDir)) {90Path target = Files.createFile(testDir.resolve("target"));91Path link = testDir.resolve("link");92Files.createSymbolicLink(link, target);93test(link);94}95}9697@Test98public void testNulls() throws Exception {99Path path = Paths.get("foo");100FileTime zero = FileTime.fromMillis(0L);101102try {103Files.setLastModifiedTime(null, zero);104assertTrue(false);105} catch (NullPointerException expected) { }106107try {108Files.setLastModifiedTime(path, null);109assertTrue(false);110} catch (NullPointerException expected) { }111112try {113Files.setLastModifiedTime(null, null);114assertTrue(false);115} catch (NullPointerException expected) { }116}117118@Test119public void testCompare() throws Exception {120Path path = Files.createFile(testDir.resolve("path"));121long timeMillis = 1512520600195L;122FileTime fileTime = FileTime.fromMillis(timeMillis);123Files.setLastModifiedTime(path, fileTime);124File file = path.toFile();125long ioTime = file.lastModified();126long nioTime = Files.getLastModifiedTime(path).toMillis();127assertTrue(ioTime == timeMillis || ioTime == 1000*(timeMillis/1000),128"File.lastModified() not in {time, 1000*(time/1000)}");129assertEquals(nioTime, ioTime,130"File.lastModified() != Files.getLastModifiedTime().toMillis()");131}132}133134135136