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

Print this page




   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 java.time.Instant;
  26 import java.time.temporal.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(groups={"implementation"})
  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]"


 112                               getClassName(dt), out, expected);
 113             new RuntimeException().printStackTrace();
 114             failure++;
 115         }
 116         total++;
 117         return out;
 118     }
 119 
 120     private void printFmtStr(Locale locale, String fmtStr) {
 121         if (verbose) {
 122             System.out.println("--------------------");
 123             System.out.printf("[%s, %s]%n", locale.toString(), fmtStr);
 124         }
 125     }
 126 
 127     private void testDate(String fmtStr, Locale locale,
 128                                  ZonedDateTime zdt, Calendar cal) {
 129         printFmtStr(locale, fmtStr);
 130         String expected = test(fmtStr, locale, null, cal);
 131         test(fmtStr, locale, expected, zdt);
 132         test(fmtStr, locale, expected, OffsetDateTime.of(zdt));
 133         test(fmtStr, locale, expected, zdt.getDateTime());
 134         test(fmtStr, locale, expected, OffsetDateTime.of(zdt).toOffsetDate());
 135         test(fmtStr, locale, expected, zdt.getDate());
 136     }
 137 
 138     private void testTime(String fmtStr, Locale locale,
 139                                  ZonedDateTime zdt, Calendar cal) {
 140         printFmtStr(locale, fmtStr);
 141         String expected = test(fmtStr, locale, null, cal);
 142         test(fmtStr, locale, expected, zdt);
 143         test(fmtStr, locale, expected, OffsetDateTime.of(zdt));
 144         test(fmtStr, locale, expected, zdt.getDateTime());
 145         test(fmtStr, locale, expected, OffsetDateTime.of(zdt).toOffsetTime());
 146         test(fmtStr, locale, expected, zdt.getTime());





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


   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 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(groups={"implementation"})
  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]"


 112                               getClassName(dt), out, expected);
 113             new RuntimeException().printStackTrace();
 114             failure++;
 115         }
 116         total++;
 117         return out;
 118     }
 119 
 120     private void printFmtStr(Locale locale, String fmtStr) {
 121         if (verbose) {
 122             System.out.println("--------------------");
 123             System.out.printf("[%s, %s]%n", locale.toString(), fmtStr);
 124         }
 125     }
 126 
 127     private void testDate(String fmtStr, Locale locale,
 128                                  ZonedDateTime zdt, Calendar cal) {
 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 }