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