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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
  28  *
  29  * All rights reserved.
  30  *
  31  * Redistribution and use in source and binary forms, with or without
  32  * modification, are permitted provided that the following conditions are met:
  33  *
  34  *  * Redistributions of source code must retain the above copyright notice,
  35  *    this list of conditions and the following disclaimer.
  36  *
  37  *  * Redistributions in binary form must reproduce the above copyright notice,
  38  *    this list of conditions and the following disclaimer in the documentation
  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package tck.java.time.chrono;
  58 
  59 import static org.testng.Assert.assertEquals;
  60 
  61 import java.time.LocalDate;
  62 import java.time.LocalDateTime;
  63 import java.time.LocalTime;
  64 import java.time.ZoneId;
  65 import java.time.ZonedDateTime;
  66 import java.time.chrono.ChronoLocalDate;
  67 import java.time.chrono.Chronology;
  68 import java.time.chrono.Era;
  69 import java.time.chrono.IsoChronology;
  70 import java.time.temporal.ChronoField;
  71 import java.time.temporal.Queries;
  72 import java.time.temporal.TemporalAccessor;
  73 import java.time.temporal.TemporalField;
  74 import java.time.temporal.TemporalQuery;
  75 import java.time.temporal.ValueRange;
  76 import java.util.List;
  77 
  78 import org.testng.annotations.Test;
  79 
  80 /**
  81  * Test.
  82  */
  83 @Test
  84 public class TCKChronology {
  85     // Can only work with IsoChronology here
  86     // others may be in separate module
  87 
  88     @Test
  89     public void factory_from_TemporalAccessor_dateWithChronlogy() {
  90         assertEquals(Chronology.from(LocalDate.of(2012, 6, 30)), IsoChronology.INSTANCE);
  91     }
  92 
  93     @Test
  94     public void factory_from_TemporalAccessor_chronology() {
  95         assertEquals(Chronology.from(new TemporalAccessor() {
  96             @Override
  97             public boolean isSupported(TemporalField field) {
  98                 throw new UnsupportedOperationException();
  99             }
 100             @Override
 101             public long getLong(TemporalField field) {
 102                 throw new UnsupportedOperationException();
 103             }
 104             @SuppressWarnings("unchecked")
 105             @Override
 106             public <R> R query(TemporalQuery<R> query) {
 107                 if (query == Queries.chronology()) {
 108                     return (R) IsoChronology.INSTANCE;
 109                 }
 110                 throw new UnsupportedOperationException();
 111             }
 112         }), IsoChronology.INSTANCE);
 113     }
 114 
 115     @Test
 116     public void factory_from_TemporalAccessor_noChronology() {
 117         assertEquals(Chronology.from(new TemporalAccessor() {
 118             @Override
 119             public boolean isSupported(TemporalField field) {
 120                 throw new UnsupportedOperationException();
 121             }
 122 
 123             @Override
 124             public long getLong(TemporalField field) {
 125                 throw new UnsupportedOperationException();
 126             }
 127 
 128             @Override
 129             public <R> R query(TemporalQuery<R> query) {
 130                 if (query == Queries.chronology()) {
 131                     return null;
 132                 }
 133                 throw new UnsupportedOperationException();
 134             }
 135         }), IsoChronology.INSTANCE);
 136     }
 137 
 138     @Test(expectedExceptions=NullPointerException.class)
 139     public void factory_from_TemporalAccessor_null() {
 140         Chronology.from(null);
 141     }
 142 
 143     //-----------------------------------------------------------------------
 144     @Test
 145     public void test_date_TemporalAccessor() {
 146         assertEquals(IsoChronology.INSTANCE.date(new TemporalAccessor() {
 147             @Override
 148             public boolean isSupported(TemporalField field) {
 149                 if (field == ChronoField.EPOCH_DAY) {
 150                     return true;
 151                 }
 152                 throw new UnsupportedOperationException();
 153             }
 154 
 155             @Override
 156             public long getLong(TemporalField field) {
 157                 if (field == ChronoField.EPOCH_DAY) {
 158                     return LocalDate.of(2012, 6, 30).toEpochDay();
 159                 }
 160                 throw new UnsupportedOperationException();
 161             }
 162 
 163             @SuppressWarnings("unchecked")
 164             @Override
 165             public <R> R query(TemporalQuery<R> query) {
 166                 if (query == Queries.localDate()) {
 167                     return (R) LocalDate.of(2012, 6, 30);
 168                 }
 169                 throw new UnsupportedOperationException();
 170             }
 171         }), LocalDate.of(2012, 6, 30));
 172     }
 173 
 174     @Test(expectedExceptions=NullPointerException.class)
 175     public void test_date_TemporalAccessor_null() {
 176         IsoChronology.INSTANCE.date(null);
 177     }
 178 
 179     //-----------------------------------------------------------------------
 180     @Test
 181     public void test_localDateTime_TemporalAccessor() {
 182         assertEquals(IsoChronology.INSTANCE.localDateTime(new TemporalAccessor() {
 183             @Override
 184             public boolean isSupported(TemporalField field) {
 185                 if (field == ChronoField.EPOCH_DAY || field == ChronoField.NANO_OF_DAY) {
 186                     return true;
 187                 }
 188                 throw new UnsupportedOperationException();
 189             }
 190 
 191             @Override
 192             public long getLong(TemporalField field) {
 193                 if (field == ChronoField.EPOCH_DAY) {
 194                     return LocalDate.of(2012, 6, 30).toEpochDay();
 195                 }
 196                 if (field == ChronoField.NANO_OF_DAY) {
 197                     return LocalTime.of(12, 30, 40).toNanoOfDay();
 198                 }
 199                 throw new UnsupportedOperationException();
 200             }
 201 
 202             @SuppressWarnings("unchecked")
 203             @Override
 204             public <R> R query(TemporalQuery<R> query) {
 205                 if (query == Queries.localDate()) {
 206                     return (R) LocalDate.of(2012, 6, 30);
 207                 }
 208                 if (query == Queries.localTime()) {
 209                     return (R) LocalTime.of(12, 30, 40);
 210                 }
 211                 throw new UnsupportedOperationException();
 212             }
 213         }), LocalDateTime.of(2012, 6, 30, 12, 30, 40));
 214     }
 215 
 216     @Test(expectedExceptions=NullPointerException.class)
 217     public void test_localDateTime_TemporalAccessor_null() {
 218         IsoChronology.INSTANCE.localDateTime(null);
 219     }
 220 
 221     //-----------------------------------------------------------------------
 222     @Test
 223     public void test_zonedDateTime_TemporalAccessor() {
 224         assertEquals(IsoChronology.INSTANCE.zonedDateTime(new TemporalAccessor() {
 225             @Override
 226             public boolean isSupported(TemporalField field) {
 227                 if (field == ChronoField.EPOCH_DAY || field == ChronoField.NANO_OF_DAY ||
 228                         field == ChronoField.INSTANT_SECONDS || field == ChronoField.NANO_OF_SECOND) {
 229                     return true;
 230                 }
 231                 throw new UnsupportedOperationException();
 232             }
 233 
 234             @Override
 235             public long getLong(TemporalField field) {
 236                 if (field == ChronoField.INSTANT_SECONDS) {
 237                     return ZonedDateTime.of(2012, 6, 30, 12, 30, 40, 0, ZoneId.of("Europe/London")).toEpochSecond();
 238                 }
 239                 if (field == ChronoField.NANO_OF_SECOND) {
 240                     return 0;
 241                 }
 242                 if (field == ChronoField.EPOCH_DAY) {
 243                     return LocalDate.of(2012, 6, 30).toEpochDay();
 244                 }
 245                 if (field == ChronoField.NANO_OF_DAY) {
 246                     return LocalTime.of(12, 30, 40).toNanoOfDay();
 247                 }
 248                 throw new UnsupportedOperationException();
 249             }
 250 
 251             @SuppressWarnings("unchecked")
 252             @Override
 253             public <R> R query(TemporalQuery<R> query) {
 254                 if (query == Queries.localDate()) {
 255                     return (R) LocalDate.of(2012, 6, 30);
 256                 }
 257                 if (query == Queries.localTime()) {
 258                     return (R) LocalTime.of(12, 30, 40);
 259                 }
 260                 if (query == Queries.zoneId() || query == Queries.zone()) {
 261                     return (R) ZoneId.of("Europe/London");
 262                 }
 263                 throw new UnsupportedOperationException();
 264             }
 265         }), ZonedDateTime.of(2012, 6, 30, 12, 30, 40, 0, ZoneId.of("Europe/London")));
 266     }
 267 
 268     @Test(expectedExceptions=NullPointerException.class)
 269     public void test_zonedDateTime_TemporalAccessor_null() {
 270         IsoChronology.INSTANCE.zonedDateTime(null);
 271     }
 272 
 273 }