1 /*
 2  * Copyright (c) 2001, 2023, 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  *
27  * @bug 4492719
28  * @summary Confirm that Message.parse() interprets time zone which uses "GMT+/-" format correctly and doesn't throw ParseException.
29  * @run junit/othervm bug4492719
30  */
31 
32 import java.util.*;
33 import java.text.*;
34 
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.BeforeAll;
37 
38 import static org.junit.jupiter.api.Assertions.fail;
39 
40 public class bug4492719 {
41 
42     // MessageFormat.parse() should be able to interpret a time zone
43     // that uses "GMT+/-".
44     @Test
45     public void testParse() throws Exception {
46         Locale savedLocale = Locale.getDefault();
47         TimeZone savedTimeZone = TimeZone.getDefault();
48         MessageFormat mf;
49         boolean err =false;
50 
51         String[] formats = {
52                 "short", "medium", "long", "full"
53         };
54         String[] timezones = {
55                 "America/Los_Angeles", "GMT", "GMT+09:00", "GMT-8:00",
56                 "GMT+123", "GMT-1234", "GMT+2", "GMT-13"
57         };
58         String text;
59 
60         Locale.setDefault(Locale.US);
61 
62         try {
63             for (int i = 0; i < timezones.length; i++) {
64                 TimeZone.setDefault(TimeZone.getTimeZone(timezones[i]));
65 
66                 for (int j = 0; j < formats.length; j++) {
67                     mf = new MessageFormat("{0,time," + formats[j] + "} - time");
68                     text = MessageFormat.format("{0,time," + formats[j] + "} - time",
69                             new Object [] { new Date(123456789012L)});
70                     Object[] objs = mf.parse(text);
71                 }
72             }
73         } catch (ParseException e) {
74             err = true;
75             System.err.println("Invalid ParseException occurred : " +
76                     e.getMessage());
77             System.err.println("    TimeZone=" + TimeZone.getDefault());
78         }
79         finally {
80             Locale.setDefault(savedLocale);
81             TimeZone.setDefault(savedTimeZone);
82             if (err) {
83                 throw new Exception("MessageFormat.parse(\"GMT format\") failed.");
84             }
85         }
86     }
87 }