< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   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 static org.testng.Assert.assertEquals;
  26 
  27 import java.time.Instant;
  28 import java.time.LocalTime;
  29 import java.time.OffsetDateTime;
  30 import java.time.ZonedDateTime;
  31 import java.time.ZoneId;
  32 
  33 import java.time.chrono.ChronoLocalDate;
  34 import java.time.chrono.ChronoLocalDateTime;
  35 import java.time.chrono.ChronoZonedDateTime;
  36 import java.time.chrono.Chronology;
  37 
  38 import java.time.temporal.ChronoField;
  39 import java.time.temporal.TemporalQueries;
  40 import java.time.temporal.TemporalAccessor;

  41 
  42 import java.util.*;
  43 
  44 import org.testng.annotations.DataProvider;
  45 import org.testng.annotations.Test;
  46 
  47 /* @test
  48  * @summary Unit test for j.u.Formatter threeten date/time support
  49  * @bug 8003680 8012638
  50  */
  51 @Test
  52 public class TestFormatter {
  53 
  54     // time
  55     private static String[] fmtStrTime = new String[] {
  56          "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]",
  57          "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]",
  58          "R:[%tR] T:[%1$tT] r:[%1$tr]",
  59          "R:[%TR] T:[%1$TT] r:[%1$Tr]"
  60     };


 136         if (o instanceof TemporalAccessor) {
 137             Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology());
 138             if (chrono != null) {
 139                 clname = clname + "(" + chrono.getId() + ")";
 140             }
 141         }
 142         if (o instanceof Calendar) {
 143             String type = ((Calendar)o).getCalendarType();
 144             clname = clname + "(" + type + ")";
 145         }
 146         return clname;
 147     }
 148 
 149     private String test(String fmtStr, Locale locale,
 150                                String expected, Object dt) {
 151         String out = new Formatter(
 152             new StringBuilder(), locale).format(fmtStr, dt).out().toString();
 153         if (verbose) {
 154             System.out.printf("%-24s  : %s%n", getClassName(dt), out);
 155         }






































 156         if (expected != null && !out.equals(expected)) {
 157             System.out.printf("%-24s  actual: %s%n                FAILED; expected: %s%n",
 158                               getClassName(dt), out, expected);
 159             new RuntimeException().printStackTrace(System.out);
 160             failure++;
 161         }
 162         total++;
 163         return out;
 164     }
 165 
 166     private void printFmtStr(Locale locale, String fmtStr) {
 167         if (verbose) {
 168             System.out.println("--------------------");
 169             System.out.printf("[%s, %s]%n", locale.toString(), fmtStr);
 170         }
 171     }
 172 
 173     private void testDate(String fmtStr, Locale locale,
 174                                  ChronoZonedDateTime<?> zdt, Calendar cal) {
 175         printFmtStr(locale, fmtStr);


   1 /*
   2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   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 static org.testng.Assert.assertEquals;
  26 
  27 import java.time.Instant;
  28 import java.time.LocalTime;
  29 import java.time.OffsetDateTime;
  30 import java.time.ZonedDateTime;
  31 import java.time.ZoneId;
  32 
  33 import java.time.chrono.ChronoLocalDate;
  34 import java.time.chrono.ChronoLocalDateTime;
  35 import java.time.chrono.ChronoZonedDateTime;
  36 import java.time.chrono.Chronology;
  37 
  38 import java.time.temporal.ChronoField;
  39 import java.time.temporal.TemporalQueries;
  40 import java.time.temporal.TemporalAccessor;
  41 import java.time.temporal.UnsupportedTemporalTypeException;
  42 
  43 import java.util.*;
  44 
  45 import org.testng.annotations.DataProvider;
  46 import org.testng.annotations.Test;
  47 
  48 /* @test
  49  * @summary Unit test for j.u.Formatter threeten date/time support
  50  * @bug 8003680 8012638
  51  */
  52 @Test
  53 public class TestFormatter {
  54 
  55     // time
  56     private static String[] fmtStrTime = new String[] {
  57          "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]",
  58          "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]",
  59          "R:[%tR] T:[%1$tT] r:[%1$tr]",
  60          "R:[%TR] T:[%1$TT] r:[%1$Tr]"
  61     };


 137         if (o instanceof TemporalAccessor) {
 138             Chronology chrono = ((TemporalAccessor)o).query(TemporalQueries.chronology());
 139             if (chrono != null) {
 140                 clname = clname + "(" + chrono.getId() + ")";
 141             }
 142         }
 143         if (o instanceof Calendar) {
 144             String type = ((Calendar)o).getCalendarType();
 145             clname = clname + "(" + type + ")";
 146         }
 147         return clname;
 148     }
 149 
 150     private String test(String fmtStr, Locale locale,
 151                                String expected, Object dt) {
 152         String out = new Formatter(
 153             new StringBuilder(), locale).format(fmtStr, dt).out().toString();
 154         if (verbose) {
 155             System.out.printf("%-24s  : %s%n", getClassName(dt), out);
 156         }
 157 
 158         // expected usually comes from Calendar which only has milliseconds
 159         // precision. So we're going to replace it's N:[nanos] stamp with
 160         // the correct value for nanos.
 161         if ((dt instanceof TemporalAccessor) && expected != null) {
 162             try {
 163                 // Get millis & nanos from the dt
 164                 final TemporalAccessor ta = (TemporalAccessor) dt;
 165                 final int nanos = ta.get(ChronoField.NANO_OF_SECOND);
 166                 final int millis = ta.get(ChronoField.MILLI_OF_SECOND);
 167                 final String nanstr = String.valueOf(nanos);
 168                 final String mistr = String.valueOf(millis);
 169 
 170                 // Compute the value of the N:[nanos] field that we expect
 171                 // to find in 'out'
 172                 final StringBuilder sb = new StringBuilder();
 173                 sb.append("N:[");
 174                 for (int i=nanstr.length(); i<9; i++) {
 175                     sb.append('0');
 176                 }
 177                 sb.append(nanos).append("]");
 178 
 179                 // Compute the truncated value of N:[nanos] field that might
 180                 // be in 'expected' when expected was built from Calendar.
 181                 final StringBuilder sbm = new StringBuilder();
 182                 sbm.append("N:[");
 183                 for (int i=mistr.length(); i<3; i++) {
 184                     sbm.append('0');
 185                 }
 186                 sbm.append(mistr).append("000000]");
 187 
 188                 // if expected contains the truncated value, replace it with
 189                 // the complete value.
 190                 expected = expected.replace(sbm.toString(), sb.toString());
 191             } catch (UnsupportedTemporalTypeException e) {
 192                 // nano seconds unsupported - nothing to do...
 193             }
 194         }
 195         if (expected != null && !out.equals(expected)) {
 196             System.out.printf("%-24s  actual: %s%n                FAILED; expected: %s%n",
 197                               getClassName(dt), out, expected);
 198             new RuntimeException().printStackTrace(System.out);
 199             failure++;
 200         }
 201         total++;
 202         return out;
 203     }
 204 
 205     private void printFmtStr(Locale locale, String fmtStr) {
 206         if (verbose) {
 207             System.out.println("--------------------");
 208             System.out.printf("[%s, %s]%n", locale.toString(), fmtStr);
 209         }
 210     }
 211 
 212     private void testDate(String fmtStr, Locale locale,
 213                                  ChronoZonedDateTime<?> zdt, Calendar cal) {
 214         printFmtStr(locale, fmtStr);


< prev index next >