Path: blob/master/test/jdk/java/util/Calendar/StampOverflow.java
41149 views
/*1* Copyright (c) 2001, 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 4404619 634881926* @summary Make sure that Calendar doesn't cause nextStamp overflow.27* @modules java.base/java.util:open28*/2930import java.lang.reflect.*;31import java.util.*;32import static java.util.Calendar.*;3334// Calendar fails when turning negative to positive (zero), not35// positive to negative with nextStamp. If a negative value was set to36// nextStamp, it would fail even with the fix. So, there's no way to37// reproduce the symptom in a short time -- at leaset it would take a38// couple of hours even if we started with Integer.MAX_VALUE. So, this39// test case just checks that set() calls don't cause any nextStamp40// overflow.4142public class StampOverflow {43public static void main(String[] args) throws IllegalAccessException {44// Get a Field for "nextStamp".45Field nextstamp = null;46try {47nextstamp = Calendar.class.getDeclaredField("nextStamp");48} catch (NoSuchFieldException e) {49throw new RuntimeException("implementation changed?", e);50}5152nextstamp.setAccessible(true);5354Calendar cal = new GregorianCalendar();55int initialValue = nextstamp.getInt(cal);56// Set nextStamp to a very large number57nextstamp.setInt(cal, Integer.MAX_VALUE - 100);5859for (int i = 0; i < 1000; i++) {60invoke(cal);61int stampValue = nextstamp.getInt(cal);62// nextStamp must not be less than initialValue.63if (stampValue < initialValue) {64throw new RuntimeException("invalid nextStamp: " + stampValue);65}66}67}6869static void invoke(Calendar cal) {70cal.clear();71cal.set(2000, NOVEMBER, 2, 0, 0, 0);72int y = cal.get(YEAR);73int m = cal.get(MONTH);74int d = cal.get(DAY_OF_MONTH);75if (y != 2000 || m != NOVEMBER || d != 2) {76throw new RuntimeException("wrong date produced ("77+ y + "/" + (m+1) + "/" + d + ")");78}79}80}818283