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.text.ParsePosition;
  65 import java.time.LocalDateTime;
  66 import java.time.ZoneId;
  67 import java.time.ZoneOffset;
  68 import java.time.ZonedDateTime;
  69 import java.time.format.DateTimeFormatterBuilder;
  70 import java.time.temporal.Queries;
  71 import java.time.temporal.TemporalAccessor;
  72 import java.util.Locale;
  73 
  74 import org.testng.annotations.BeforeMethod;
  75 import org.testng.annotations.DataProvider;
  76 import org.testng.annotations.Test;
  77 
  78 /**
  79  * Test DateTimeFormatterBuilder.appendZoneId().
  80  */
  81 @Test
  82 public class TCKZoneIdPrinterParser {
  83 
  84     private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC;
  85     private static final ZoneOffset OFFSET_P0123 = ZoneOffset.ofHoursMinutes(1, 23);
  86     private static final ZoneId EUROPE_PARIS = ZoneId.of("Europe/Paris");
  87     private static final ZoneId AMERICA_NEW_YORK = ZoneId.of("America/New_York");
  88     private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40);
  89 
  90     private DateTimeFormatterBuilder builder;
  91     private ParsePosition pos;
  92 
  93     @BeforeMethod
  94     public void setUp() {
  95         builder = new DateTimeFormatterBuilder();
  96         pos = new ParsePosition(0);
  97     }
  98 
  99     //-----------------------------------------------------------------------
 100     @DataProvider(name="print")
 101     Object[][] data_print() {
 102         return new Object[][] {
 103                 {DT_2012_06_30_12_30_40, EUROPE_PARIS, "Europe/Paris"},
 104                 {DT_2012_06_30_12_30_40, AMERICA_NEW_YORK, "America/New_York"},
 105                 {DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
 106                 {DT_2012_06_30_12_30_40, OFFSET_P0123, "+01:23"},
 107         };
 108     }
 109 
 110     @Test(dataProvider="print")
 111     public void test_print(LocalDateTime ldt, ZoneId zone, String expected) {
 112         ZonedDateTime zdt = ldt.atZone(zone);
 113         builder.appendZoneId();
 114         String output = builder.toFormatter().format(zdt);
 115         assertEquals(output, expected);
 116     }
 117 
 118     @Test(dataProvider="print")
 119     public void test_print_pattern_VV(LocalDateTime ldt, ZoneId zone, String expected) {
 120         ZonedDateTime zdt = ldt.atZone(zone);
 121         builder.appendPattern("VV");
 122         String output = builder.toFormatter().format(zdt);
 123         assertEquals(output, expected);
 124     }
 125 
 126     //-----------------------------------------------------------------------
 127     @Test(expectedExceptions=IllegalArgumentException.class)
 128     public void test_print_pattern_V1rejected() {
 129         builder.appendPattern("V");
 130     }
 131 
 132     @Test(expectedExceptions=IllegalArgumentException.class)
 133     public void test_print_pattern_V3rejected() {
 134         builder.appendPattern("VVV");
 135     }
 136 
 137     @Test(expectedExceptions=IllegalArgumentException.class)
 138     public void test_print_pattern_V4rejected() {
 139         builder.appendPattern("VVVV");
 140     }
 141 
 142     @Test(expectedExceptions=IllegalArgumentException.class)
 143     public void test_print_pattern_V5rejected() {
 144         builder.appendPattern("VVVVV");
 145     }
 146 
 147     //-----------------------------------------------------------------------
 148     @DataProvider(name="parseSuccess")
 149     Object[][] data_parseSuccess() {
 150         return new Object[][] {
 151                 {"Z", 1, -1, ZoneOffset.UTC},
 152                 {"UTC", 3, -1, ZoneOffset.UTC},
 153                 {"UT", 2, -1, ZoneOffset.UTC},
 154                 {"GMT", 3, -1, ZoneOffset.UTC},
 155                 {"UTC0", 4, -1, ZoneOffset.UTC},
 156                 {"UT0", 3, -1, ZoneOffset.UTC},
 157                 {"GMT0", 4, -1, ZoneOffset.UTC},
 158 
 159                 {"+00:00", 6, -1, ZoneOffset.UTC},
 160                 {"UTC+00:00", 9, -1, ZoneOffset.UTC},
 161                 {"UT+00:00", 8, -1, ZoneOffset.UTC},
 162                 {"GMT+00:00", 9, -1, ZoneOffset.UTC},
 163                 {"-00:00", 6, -1, ZoneOffset.UTC},
 164                 {"UTC-00:00", 9, -1, ZoneOffset.UTC},
 165                 {"UT-00:00", 8, -1, ZoneOffset.UTC},
 166                 {"GMT-00:00", 9, -1, ZoneOffset.UTC},
 167 
 168                 {"+01:30", 6, -1, ZoneOffset.ofHoursMinutes(1, 30)},
 169                 {"UTC+01:30", 9, -1, ZoneOffset.ofHoursMinutes(1, 30)},
 170                 {"UT+02:30", 8, -1, ZoneOffset.ofHoursMinutes(2, 30)},
 171                 {"GMT+03:30", 9, -1, ZoneOffset.ofHoursMinutes(3, 30)},
 172                 {"-01:30", 6, -1, ZoneOffset.ofHoursMinutes(-1, -30)},
 173                 {"UTC-01:30", 9, -1, ZoneOffset.ofHoursMinutes(-1, -30)},
 174                 {"UT-02:30", 8, -1, ZoneOffset.ofHoursMinutes(-2, -30)},
 175                 {"GMT-03:30", 9, -1, ZoneOffset.ofHoursMinutes(-3, -30)},
 176 
 177                 // fallback to UTC
 178                 {"UTC-01:WW", 3, -1, ZoneOffset.UTC},
 179                 {"UT-02:WW", 2, -1, ZoneOffset.UTC},
 180                 {"GMT-03:WW", 3, -1, ZoneOffset.UTC},
 181                 {"Z0", 1, -1, ZoneOffset.UTC},
 182                 {"UTC1", 3, -1, ZoneOffset.UTC},
 183 
 184                 // Z not parsed as zero
 185                 {"UTCZ", 3, -1, ZoneOffset.UTC},
 186                 {"UTZ", 2, -1, ZoneOffset.UTC},
 187                 {"GMTZ", 3, -1, ZoneOffset.UTC},
 188 
 189                 // fail to parse
 190                 {"", 0, 0, null},
 191                 {"A", 0, 0, null},
 192                 {"UZ", 0, 0, null},
 193                 {"GMA", 0, 0, null},
 194                 {"0", 0, 0, null},
 195                 {"+", 0, 0, null},
 196                 {"-", 0, 0, null},
 197 
 198                 // zone IDs
 199                 {"Europe/London", 13, -1, ZoneId.of("Europe/London")},
 200                 {"America/New_York", 16, -1, ZoneId.of("America/New_York")},
 201                 {"America/Bogusville", 0, 0, null},
 202         };
 203     }
 204 
 205     @Test(dataProvider="parseSuccess")
 206     public void test_parseSuccess_plain(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) {
 207         builder.appendZoneId();
 208         TemporalAccessor parsed = builder.toFormatter().parseUnresolved(text, pos);
 209         assertEquals(pos.getErrorIndex(), expectedErrorIndex);
 210         assertEquals(pos.getIndex(), expectedIndex);
 211         if (expected != null) {
 212             assertEquals(parsed.query(Queries.zoneId()), expected);
 213             assertEquals(parsed.query(Queries.offset()), null);
 214             assertEquals(parsed.query(Queries.zone()), expected);
 215         } else {
 216             assertEquals(parsed, null);
 217         }
 218     }
 219 
 220     @Test(dataProvider="parseSuccess")
 221     public void test_parseSuccess_prefix(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) {
 222         builder.appendZoneId();
 223         pos.setIndex(3);
 224         TemporalAccessor parsed = builder.toFormatter().parseUnresolved("XXX" + text, pos);
 225         assertEquals(pos.getErrorIndex(), expectedErrorIndex >= 0  ? expectedErrorIndex + 3 : expectedErrorIndex);
 226         assertEquals(pos.getIndex(), expectedIndex + 3);
 227         if (expected != null) {
 228             assertEquals(parsed.query(Queries.zoneId()), expected);
 229             assertEquals(parsed.query(Queries.offset()), null);
 230             assertEquals(parsed.query(Queries.zone()), expected);
 231         } else {
 232             assertEquals(parsed, null);
 233         }
 234     }
 235 
 236     @Test(dataProvider="parseSuccess")
 237     public void test_parseSuccess_suffix(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) {
 238         builder.appendZoneId();
 239         TemporalAccessor parsed = builder.toFormatter().parseUnresolved(text + "XXX", pos);
 240         assertEquals(pos.getErrorIndex(), expectedErrorIndex);
 241         assertEquals(pos.getIndex(), expectedIndex);
 242         if (expected != null) {
 243             assertEquals(parsed.query(Queries.zoneId()), expected);
 244             assertEquals(parsed.query(Queries.offset()), null);
 245             assertEquals(parsed.query(Queries.zone()), expected);
 246         } else {
 247             assertEquals(parsed, null);
 248         }
 249     }
 250 
 251     @Test(dataProvider="parseSuccess")
 252     public void test_parseSuccess_caseSensitive(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) {
 253         builder.parseCaseSensitive().appendZoneId();
 254         TemporalAccessor parsed = builder.toFormatter().parseUnresolved(text.toLowerCase(Locale.ENGLISH), pos);
 255         if (text.matches("[^A-Z]*[A-Z].*")) {  // if input has letters
 256             assertEquals(pos.getErrorIndex() >= 0, true);
 257             assertEquals(pos.getIndex(), 0);
 258             assertEquals(parsed, null);
 259         } else {
 260             // case sensitive made no difference
 261             assertEquals(pos.getIndex(), expectedIndex);
 262             assertEquals(pos.getErrorIndex(), expectedErrorIndex);
 263             if (expected != null) {
 264                 assertEquals(parsed.query(Queries.zoneId()), expected);
 265                 assertEquals(parsed.query(Queries.offset()), null);
 266                 assertEquals(parsed.query(Queries.zone()), expected);
 267             } else {
 268                 assertEquals(parsed, null);
 269             }
 270         }
 271     }
 272 
 273     @Test(dataProvider="parseSuccess")
 274     public void test_parseSuccess_caseInsensitive(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) {
 275         builder.parseCaseInsensitive().appendZoneId();
 276         TemporalAccessor parsed = builder.toFormatter().parseUnresolved(text.toLowerCase(Locale.ENGLISH), pos);
 277         assertEquals(pos.getErrorIndex(), expectedErrorIndex);
 278         assertEquals(pos.getIndex(), expectedIndex);
 279         if (expected != null) {
 280             assertEquals(parsed.query(Queries.zoneId()), expected);
 281             assertEquals(parsed.query(Queries.offset()), null);
 282             assertEquals(parsed.query(Queries.zone()), expected);
 283         } else {
 284             assertEquals(parsed, null);
 285         }
 286     }
 287 
 288 }