< prev index next >

test/jdk/java/util/Calendar/CalendarTest.java

Print this page
rev 51570 : [mq]: 8210142
   1 /*
   2  * Copyright (c) 1997, 2016, 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 /**
  25  * @test
  26  * @bug 4064654 4374886 4984320 4984574 4944795
  27  * @summary test for Calendar
  28  * @library /java/text/testlib

  29  * @run main CalendarTest
  30  * @key randomness
  31  */
  32 
  33 import java.io.File;
  34 import java.io.FileInputStream;
  35 import java.io.FileOutputStream;
  36 import java.io.IOException;
  37 import java.io.ObjectInputStream;
  38 import java.io.ObjectOutput;
  39 import java.io.ObjectOutputStream;

  40 import java.util.Calendar;
  41 import java.util.Date;
  42 import java.util.GregorianCalendar;
  43 import java.util.Locale;
  44 import java.util.SimpleTimeZone;
  45 import java.util.TimeZone;
  46 
  47 import static java.util.Calendar.*;
  48 
  49 public class CalendarTest extends IntlTest {
  50 
  51     static final int ONE_DAY = 24 * 60 * 60 * 1000;
  52     static final int EPOCH_JULIAN = 2440588;
  53 
  54     public static void main(String argv[]) throws Exception {
  55         new CalendarTest().run(argv);
  56     }
  57 
  58     /**
  59      * Test the behavior of the GregorianCalendar around the changeover.


1159             Locale.setDefault(Locale.US);
1160             TimeZone.setDefault(TimeZone.getTimeZone("PST"));
1161 
1162             Calendar cal = Calendar.getInstance();
1163             cal.set(YEAR, 2001);
1164             cal.set(MONTH, OCTOBER);
1165             cal.set(WEEK_OF_YEAR, 4);
1166             cal.set(DAY_OF_WEEK, 2);
1167 
1168             if (cal.get(YEAR) != 2001
1169                     || cal.get(MONTH) != JANUARY
1170                     || cal.get(DATE) != 22
1171                     || cal.get(DAY_OF_WEEK) != MONDAY) {
1172                 errln("Failed : got " + cal.getTime() + ", expected Mon Jan 22, 2001");
1173             }
1174         } finally {
1175             Locale.setDefault(savedLocale);
1176             TimeZone.setDefault(savedTimeZone);
1177         }
1178     }























1179 }
1180 
1181 //eof
   1 /*
   2  * Copyright (c) 1997, 2018, 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 /**
  25  * @test
  26  * @bug 4064654 4374886 4984320 4984574 4944795 8210142
  27  * @summary test for Calendar
  28  * @library /java/text/testlib
  29  * @modules java.base/java.util:+open
  30  * @run main CalendarTest
  31  * @key randomness
  32  */
  33 
  34 import java.io.File;
  35 import java.io.FileInputStream;
  36 import java.io.FileOutputStream;
  37 import java.io.IOException;
  38 import java.io.ObjectInputStream;
  39 import java.io.ObjectOutput;
  40 import java.io.ObjectOutputStream;
  41 import java.lang.reflect.Field;
  42 import java.util.Calendar;
  43 import java.util.Date;
  44 import java.util.GregorianCalendar;
  45 import java.util.Locale;
  46 import java.util.SimpleTimeZone;
  47 import java.util.TimeZone;
  48 
  49 import static java.util.Calendar.*;
  50 
  51 public class CalendarTest extends IntlTest {
  52 
  53     static final int ONE_DAY = 24 * 60 * 60 * 1000;
  54     static final int EPOCH_JULIAN = 2440588;
  55 
  56     public static void main(String argv[]) throws Exception {
  57         new CalendarTest().run(argv);
  58     }
  59 
  60     /**
  61      * Test the behavior of the GregorianCalendar around the changeover.


1161             Locale.setDefault(Locale.US);
1162             TimeZone.setDefault(TimeZone.getTimeZone("PST"));
1163 
1164             Calendar cal = Calendar.getInstance();
1165             cal.set(YEAR, 2001);
1166             cal.set(MONTH, OCTOBER);
1167             cal.set(WEEK_OF_YEAR, 4);
1168             cal.set(DAY_OF_WEEK, 2);
1169 
1170             if (cal.get(YEAR) != 2001
1171                     || cal.get(MONTH) != JANUARY
1172                     || cal.get(DATE) != 22
1173                     || cal.get(DAY_OF_WEEK) != MONDAY) {
1174                 errln("Failed : got " + cal.getTime() + ", expected Mon Jan 22, 2001");
1175             }
1176         } finally {
1177             Locale.setDefault(savedLocale);
1178             TimeZone.setDefault(savedTimeZone);
1179         }
1180     }
1181     
1182     public void TestClonedSharedZones() throws NoSuchFieldException, IllegalAccessException {
1183         Field zone = Calendar.class.getDeclaredField("zone");
1184         zone.setAccessible(true);
1185         Field sharedZone = Calendar.class.getDeclaredField("sharedZone");
1186         sharedZone.setAccessible(true);
1187 
1188         // create a new calendar with any date, and clone it.
1189         Calendar c1 = new GregorianCalendar();
1190         Calendar c2 = (Calendar) c1.clone();
1191 
1192         // c1 should have a shared zone
1193         if (!sharedZone.getBoolean(c1)) {
1194             errln("Failed : c1.sharedZone == false");
1195         } else {
1196             // c2 should not have a shared zone
1197             if (sharedZone.getBoolean(c2)) {
1198                 errln("Failed : c2.sharedZone == true");
1199             } else if (zone.get(c1) == zone.get(c2)) {
1200                 errln("Failed : c1.zone == c2.zone");
1201             }
1202         }
1203     }
1204 }
1205 
1206 //eof
< prev index next >