test/java/util/Formatter/BasicDateTime.java

Print this page
rev 10699 : 8058887: (fmt) Improve java/util/Formatter test coverage of group separators and width

*** 1,7 **** /* ! * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 34,65 **** import java.math.BigDecimal; import java.math.BigInteger; import java.text.DateFormatSymbols; import java.util.*; - - - import static java.util.Calendar.*; import static java.util.SimpleTimeZone.*; import java.util.regex.Pattern; - public class BasicDateTime extends Basic { private static void test(String fs, String exp, Object ... args) { Formatter f = new Formatter(new StringBuilder(), Locale.US); f.format(fs, args); ck(fs, exp, f.toString()); } private static void test(Locale l, String fs, String exp, Object ... args) { Formatter f = new Formatter(new StringBuilder(), l); f.format(fs, args); ck(fs, exp, f.toString()); } private static void test(String fs, Object ... args) { Formatter f = new Formatter(new StringBuilder(), Locale.US); f.format(fs, args); --- 34,69 ---- import java.math.BigDecimal; import java.math.BigInteger; import java.text.DateFormatSymbols; import java.util.*; import static java.util.Calendar.*; import static java.util.SimpleTimeZone.*; import java.util.regex.Pattern; public class BasicDateTime extends Basic { private static void test(String fs, String exp, Object ... args) { Formatter f = new Formatter(new StringBuilder(), Locale.US); f.format(fs, args); ck(fs, exp, f.toString()); + + f = new Formatter(new StringBuilder(), Locale.US); + f.format("foo " + fs + " bar", args); + ck(fs, "foo " + exp + " bar", f.toString()); } private static void test(Locale l, String fs, String exp, Object ... args) { Formatter f = new Formatter(new StringBuilder(), l); f.format(fs, args); ck(fs, exp, f.toString()); + + f = new Formatter(new StringBuilder(), l); + f.format("foo " + fs + " bar", args); + ck(fs, "foo " + exp + " bar", f.toString()); } private static void test(String fs, Object ... args) { Formatter f = new Formatter(new StringBuilder(), Locale.US); f.format(fs, args);
*** 133,143 **** fail(fs, ex); else pass(); } - private static void testDateTime(String fs, String exp, Calendar c) { testDateTime(fs, exp, c, true); } private static void testDateTime(String fs, String exp, Calendar c, boolean upper) { --- 137,146 ----
*** 212,337 **** //----------------------------------------------------------------- testDateTime("%tp", (i <12 ? "am" : "pm"), c); } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public static void test() { TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800")); // Any characters not explicitly defined as conversions, date/time // conversion suffixes, or flags are illegal and are reserved for --- 215,224 ----
*** 513,1680 **** //--------------------------------------------------------------------- tryCatch("%F", UnknownFormatConversionException.class); tryCatch("%#g", FormatFlagsConversionMismatchException.class); ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !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! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! //--------------------------------------------------------------------- ! // %t ! // ! // Date/Time conversions applicable to Calendar, Date, and long. ! //--------------------------------------------------------------------- ! test("%tA", "null", (Object)null); ! test("%TA", "NULL", (Object)null); ! ! //--------------------------------------------------------------------- ! // %t - errors ! //--------------------------------------------------------------------- ! tryCatch("%t", UnknownFormatConversionException.class); ! tryCatch("%T", UnknownFormatConversionException.class); ! tryCatch("%tP", UnknownFormatConversionException.class); ! tryCatch("%TP", UnknownFormatConversionException.class); ! tryCatch("%.5tB", IllegalFormatPrecisionException.class); ! tryCatch("%#tB", FormatFlagsConversionMismatchException.class); ! tryCatch("%-tB", MissingFormatWidthException.class); ! ! ! //--------------------------------------------------------------------- ! // %t - create test Calendar ! //--------------------------------------------------------------------- ! ! // Get the supported ids for GMT-08:00 (Pacific Standard Time) ! String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); ! // Create a Pacific Standard Time time zone ! SimpleTimeZone tz = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); ! // public GregorianCalendar(TimeZone zone, Locale aLocale); ! Calendar c0 = new GregorianCalendar(tz, Locale.US); ! // public final void set(int year, int month, int date, ! // int hourOfDay, int minute, int second); ! c0.set(1995, MAY, 23, 19, 48, 34); ! c0.set(Calendar.MILLISECOND, 584); ! ! //--------------------------------------------------------------------- ! // %t - Minutes, {nano,milli}*seconds ! // ! // testDateTime() verifies the expected output for all applicable types ! // (Calendar, Date, and long). It also verifies output for "%t" and ! // "%T". Thus it is sufficient to invoke that method once per ! // conversion/expected output. ! //--------------------------------------------------------------------- ! testDateTime("%tM", "48", c0); ! testDateTime("%tN", "584000000", c0); ! testDateTime("%tL", "584", c0); ! // testDateTime("%tQ", "801283714584", c0); ! ! testDateTime("%ts", String.valueOf(c0.getTimeInMillis() / 1000), c0); ! testDateTime("%tS", "34", c0); ! testDateTime("%tT", "19:48:34", c0); ! ! //--------------------------------------------------------------------- ! // %t - Hours, morning/afternoon markers ! // ! // testHours() iterates through all twenty-four hours to verify ! // numeric return value and morning/afternoon markers. ! //--------------------------------------------------------------------- ! testHours(); ! ! //--------------------------------------------------------------------- ! // %t - Portions of date [ day, month, dates, weeks ] ! //--------------------------------------------------------------------- ! testDateTime("%ta", "Tue", c0); ! testDateTime("%tA", "Tuesday", c0); ! testDateTime("%tb", "May", c0); ! testDateTime("%tB", "May", c0); ! testDateTime("%tC", "19", c0); ! testDateTime("%td", "23", c0); ! testDateTime("%te", "23", c0); ! testDateTime("%th", "May", c0); ! testDateTime("%tj", "143", c0); ! testDateTime("%tm", "05", c0); ! testDateTime("%ty", "95", c0); ! testDateTime("%tY", "1995", c0); ! ! //--------------------------------------------------------------------- ! // %t - TimeZone ! //--------------------------------------------------------------------- ! testDateTime("%tz", "-0800", c0); ! testDateTime("%tZ", "PST", c0); ! ! //--------------------------------------------------------------------- ! // %tz should always adjust for DST ! //--------------------------------------------------------------------- ! TimeZone dtz = TimeZone.getDefault(); ! ! // Artificial TimeZone based on PST with 3:15 DST always in effect ! TimeZone atz = new SimpleTimeZone(-8 * 60 * 60 * 1000, "AlwaysDST", ! JANUARY, 1, 0, 0, STANDARD_TIME, ! // 24hrs - 1m = 60 * 60 * 1000 * 24 - 1 ! DECEMBER, 31, 0, 60 * 60 * 1000 * 24 - 1, STANDARD_TIME, ! (int)(60 * 60 * 1000 * 3.25)); ! TimeZone.setDefault(atz); ! testDateTime("%tz", "-0445", Calendar.getInstance(atz)); ! ! // Restore the TimeZone and verify ! TimeZone.setDefault(dtz); ! if (atz.hasSameRules(TimeZone.getDefault())) ! throw new RuntimeException("Default TimeZone not restored"); //--------------------------------------------------------------------- // %t - Composites //--------------------------------------------------------------------- testDateTime("%tr", "07:48:34 PM", c0); --- 400,508 ---- //--------------------------------------------------------------------- tryCatch("%F", UnknownFormatConversionException.class); tryCatch("%#g", FormatFlagsConversionMismatchException.class); + //--------------------------------------------------------------------- + // %t + // + // Date/Time conversions applicable to Calendar, Date, and long. + //--------------------------------------------------------------------- + test("%tA", "null", (Object)null); + test("%TA", "NULL", (Object)null); + //--------------------------------------------------------------------- + // %t - errors + //--------------------------------------------------------------------- + tryCatch("%t", UnknownFormatConversionException.class); + tryCatch("%T", UnknownFormatConversionException.class); + tryCatch("%tP", UnknownFormatConversionException.class); + tryCatch("%TP", UnknownFormatConversionException.class); + tryCatch("%.5tB", IllegalFormatPrecisionException.class); + tryCatch("%#tB", FormatFlagsConversionMismatchException.class); + tryCatch("%-tB", MissingFormatWidthException.class); + //--------------------------------------------------------------------- + // %t - create test Calendar + //--------------------------------------------------------------------- + // Get the supported ids for GMT-08:00 (Pacific Standard Time) + String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); + // Create a Pacific Standard Time time zone + SimpleTimeZone tz = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); + // public GregorianCalendar(TimeZone zone, Locale aLocale); + Calendar c0 = new GregorianCalendar(tz, Locale.US); + // public final void set(int year, int month, int date, + // int hourOfDay, int minute, int second); + c0.set(1995, MAY, 23, 19, 48, 34); + c0.set(Calendar.MILLISECOND, 584); + //--------------------------------------------------------------------- + // %t - Minutes, {nano,milli}*seconds + // + // testDateTime() verifies the expected output for all applicable types + // (Calendar, Date, and long). It also verifies output for "%t" and + // "%T". Thus it is sufficient to invoke that method once per + // conversion/expected output. + //--------------------------------------------------------------------- + testDateTime("%tM", "48", c0); + testDateTime("%tN", "584000000", c0); + testDateTime("%tL", "584", c0); + // testDateTime("%tQ", "801283714584", c0); + testDateTime("%ts", String.valueOf(c0.getTimeInMillis() / 1000), c0); + testDateTime("%tS", "34", c0); + testDateTime("%tT", "19:48:34", c0); + //--------------------------------------------------------------------- + // %t - Hours, morning/afternoon markers + // + // testHours() iterates through all twenty-four hours to verify + // numeric return value and morning/afternoon markers. + //--------------------------------------------------------------------- + testHours(); + //--------------------------------------------------------------------- + // %t - Portions of date [ day, month, dates, weeks ] + //--------------------------------------------------------------------- + testDateTime("%ta", "Tue", c0); + testDateTime("%tA", "Tuesday", c0); + testDateTime("%tb", "May", c0); + testDateTime("%tB", "May", c0); + testDateTime("%tC", "19", c0); + testDateTime("%td", "23", c0); + testDateTime("%te", "23", c0); + testDateTime("%th", "May", c0); + testDateTime("%tj", "143", c0); + testDateTime("%tm", "05", c0); + testDateTime("%ty", "95", c0); + testDateTime("%tY", "1995", c0); + //--------------------------------------------------------------------- + // %t - TimeZone + //--------------------------------------------------------------------- + testDateTime("%tz", "-0800", c0); + testDateTime("%tZ", "PST", c0); + //--------------------------------------------------------------------- + // %tz should always adjust for DST + //--------------------------------------------------------------------- + TimeZone dtz = TimeZone.getDefault(); + // Artificial TimeZone based on PST with 3:15 DST always in effect + TimeZone atz = new SimpleTimeZone(-8 * 60 * 60 * 1000, "AlwaysDST", + JANUARY, 1, 0, 0, STANDARD_TIME, + // 24hrs - 1m = 60 * 60 * 1000 * 24 - 1 + DECEMBER, 31, 0, 60 * 60 * 1000 * 24 - 1, STANDARD_TIME, + (int)(60 * 60 * 1000 * 3.25)); + TimeZone.setDefault(atz); + testDateTime("%tz", "-0445", Calendar.getInstance(atz)); ! // Restore the TimeZone and verify ! TimeZone.setDefault(dtz); ! if (atz.hasSameRules(TimeZone.getDefault())) ! throw new RuntimeException("Default TimeZone not restored"); //--------------------------------------------------------------------- // %t - Composites //--------------------------------------------------------------------- testDateTime("%tr", "07:48:34 PM", c0);
*** 1683,1693 **** testDateTime("%tD", "05/23/95", c0); testDateTime("%tF", "1995-05-23", c0); testDateTime("%-12tF", "1995-05-23 ", c0); testDateTime("%12tF", " 1995-05-23", c0); - //--------------------------------------------------------------------- // %n //--------------------------------------------------------------------- test("%n", System.getProperty("line.separator"), (Object)null); test("%n", System.getProperty("line.separator"), ""); --- 511,520 ----