1 /*
   2  * Copyright (c) 2014, 2017, 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 
  24 import java.io.File;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.nio.file.attribute.FileTime;
  29 
  30 import org.testng.annotations.Test;
  31 import org.testng.annotations.AfterClass;
  32 import org.testng.annotations.BeforeClass;
  33 
  34 import static org.testng.Assert.assertEquals;
  35 import static org.testng.Assert.assertTrue;
  36 import static org.testng.Assert.assertFalse;
  37 
  38 /**
  39  * @test
  40  * @bug 4313887 8062949 8191872
  41  * @library ..
  42  * @run testng SetLastModifiedTime
  43  * @summary Unit test for Files.setLastModifiedTime
  44  */
  45 
  46 public class SetLastModifiedTime {
  47 
  48     static Path testDir;
  49 
  50     @BeforeClass
  51     void createTestDirectory() throws Exception {
  52         testDir = TestUtil.createTemporaryDirectory();
  53     }
  54 
  55     @AfterClass
  56     void removeTestDirectory() throws Exception {
  57         TestUtil.removeAll(testDir);
  58     }
  59 
  60     /**
  61      * Exercise Files.setLastModifiedTime on the given file
  62      */
  63     void test(Path path) throws Exception {
  64         FileTime now = Files.getLastModifiedTime(path);
  65         FileTime zero = FileTime.fromMillis(0L);
  66 
  67         Path result = Files.setLastModifiedTime(path, zero);
  68         assertTrue(result == path);
  69         assertEquals(Files.getLastModifiedTime(path), zero);
  70 
  71         result = Files.setLastModifiedTime(path, now);
  72         assertTrue(result == path);
  73         assertEquals(Files.getLastModifiedTime(path), now);
  74     }
  75 
  76     @Test
  77     public void testRegularFile() throws Exception {
  78         Path file = Files.createFile(testDir.resolve("file"));
  79         test(file);
  80     }
  81 
  82     @Test
  83     public void testDirectory() throws Exception {
  84         Path dir = Files.createDirectory(testDir.resolve("dir"));
  85         test(dir);
  86     }
  87 
  88     @Test
  89     public void testSymbolicLink() throws Exception {
  90         if (TestUtil.supportsLinks(testDir)) {
  91             Path target = Files.createFile(testDir.resolve("target"));
  92             Path link = testDir.resolve("link");
  93             Files.createSymbolicLink(link, target);
  94             test(link);
  95         }
  96     }
  97 
  98     @Test
  99     public void testNulls() throws Exception {
 100         Path path = Paths.get("foo");
 101         FileTime zero = FileTime.fromMillis(0L);
 102 
 103         try {
 104             Files.setLastModifiedTime(null, zero);
 105             assertTrue(false);
 106         } catch (NullPointerException expected) { }
 107 
 108         try {
 109             Files.setLastModifiedTime(path, null);
 110             assertTrue(false);
 111         } catch (NullPointerException expected) { }
 112 
 113         try {
 114             Files.setLastModifiedTime(null, null);
 115             assertTrue(false);
 116         } catch (NullPointerException expected) { }
 117     }
 118 
 119     @Test
 120     public void testCompare() throws Exception {
 121         Path path = Files.createFile(testDir.resolve("path"));
 122         long timeMillis = 1512520600195L;
 123         FileTime fileTime = FileTime.fromMillis(timeMillis);
 124         Files.setLastModifiedTime(path, fileTime);
 125         File file = path.toFile();
 126         long ioTime = file.lastModified();
 127         long nioTime = Files.getLastModifiedTime(path).toMillis();
 128         assertTrue(ioTime == timeMillis || ioTime == 1000*(timeMillis/1000),
 129             "File.lastModified() not in {time, 1000*(time/1000)}");
 130         assertEquals(nioTime, ioTime,
 131             "File.lastModified() != Files.getLastModifiedTime().toMillis()");
 132     }
 133 }
 134