Path: blob/master/test/jdk/java/util/Date/DateGregorianCalendarTest.java
41149 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 461484226* @summary Make sure that a Date and a GregorianCalendar produce the same date/time. Both are new implementations in 1.5.27* @run main DateGregorianCalendarTest 1528*/2930import java.util.*;31import static java.util.GregorianCalendar.*;3233// Usage: java DateGregorianCalendarTest [duration]3435@SuppressWarnings("deprecation")36public class DateGregorianCalendarTest {37static volatile boolean runrun = true;38static int nThreads;3940public static void main(String[] args) {41int duration = 600;42if (args.length == 1) {43duration = Math.max(10, Integer.parseInt(args[0]));44}4546TimeZone savedTZ = TimeZone.getDefault();47try {48TimeZone.setDefault(TimeZone.getTimeZone("GMT"));49Thread[] t = new Thread[10]; // for future bugs...50int index = 0;51t[index++] = new Thread(new Runnable() {52public void run() {53GregorianCalendar gc = new GregorianCalendar();5455long delta = (long)(279 * 365.2422 * 24 * 60 * 60 * 1000);56long count = 0;57try {58for (long t = Long.MIN_VALUE; runrun && t < Long.MAX_VALUE-delta; t += delta) {59gc.setTimeInMillis(t);60Date date = new Date(t);61int y;62if (!((y = gc.get(YEAR)) == (date.getYear()+1900) &&63gc.get(MONTH) == date.getMonth() &&64gc.get(DAY_OF_MONTH) == date.getDate() &&65gc.get(HOUR_OF_DAY) == date.getHours() &&66gc.get(MINUTE) == date.getMinutes() &&67gc.get(SECOND) == date.getSeconds())) {68throw new RuntimeException("GregorinCalendar and Date returned different dates."69+" (millis=" + t + ")\n"70+"GC=" + gc + "\nDate=" + date);71}72++count;73if (y >= 1) {74delta = (long)(365.2422 * 24 * 60 * 60 * 1000);75}76if (y >= 1970) {77delta = (24 * 60 * 60 * 1000);78}79if (y >= 2039) {80delta = (long)(279 * 365.2422 * 24 * 60 * 60 * 1000);81}82}83if (runrun) {84System.out.println("Part I (count="+count+"): Passed");85} else {86System.out.println("Part I (count="+count+"): Incomplete");87}88} catch (RuntimeException e) {89System.out.println("Part I (count="+count+"): FAILED");90runrun = false;91throw e;92} finally {93decrementCounter();94}95}96});9798t[index++] = new Thread(new Runnable() {99public void run() {100GregorianCalendar gc = new GregorianCalendar();101102long count = 0;103int delta;104try {105for (long year = Integer.MIN_VALUE+1900;106runrun && year <= Integer.MAX_VALUE; year += delta) {107checkTimes(gc, year, JANUARY, 1, 0, 0, 0);108++count;109delta = getDelta((int)year);110}111112for (long month = Integer.MIN_VALUE;113runrun && month <= Integer.MAX_VALUE; month += delta) {114checkTimes(gc, 1900, month, 1, 0, 0, 0);115++count;116delta = getDelta(gc.get(YEAR));117}118119for (long dayOfMonth = Integer.MIN_VALUE;120runrun && dayOfMonth <= Integer.MAX_VALUE; dayOfMonth += delta) {121checkTimes(gc, 1900, JANUARY, dayOfMonth, 0, 0, 0);122++count;123delta = getDelta(gc.get(YEAR));124}125if (runrun) {126System.out.println("Part II (count="+count+"): Passed");127} else {128System.out.println("Part II (count="+count+"): Incomplete");129}130} catch (RuntimeException e) {131System.out.println("Part II (count="+count+"): FAILED");132runrun = false;133throw e;134} finally {135decrementCounter();136}137}138});139140// t3 takes more than 10 minutes (on Ultra-60 450MHz) without141// the 4936355 fix due to getting the small delta.142t[index++] = new Thread(new Runnable() {143public void run() {144GregorianCalendar gc = new GregorianCalendar();145146long count = 0;147int delta;148try {149for (long hourOfDay = Integer.MIN_VALUE;150runrun && hourOfDay <= Integer.MAX_VALUE; hourOfDay += delta) {151checkTimes(gc, 1970, JANUARY, 1, hourOfDay, 0, 0);152++count;153delta = getDelta(gc.get(YEAR));154}155for (long minutes = Integer.MIN_VALUE;156runrun && minutes <= Integer.MAX_VALUE; minutes += delta) {157checkTimes(gc, 1970, JANUARY, 1, 0, minutes, 0);158++count;159delta = getDelta(gc.get(YEAR)) * 60;160}161for (long seconds = Integer.MIN_VALUE;162runrun && seconds <= Integer.MAX_VALUE; seconds += delta) {163checkTimes(gc, 1970, JANUARY, 1, 0, 0, seconds);164++count;165delta = getDelta(gc.get(YEAR)) * 60 * 60;166}167if (runrun) {168System.out.println("Part III (count="+count+"): Passed");169} else {170System.out.println("Part III (count="+count+"): Incomplete");171}172} catch (RuntimeException e) {173System.out.println("Part III (count="+count+"): FAILED");174runrun = false;175throw e;176} finally {177decrementCounter();178}179}180});181182for (int i = 0; i < index; i++) {183incrementCounter();184t[i].start();185}186187try {188for (int i = 0; getCounter() > 0 && i < duration; i++) {189Thread.sleep(1000);190}191runrun = false;192for (int i = 0; i < index; i++) {193t[i].join();194}195} catch (InterruptedException e) {196}197} finally {198TimeZone.setDefault(savedTZ);199}200}201202static void checkTimes(GregorianCalendar gc, long year, long month, long dayOfMonth,203long hourOfDay, long minutes, long seconds) {204gc.clear();205gc.set((int)year, (int)month, (int)dayOfMonth, (int)hourOfDay, (int)minutes, (int)seconds);206long time = gc.getTimeInMillis();207Date date = new Date((int)year - 1900, (int)month, (int)dayOfMonth,208(int)hourOfDay, (int)minutes, (int)seconds);209long time2 = date.getTime();210if (time != time2) {211throw new RuntimeException("GregorinCalendar and Date returned different values.\n"212+"year="+year+", month="+month+", dayOfMonth="+dayOfMonth213+"\nhourOfDay="+hourOfDay+", minutes="+minutes+", seconds="+seconds214+"\ntime=" + time + ", time2=" + time2215+"\nGC=" + gc + "\nDate=" + date);216}217}218219static final int getDelta(int year) {220return (year >= 1970 && year <= 2039) ? 1 : 1<<13;221}222223synchronized static void incrementCounter() {224nThreads++;225}226227synchronized static void decrementCounter() {228nThreads--;229}230231synchronized static int getCounter() {232return nThreads;233}234}235236237