1/* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 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 24import java.nio.file.Files; 25import java.nio.file.Path; 26import java.nio.file.Paths; 27import java.nio.file.attribute.FileTime; 28 29import org.testng.annotations.Test; 30import org.testng.annotations.AfterClass; 31import org.testng.annotations.BeforeClass; 32 33import static org.testng.Assert.assertEquals; 34import static org.testng.Assert.assertTrue; 35import static org.testng.Assert.assertFalse; 36 37/** 38 * @test 39 * @bug 4313887 8062949 40 * @library .. 41 * @run testng SetLastModifiedTime 42 * @summary Unit test for Files.setLastModifiedTime 43 */ 44 45public class SetLastModifiedTime { 46 47 static Path testDir; 48 49 @BeforeClass 50 void createTestDirectory() throws Exception { 51 testDir = TestUtil.createTemporaryDirectory(); 52 } 53 54 @AfterClass 55 void removeTestDirectory() throws Exception { 56 TestUtil.removeAll(testDir); 57 } 58 59 /** 60 * Exercise Files.setLastModifiedTime on the given file 61 */ 62 void test(Path path) throws Exception { 63 FileTime now = Files.getLastModifiedTime(path); 64 FileTime zero = FileTime.fromMillis(0L); 65 66 Path result = Files.setLastModifiedTime(path, zero); 67 assertTrue(result == path); 68 assertEquals(Files.getLastModifiedTime(path), zero); 69 70 result = Files.setLastModifiedTime(path, now); 71 assertTrue(result == path); 72 assertEquals(Files.getLastModifiedTime(path), now); 73 } 74 75 @Test 76 public void testRegularFile() throws Exception { 77 Path file = Files.createFile(testDir.resolve("file")); 78 test(file); 79 } 80 81 @Test 82 public void testDirectory() throws Exception { 83 Path dir = Files.createDirectory(testDir.resolve("dir")); 84 test(dir); 85 } 86 87 @Test 88 public void testSymbolicLink() throws Exception { 89 if (TestUtil.supportsLinks(testDir)) { 90 Path target = Files.createFile(testDir.resolve("target")); 91 Path link = testDir.resolve("link"); 92 Files.createSymbolicLink(link, target); 93 test(link); 94 } 95 } 96 97 @Test 98 public void testNulls() throws Exception { 99 Path path = Paths.get("foo"); 100 FileTime zero = FileTime.fromMillis(0L); 101 102 try { 103 Files.setLastModifiedTime(null, zero); 104 assertTrue(false); 105 } catch (NullPointerException expected) { } 106 107 try { 108 Files.setLastModifiedTime(path, null); 109 assertTrue(false); 110 } catch (NullPointerException expected) { } 111 112 try { 113 Files.setLastModifiedTime(null, null); 114 assertTrue(false); 115 } catch (NullPointerException expected) { } 116 } 117} 118 119