Path: blob/master/test/jdk/java/text/Format/DateFormat/Bug4322313.java
41152 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4322313 4833268 6302990 630430526* @library /java/text/testlib27* @summary Make sure that new implementation for28* SimpleDateFormat.parse('z' or 'Z') and format('z' or 'Z') work correctly.29*/3031import java.io.*;32import java.text.*;33import java.util.*;3435public class Bug4322313 extends IntlTest {3637public void Test4322313() {38Locale savedLocale = Locale.getDefault();39TimeZone savedTimeZone = TimeZone.getDefault();40boolean err = false;41long mpm = 60 * 1000; /* Milliseconds per a minute */4243Locale[] locs = {Locale.US, Locale.JAPAN, Locale.UK, new Locale("ar")};4445String[] formats = {46"z",47"Z",48};4950Object[][] valids = {51/* given ID offset format('z'), ('Z') index */52{"GMT+03:04", -184L * mpm, "GMT+03:04", "+0304", 9},53{"GMT+13:42", -822L * mpm, "GMT+13:42", "+1342", 9},54{"GMT+00:00", 0L, "GMT+00:00", "+0000", 9},55{"GMT+1:11", -71L * mpm, "GMT+01:11", "+0111", 8},56{"GMT +13:42", 0L, "GMT", "+0000", 3},57{" GMT", 0L, "GMT", "+0000", 4},58{"+0304", -184L * mpm, "GMT+03:04", "+0304", 5},59{"+1342", -822L * mpm, "GMT+13:42", "+1342", 5},60{"+0000", 0L, "GMT+00:00", "+0000", 5},61{" +1342", -822L * mpm, "GMT+13:42", "+1342", 6},62/* ISO-LATIN-1 digits */63{"GMT+\u0030\u0031:\u0032\u0033", -83L * mpm, "GMT+01:23", "+0123", 9},6465/* In fact, this test case is skipped because TimeZone class can't66* recognize TimeZone IDs like "+00234" or "-00234".67*/68{"+00234", -23L * mpm, "GMT+00:23", "+0023", 5},6970{"GMT-03:04", 184L * mpm, "GMT-03:04", "-0304", 9},71{"GMT-13:42", 822L * mpm, "GMT-13:42", "-1342", 9},72{"GMT-00:00", 0L, "GMT+00:00", "+0000", 9},73{"GMT-1:11", 71L * mpm, "GMT-01:11", "-0111", 8},74{"GMT -13:42", 0L, "GMT", "+0000", 3},75{"-0304", 184L * mpm, "GMT-03:04", "-0304", 5},76{"-1342", 822L * mpm, "GMT-13:42", "-1342", 5},77{" -1342", 822L * mpm, "GMT-13:42", "-1342", 6},78/* ISO-LATIN-1 digits */79{"GMT-\u0030\u0031:\u0032\u0033", 83L * mpm, "GMT-01:23", "-0123", 9},80/* In fact, this test case is skipped because TimeZone class can't81* recognize TimeZone IDs like "+00234" or "-00234".82*/83{"-00234", 23L * mpm, "GMT+00:23", "-0023", 5},84};8586Object[][] invalids = {87/* given ID error index */88{"GMT+8", 5},89{"GMT+18", 6},90{"GMT+208", 6},91{"GMT+0304", 6},92{"GMT+42195", 5},93{"GMT+5:8", 7},94{"GMT+23:60", 8},95{"GMT+11:1", 8},96{"GMT+24:13", 5},97{"GMT+421:950", 5},98{"GMT+0a:0A", 5},99{"GMT+ 13:42", 4},100{"GMT+13 :42", 6},101{"GMT+13: 42", 7},102{"GMT+-13:42", 4},103{"G M T", 0},104{"+8", 2},105{"+18", 3},106{"+208", 4},107{"+2360", 4},108{"+2413", 2},109{"+42195", 2},110{"+0AbC", 2},111{"+ 1342", 1},112{"+-1342", 1},113{"1342", 0},114/* Arabic-Indic digits */115{"GMT+\u0660\u0661:\u0662\u0663", 4},116/* Extended Arabic-Indic digits */117{"GMT+\u06f0\u06f1:\u06f2\u06f3", 4},118/* Devanagari digits */119{"GMT+\u0966\u0967:\u0968\u0969", 4},120/* Fullwidth digits */121{"GMT+\uFF10\uFF11:\uFF12\uFF13", 4},122123{"GMT-8", 5},124{"GMT-18", 6},125{"GMT-208", 6},126{"GMT-0304", 6},127{"GMT-42195", 5},128{"GMT-5:8", 7},129{"GMT-23:60", 8},130{"GMT-11:1", 8},131{"GMT-24:13", 5},132{"GMT-421:950", 5},133{"GMT-0a:0A", 5},134{"GMT- 13:42", 4},135{"GMT-13 :42", 6},136{"GMT-13: 42", 7},137{"GMT-+13:42", 4},138{"-8", 2},139{"-18", 3},140{"-208", 4},141{"-2360", 4},142{"-2413", 2},143{"-42195", 2},144{"-0AbC", 2},145{"- 1342", 1},146{"--1342", 1},147{"-802", 2},148/* Arabic-Indic digits */149{"GMT-\u0660\u0661:\u0662\u0663", 4},150/* Extended Arabic-Indic digits */151{"GMT-\u06f0\u06f1:\u06f2\u06f3", 4},152/* Devanagari digits */153{"GMT-\u0966\u0967:\u0968\u0969", 4},154/* Fullwidth digits */155{"GMT-\uFF10\uFF11:\uFF12\uFF13", 4},156};157158try {159for (int i=0; i < locs.length; i++) {160Locale locale = locs[i];161Locale.setDefault(locale);162163for (int j=0; j < formats.length; j++) {164TimeZone.setDefault(TimeZone.getTimeZone("GMT"));165SimpleDateFormat sdf = new SimpleDateFormat(formats[j]);166Date date;167168/* Okay case */169for (int k=0; k < valids.length; k++) {170ParsePosition pos = new ParsePosition(0);171try {172date = sdf.parse((String)valids[k][0], pos);173}174catch (Exception e) {175err = true;176System.err.println("\tParse Error [Locale=" +177locale + ", " + formats[j] +178"/\"" + valids[k][0] +179"\"] Unexpected Exception occurred: " + e);180continue;181}182183int offset = pos.getIndex();184if (offset != ((Integer)valids[k][4]).intValue()) {185err = true;186System.err.println("\tParse Error [Locale=" +187locale + ", " + formats[j] +188"/\"" + valids[k][0] +189"\"] invalid index: expected:" + valids[k][4] +190", got:" + offset);191}192193if (date.getTime() != ((Long)valids[k][1]).longValue()) {194err = true;195System.err.println("\tParse Error [Locale=" +196locale + ", " + formats[j] +197"/\"" + valids[k][0] +198"\"] expected:" + valids[k][1] +199", got:" + date.getTime() + ", " + date);200} else {201/*202logln("\tParse Okay [Locale=" +203locale) + ", " + formats[j] +204"/\"" + valids[k][0] +205"\"] expected:" + valids[k][1] +206", got:" + date.getTime() + ", " + date);207*/208209try {210date = sdf.parse((String)valids[k][0]);211}212catch (Exception e) {213err = true;214System.err.println("\tParse Error [Locale=" +215locale + ", " + formats[j] +216"/\"" + valids[k][0] +217"\"] Unexpected Exception occurred: " + e);218continue;219}220221/* Since TimeZone.getTimeZone() don't treat222* "+00234" or "-00234" as a valid ID, skips.223*/224if (((String)valids[k][0]).length() == 6) {225continue;226}227228/* Since TimeZone.getTimeZone() don't recognize229* +hhmm/-hhmm format, add "GMT" as prefix.230*/231sdf.setTimeZone(TimeZone.getTimeZone(232(((((String)valids[k][0]).charAt(0) != 'G') ?233"GMT" : "") + valids[k][0])));234StringBuffer s = new StringBuffer();235sdf.format(date, s, new FieldPosition(0));236sdf.setTimeZone(TimeZone.getTimeZone("GMT"));237238String got = s.toString();239String expected = (String)valids[k][2+j];240if (!got.equals(expected) &&241// special case to allow the difference between242// DateFormatSymbols.getZoneStrings() and243// TimeZone.getDisplayName() for "GMT+-00:00"244!(got.equals("GMT-00:00") &&245expected.equals("GMT+00:00"))) {246err = true;247System.err.println("\tFormat Error [Locale=" +248locale + ", " +249formats[j] + "/\"" + valids[k][0] +250"\"] expected:" + valids[k][2+j] +251", got:" + s + ", " + date);252} else {253/*254logln("\tFormat Okay [Locale=" +255locale + ", " +256formats[j] + "/\"" + valids[k][0] +257"\"] expected:" + valids[k][2+j] +258", got:" + s + ", " + date);259*/260}261}262}263264/* Error case 1265* using SimpleDateFormat.parse(String, ParsePosition)266*/267for (int k=0; k < invalids.length; k++) {268ParsePosition pos = new ParsePosition(0);269try {270date = sdf.parse((String)invalids[k][0], pos);271if (date != null) {272err = true;273System.err.println("\tParse Error [Locale=" +274locale + ", " + formats[j] +275"/\"" + invalids[k][0] +276"\"] expected:null , got:" + date);277}278int offset = pos.getErrorIndex();279if (offset != ((Integer)invalids[k][1]).intValue()) {280err = true;281System.err.println("\tParse Error [Locale=" +282locale + ", " + formats[j] +283"/\"" + invalids[k][0] +284"\"] incorrect offset. expected:" +285invalids[k][1] + ", got: " + offset);286} else {287/*288logln("\tParse Okay [Locale=" +289locale + ", " + formats[j] +290"/\"" + invalids[k][0] +291"\"] correct offset: " + offset);292*/293}294}295catch (Exception e) {296err = true;297System.err.println("\tParse Error [Locale=" +298locale + ", " + formats[j] +299"/\"" + invalids[k][0] +300"\"] Unexpected Exception occurred: " + e);301}302}303304/* Error case 2305* using DateFormat.parse(String)306*/307boolean correctParseException = false;308for (int k=0; k < invalids.length; k++) {309try {310date = sdf.parse((String)invalids[k][0]);311}312catch (ParseException e) {313correctParseException = true;314int offset = e.getErrorOffset();315if (offset != ((Integer)invalids[k][1]).intValue()) {316err = true;317System.err.println("\tParse Error [Locale=" +318locale + ", " + formats[j] +319"/\"" + invalids[k][0] +320"\"] Expected exception occurred with an incorrect offset. expected:" +321invalids[k][1] + ", got: " + offset);322} else {323/*324logln("\tParse Okay [Locale=" +325locale + ", " + formats[j] +326"/\"" + invalids[k][0] +327"\"] Expected exception occurred with an correct offset: "328+ offset);329*/330}331}332catch (Exception e) {333err = true;334System.err.println("\tParse Error [Locale=" +335locale + ", " + formats[j] +336"/\"" + invalids[k][0] +337"\"] Invalid exception occurred: " + e);338}339finally {340if (!correctParseException) {341err = true;342System.err.println("\tParse Error: [Locale=" +343locale + ", " + formats[j] +344"/\"" + invalids[k][0] +345"\"] Expected exception didn't occur.");346}347}348}349}350}351}352finally {353Locale.setDefault(savedLocale);354TimeZone.setDefault(savedTimeZone);355if (err) {356errln("SimpleDateFormat.parse()/format() test failed");357}358}359}360361public static void main(String[] args) throws Exception {362new Bug4322313().run(args);363}364}365366367