test/java/time/test/java/util/TestFormatter.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.java.util;
  24 
  25 import java.time.Instant;
  26 import java.time.OffsetDateTime;
  27 import java.time.ZonedDateTime;

  28 import java.time.temporal.ChronoField;
  29 
  30 import java.util.*;
  31 
  32 import org.testng.annotations.Test;
  33 import static org.testng.Assert.assertEquals;
  34 
  35 /* @test
  36  * @summary Unit test for j.u.Formatter threeten date/time support

  37  */
  38 @Test
  39 public class TestFormatter {
  40 
  41     // time
  42     private static String[] fmtStrTime = new String[] {
  43          "H:[%tH] I:[%1$tI] k:[%1$tk] l:[%1$tl] M:[%1$tM] S:[%1$tS] L:[%1$tL] N:[%1$tN] p:[%1$tp]",
  44          "H:[%TH] I:[%1$TI] k:[%1$Tk] l:[%1$Tl] M:[%1$TM] S:[%1$TS] L:[%1$TL] N:[%1$TN] p:[%1$Tp]",
  45          "R:[%tR] T:[%1$tT] r:[%1$tr]",
  46          "R:[%TR] T:[%1$TT] r:[%1$Tr]"
  47     };
  48     // date
  49     private static String[] fmtStrDate = new String[] {
  50         "B:[%tB] b:[%1$tb] h:[%1$th] A:[%1$tA] a:[%1$ta] C:[%1$tC] Y:[%1$tY] y:[%1$ty] j:[%1$tj] m:[%1$tm] d:[%1$td] e:[%1$te]",
  51         "B:[%TB] b:[%1$Tb] h:[%1$Th] A:[%1$TA] a:[%1$Ta] C:[%1$TC] Y:[%1$TY] y:[%1$Ty] j:[%1$Tj] m:[%1$Tm] d:[%1$Td] e:[%1$Te]",
  52         "D:[%tD] F:[%1$tF]",
  53         "D:[%TD] F:[%1$TF]"
  54     };
  55 
  56     private int total = 0;
  57     private int failure = 0;
  58     private boolean verbose = true;
  59 
  60     @Test
  61     public void test () {
  62 
  63         int N = 12;
  64         //locales = Locale.getAvailableLocales();
  65         Locale[] locales = new Locale[] {
  66            Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE};
  67 
  68         Random r = new Random();
  69         ZonedDateTime  zdt = ZonedDateTime.now();







  70         while (N-- > 0) {

  71             zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
  72                      .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
  73             Instant instant = zdt.toInstant();
  74             Calendar cal = Calendar.getInstance();
  75             cal.setTimeInMillis(instant.toEpochMilli());
  76 
  77             for (Locale locale : locales) {
  78                     for (String fmtStr : fmtStrDate) {
  79                     testDate(fmtStr, locale, zdt, cal);
  80                 }
  81                 for (String fmtStr : fmtStrTime) {
  82                     testTime(fmtStr, locale, zdt, cal);
  83                 }
  84                 testZoneId(locale, zdt, cal);
  85                 testInstant(locale, instant, zdt, cal);
  86             }
  87         }

  88         if (verbose) {
  89             if (failure != 0) {
  90                 System.out.println("Total " + failure + "/" + total + " tests failed");
  91             } else {
  92                 System.out.println("All tests (" + total + ") PASSED");
  93             }
  94         }
  95         assertEquals(failure, 0);
  96     }
  97 
  98     private String getClassName(Object o) {
  99         Class c = o.getClass();
 100         return c.getName().substring(c.getPackage().getName().length() + 1);
 101     }
 102 
 103     private String test(String fmtStr, Locale locale,
 104                                String expected, Object dt) {
 105         String out = new Formatter(
 106             new StringBuilder(), locale).format(fmtStr, dt).out().toString();
 107         if (verbose) {


 129         printFmtStr(locale, fmtStr);
 130         String expected = test(fmtStr, locale, null, cal);
 131         test(fmtStr, locale, expected, zdt);
 132         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 133         test(fmtStr, locale, expected, zdt.toLocalDateTime());
 134         test(fmtStr, locale, expected, zdt.toLocalDate());
 135     }
 136 
 137     private void testTime(String fmtStr, Locale locale,
 138                                  ZonedDateTime zdt, Calendar cal) {
 139         printFmtStr(locale, fmtStr);
 140         String expected = test(fmtStr, locale, null, cal);
 141         test(fmtStr, locale, expected, zdt);
 142         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 143         test(fmtStr, locale, expected, zdt.toLocalDateTime());
 144         test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime());
 145         test(fmtStr, locale, expected, zdt.toLocalTime());
 146     }
 147 
 148     private String toZoneIdStr(String expected) {




 149         return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}")
 150                        .replaceAll("GMT|UTC|UT", "Z");
 151     }
 152 
 153     private void testZoneId(Locale locale, ZonedDateTime zdt, Calendar cal) {
 154         String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]";
 155         printFmtStr(locale, fmtStr);
 156         String expected = toZoneIdStr(test(fmtStr, locale, null, cal));
 157         test(fmtStr, locale, expected, zdt);
 158         // get a new cal with fixed tz
 159         Calendar cal0 = Calendar.getInstance();
 160         cal0.setTimeInMillis(zdt.toInstant().toEpochMilli());
 161         cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId()));
 162         expected = toZoneIdStr(test(fmtStr, locale, null, cal0));
 163         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 164         test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime());
 165 
 166         // datetime + zid
 167         fmtStr = "c:[%tc] c:[%1$Tc]";
 168         printFmtStr(locale, fmtStr);
 169         expected = toZoneIdStr(test(fmtStr, locale, null, cal));
 170         test(fmtStr, locale, expected, zdt);
 171     }
 172 
 173     private void testInstant(Locale locale, Instant instant,
 174                              ZonedDateTime zdt, Calendar cal) {
 175         String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]";
 176         printFmtStr(locale, fmtStr);
 177         String expected = test(fmtStr, locale, null, cal);
 178         test(fmtStr, locale, expected, instant);
 179         test(fmtStr, locale, expected, zdt);
 180         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 181     }
 182 }


   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.java.util;
  24 
  25 import java.time.Instant;
  26 import java.time.OffsetDateTime;
  27 import java.time.ZonedDateTime;
  28 import java.time.ZoneId;
  29 import java.time.temporal.ChronoField;
  30 
  31 import java.util.*;
  32 
  33 import org.testng.annotations.Test;
  34 import static org.testng.Assert.assertEquals;
  35 
  36 /* @test
  37  * @summary Unit test for j.u.Formatter threeten date/time support
  38  * @bug 8012638
  39  */
  40 @Test
  41 public class TestFormatter {
  42 
  43     // time
  44     private static String[] fmtStrTime = new String[] {
  45          "H:[%tH] I:[%1$tI] k:[%1$tk] l:[%1$tl] M:[%1$tM] S:[%1$tS] L:[%1$tL] N:[%1$tN] p:[%1$tp]",
  46          "H:[%TH] I:[%1$TI] k:[%1$Tk] l:[%1$Tl] M:[%1$TM] S:[%1$TS] L:[%1$TL] N:[%1$TN] p:[%1$Tp]",
  47          "R:[%tR] T:[%1$tT] r:[%1$tr]",
  48          "R:[%TR] T:[%1$TT] r:[%1$Tr]"
  49     };
  50     // date
  51     private static String[] fmtStrDate = new String[] {
  52         "B:[%tB] b:[%1$tb] h:[%1$th] A:[%1$tA] a:[%1$ta] C:[%1$tC] Y:[%1$tY] y:[%1$ty] j:[%1$tj] m:[%1$tm] d:[%1$td] e:[%1$te]",
  53         "B:[%TB] b:[%1$Tb] h:[%1$Th] A:[%1$TA] a:[%1$Ta] C:[%1$TC] Y:[%1$TY] y:[%1$Ty] j:[%1$Tj] m:[%1$Tm] d:[%1$Td] e:[%1$Te]",
  54         "D:[%tD] F:[%1$tF]",
  55         "D:[%TD] F:[%1$TF]"
  56     };
  57 
  58     private int total = 0;
  59     private int failure = 0;
  60     private boolean verbose = true;
  61 
  62     @Test
  63     public void test () {
  64 
  65         int N = 12;
  66         //locales = Locale.getAvailableLocales();
  67         Locale[] locales = new Locale[] {
  68            Locale.ENGLISH, Locale.FRENCH, Locale.JAPANESE, Locale.CHINESE};

  69         Random r = new Random();
  70         ZonedDateTime  zdt0 = ZonedDateTime.now();
  71         ZonedDateTime[] zdts = new ZonedDateTime[] {
  72             zdt0,
  73             zdt0.withZoneSameLocal(ZoneId.of("UTC")),
  74             zdt0.withZoneSameLocal(ZoneId.of("GMT")),
  75             zdt0.withZoneSameLocal(ZoneId.of("UT")),
  76         };
  77 
  78         while (N-- > 0) {
  79             for (ZonedDateTime zdt : zdts) {
  80                 zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
  81                          .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
  82                 Instant instant = zdt.toInstant();
  83                 Calendar cal = Calendar.getInstance();
  84                 cal.setTimeInMillis(instant.toEpochMilli());
  85                 cal.setTimeZone(TimeZone.getTimeZone(zdt.getZone()));
  86                 for (Locale locale : locales) {
  87                     for (String fmtStr : fmtStrDate) {
  88                         testDate(fmtStr, locale, zdt, cal);
  89                     }
  90                     for (String fmtStr : fmtStrTime) {
  91                         testTime(fmtStr, locale, zdt, cal);
  92                     }
  93                     testZoneId(locale, zdt, cal);
  94                     testInstant(locale, instant, zdt, cal);
  95                 }
  96             }
  97         }
  98         if (verbose) {
  99             if (failure != 0) {
 100                 System.out.println("Total " + failure + "/" + total + " tests failed");
 101             } else {
 102                 System.out.println("All tests (" + total + ") PASSED");
 103             }
 104         }
 105         assertEquals(failure, 0);
 106     }
 107 
 108     private String getClassName(Object o) {
 109         Class c = o.getClass();
 110         return c.getName().substring(c.getPackage().getName().length() + 1);
 111     }
 112 
 113     private String test(String fmtStr, Locale locale,
 114                                String expected, Object dt) {
 115         String out = new Formatter(
 116             new StringBuilder(), locale).format(fmtStr, dt).out().toString();
 117         if (verbose) {


 139         printFmtStr(locale, fmtStr);
 140         String expected = test(fmtStr, locale, null, cal);
 141         test(fmtStr, locale, expected, zdt);
 142         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 143         test(fmtStr, locale, expected, zdt.toLocalDateTime());
 144         test(fmtStr, locale, expected, zdt.toLocalDate());
 145     }
 146 
 147     private void testTime(String fmtStr, Locale locale,
 148                                  ZonedDateTime zdt, Calendar cal) {
 149         printFmtStr(locale, fmtStr);
 150         String expected = test(fmtStr, locale, null, cal);
 151         test(fmtStr, locale, expected, zdt);
 152         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 153         test(fmtStr, locale, expected, zdt.toLocalDateTime());
 154         test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime());
 155         test(fmtStr, locale, expected, zdt.toLocalTime());
 156     }
 157 
 158     private String toZoneIdStr(String expected) {
 159         return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}");
 160     }
 161 
 162     private String toZoneOffsetStr(String expected) {
 163         return expected.replaceAll("(?:GMT|UTC)(?<off>[+\\-]?[0-9]{2}:[0-9]{2})", "${off}")
 164                        .replaceAll("GMT|UTC|UT", "Z");
 165     }
 166 
 167     private void testZoneId(Locale locale, ZonedDateTime zdt, Calendar cal) {
 168         String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]";
 169         printFmtStr(locale, fmtStr);
 170         String expected = toZoneIdStr(test(fmtStr, locale, null, cal));
 171         test(fmtStr, locale, expected, zdt);
 172         // get a new cal with fixed tz
 173         Calendar cal0 = Calendar.getInstance();
 174         cal0.setTimeInMillis(zdt.toInstant().toEpochMilli());
 175         cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId()));
 176         expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0));
 177         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 178         test(fmtStr, locale, expected, zdt.toOffsetDateTime().toOffsetTime());
 179 
 180         // datetime + zid
 181         fmtStr = "c:[%tc] c:[%1$Tc]";
 182         printFmtStr(locale, fmtStr);
 183         expected = toZoneIdStr(test(fmtStr, locale, null, cal));
 184         test(fmtStr, locale, expected, zdt);
 185     }
 186 
 187     private void testInstant(Locale locale, Instant instant,
 188                              ZonedDateTime zdt, Calendar cal) {
 189         String fmtStr = "s:[%ts] s:[%1$Ts] Q:[%1$tQ] Q:[%1$TQ]";
 190         printFmtStr(locale, fmtStr);
 191         String expected = test(fmtStr, locale, null, cal);
 192         test(fmtStr, locale, expected, instant);
 193         test(fmtStr, locale, expected, zdt);
 194         test(fmtStr, locale, expected, zdt.toOffsetDateTime());
 195     }
 196 }