Path: blob/master/test/jdk/java/text/Format/DateFormat/Bug8193444.java
41152 views
/*1* Copyright (c) 2018, 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*/22/*23* @test24* @bug 819344425* @summary Checks SimpleDateFormat.format/parse for the AIOOB exception when26* formatting/parsing dates through a pattern string that contains a27* sequence of 256 or more non-ASCII unicode characters.28* @run testng/othervm Bug819344429*/30import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;3233import java.text.DateFormat;34import java.text.ParseException;35import java.text.SimpleDateFormat;36import java.util.Date;3738import static org.testng.Assert.assertEquals;3940public class Bug8193444 {4142private static final String NON_ASCII_CHAR = "\u263A";4344@DataProvider(name = "dateFormat")45Object[][] dateFormatData() {46return new Object[][]{47// short_length (between 0 and 254)48{250},49// boundary50{254},51// long_length52{257},};53}5455@Test(dataProvider = "dateFormat")56public void testDateFormatAndParse(int length)57throws ParseException {5859String pattern = NON_ASCII_CHAR.repeat(length);6061DateFormat df = new SimpleDateFormat(pattern);62// format() should not throw AIOOB exception63String result = df.format(new Date());6465// Since the tested format patterns do not contain any character66// representing date/time field, those characters are not interpreted,67// they are simply copied into the output string during formatting68assertEquals(result, pattern, "Failed to format the date using"69+ " pattern of length: " + length);7071// The format pattern used by this SimpleDateFormat72// contains a sequence of non-ASCII characters, which does not73// represent any date/time field. The same sequence is given74// for parsing, just to check that the parsing does75// not throw any AIOOB exception.76// Although as per the parse() specification, the calendar's default77// values of the date-time fields are used for any missing78// date-time information, but checking that is not the intention of79// this test.80df.parse(pattern);81}8283}848586