1319N/A/*
3909N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1319N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1319N/A *
1319N/A * This code is free software; you can redistribute it and/or modify it
1319N/A * under the terms of the GNU General Public License version 2 only, as
1319N/A * published by the Free Software Foundation.
1319N/A *
1319N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1319N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1319N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1319N/A * version 2 for more details (a copy is included in the LICENSE file that
1319N/A * accompanied this code).
1319N/A *
1319N/A * You should have received a copy of the GNU General Public License version
1319N/A * 2 along with this work; if not, write to the Free Software Foundation,
1319N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1319N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1319N/A */
1319N/A
1319N/A/* @test
1319N/A * @bug 6844313
1319N/A * @summary Unit test for java.nio.file.FileTime
1319N/A */
1319N/A
1319N/Aimport java.nio.file.attribute.FileTime;
1319N/Aimport java.util.concurrent.TimeUnit;
1319N/Aimport static java.util.concurrent.TimeUnit.*;
3471N/Aimport java.util.Random;
1319N/A
1319N/Apublic class Basic {
1319N/A
3471N/A static final Random rand = new Random();
3471N/A
3471N/A public static void main(String[] args) {
1319N/A long now = System.currentTimeMillis();
1319N/A long tomorrowInDays = TimeUnit.DAYS.convert(now, MILLISECONDS) + 1;
3471N/A long yesterdayInDays = TimeUnit.DAYS.convert(now, MILLISECONDS) - 1;
1319N/A
1319N/A // equals
1319N/A eq(now, MILLISECONDS, now, MILLISECONDS);
1319N/A eq(now, MILLISECONDS, now*1000L, MICROSECONDS);
1319N/A neq(now, MILLISECONDS, 0, MILLISECONDS);
1319N/A neq(now, MILLISECONDS, 0, MICROSECONDS);
1319N/A
1319N/A // compareTo
1319N/A cmp(now, MILLISECONDS, now, MILLISECONDS, 0);
1319N/A cmp(now, MILLISECONDS, now*1000L, MICROSECONDS, 0);
1319N/A cmp(now, MILLISECONDS, now-1234, MILLISECONDS, 1);
1319N/A cmp(now, MILLISECONDS, now+1234, MILLISECONDS, -1);
1319N/A cmp(tomorrowInDays, DAYS, now, MILLISECONDS, 1);
1319N/A cmp(now, MILLISECONDS, tomorrowInDays, DAYS, -1);
3471N/A cmp(yesterdayInDays, DAYS, now, MILLISECONDS, -1);
3471N/A cmp(now, MILLISECONDS, yesterdayInDays, DAYS, 1);
3471N/A cmp(yesterdayInDays, DAYS, now, MILLISECONDS, -1);
3471N/A cmp(Long.MAX_VALUE, DAYS, Long.MAX_VALUE, NANOSECONDS, 1);
3471N/A cmp(Long.MAX_VALUE, DAYS, Long.MIN_VALUE, NANOSECONDS, 1);
3471N/A cmp(Long.MIN_VALUE, DAYS, Long.MIN_VALUE, NANOSECONDS, -1);
3471N/A cmp(Long.MIN_VALUE, DAYS, Long.MAX_VALUE, NANOSECONDS, -1);
3471N/A
3471N/A // to(TimeUnit)
3471N/A to(MILLISECONDS.convert(1, DAYS) - 1, MILLISECONDS);
3471N/A to(MILLISECONDS.convert(1, DAYS) + 0, MILLISECONDS);
3471N/A to(MILLISECONDS.convert(1, DAYS) + 1, MILLISECONDS);
3471N/A to(1, MILLISECONDS);
3471N/A to(0, MILLISECONDS);
3471N/A to(1, MILLISECONDS);
3471N/A to(MILLISECONDS.convert(-1, DAYS) - 1, MILLISECONDS);
3471N/A to(MILLISECONDS.convert(-1, DAYS) + 0, MILLISECONDS);
3471N/A to(MILLISECONDS.convert(-1, DAYS) + 1, MILLISECONDS);
3471N/A for (TimeUnit unit: TimeUnit.values()) {
3471N/A for (int i=0; i<100; i++) { to(rand.nextLong(), unit); }
3471N/A to(Long.MIN_VALUE, unit);
3471N/A to(Long.MAX_VALUE, unit);
3471N/A }
1319N/A
1319N/A // toString
1319N/A ts(1L, DAYS, "1970-01-02T00:00:00Z");
1319N/A ts(1L, HOURS, "1970-01-01T01:00:00Z");
1319N/A ts(1L, MINUTES, "1970-01-01T00:01:00Z");
1319N/A ts(1L, SECONDS, "1970-01-01T00:00:01Z");
1319N/A ts(1L, MILLISECONDS, "1970-01-01T00:00:00.001Z");
1319N/A ts(1L, MICROSECONDS, "1970-01-01T00:00:00.000001Z");
1319N/A ts(1L, NANOSECONDS, "1970-01-01T00:00:00.000000001Z");
3471N/A ts(999999999L, NANOSECONDS, "1970-01-01T00:00:00.999999999Z");
3471N/A ts(9999999999L, NANOSECONDS, "1970-01-01T00:00:09.999999999Z");
1319N/A
1319N/A ts(-1L, DAYS, "1969-12-31T00:00:00Z");
1319N/A ts(-1L, HOURS, "1969-12-31T23:00:00Z");
1319N/A ts(-1L, MINUTES, "1969-12-31T23:59:00Z");
1319N/A ts(-1L, SECONDS, "1969-12-31T23:59:59Z");
1319N/A ts(-1L, MILLISECONDS, "1969-12-31T23:59:59.999Z");
1319N/A ts(-1L, MICROSECONDS, "1969-12-31T23:59:59.999999Z");
1319N/A ts(-1L, NANOSECONDS, "1969-12-31T23:59:59.999999999Z");
3471N/A ts(-999999999L, NANOSECONDS, "1969-12-31T23:59:59.000000001Z");
3471N/A ts(-9999999999L, NANOSECONDS, "1969-12-31T23:59:50.000000001Z");
1319N/A
1319N/A ts(-62135596799999L, MILLISECONDS, "0001-01-01T00:00:00.001Z");
1319N/A ts(-62135596800000L, MILLISECONDS, "0001-01-01T00:00:00Z");
1319N/A ts(-62135596800001L, MILLISECONDS, "-0001-12-31T23:59:59.999Z");
1319N/A
1319N/A ts(253402300799999L, MILLISECONDS, "9999-12-31T23:59:59.999Z");
1319N/A ts(-377642044800001L, MILLISECONDS, "-9999-12-31T23:59:59.999Z");
1319N/A
1319N/A // NTFS epoch in usec.
1319N/A ts(-11644473600000000L, MICROSECONDS, "1601-01-01T00:00:00Z");
1319N/A
1319N/A // nulls
1319N/A try {
1319N/A FileTime.from(0L, null);
1319N/A throw new RuntimeException("NullPointerException expected");
1319N/A } catch (NullPointerException npe) { }
1319N/A FileTime time = FileTime.fromMillis(now);
1319N/A if (time.equals(null))
1319N/A throw new RuntimeException("should not be equal to null");
1319N/A try {
1319N/A time.compareTo(null);
1319N/A throw new RuntimeException("NullPointerException expected");
1319N/A } catch (NullPointerException npe) { }
1319N/A }
1319N/A
1319N/A static void cmp(long v1, TimeUnit u1, long v2, TimeUnit u2, int expected) {
1319N/A int result = FileTime.from(v1, u1).compareTo(FileTime.from(v2, u2));
1319N/A if (result != expected)
1319N/A throw new RuntimeException("unexpected order");
1319N/A }
1319N/A
1319N/A static void eq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
1319N/A FileTime t1 = FileTime.from(v1, u1);
1319N/A FileTime t2 = FileTime.from(v2, u2);
1319N/A if (!t1.equals(t2))
1319N/A throw new RuntimeException("not equal");
1319N/A if (t1.hashCode() != t2.hashCode())
1319N/A throw new RuntimeException("hashCodes should be equal");
1319N/A }
1319N/A
1319N/A static void neq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
1319N/A FileTime t1 = FileTime.from(v1, u1);
1319N/A FileTime t2 = FileTime.from(v2, u2);
1319N/A if (t1.equals(t2))
1319N/A throw new RuntimeException("should not be equal");
1319N/A }
1319N/A
3471N/A static void to(long v, TimeUnit unit) {
3471N/A FileTime t = FileTime.from(v, unit);
3471N/A for (TimeUnit u: TimeUnit.values()) {
3471N/A long result = t.to(u);
3471N/A long expected = u.convert(v, unit);
3471N/A if (result != expected) {
3471N/A throw new RuntimeException("unexpected result");
3471N/A }
3471N/A }
3471N/A }
3471N/A
3471N/A static void ts(long v, TimeUnit unit, String expected) {
3471N/A String result = FileTime.from(v, unit).toString();
3471N/A if (!result.equals(expected)) {
3471N/A System.err.format("FileTime.from(%d, %s).toString() failed\n", v, unit);
3471N/A System.err.format("Expected: %s\n", expected);
3471N/A System.err.format(" Got: %s\n", result);
3471N/A throw new RuntimeException();
3471N/A }
1319N/A }
1319N/A}