Path: blob/master/test/jdk/java/text/Format/DateFormat/Bug6335238.java
41152 views
/*1* Copyright (c) 2006, 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* @test 1.1 06/01/2425* @bug 633523826* @summary Make sure that both the original and cloned SimpleDateFormat coexistindependently and don't cut off each other.27* @run main Bug6335238 1028*/2930import java.text.ParseException;31import java.text.SimpleDateFormat;32import java.util.Date;33import java.util.Locale;34import java.util.TimeZone;3536// Usage: java Bug6335238 [duration]37public class Bug6335238 {3839static final long UTC_LONG = 974534460000L;40static final String TIME_STRING = "2000/11/18 00:01:00";41static SimpleDateFormat masterSdf;42static int duration = 180;43static boolean stopped = false;44static boolean err = false;4546public static void main(String[] args) {47if (args.length == 1) {48duration = Math.max(10, Integer.parseInt(args[0]));49}50Locale savedLocale = Locale.getDefault();51TimeZone savedTimeZone = TimeZone.getDefault();5253TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));54Locale.setDefault(Locale.US);5556masterSdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");5758try {59// Once it is used, DecimalFormat becomes not thread-safe.60Date d = masterSdf.parse(TIME_STRING);6162new Bug6335238();63} catch (Exception e) {64System.err.println(e);65err = true;66} finally {67TimeZone.setDefault(savedTimeZone);68Locale.setDefault(savedLocale);6970if (err) {71throw new RuntimeException("Failed: Multiple DateFormat instances didn't work correctly.");72} else {73System.out.println("Passed.");74}75}76}7778public Bug6335238() {79stopped = false;8081DateParseThread d1 = new DateParseThread();82DateFormatThread d2 = new DateFormatThread();83DateParseThread d3 = new DateParseThread();84DateFormatThread d4 = new DateFormatThread();8586d1.start();87d2.start();88d3.start();89d4.start();9091try {92Thread.sleep(duration * 1000);93}94catch (Exception e) {95System.err.println(e);96err = true;97}9899stopped = true;100}101102class DateFormatThread extends Thread {103104public void run() {105int i = 0;106107while (!stopped) {108SimpleDateFormat sdf;109synchronized (masterSdf) {110sdf = (SimpleDateFormat)masterSdf.clone();111}112113i++;114String s = sdf.format(new Date(UTC_LONG));115116if (!s.equals(TIME_STRING)) {117stopped = true;118err = true;119120throw new RuntimeException("Formatting Date Error: counter=" +121i + ", Got<" + s + "> != Expected<" + TIME_STRING + ">");122}123}124}125}126127class DateParseThread extends Thread {128129public void run() {130int i = 0;131132while (!stopped) {133SimpleDateFormat sdf;134synchronized (masterSdf) {135sdf = (SimpleDateFormat)masterSdf.clone();136}137138i++;139Date date;140try {141date = sdf.parse(TIME_STRING);142long t = date.getTime();143144if (t != UTC_LONG) {145stopped = true;146err = true;147148throw new RuntimeException("Parsing Date Error: counter=" +149i + " Got:" + t + "<" + sdf.format(date) +150"> != " + UTC_LONG);151}152}153catch (ParseException e) {154stopped = true;155err = true;156157throw new RuntimeException(e);158}159}160}161}162}163164165