Path: blob/master/test/jdk/java/util/Calendar/ResolutionTest.java
41149 views
/*1* Copyright (c) 2007, 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 645284826* @summary Make sure that the resolution of (WEKK_OF_MONTH +27* DAY_OF_WEEK) and (DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK) works as28* specified in the API.29* @key randomness30*/3132import java.util.*;33import static java.util.Calendar.*;3435public class ResolutionTest {36static Random rand = new Random();3738public static void main(String[] args) {39for (int year = 1995; year < 2011; year++) {40for (int month = JANUARY; month <= DECEMBER; month++) {41for (int dow = SUNDAY; dow <= SATURDAY; dow++) {42test(year, month, dow);43}44}45}46}4748static void test(int year, int month, int dow) {49Calendar cal = new GregorianCalendar(year, month, 1);50int max = cal.getActualMaximum(DAY_OF_MONTH);51ArrayList<Integer> list = new ArrayList<Integer>();52for (int d = 1; d <= max; d++) {53cal.clear();54cal.set(year, month, d);55if (cal.get(DAY_OF_WEEK) == dow) {56list.add(d);57}58}59for (int i = 0; i < 100; i++) {60int nth = rand.nextInt(list.size()); // 0-based61int day = list.get(nth);62nth++; // 1-based63testDayOfWeekInMonth(year, month, nth, dow, day);64}6566// Put WEEK_OF_MONTH-DAY_OF_MONTH pairs67list = new ArrayList<Integer>();68for (int d = 1; d <= max; d++) {69cal.clear();70cal.set(year, month, d);71if (cal.get(DAY_OF_WEEK) == dow) {72list.add(cal.get(WEEK_OF_MONTH));73list.add(d);74}75}76for (int i = 0; i < list.size(); i++) {77int nth = list.get(i++);78int day = list.get(i);79testWeekOfMonth(year, month, nth, dow, day);80}81}8283static Koyomi cal = new Koyomi();8485static void testDayOfWeekInMonth(int year, int month, int nth, int dow, int expected) {86// don't call clear() here87cal.set(YEAR, year);88cal.set(MONTH, month);89// Set DAY_OF_WEEK_IN_MONTH before DAY_OF_WEEK90cal.set(DAY_OF_WEEK_IN_MONTH, nth);91cal.set(DAY_OF_WEEK, dow);92if (!cal.checkDate(year, month, expected)) {93throw new RuntimeException(String.format("DOWIM: year=%d, month=%d, nth=%d, dow=%d:%s%n",94year, month+1, nth, dow, cal.getMessage()));95}96}9798static void testWeekOfMonth(int year, int month, int nth, int dow, int expected) {99// don't call clear() here100cal.set(YEAR, year);101cal.set(MONTH, month);102// Set WEEK_OF_MONTH before DAY_OF_WEEK103cal.set(WEEK_OF_MONTH, nth);104cal.set(DAY_OF_WEEK, dow);105if (!cal.checkDate(year, month, expected)) {106throw new RuntimeException(String.format("WOM: year=%d, month=%d, nth=%d, dow=%d:%s%n",107year, month+1, nth, dow, cal.getMessage()));108}109}110}111112113