2702N/A/*
2702N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2702N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2702N/A *
2702N/A * This code is free software; you can redistribute it and/or modify it
2702N/A * under the terms of the GNU General Public License version 2 only, as
2702N/A * published by the Free Software Foundation.
2702N/A *
2702N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2702N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2702N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2702N/A * version 2 for more details (a copy is included in the LICENSE file that
2702N/A * accompanied this code).
2702N/A *
2702N/A * You should have received a copy of the GNU General Public License version
2702N/A * 2 along with this work; if not, write to the Free Software Foundation,
2702N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2702N/A *
2702N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2702N/A * or visit www.oracle.com if you need additional information or have any
2702N/A * questions.
2702N/A */
2702N/A
2702N/A/*
2702N/A * @test
2702N/A * @bug 4267450
2702N/A * @summary Unit test for week date support
2702N/A */
2702N/A
2702N/Aimport java.text.*;
2702N/Aimport java.util.*;
2702N/Aimport static java.util.GregorianCalendar.*;
2702N/A
2702N/Apublic class WeekDateTest {
2702N/A static SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");
2702N/A static SimpleDateFormat ywdFormat = new SimpleDateFormat("YYYY-'W'ww-u");
2702N/A static {
2702N/A ymdFormat.setCalendar(newCalendar());
2702N/A ywdFormat.setCalendar(newCalendar());
2702N/A }
2702N/A
2702N/A // Round-trip Data
2702N/A static final String[][] roundTripData = {
2702N/A { "2005-01-01", "2004-W53-6" },
2702N/A { "2005-01-02", "2004-W53-7" },
2702N/A { "2005-12-31", "2005-W52-6" },
2702N/A { "2007-01-01", "2007-W01-1" },
2702N/A { "2007-12-30", "2007-W52-7" },
2702N/A { "2007-12-31", "2008-W01-1" },
2702N/A { "2008-01-01", "2008-W01-2" },
2702N/A { "2008-12-29", "2009-W01-1" },
2702N/A { "2008-12-31", "2009-W01-3" },
2702N/A { "2009-01-01", "2009-W01-4" },
2702N/A { "2009-12-31", "2009-W53-4" },
2702N/A { "2010-01-03", "2009-W53-7" },
2702N/A { "2009-12-31", "2009-W53-4" },
2702N/A { "2010-01-01", "2009-W53-5" },
2702N/A { "2010-01-02", "2009-W53-6" },
2702N/A { "2010-01-03", "2009-W53-7" },
2702N/A { "2008-12-28", "2008-W52-7" },
2702N/A { "2008-12-29", "2009-W01-1" },
2702N/A { "2008-12-30", "2009-W01-2" },
2702N/A { "2008-12-31", "2009-W01-3" },
2702N/A { "2009-01-01", "2009-W01-4" },
2702N/A { "2009-01-01", "2009-W01-4" },
2702N/A };
2702N/A
2702N/A // Data for leniency test
2702N/A static final String[][] leniencyData = {
2702N/A { "2008-12-28", "2009-W01-0" },
2702N/A { "2010-01-04", "2009-W53-8" },
2702N/A { "2008-12-29", "2008-W53-1" },
2702N/A };
2702N/A
2702N/A static final String[] invalidData = {
2702N/A "2010-W00-1",
2702N/A "2010-W55-1",
2702N/A "2010-W03-0",
2702N/A "2010-W04-8",
2702N/A "2010-W04-19"
2702N/A };
2702N/A
2702N/A public static void main(String[] args) throws Exception {
2702N/A formatTest(roundTripData);
2702N/A parseTest(roundTripData);
2702N/A parseTest(leniencyData);
2702N/A nonLenientTest(invalidData);
2702N/A noWeekDateSupport();
2702N/A }
2702N/A
2702N/A private static void formatTest(String[][] data) throws Exception {
2702N/A for (String[] dates : data) {
2702N/A String regularDate = dates[0];
2702N/A String weekDate = dates[1];
2702N/A Date date = null;
2702N/A date = ymdFormat.parse(regularDate);
2702N/A String s = ywdFormat.format(date);
2702N/A if (!s.equals(weekDate)) {
2702N/A throw new RuntimeException("format: got="+s+", expecetd="+weekDate);
2702N/A }
2702N/A }
2702N/A }
2702N/A
2702N/A private static void parseTest(String[][] data) throws Exception {
2702N/A for (String[] dates : data) {
2702N/A String regularDate = dates[0];
2702N/A String weekDate = dates[1];
2702N/A Date date1 = null, date2 = null;
2702N/A date1 = ymdFormat.parse(regularDate);
2702N/A date2 = ywdFormat.parse(weekDate);
2702N/A if (!date1.equals(date2)) {
2702N/A System.err.println(regularDate + ": date1 = " + date1);
2702N/A System.err.println(weekDate + ": date2 = " + date2);
2702N/A throw new RuntimeException("parse: date1 != date2");
2702N/A }
2702N/A }
2702N/A }
2702N/A
2702N/A
2702N/A // Non-lenient mode test
2702N/A private static void nonLenientTest(String[] data) {
2702N/A ywdFormat.setLenient(false);
2702N/A for (String date : data) {
2702N/A try {
2702N/A Date d = ywdFormat.parse(date);
2702N/A throw new RuntimeException("No ParseException thrown with " + date);
2702N/A } catch (ParseException e) {
2702N/A // OK
2702N/A }
2702N/A }
2702N/A ywdFormat.setLenient(true);
2702N/A }
2702N/A
2702N/A
2702N/A private static void noWeekDateSupport() throws Exception {
2702N/A // Tests with Japanese Imperial Calendar that doesn't support week dates.
2702N/A Calendar jcal = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
2702N/A new Locale("ja", "JP", "JP"));
2702N/A
2702N/A jcal.setFirstDayOfWeek(MONDAY);
2702N/A jcal.setMinimalDaysInFirstWeek(4);
2702N/A SimpleDateFormat sdf = new SimpleDateFormat("Y-'W'ww-u");
2702N/A sdf.setCalendar(jcal);
2702N/A Date d = sdf.parse("21-W01-3"); // 2008-12-31 == H20-12-31
2702N/A GregorianCalendar gcal = newCalendar();
2702N/A gcal.setTime(d);
2702N/A if (gcal.get(YEAR) != 2008
2702N/A || gcal.get(MONTH) != DECEMBER
2702N/A || gcal.get(DAY_OF_MONTH) != 31) {
2702N/A String s = String.format("noWeekDateSupport: got %04d-%02d-%02d, expected 2008-12-31%n",
2702N/A gcal.get(YEAR),
2702N/A gcal.get(MONTH)+1,
2702N/A gcal.get(DAY_OF_MONTH));
2702N/A throw new RuntimeException(s);
2702N/A }
2702N/A }
2702N/A
2702N/A private static GregorianCalendar newCalendar() {
2702N/A // Use GMT to avoid any surprises related DST transitions.
2702N/A GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
2702N/A // Setup the ISO 8601 compatible parameters
2702N/A cal.setFirstDayOfWeek(MONDAY);
2702N/A cal.setMinimalDaysInFirstWeek(4);
2702N/A return cal;
2702N/A }
2702N/A}