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 
  24 /*
  25  * This file is available under and governed by the GNU General Public
  26  * License version 2 only, as published by the Free Software Foundation.
  27  * However, the following notice accompanied the original version of this
  28  * file:
  29  *
  30  * Copyright (c) 2010-2012, Stephen Colebourne & Michael Nascimento Santos
  31  *
  32  * All rights reserved.
  33  *
  34  * Redistribution and use in source and binary forms, with or without
  35  * modification, are permitted provided that the following conditions are met:
  36  *
  37  *  * Redistributions of source code must retain the above copyright notice,
  38  *    this list of conditions and the following disclaimer.
  39  *
  40  *  * Redistributions in binary form must reproduce the above copyright notice,
  41  *    this list of conditions and the following disclaimer in the documentation
  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  45  *    may be used to endorse or promote products derived from this software
  46  *    without specific prior written permission.
  47  *
  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package tck.java.time.format;
  61 
  62 import static org.testng.Assert.assertEquals;
  63 
  64 import java.time.LocalDateTime;
  65 import java.time.ZoneId;
  66 import java.time.ZoneOffset;
  67 import java.time.ZonedDateTime;
  68 import java.time.format.DateTimeFormatter;
  69 import java.time.format.DateTimeFormatterBuilder;
  70 
  71 import org.testng.annotations.BeforeMethod;
  72 import org.testng.annotations.DataProvider;
  73 import org.testng.annotations.Test;
  74 
  75 /**
  76  * Test DateTimeFormatterBuilder.appendOffset().
  77  */
  78 @Test
  79 public class TCKOffsetPrinterParser {
  80 
  81     private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC;
  82     private static final ZoneOffset OFFSET_P0100 = ZoneOffset.ofHours(1);
  83     private static final ZoneOffset OFFSET_P0123 = ZoneOffset.ofHoursMinutes(1, 23);
  84     private static final ZoneOffset OFFSET_P0023 = ZoneOffset.ofHoursMinutes(0, 23);
  85     private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45);
  86     private static final ZoneOffset OFFSET_P000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, 45);
  87     private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1);
  88     private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23);
  89     private static final ZoneOffset OFFSET_M0023 = ZoneOffset.ofHoursMinutes(0, -23);
  90     private static final ZoneOffset OFFSET_M012345 = ZoneOffset.ofHoursMinutesSeconds(-1, -23, -45);
  91     private static final ZoneOffset OFFSET_M000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, -45);
  92     private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40);
  93 
  94     private DateTimeFormatterBuilder builder;
  95 
  96     @BeforeMethod
  97     public void setUp() {
  98         builder = new DateTimeFormatterBuilder();
  99     }
 100 
 101     //-----------------------------------------------------------------------
 102     @DataProvider(name="print")
 103     Object[][] data_print() {
 104         return new Object[][] {
 105                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 106                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+01"},
 107                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+01"},
 108                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "Z"},
 109                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+01"},
 110                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P000045, "Z"},
 111                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-01"},
 112                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-01"},
 113                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "Z"},
 114                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M012345, "-01"},
 115                 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "Z"},
 116 
 117                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 118                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+01"},
 119                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+0123"},
 120                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "+0023"},
 121                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+0123"},
 122                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_P000045, "Z"},
 123                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-01"},
 124                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-0123"},
 125                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "-0023"},
 126                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_M012345, "-0123"},
 127                 {"+HHmm", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "Z"},
 128 
 129                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 130                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+0100"},
 131                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+0123"},
 132                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "+0023"},
 133                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+0123"},
 134                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_P000045, "Z"},
 135                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-0100"},
 136                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-0123"},
 137                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "-0023"},
 138                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_M012345, "-0123"},
 139                 {"+HHMM", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "Z"},
 140 
 141                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 142                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+01:00"},
 143                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+01:23"},
 144                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "+00:23"},
 145                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+01:23"},
 146                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_P000045, "Z"},
 147                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-01:00"},
 148                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-01:23"},
 149                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "-00:23"},
 150                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_M012345, "-01:23"},
 151                 {"+HH:MM", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "Z"},
 152 
 153                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 154                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+0100"},
 155                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+0123"},
 156                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "+0023"},
 157                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+012345"},
 158                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_P000045, "+000045"},
 159                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-0100"},
 160                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-0123"},
 161                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "-0023"},
 162                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_M012345, "-012345"},
 163                 {"+HHMMss", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "-000045"},
 164 
 165                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 166                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+01:00"},
 167                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+01:23"},
 168                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "+00:23"},
 169                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+01:23:45"},
 170                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "-00:00:45"},
 171                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-01:00"},
 172                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-01:23"},
 173                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "-00:23"},
 174                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M012345, "-01:23:45"},
 175                 {"+HH:MM:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "-00:00:45"},
 176 
 177                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 178                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+010000"},
 179                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+012300"},
 180                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "+002300"},
 181                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+012345"},
 182                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "-000045"},
 183                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-010000"},
 184                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-012300"},
 185                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "-002300"},
 186                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_M012345, "-012345"},
 187                 {"+HHMMSS", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "-000045"},
 188 
 189                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 190                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+01:00:00"},
 191                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+01:23:00"},
 192                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "+00:23:00"},
 193                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+01:23:45"},
 194                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "-00:00:45"},
 195                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-01:00:00"},
 196                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-01:23:00"},
 197                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "-00:23:00"},
 198                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_M012345, "-01:23:45"},
 199                 {"+HH:MM:SS", "Z", DT_2012_06_30_12_30_40, OFFSET_M000045, "-00:00:45"},
 200         };
 201     }
 202 
 203     @Test(dataProvider="print")
 204     public void test_print(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
 205         ZonedDateTime zdt = ldt.atZone(zone);
 206         builder.appendOffset(offsetPattern, noOffset);
 207         String output = builder.toFormatter().format(zdt);
 208         assertEquals(output, expected);
 209     }
 210 
 211     //-----------------------------------------------------------------------
 212     @Test(dataProvider="print")
 213     public void test_print_pattern_X(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
 214         String pattern = null;
 215         if (offsetPattern.equals("+HHmm") && noOffset.equals("Z")) {
 216             pattern = "X";
 217         } else if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
 218             pattern = "XX";
 219         } else if (offsetPattern.equals("+HH:MM") && noOffset.equals("Z")) {
 220             pattern = "XXX";
 221         } else if (offsetPattern.equals("+HHMMss") && noOffset.equals("Z")) {
 222             pattern = "XXXX";
 223         } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
 224             pattern = "XXXXX";
 225         }
 226         if (pattern != null) {
 227             ZonedDateTime zdt = ldt.atZone(zone);
 228             builder.appendPattern(pattern);
 229             String output = builder.toFormatter().format(zdt);
 230             assertEquals(output, expected);
 231         }
 232     }
 233 
 234     @Test(dataProvider="print")
 235     public void test_print_pattern_x(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
 236         String pattern = null;
 237         String zero = null;
 238         if (offsetPattern.equals("+HHmm") && noOffset.equals("Z")) {
 239             pattern = "x";
 240             zero = "+00";
 241         } else if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
 242             pattern = "xx";
 243             zero = "+0000";
 244         } else if (offsetPattern.equals("+HH:MM") && noOffset.equals("Z")) {
 245             pattern = "xxx";
 246             zero = "+00:00";
 247         } else if (offsetPattern.equals("+HHMMss") && noOffset.equals("Z")) {
 248             pattern = "xxxx";
 249             zero = "+0000";
 250         } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
 251             pattern = "xxxxx";
 252             zero = "+00:00";
 253         }
 254         if (pattern != null) {
 255             ZonedDateTime zdt = ldt.atZone(zone);
 256             builder.appendPattern(pattern);
 257             String output = builder.toFormatter().format(zdt);
 258             assertEquals(output, (expected.equals("Z") ? zero : expected));
 259         }
 260     }
 261 
 262     @Test(dataProvider="print")
 263     public void test_print_pattern_Z(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
 264         String pattern = null;
 265         if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
 266             ZonedDateTime zdt = ldt.atZone(zone);
 267             DateTimeFormatter f1 = new DateTimeFormatterBuilder().appendPattern("Z").toFormatter();
 268             String output1 = f1.format(zdt);
 269             assertEquals(output1, (expected.equals("Z") ? "+0000" : expected));
 270 
 271             DateTimeFormatter f2 = new DateTimeFormatterBuilder().appendPattern("ZZ").toFormatter();
 272             String output2 = f2.format(zdt);
 273             assertEquals(output2, (expected.equals("Z") ? "+0000" : expected));
 274 
 275             DateTimeFormatter f3 = new DateTimeFormatterBuilder().appendPattern("ZZZ").toFormatter();
 276             String output3 = f3.format(zdt);
 277             assertEquals(output3, (expected.equals("Z") ? "+0000" : expected));
 278         }
 279     }
 280 
 281     //-----------------------------------------------------------------------
 282     @Test(expectedExceptions=IllegalArgumentException.class)
 283     public void test_print_pattern_X6rejected() {
 284         builder.appendPattern("XXXXXX");
 285     }
 286 
 287     @Test(expectedExceptions=IllegalArgumentException.class)
 288     public void test_print_pattern_x6rejected() {
 289         builder.appendPattern("xxxxxx");
 290     }
 291 
 292     @Test(expectedExceptions=IllegalArgumentException.class)
 293     public void test_print_pattern_Z4rejected() {
 294         builder.appendPattern("ZZZZ");
 295     }
 296 
 297 }