< prev index next >

test/jdk/java/nio/file/attribute/BasicFileAttributeView/SetTimesNanos.java

Print this page

        

@@ -32,39 +32,31 @@
 import java.nio.file.FileStore;
 import java.nio.file.Path;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.nio.file.attribute.BasicFileAttributeView;
 import java.nio.file.attribute.FileTime;
-import java.time.Instant;
-import java.time.ZoneId;
-import java.time.ZoneOffset;
-import java.time.format.DateTimeFormatter;
-import java.util.Arrays;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
 public class SetTimesNanos {
     public static void main(String[] args) throws IOException,
         InterruptedException {
 
         Path dirPath = Path.of("test");
         Path dir = Files.createDirectory(dirPath);
         FileStore store = Files.getFileStore(dir);
-        System.out.format("FileStore: %s on %s (%s)%n", dir, store.name(),
-            store.type());
-        if (System.getProperty("os.name").toLowerCase().startsWith("mac") &&
-            store.type().equalsIgnoreCase("hfs")) {
-            System.err.println
-                ("HFS on macOS does not have nsec timestamps: skipping test");
-            return;
-        }
-        testNanos(dir);
+        String type = store.type().toLowerCase();
+        boolean strict = Set.of("apfs", "ext4", "xfs", "zfs").contains(type);
+        System.out.format("FileStore: %s on %s (%s, strict=%s)%n",
+            dir, store.name(), store.type(), strict);
+        testNanos(dir, strict);
 
         Path file = Files.createFile(dir.resolve("test.dat"));
-        testNanos(file);
+        testNanos(file, strict);
     }
 
-    private static void testNanos(Path path) throws IOException {
+    private static void testNanos(Path path, boolean strict) throws IOException {
         // Set modification and access times
         // Time stamp = "2017-01-01 01:01:01.123456789";
         long timeNanos = 1_483_261_261L*1_000_000_000L + 123_456_789L;
         FileTime pathTime = FileTime.from(timeNanos, TimeUnit.NANOSECONDS);
         BasicFileAttributeView view =

@@ -80,12 +72,34 @@
         FileTime[] times = new FileTime[] {attrs.lastModifiedTime(),
             attrs.lastAccessTime()};
         for (int i = 0; i < timeNames.length; i++) {
             long nanos = times[i].to(TimeUnit.NANOSECONDS);
             if (nanos != timeNanos) {
+                boolean failed = strict;
+                if (!failed) {
+                    String expected = String.valueOf(timeNanos);
+                    String actual = String.valueOf(nanos);
+                    int len = actual.length();
+                    failed = len != expected.length();
+                    if (!failed) {
+                        int nz = 0;
+                        for (int j = len - 1; j >= 0; j--) {
+                            if (actual.charAt(j) == '0') {
+                                nz++;
+                            } else {
+                                break;
+                            }
+                        }
+                        failed = (nz % 3) != 0 ||
+                            !actual.substring(0, len - nz).equals(
+                                expected.substring(0, len - nz));
+                    }
+                }
+                if (failed) {
                 throw new RuntimeException("Expected " + timeNames[i] +
                     " timestamp to be '" + timeNanos + "', but was '" +
                     nanos + "'");
             }
         }
     }
+    }
 }
< prev index next >