Path: blob/master/test/jdk/java/text/Format/DateFormat/ISO8601ZoneTest.java
41152 views
/*1* Copyright (c) 2010, 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 491963226* @summary Unit test for ISO8601 time zone format support27*/2829import java.text.*;30import java.util.*;3132public class ISO8601ZoneTest {33static final Date TIMESTAMP = new Date(1283758039020L);3435static final String[][] formatData = {36// time zone name, expected output at TIMESTAMP37{ "America/Los_Angeles", "2010-09-06T00:27:19.020-07", },38{ "America/Los_Angeles", "2010-09-06T00:27:19.020-0700", },39{ "America/Los_Angeles", "2010-09-06T00:27:19.020-07:00", },40{ "Australia/Sydney", "2010-09-06T17:27:19.020+10", },41{ "Australia/Sydney", "2010-09-06T17:27:19.020+1000", },42{ "Australia/Sydney", "2010-09-06T17:27:19.020+10:00", },43{ "GMT-07:00", "2010-09-06T00:27:19.020-07", },44{ "GMT-07:00", "2010-09-06T00:27:19.020-0700", },45{ "GMT-07:00", "2010-09-06T00:27:19.020-07:00", },46{ "UTC", "2010-09-06T07:27:19.020Z", },47{ "UTC", "2010-09-06T07:27:19.020Z", },48{ "UTC", "2010-09-06T07:27:19.020Z", },49};5051static final String[] zones = {52"America/Los_Angeles", "Australia/Sydney", "GMT-07:00",53"UTC", "GMT+05:30", "GMT-01:23",54};5556static final String[] isoZoneFormats = {57"yyyy-MM-dd'T'HH:mm:ss.SSSX",58"yyyy-MM-dd'T'HH:mm:ss.SSSXX",59"yyyy-MM-dd'T'HH:mm:ss.SSSXXX",60};6162// badData[][0] - format63// badData[][1] - (bad) text to be parsed64// badData[][2] - subtext at the end of which a parse error is detected65static final String[][] badData = {66{ "X", "1", "1" },67{ "X", "+1", "+1" },68{ "X", "-2", "-2" },69{ "X", "-24", "-2" },70{ "X", "+24", "+2" },7172{ "XX", "9", "9" },73{ "XX", "23", "2" },74{ "XX", "234", "2" },75{ "XX", "3456", "3" },76{ "XX", "23456", "2" },77{ "XX", "+1", "+1" },78{ "XX", "-12", "-12" },79{ "XX", "+123", "+123" },80{ "XX", "-12:34", "-12" },81{ "XX", "+12:34", "+12" },82{ "XX", "-2423", "-2" },83{ "XX", "+2423", "+2" },84{ "XX", "-1260", "-126" },85{ "XX", "+1260", "+126" },8687{ "XXX", "9", "9" },88{ "XXX", "23", "2" },89{ "XXX", "234", "2" },90{ "XXX", "3456", "3" },91{ "XXX", "23456", "2" },92{ "XXX", "2:34", "2" },93{ "XXX", "12:4", "1" },94{ "XXX", "12:34", "1" },95{ "XXX", "-1", "-1" },96{ "XXX", "+1", "+1" },97{ "XXX", "-12", "-12" },98{ "XXX", "+12", "+12" },99{ "XXX", "-123", "-12" },100{ "XXX", "+123", "+12" },101{ "XXX", "-1234", "-12" },102{ "XXX", "+1234", "+12" },103{ "XXX", "+24:23", "+2" },104{ "XXX", "+12:60", "+12:6" },105{ "XXX", "+1:23", "+1" },106{ "XXX", "+12:3", "+12:3" },107};108109static String[] badFormats = {110"XXXX", "XXXXX", "XXXXXX",111};112113public static void main(String[] args) throws Exception {114TimeZone tz = TimeZone.getDefault();115Locale loc = Locale.getDefault();116Locale.setDefault(Locale.US);117118try {119for (int i = 0; i < formatData.length; i++) {120TimeZone.setDefault(TimeZone.getTimeZone(formatData[i][0]));121formatTest(isoZoneFormats[i % isoZoneFormats.length],122formatData[i][1]);123}124125for (String zone : zones) {126TimeZone.setDefault(TimeZone.getTimeZone(zone));127for (String fmt : isoZoneFormats) {128roundtripTest(fmt);129SimpleDateFormat f = new SimpleDateFormat(fmt);130}131132}133134for (String[] d : badData) {135badDataParsing(d[0], d[1], d[2].length());136}137138for (String fmt : badFormats) {139badFormat(fmt);140}141} finally {142TimeZone.setDefault(tz);143Locale.setDefault(loc);144}145146}147148static void formatTest(String fmt, String expected) throws Exception {149SimpleDateFormat sdf = new SimpleDateFormat(fmt);150String s = sdf.format(TIMESTAMP);151if (!expected.equals(s)) {152throw new RuntimeException("formatTest: got " + s153+ ", expected " + expected);154}155156Date d = sdf.parse(s);157if (d.getTime() != TIMESTAMP.getTime()) {158throw new RuntimeException("formatTest: parse(" + s159+ "), got " + d.getTime()160+ ", expected " + TIMESTAMP.getTime());161}162163ParsePosition pos = new ParsePosition(0);164d = sdf.parse(s + "123", pos);165if (d.getTime() != TIMESTAMP.getTime()) {166throw new RuntimeException("formatTest: parse(" + s167+ "), got " + d.getTime()168+ ", expected " + TIMESTAMP.getTime());169}170if (pos.getIndex() != s.length()) {171throw new RuntimeException("formatTest: wrong resulting parse position: "172+ pos.getIndex() + ", expected " + s.length());173}174}175176static void roundtripTest(String fmt) throws Exception {177SimpleDateFormat sdf = new SimpleDateFormat(fmt);178Date date = new Date();179180int fractionalHour = sdf.getTimeZone().getOffset(date.getTime());181fractionalHour %= 3600000; // fraction of hour182183String s = sdf.format(date);184Date pd = sdf.parse(s);185long diffsInMillis = pd.getTime() - date.getTime();186if (diffsInMillis != 0) {187if (diffsInMillis != fractionalHour) {188throw new RuntimeException("fmt= " + fmt189+ ", diff="+diffsInMillis190+ ", fraction=" + fractionalHour);191}192}193}194195196static void badDataParsing(String fmt, String text, int expectedErrorIndex) {197SimpleDateFormat sdf = new SimpleDateFormat(fmt);198try {199sdf.parse(text);200throw new RuntimeException("didn't throw an exception: fmt=" + fmt201+ ", text=" + text);202} catch (ParseException e) {203// OK204}205206ParsePosition pos = new ParsePosition(0);207Date d = sdf.parse(text, pos);208int errorIndex = pos.getErrorIndex();209if (d != null || errorIndex != expectedErrorIndex) {210throw new RuntimeException("Bad error index=" + errorIndex211+ ", expected=" + expectedErrorIndex212+ ", fmt=" + fmt + ", text=" + text);213}214}215216static void badFormat(String fmt) {217try {218SimpleDateFormat sdf = new SimpleDateFormat(fmt);219throw new RuntimeException("Constructor didn't throw an exception: fmt=" + fmt);220} catch (IllegalArgumentException e) {221// OK222}223try {224SimpleDateFormat sdf = new SimpleDateFormat();225sdf.applyPattern(fmt);226throw new RuntimeException("applyPattern didn't throw an exception: fmt=" + fmt);227} catch (IllegalArgumentException e) {228// OK229}230}231}232233234