< prev index next >

test/java/util/Calendar/Builder/BuilderTest.java

Print this page


   1 /*
   2  * Copyright (c) 2013, 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 4745761
  27  * @summary Unit test for Calendar.Builder.
  28  */
  29 

  30 import java.util.*;
  31 import static java.util.Calendar.*;
  32 
  33 public class BuilderTest {
  34     private static final Locale jaJPJP = new Locale("ja", "JP", "JP");
  35     private static final Locale thTH = new Locale("th", "TH");
  36     private static final TimeZone LA = TimeZone.getTimeZone("America/Los_Angeles");
  37     private static final TimeZone TOKYO = TimeZone.getTimeZone("Asia/Tokyo");
  38     private static int error;
  39 
  40     public static void main(String[] args) {
  41         TimeZone tz = TimeZone.getDefault();
  42         Locale loc = Locale.getDefault();
  43         try {
  44             TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
  45             Locale.setDefault(Locale.US);
  46             Calendar.Builder calb;
  47             Calendar cal, expected;
  48 
  49             // set instant


 115             expected.clear();
 116             check(cal, expected);
 117 
 118             // Thai Buddhist calendar
 119             calb = builder();
 120             cal = calb.setCalendarType("buddhist").setDate(2556, JANUARY, 31).build();
 121             expected = Calendar.getInstance(thTH);
 122             expected.clear();
 123             expected.set(2556, JANUARY, 31);
 124             check(cal, expected);
 125             // setLocale
 126             calb = builder();
 127             cal = calb.setLocale(thTH).setDate(2556, JANUARY, 31).build();
 128             check(cal, expected);
 129 
 130             // Japanese Imperial calendar
 131             cal = builder().setCalendarType("japanese")
 132                 .setFields(YEAR, 1, DAY_OF_YEAR, 1).build();
 133             expected = Calendar.getInstance(jaJPJP);
 134             expected.clear();

 135             expected.set(1, JANUARY, 8);



 136             check(cal, expected);
 137             // setLocale
 138             calb = builder();
 139             cal = calb.setLocale(jaJPJP).setFields(YEAR, 1, DAY_OF_YEAR, 1).build();
 140             check(cal, expected);
 141 
 142             testExceptions();
 143         } finally {
 144             // Restore default Locale and TimeZone
 145             Locale.setDefault(loc);
 146             TimeZone.setDefault(tz);
 147         }
 148         if (error > 0) {
 149             throw new RuntimeException("Failed");
 150         }
 151     }
 152 
 153     private static void testExceptions() {
 154         Calendar.Builder calb;
 155         Calendar cal;


   1 /*
   2  * Copyright (c) 2013, 2019, 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 4745761
  27  * @summary Unit test for Calendar.Builder.
  28  */
  29 
  30 import java.time.LocalDateTime;
  31 import java.util.*;
  32 import static java.util.Calendar.*;
  33 
  34 public class BuilderTest {
  35     private static final Locale jaJPJP = new Locale("ja", "JP", "JP");
  36     private static final Locale thTH = new Locale("th", "TH");
  37     private static final TimeZone LA = TimeZone.getTimeZone("America/Los_Angeles");
  38     private static final TimeZone TOKYO = TimeZone.getTimeZone("Asia/Tokyo");
  39     private static int error;
  40 
  41     public static void main(String[] args) {
  42         TimeZone tz = TimeZone.getDefault();
  43         Locale loc = Locale.getDefault();
  44         try {
  45             TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
  46             Locale.setDefault(Locale.US);
  47             Calendar.Builder calb;
  48             Calendar cal, expected;
  49 
  50             // set instant


 116             expected.clear();
 117             check(cal, expected);
 118 
 119             // Thai Buddhist calendar
 120             calb = builder();
 121             cal = calb.setCalendarType("buddhist").setDate(2556, JANUARY, 31).build();
 122             expected = Calendar.getInstance(thTH);
 123             expected.clear();
 124             expected.set(2556, JANUARY, 31);
 125             check(cal, expected);
 126             // setLocale
 127             calb = builder();
 128             cal = calb.setLocale(thTH).setDate(2556, JANUARY, 31).build();
 129             check(cal, expected);
 130 
 131             // Japanese Imperial calendar
 132             cal = builder().setCalendarType("japanese")
 133                 .setFields(YEAR, 1, DAY_OF_YEAR, 1).build();
 134             expected = Calendar.getInstance(jaJPJP);
 135             expected.clear();
 136             if (LocalDateTime.now().isBefore(LocalDateTime.of(2019, 5, 1, 0, 0))) {
 137             expected.set(1, JANUARY, 8);
 138             } else {
 139                 expected.set(1, MAY, 1);
 140             }
 141             check(cal, expected);
 142             // setLocale
 143             calb = builder();
 144             cal = calb.setLocale(jaJPJP).setFields(YEAR, 1, DAY_OF_YEAR, 1).build();
 145             check(cal, expected);
 146 
 147             testExceptions();
 148         } finally {
 149             // Restore default Locale and TimeZone
 150             Locale.setDefault(loc);
 151             TimeZone.setDefault(tz);
 152         }
 153         if (error > 0) {
 154             throw new RuntimeException("Failed");
 155         }
 156     }
 157 
 158     private static void testExceptions() {
 159         Calendar.Builder calb;
 160         Calendar cal;


< prev index next >