1 /*
   2  * Copyright (c) 2009, 2013, 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 /* @test
  25  * @bug 6844313 8011647
  26  * @summary Unit test for java.nio.file.FileTime
  27  */
  28 
  29 
  30 import java.nio.file.attribute.FileTime;
  31 import java.time.Instant;
  32 import java.util.concurrent.TimeUnit;
  33 import static java.util.concurrent.TimeUnit.*;
  34 import java.util.Random;
  35 import java.util.EnumSet;
  36 
  37 public class Basic {
  38 
  39     static final Random rand = new Random();
  40 
  41     public static void main(String[] args) {
  42         long now = System.currentTimeMillis();
  43         long tomorrowInDays = TimeUnit.DAYS.convert(now, MILLISECONDS) + 1;
  44         long yesterdayInDays = TimeUnit.DAYS.convert(now, MILLISECONDS) - 1;
  45 
  46         Instant nowInstant = Instant.ofEpochMilli(now);
  47 
  48         // equals
  49         eq(now, MILLISECONDS, now, MILLISECONDS);
  50         eq(now, MILLISECONDS, now*1000L, MICROSECONDS);
  51         neq(now, MILLISECONDS, 0, MILLISECONDS);
  52         neq(now, MILLISECONDS, 0, MICROSECONDS);
  53 
  54         eq(nowInstant, now, MILLISECONDS);
  55         eq(nowInstant, now*1000L, MICROSECONDS);
  56         neq(nowInstant, 0, MILLISECONDS);
  57         neq(nowInstant, 0, MICROSECONDS);
  58 
  59         // compareTo
  60         cmp(now, MILLISECONDS, now, MILLISECONDS, 0);
  61         cmp(now, MILLISECONDS, now*1000L, MICROSECONDS, 0);
  62         cmp(now, MILLISECONDS, now-1234, MILLISECONDS, 1);
  63         cmp(now, MILLISECONDS, now+1234, MILLISECONDS, -1);
  64 
  65         cmp(tomorrowInDays, DAYS, now, MILLISECONDS, 1);
  66         cmp(now, MILLISECONDS, tomorrowInDays, DAYS, -1);
  67         cmp(yesterdayInDays, DAYS, now, MILLISECONDS, -1);
  68         cmp(now, MILLISECONDS, yesterdayInDays, DAYS, 1);
  69         cmp(yesterdayInDays, DAYS, now, MILLISECONDS, -1);
  70 
  71         cmp(Long.MAX_VALUE, DAYS, Long.MAX_VALUE, NANOSECONDS, 1);
  72         cmp(Long.MAX_VALUE, DAYS, Long.MIN_VALUE, NANOSECONDS, 1);
  73         cmp(Long.MIN_VALUE, DAYS, Long.MIN_VALUE, NANOSECONDS, -1);
  74         cmp(Long.MIN_VALUE, DAYS, Long.MAX_VALUE, NANOSECONDS, -1);
  75 
  76         cmp(Instant.MIN, Long.MIN_VALUE, DAYS, 1);
  77         cmp(Instant.MIN, Long.MIN_VALUE, HOURS, 1);
  78         cmp(Instant.MIN, Long.MIN_VALUE, MINUTES, 1);
  79         cmp(Instant.MIN, Long.MIN_VALUE, SECONDS, 1);
  80         cmp(Instant.MIN, Instant.MIN.getEpochSecond() - 1, SECONDS, 1);
  81         cmp(Instant.MIN, Instant.MIN.getEpochSecond() - 100, SECONDS, 1);
  82         cmp(Instant.MIN, Instant.MIN.getEpochSecond(), SECONDS, 0);
  83 
  84         cmp(Instant.MAX, Long.MAX_VALUE, DAYS, -1);
  85         cmp(Instant.MAX, Long.MAX_VALUE, HOURS, -1);
  86         cmp(Instant.MAX, Long.MAX_VALUE, MINUTES, -1);
  87         cmp(Instant.MAX, Long.MAX_VALUE, SECONDS, -1);
  88         cmp(Instant.MAX, Instant.MAX.getEpochSecond() + 1, SECONDS, -1);
  89         cmp(Instant.MAX, Instant.MAX.getEpochSecond() + 100, SECONDS, -1);
  90         cmp(Instant.MAX, Instant.MAX.getEpochSecond(), SECONDS, 0);
  91 
  92         cmp(nowInstant, now, MILLISECONDS, 0);
  93         cmp(nowInstant, now*1000L, MICROSECONDS, 0);
  94         cmp(nowInstant, now-1234, MILLISECONDS, 1);
  95         cmp(nowInstant, now+1234, MILLISECONDS, -1);
  96         cmp(nowInstant, tomorrowInDays, DAYS, -1);
  97         cmp(nowInstant, yesterdayInDays, DAYS, 1);
  98 
  99         // to(TimeUnit)
 100         to(MILLISECONDS.convert(1, DAYS) - 1, MILLISECONDS);
 101         to(MILLISECONDS.convert(1, DAYS) + 0, MILLISECONDS);
 102         to(MILLISECONDS.convert(1, DAYS) + 1, MILLISECONDS);
 103         to(1, MILLISECONDS);
 104         to(0, MILLISECONDS);
 105         to(1, MILLISECONDS);
 106         to(MILLISECONDS.convert(-1, DAYS) - 1, MILLISECONDS);
 107         to(MILLISECONDS.convert(-1, DAYS) + 0, MILLISECONDS);
 108         to(MILLISECONDS.convert(-1, DAYS) + 1, MILLISECONDS);
 109         for (TimeUnit unit: TimeUnit.values()) {
 110             for (int i=0; i<100; i++) { to(rand.nextLong(), unit); }
 111             to(Long.MIN_VALUE, unit);
 112             to(Long.MAX_VALUE, unit);
 113         }
 114 
 115         // toInstant()
 116         int N = 1000;
 117         for (TimeUnit unit : EnumSet.allOf(TimeUnit.class)) {
 118             for (int i = 0; i < N; i++) {
 119                 long value = rand.nextLong();
 120                 FileTime ft = FileTime.from(value, unit);
 121                 Instant instant = ft.toInstant();
 122                 if (instant != Instant.MIN && instant != Instant.MAX) {
 123                     eqTime(value, unit, instant);
 124                 }
 125             }
 126         }
 127         for (TimeUnit unit : EnumSet.allOf(TimeUnit.class)) {
 128             long value = Long.MIN_VALUE;
 129             FileTime ft = FileTime.from(value, unit);
 130             Instant instant = ft.toInstant();
 131             if (unit.compareTo(TimeUnit.SECONDS) < 0) {
 132                 eqTime(value, unit, instant);
 133             } else if (!instant.equals(Instant.MIN)) {
 134                 throw new RuntimeException("should overflow to MIN");
 135             }
 136             value = Long.MAX_VALUE;
 137             ft = FileTime.from(value, unit);
 138             instant = ft.toInstant();
 139             if (unit.compareTo(TimeUnit.SECONDS) < 0) {
 140                 eqTime(value, unit, instant);
 141             } else if (!instant.equals(Instant.MAX)) {
 142                 throw new RuntimeException("should overflow to MAX");
 143             }
 144         }
 145 
 146         // from(Instant)
 147         final long MAX_SECOND = 31556889864403199L;
 148         for (int i = 0; i < N; i++) {
 149             long v = rand.nextLong();
 150             long secs = v % MAX_SECOND;
 151             Instant instant = Instant.ofEpochSecond(secs, rand.nextInt(1000_000_000));
 152             FileTime ft = FileTime.from(instant);
 153             if (!ft.toInstant().equals(instant) || ft.to(SECONDS)  != secs) {
 154                 throw new RuntimeException("from(Instant) failed");
 155             }
 156             long millis = v;
 157             instant = Instant.ofEpochMilli(millis);
 158             ft = FileTime.from(instant);
 159             if (!ft.toInstant().equals(instant) ||
 160                 ft.toMillis()  != instant.toEpochMilli()) {
 161                 throw new RuntimeException("from(Instant) failed");
 162             }
 163             long nanos = v;
 164             ft = FileTime.from(nanos, NANOSECONDS);
 165             secs = nanos / 1000_000_000;
 166             nanos = nanos % 1000_000_000;
 167             instant = Instant.ofEpochSecond(secs, nanos);
 168             if (!ft.equals(FileTime.from(instant))) {
 169                 throw new RuntimeException("from(Instant) failed");
 170             }
 171         }
 172 
 173         // toString
 174         ts(1L, DAYS, "1970-01-02T00:00:00Z");
 175         ts(1L, HOURS, "1970-01-01T01:00:00Z");
 176         ts(1L, MINUTES, "1970-01-01T00:01:00Z");
 177         ts(1L, SECONDS, "1970-01-01T00:00:01Z");
 178         ts(1L, MILLISECONDS, "1970-01-01T00:00:00.001Z");
 179         ts(1L, MICROSECONDS, "1970-01-01T00:00:00.000001Z");
 180         ts(1L, NANOSECONDS, "1970-01-01T00:00:00.000000001Z");
 181         ts(999999999L, NANOSECONDS, "1970-01-01T00:00:00.999999999Z");
 182         ts(9999999999L, NANOSECONDS, "1970-01-01T00:00:09.999999999Z");
 183 
 184         ts(-1L, DAYS, "1969-12-31T00:00:00Z");
 185         ts(-1L, HOURS, "1969-12-31T23:00:00Z");
 186         ts(-1L, MINUTES, "1969-12-31T23:59:00Z");
 187         ts(-1L, SECONDS, "1969-12-31T23:59:59Z");
 188         ts(-1L, MILLISECONDS, "1969-12-31T23:59:59.999Z");
 189         ts(-1L, MICROSECONDS, "1969-12-31T23:59:59.999999Z");
 190         ts(-1L, NANOSECONDS, "1969-12-31T23:59:59.999999999Z");
 191         ts(-999999999L, NANOSECONDS, "1969-12-31T23:59:59.000000001Z");
 192         ts(-9999999999L, NANOSECONDS, "1969-12-31T23:59:50.000000001Z");
 193 
 194         ts(-62135596799999L, MILLISECONDS, "0001-01-01T00:00:00.001Z");
 195         ts(-62135596800000L, MILLISECONDS, "0001-01-01T00:00:00Z");
 196         ts(-62135596800001L, MILLISECONDS, "-0001-12-31T23:59:59.999Z");
 197 
 198         ts(253402300799999L, MILLISECONDS, "9999-12-31T23:59:59.999Z");
 199         ts(-377642044800001L, MILLISECONDS, "-9999-12-31T23:59:59.999Z");
 200 
 201         // NTFS epoch in usec.
 202         ts(-11644473600000000L, MICROSECONDS, "1601-01-01T00:00:00Z");
 203 
 204         ts(Instant.MIN, "-1000000001-01-01T00:00:00Z");
 205         ts(Instant.MAX, "1000000000-12-31T23:59:59.999999999Z");
 206 
 207         try {
 208             FileTime.from(0L, null);
 209             throw new RuntimeException("NullPointerException expected");
 210         } catch (NullPointerException npe) { }
 211         try {
 212             FileTime.from(null);
 213             throw new RuntimeException("NullPointerException expected");
 214         } catch (NullPointerException npe) { }
 215 
 216         FileTime time = FileTime.fromMillis(now);
 217         if (time.equals(null))
 218             throw new RuntimeException("should not be equal to null");
 219         try {
 220             time.compareTo(null);
 221             throw new RuntimeException("NullPointerException expected");
 222         } catch (NullPointerException npe) { }
 223 
 224         // Instant + toMilli() overflow
 225         overflow(Long.MAX_VALUE,
 226                  FileTime.from(Instant.MAX).toMillis());
 227         overflow(Long.MAX_VALUE,
 228                  FileTime.from(Instant.ofEpochSecond(Long.MAX_VALUE / 1000 + 1))
 229                          .toMillis());
 230         overflow(Long.MIN_VALUE,
 231                  FileTime.from(Instant.MIN).toMillis());
 232         overflow(Long.MIN_VALUE,
 233                  FileTime.from(Instant.ofEpochSecond(Long.MIN_VALUE / 1000 - 1))
 234                          .toMillis());
 235 
 236         // Instant + to(TimeUnit) overflow
 237         overflow(Long.MAX_VALUE,
 238                  FileTime.from(Instant.ofEpochSecond(Long.MAX_VALUE / 1000 + 1))
 239                          .to(MILLISECONDS));
 240         overflow(Long.MAX_VALUE,
 241                  FileTime.from(Instant.ofEpochSecond(Long.MAX_VALUE / 1000,
 242                                                      MILLISECONDS.toNanos(1000)))
 243                     .to(MILLISECONDS));
 244         overflow(Long.MIN_VALUE,
 245                  FileTime.from(Instant.ofEpochSecond(Long.MIN_VALUE / 1000 - 1))
 246                          .to(MILLISECONDS));
 247         overflow(Long.MIN_VALUE,
 248                  FileTime.from(Instant.ofEpochSecond(Long.MIN_VALUE / 1000,
 249                                                      -MILLISECONDS.toNanos(1)))
 250                          .to(MILLISECONDS));
 251     }
 252 
 253     static void overflow(long minmax, long v) {
 254         if (v != minmax)
 255             throw new RuntimeException("saturates to Long.MIN/MAX_VALUE expected");
 256     }
 257 
 258     static void cmp(long v1, TimeUnit u1, long v2, TimeUnit u2, int expected) {
 259         int result = FileTime.from(v1, u1).compareTo(FileTime.from(v2, u2));
 260         if (result != expected)
 261             throw new RuntimeException("unexpected order");
 262     }
 263 
 264     static void cmp(Instant ins, long v2, TimeUnit u2, int expected) {
 265         int result = FileTime.from(ins).compareTo(FileTime.from(v2, u2));
 266         if (result != expected)
 267             throw new RuntimeException("unexpected order");
 268     }
 269 
 270     static void eq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
 271         FileTime t1 = FileTime.from(v1, u1);
 272         FileTime t2 = FileTime.from(v2, u2);
 273         if (!t1.equals(t2))
 274             throw new RuntimeException("not equal");
 275         if (t1.hashCode() != t2.hashCode())
 276             throw new RuntimeException("hashCodes should be equal");
 277     }
 278 
 279     static void eq(Instant ins, long v2, TimeUnit u2) {
 280         FileTime t1 = FileTime.from(ins);
 281         FileTime t2 = FileTime.from(v2, u2);
 282         if (!t1.equals(t2))
 283             throw new RuntimeException("not equal");
 284         if (t1.hashCode() != t2.hashCode())
 285             throw new RuntimeException("hashCodes should be equal");
 286     }
 287 
 288     static void eqTime(long value, TimeUnit unit, Instant instant) {
 289         long secs = SECONDS.convert(value, unit);
 290         long nanos = NANOSECONDS.convert(value - unit.convert(secs, SECONDS), unit);
 291         if (nanos < 0) {    // normalize nanoOfSecond to positive
 292             secs -= 1;
 293             nanos += 1000_000_000;
 294         }
 295         if (secs != instant.getEpochSecond() || (int)nanos != instant.getNano()) {
 296             System.err.println(" ins=" + instant);
 297             throw new RuntimeException("ft and instant are not the same time point");
 298         }
 299     }
 300 
 301     static void neq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
 302         FileTime t1 = FileTime.from(v1, u1);
 303         FileTime t2 = FileTime.from(v2, u2);
 304         if (t1.equals(t2))
 305             throw new RuntimeException("should not be equal");
 306     }
 307 
 308     static void neq(Instant ins, long v2, TimeUnit u2) {
 309         FileTime t1 = FileTime.from(ins);
 310         FileTime t2 = FileTime.from(v2, u2);
 311         if (t1.equals(t2))
 312             throw new RuntimeException("should not be equal");
 313     }
 314 
 315     static void to(long v, TimeUnit unit) {
 316         FileTime t = FileTime.from(v, unit);
 317         for (TimeUnit u: TimeUnit.values()) {
 318             long result = t.to(u);
 319             long expected = u.convert(v, unit);
 320             if (result != expected) {
 321                 throw new RuntimeException("unexpected result");
 322             }
 323         }
 324     }
 325 
 326     static void ts(long v, TimeUnit unit, String expected) {
 327         String result = FileTime.from(v, unit).toString();
 328         if (!result.equals(expected)) {
 329             System.err.format("FileTime.from(%d, %s).toString() failed\n", v, unit);
 330             System.err.format("Expected: %s\n", expected);
 331             System.err.format("     Got: %s\n", result);
 332             throw new RuntimeException();
 333         }
 334     }
 335 
 336     static void ts(Instant instant, String expected) {
 337         String result = FileTime.from(instant).toString();
 338         if (!result.equals(expected)) {
 339             System.err.format("FileTime.from(%s).toString() failed\n", instant);
 340             System.err.format("Expected: %s\n", expected);
 341             System.err.format("     Got: %s\n", result);
 342             throw new RuntimeException();
 343         }
 344     }
 345 }