< prev index next >

test/jdk/java/text/Format/DateFormat/Bug4322313.java

Print this page

  1 /*
  2  * Copyright (c) 2001, 2022, 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 4322313 4833268 6302990 6304305
 27  * @library /java/text/testlib
 28  * @summary Make sure that new implementation for
 29  * SimpleDateFormat.parse('z' or 'Z') and format('z' or 'Z') work correctly.

 30  */
 31 
 32 import java.io.*;
 33 import java.text.*;
 34 import java.util.*;
 35 
 36 public class Bug4322313 extends IntlTest {
 37 





 38     public void Test4322313() {
 39         Locale savedLocale = Locale.getDefault();
 40         TimeZone savedTimeZone = TimeZone.getDefault();
 41         boolean err = false;
 42         long mpm = 60 * 1000;   /* Milliseconds per a minute */
 43 
 44         Locale[] locs = {Locale.US, Locale.JAPAN, Locale.UK, Locale.of("ar")};
 45 
 46         String[] formats = {
 47             "z",
 48             "Z",
 49         };
 50 
 51         Object[][] valids = {
 52           /* given ID      offset                format('z'), ('Z')    index */
 53             {"GMT+03:04",  -184L * mpm, "GMT+03:04", "+0304", 9},
 54             {"GMT+13:42",  -822L * mpm, "GMT+13:42", "+1342", 9},
 55             {"GMT+00:00",   0L,         "GMT+00:00", "+0000", 9},
 56             {"GMT+1:11",   -71L * mpm,  "GMT+01:11", "+0111", 8},
 57             {"GMT +13:42",  0L,         "GMT",       "+0000", 3},

183 
184                         int offset = pos.getIndex();
185                         if (offset != ((Integer)valids[k][4]).intValue()) {
186                             err = true;
187                             System.err.println("\tParse  Error [Locale=" +
188                                 locale + ", " + formats[j] +
189                                 "/\"" + valids[k][0] +
190                                 "\"] invalid index: expected:" + valids[k][4] +
191                                 ", got:" + offset);
192                         }
193 
194                         if (date.getTime() != ((Long)valids[k][1]).longValue()) {
195                             err = true;
196                             System.err.println("\tParse  Error [Locale=" +
197                                 locale + ", " + formats[j] +
198                                 "/\"" + valids[k][0] +
199                                 "\"] expected:" + valids[k][1] +
200                                 ", got:" + date.getTime() + ", " + date);
201                         } else {
202 /*
203                             logln("\tParse  Okay  [Locale=" +
204                                 locale) + ", " + formats[j] +
205                                 "/\"" + valids[k][0] +
206                                 "\"] expected:" + valids[k][1] +
207                                 ", got:" + date.getTime() + ", " + date);
208 */
209 
210                             try {
211                                 date = sdf.parse((String)valids[k][0]);
212                             }
213                             catch (Exception e) {
214                                 err = true;
215                                 System.err.println("\tParse  Error [Locale=" +
216                                     locale + ", " + formats[j] +
217                                     "/\"" + valids[k][0] +
218                                     "\"] Unexpected Exception occurred: " + e);
219                                 continue;
220                             }
221 
222                             /* Since TimeZone.getTimeZone() don't treat
223                              * "+00234" or "-00234" as a valid ID, skips.

235                             StringBuffer s = new StringBuffer();
236                             sdf.format(date, s, new FieldPosition(0));
237                             sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
238 
239                             String got = s.toString();
240                             String expected = (String)valids[k][2+j];
241                             if (!got.equals(expected) &&
242                                 // special case to allow the difference between
243                                 // DateFormatSymbols.getZoneStrings() and
244                                 // TimeZone.getDisplayName() for "GMT+-00:00"
245                                 !(got.equals("GMT-00:00") &&
246                                   expected.equals("GMT+00:00"))) {
247                                 err = true;
248                                 System.err.println("\tFormat Error [Locale=" +
249                                     locale + ", " +
250                                     formats[j] + "/\"" + valids[k][0] +
251                                     "\"] expected:" + valids[k][2+j] +
252                                     ", got:" + s + ", " + date);
253                             } else {
254 /*
255                                 logln("\tFormat Okay  [Locale=" +
256                                     locale + ", " +
257                                     formats[j] + "/\"" + valids[k][0] +
258                                     "\"] expected:" + valids[k][2+j] +
259                                     ", got:" + s + ", " + date);
260 */
261                             }
262                         }
263                     }
264 
265                     /* Error case 1
266                      *   using SimpleDateFormat.parse(String, ParsePosition)
267                      */
268                     for (int k=0; k < invalids.length; k++) {
269                         ParsePosition pos = new ParsePosition(0);
270                         try {
271                             date = sdf.parse((String)invalids[k][0], pos);
272                             if (date != null) {
273                                 err = true;
274                                 System.err.println("\tParse  Error [Locale=" +
275                                     locale + ", " + formats[j] +
276                                     "/\"" + invalids[k][0] +
277                                     "\"] expected:null , got:" + date);
278                             }
279                             int offset = pos.getErrorIndex();
280                             if (offset != ((Integer)invalids[k][1]).intValue()) {
281                                 err = true;
282                                 System.err.println("\tParse  Error [Locale=" +
283                                     locale + ", " + formats[j] +
284                                     "/\"" + invalids[k][0] +
285                                     "\"] incorrect offset. expected:" +
286                                     invalids[k][1] + ", got: " + offset);
287                             } else {
288 /*
289                                 logln("\tParse  Okay  [Locale=" +
290                                     locale + ", " + formats[j] +
291                                     "/\"" + invalids[k][0] +
292                                     "\"] correct offset: " + offset);
293 */
294                             }
295                         }
296                         catch (Exception e) {
297                             err = true;
298                             System.err.println("\tParse  Error [Locale=" +
299                                 locale + ", " + formats[j] +
300                                 "/\"" + invalids[k][0] +
301                                 "\"] Unexpected Exception occurred: " + e);
302                         }
303                     }
304 
305                     /* Error case 2
306                      *   using DateFormat.parse(String)
307                      */
308                     boolean correctParseException = false;
309                     for (int k=0; k < invalids.length; k++) {
310                         try {
311                             date = sdf.parse((String)invalids[k][0]);
312                         }
313                         catch (ParseException e) {
314                             correctParseException = true;
315                             int offset = e.getErrorOffset();
316                             if (offset != ((Integer)invalids[k][1]).intValue()) {
317                                 err = true;
318                                 System.err.println("\tParse  Error [Locale=" +
319                                     locale + ", " + formats[j] +
320                                     "/\"" + invalids[k][0] +
321                                     "\"] Expected exception occurred with an incorrect offset. expected:" +
322                                     invalids[k][1] + ", got: " + offset);
323                             } else {
324 /*
325                                 logln("\tParse  Okay  [Locale=" +
326                                     locale + ", " + formats[j] +
327                                     "/\"" + invalids[k][0] +
328                                     "\"] Expected exception occurred with an correct offset: "
329                                     + offset);
330 */
331                             }
332                         }
333                         catch (Exception e) {
334                             err = true;
335                             System.err.println("\tParse  Error [Locale=" +
336                                 locale + ", " + formats[j] +
337                                 "/\"" + invalids[k][0] +
338                                 "\"] Invalid exception occurred: " + e);
339                         }
340                         finally {
341                             if (!correctParseException) {
342                                 err = true;
343                                 System.err.println("\tParse  Error: [Locale=" +
344                                     locale + ", " + formats[j] +
345                                     "/\"" + invalids[k][0] +
346                                     "\"] Expected exception didn't occur.");
347                             }
348                         }
349                     }
350                 }
351             }
352         }
353         finally {
354             Locale.setDefault(savedLocale);
355             TimeZone.setDefault(savedTimeZone);
356             if (err) {
357                 errln("SimpleDateFormat.parse()/format() test failed");
358             }
359         }
360     }
361 
362     public static void main(String[] args) throws Exception {
363         new Bug4322313().run(args);
364     }
365 }

  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  * @bug 4322313 4833268 6302990 6304305

 27  * @summary Make sure that new implementation for
 28  * SimpleDateFormat.parse('z' or 'Z') and format('z' or 'Z') work correctly.
 29  * @run junit Bug4322313
 30  */
 31 
 32 import java.io.*;
 33 import java.text.*;
 34 import java.util.*;
 35 
 36 import org.junit.jupiter.api.Test;
 37 
 38 import static org.junit.jupiter.api.Assertions.fail;
 39 
 40 public class Bug4322313 {
 41 
 42     @Test
 43     public void Test4322313() {
 44         Locale savedLocale = Locale.getDefault();
 45         TimeZone savedTimeZone = TimeZone.getDefault();
 46         boolean err = false;
 47         long mpm = 60 * 1000;   /* Milliseconds per a minute */
 48 
 49         Locale[] locs = {Locale.US, Locale.JAPAN, Locale.UK, Locale.of("ar")};
 50 
 51         String[] formats = {
 52             "z",
 53             "Z",
 54         };
 55 
 56         Object[][] valids = {
 57           /* given ID      offset                format('z'), ('Z')    index */
 58             {"GMT+03:04",  -184L * mpm, "GMT+03:04", "+0304", 9},
 59             {"GMT+13:42",  -822L * mpm, "GMT+13:42", "+1342", 9},
 60             {"GMT+00:00",   0L,         "GMT+00:00", "+0000", 9},
 61             {"GMT+1:11",   -71L * mpm,  "GMT+01:11", "+0111", 8},
 62             {"GMT +13:42",  0L,         "GMT",       "+0000", 3},

188 
189                         int offset = pos.getIndex();
190                         if (offset != ((Integer)valids[k][4]).intValue()) {
191                             err = true;
192                             System.err.println("\tParse  Error [Locale=" +
193                                 locale + ", " + formats[j] +
194                                 "/\"" + valids[k][0] +
195                                 "\"] invalid index: expected:" + valids[k][4] +
196                                 ", got:" + offset);
197                         }
198 
199                         if (date.getTime() != ((Long)valids[k][1]).longValue()) {
200                             err = true;
201                             System.err.println("\tParse  Error [Locale=" +
202                                 locale + ", " + formats[j] +
203                                 "/\"" + valids[k][0] +
204                                 "\"] expected:" + valids[k][1] +
205                                 ", got:" + date.getTime() + ", " + date);
206                         } else {
207 /*
208                             System.out.println("\tParse  Okay  [Locale=" +
209                                 locale) + ", " + formats[j] +
210                                 "/\"" + valids[k][0] +
211                                 "\"] expected:" + valids[k][1] +
212                                 ", got:" + date.getTime() + ", " + date);
213 */
214 
215                             try {
216                                 date = sdf.parse((String)valids[k][0]);
217                             }
218                             catch (Exception e) {
219                                 err = true;
220                                 System.err.println("\tParse  Error [Locale=" +
221                                     locale + ", " + formats[j] +
222                                     "/\"" + valids[k][0] +
223                                     "\"] Unexpected Exception occurred: " + e);
224                                 continue;
225                             }
226 
227                             /* Since TimeZone.getTimeZone() don't treat
228                              * "+00234" or "-00234" as a valid ID, skips.

240                             StringBuffer s = new StringBuffer();
241                             sdf.format(date, s, new FieldPosition(0));
242                             sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
243 
244                             String got = s.toString();
245                             String expected = (String)valids[k][2+j];
246                             if (!got.equals(expected) &&
247                                 // special case to allow the difference between
248                                 // DateFormatSymbols.getZoneStrings() and
249                                 // TimeZone.getDisplayName() for "GMT+-00:00"
250                                 !(got.equals("GMT-00:00") &&
251                                   expected.equals("GMT+00:00"))) {
252                                 err = true;
253                                 System.err.println("\tFormat Error [Locale=" +
254                                     locale + ", " +
255                                     formats[j] + "/\"" + valids[k][0] +
256                                     "\"] expected:" + valids[k][2+j] +
257                                     ", got:" + s + ", " + date);
258                             } else {
259 /*
260                                 System.out.println("\tFormat Okay  [Locale=" +
261                                     locale + ", " +
262                                     formats[j] + "/\"" + valids[k][0] +
263                                     "\"] expected:" + valids[k][2+j] +
264                                     ", got:" + s + ", " + date);
265 */
266                             }
267                         }
268                     }
269 
270                     /* Error case 1
271                      *   using SimpleDateFormat.parse(String, ParsePosition)
272                      */
273                     for (int k=0; k < invalids.length; k++) {
274                         ParsePosition pos = new ParsePosition(0);
275                         try {
276                             date = sdf.parse((String)invalids[k][0], pos);
277                             if (date != null) {
278                                 err = true;
279                                 System.err.println("\tParse  Error [Locale=" +
280                                     locale + ", " + formats[j] +
281                                     "/\"" + invalids[k][0] +
282                                     "\"] expected:null , got:" + date);
283                             }
284                             int offset = pos.getErrorIndex();
285                             if (offset != ((Integer)invalids[k][1]).intValue()) {
286                                 err = true;
287                                 System.err.println("\tParse  Error [Locale=" +
288                                     locale + ", " + formats[j] +
289                                     "/\"" + invalids[k][0] +
290                                     "\"] incorrect offset. expected:" +
291                                     invalids[k][1] + ", got: " + offset);
292                             } else {
293 /*
294                                 System.out.println("\tParse  Okay  [Locale=" +
295                                     locale + ", " + formats[j] +
296                                     "/\"" + invalids[k][0] +
297                                     "\"] correct offset: " + offset);
298 */
299                             }
300                         }
301                         catch (Exception e) {
302                             err = true;
303                             System.err.println("\tParse  Error [Locale=" +
304                                 locale + ", " + formats[j] +
305                                 "/\"" + invalids[k][0] +
306                                 "\"] Unexpected Exception occurred: " + e);
307                         }
308                     }
309 
310                     /* Error case 2
311                      *   using DateFormat.parse(String)
312                      */
313                     boolean correctParseException = false;
314                     for (int k=0; k < invalids.length; k++) {
315                         try {
316                             date = sdf.parse((String)invalids[k][0]);
317                         }
318                         catch (ParseException e) {
319                             correctParseException = true;
320                             int offset = e.getErrorOffset();
321                             if (offset != ((Integer)invalids[k][1]).intValue()) {
322                                 err = true;
323                                 System.err.println("\tParse  Error [Locale=" +
324                                     locale + ", " + formats[j] +
325                                     "/\"" + invalids[k][0] +
326                                     "\"] Expected exception occurred with an incorrect offset. expected:" +
327                                     invalids[k][1] + ", got: " + offset);
328                             } else {
329 /*
330                                 System.out.println("\tParse  Okay  [Locale=" +
331                                     locale + ", " + formats[j] +
332                                     "/\"" + invalids[k][0] +
333                                     "\"] Expected exception occurred with an correct offset: "
334                                     + offset);
335 */
336                             }
337                         }
338                         catch (Exception e) {
339                             err = true;
340                             System.err.println("\tParse  Error [Locale=" +
341                                 locale + ", " + formats[j] +
342                                 "/\"" + invalids[k][0] +
343                                 "\"] Invalid exception occurred: " + e);
344                         }
345                         finally {
346                             if (!correctParseException) {
347                                 err = true;
348                                 System.err.println("\tParse  Error: [Locale=" +
349                                     locale + ", " + formats[j] +
350                                     "/\"" + invalids[k][0] +
351                                     "\"] Expected exception didn't occur.");
352                             }
353                         }
354                     }
355                 }
356             }
357         }
358         finally {
359             Locale.setDefault(savedLocale);
360             TimeZone.setDefault(savedTimeZone);
361             if (err) {
362                 fail("SimpleDateFormat.parse()/format() test failed");
363             }
364         }
365     }
366 



367 }
< prev index next >