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) 2009-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 test.java.time.format;
  61 
  62 import static org.testng.Assert.assertEquals;
  63 import static org.testng.Assert.assertTrue;
  64 
  65 import java.util.Set;
  66 
  67 import java.text.ParsePosition;
  68 import java.time.ZoneId;
  69 import java.time.format.DateTimeBuilder;
  70 import java.time.format.DateTimeFormatter;
  71 import java.time.format.TextStyle;
  72 import java.time.format.DateTimeFormatterBuilder;
  73 import java.time.temporal.Queries;
  74 import java.time.zone.ZoneRulesProvider;
  75 
  76 import org.testng.annotations.DataProvider;
  77 import org.testng.annotations.Test;
  78 
  79 /**
  80  * Test ZonePrinterParser.
  81  */
  82 @Test(groups={"implementation"})
  83 public class TestZoneIdParser extends AbstractTestPrinterParser {
  84 
  85     private static final String AMERICA_DENVER = "America/Denver";
  86     private static final ZoneId TIME_ZONE_DENVER = ZoneId.of(AMERICA_DENVER);
  87 
  88     private DateTimeFormatter getFormatter0(TextStyle style) {
  89         if (style == null)
  90             return builder.appendZoneId().toFormatter(locale).withSymbols(symbols);
  91         return builder.appendZoneText(style).toFormatter(locale).withSymbols(symbols);
  92     }
  93 
  94     //-----------------------------------------------------------------------
  95     @DataProvider(name="error")
  96     Object[][] data_error() {
  97         return new Object[][] {
  98             {null, "hello", -1, IndexOutOfBoundsException.class},
  99             {null, "hello", 6, IndexOutOfBoundsException.class},
 100         };
 101     }
 102 
 103     @Test(dataProvider="error")
 104     public void test_parse_error(TextStyle style, String text, int pos, Class<?> expected) {
 105         try {
 106             getFormatter0(style).parseToBuilder(text, new ParsePosition(pos));
 107             assertTrue(false);
 108         } catch (RuntimeException ex) {
 109             assertTrue(expected.isInstance(ex));
 110         }
 111     }
 112 
 113     //-----------------------------------------------------------------------
 114     public void test_parse_exactMatch_Denver() throws Exception {
 115         ParsePosition pos = new ParsePosition(0);
 116         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder(AMERICA_DENVER, pos);
 117         assertEquals(pos.getIndex(), AMERICA_DENVER.length());
 118         assertParsed(dtb, TIME_ZONE_DENVER);
 119     }
 120 
 121     public void test_parse_startStringMatch_Denver() throws Exception {
 122         ParsePosition pos = new ParsePosition(0);
 123         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder(AMERICA_DENVER + "OTHER", pos);
 124         assertEquals(pos.getIndex(), AMERICA_DENVER.length());
 125         assertParsed(dtb, TIME_ZONE_DENVER);
 126     }
 127 
 128     public void test_parse_midStringMatch_Denver() throws Exception {
 129         ParsePosition pos = new ParsePosition(5);
 130         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder("OTHER" + AMERICA_DENVER + "OTHER", pos);
 131         assertEquals(pos.getIndex(), 5 + AMERICA_DENVER.length());
 132         assertParsed(dtb, TIME_ZONE_DENVER);
 133     }
 134 
 135     public void test_parse_endStringMatch_Denver() throws Exception {
 136         ParsePosition pos = new ParsePosition(5);
 137         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder("OTHER" + AMERICA_DENVER, pos);
 138         assertEquals(pos.getIndex(), 5 + AMERICA_DENVER.length());
 139         assertParsed(dtb, TIME_ZONE_DENVER);
 140     }
 141 
 142     public void test_parse_partialMatch() throws Exception {
 143         ParsePosition pos = new ParsePosition(5);
 144         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder("OTHERAmerica/Bogusville", pos);
 145         assertEquals(pos.getErrorIndex(), 5);  // TBD: -6 ?
 146         assertEquals(dtb, null);
 147     }
 148 
 149     //-----------------------------------------------------------------------
 150     @DataProvider(name="zones")
 151     Object[][] populateTestData() {
 152         Set<String> ids = ZoneRulesProvider.getAvailableZoneIds();
 153         Object[][] rtnval = new Object[ids.size()][];
 154         int i = 0;
 155         for (String id : ids) {
 156             rtnval[i++] = new Object[] { id, ZoneId.of(id) };
 157         }
 158         return rtnval;
 159     }
 160 
 161     @Test(dataProvider="zones")
 162     public void test_parse_exactMatch(String parse, ZoneId expected) throws Exception {
 163         ParsePosition pos = new ParsePosition(0);
 164         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder(parse, pos);
 165         assertEquals(pos.getIndex(), parse.length());
 166         assertParsed(dtb, expected);
 167     }
 168 
 169     @Test(dataProvider="zones")
 170     public void test_parse_startMatch(String parse, ZoneId expected) throws Exception {
 171         String append = " OTHER";
 172         parse += append;
 173         ParsePosition pos = new ParsePosition(0);
 174         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder(parse, pos);
 175         assertEquals(pos.getIndex(), parse.length() - append.length());
 176         assertParsed(dtb, expected);
 177     }
 178 
 179     //-----------------------------------------------------------------------
 180     public void test_parse_caseInsensitive() throws Exception {
 181         DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendZoneId().toFormatter();
 182         DateTimeFormatter fmtCI = new DateTimeFormatterBuilder().parseCaseInsensitive()
 183                                                                 .appendZoneId()
 184                                                                 .toFormatter();
 185         for (String zidStr : ZoneRulesProvider.getAvailableZoneIds()) {
 186             ZoneId zid = ZoneId.of(zidStr);
 187             assertEquals(fmt.parse(zidStr, Queries.zoneId()), zid);
 188             assertEquals(fmtCI.parse(zidStr.toLowerCase(), Queries.zoneId()), zid);
 189             assertEquals(fmtCI.parse(zidStr.toUpperCase(), Queries.zoneId()), zid);
 190             ParsePosition pos = new ParsePosition(5);
 191             assertEquals(fmtCI.parseToBuilder("OTHER" + zidStr.toLowerCase() + "OTHER", pos)
 192                               .query(Queries.zoneId()), zid);
 193             assertEquals(pos.getIndex(), zidStr.length() + 5);
 194             pos = new ParsePosition(5);
 195             assertEquals(fmtCI.parseToBuilder("OTHER" + zidStr.toUpperCase() + "OTHER", pos)
 196                               .query(Queries.zoneId()), zid);
 197             assertEquals(pos.getIndex(), zidStr.length() + 5);
 198         }
 199     }
 200 
 201     //-----------------------------------------------------------------------
 202     /*
 203     public void test_parse_endStringMatch_utc() throws Exception {
 204         ParsePosition pos = new ParsePosition(5);
 205         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder("OTHERUTC", pos);
 206         assertEquals(pos.getIndex(), 8);
 207         assertParsed(dtb, ZoneOffset.UTC);
 208     }
 209 
 210     public void test_parse_endStringMatch_utc_plus1() throws Exception {
 211         ParsePosition pos = new ParsePosition(5);
 212         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder("OTHERUTC+01:00", pos);
 213         assertEquals(pos.getIndex(), 14);
 214         assertParsed(dtb, ZoneId.of("UTC+01:00"));
 215     }
 216 
 217     //-----------------------------------------------------------------------
 218     public void test_parse_midStringMatch_utc() throws Exception {
 219         ParsePosition pos = new ParsePosition(5);
 220         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder("OTHERUTCOTHER", pos);
 221         assertEquals(pos.getIndex(), 8);
 222         assertParsed(dtb, ZoneOffset.UTC);
 223     }
 224 
 225     public void test_parse_midStringMatch_utc_plus1() throws Exception {
 226         ParsePosition pos = new ParsePosition(5);
 227         DateTimeBuilder dtb = getFormatter0(null).parseToBuilder("OTHERUTC+01:00OTHER", pos);
 228         assertEquals(pos.getIndex(), 14);
 229         assertParsed(dtb, ZoneId.of("UTC+01:00"));
 230     }
 231     */
 232     //-----------------------------------------------------------------------
 233     public void test_toString_id() {
 234         assertEquals(getFormatter0(null).toString(), "ZoneId()");
 235     }
 236 
 237     public void test_toString_text() {
 238         assertEquals(getFormatter0(TextStyle.FULL).toString(), "ZoneText(FULL)");
 239     }
 240 
 241     private void assertParsed(DateTimeBuilder dtb, ZoneId expectedZone) {
 242         assertEquals(dtb.query(ZoneId::from), expectedZone);
 243     }
 244 
 245 }