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.calendar;
  58 
  59 import static org.testng.Assert.assertEquals;
  60 import static org.testng.Assert.assertFalse;
  61 import static org.testng.Assert.assertTrue;
  62 
  63 import java.time.DateTimeException;
  64 import java.time.LocalDate;
  65 import java.time.LocalDateTime;
  66 import java.time.LocalTime;
  67 import java.time.Month;
  68 import java.time.ZoneOffset;
  69 import java.time.calendar.MinguoChrono;

  70 import java.time.temporal.Adjusters;
  71 import java.time.temporal.ChronoUnit;
  72 import java.time.temporal.ChronoZonedDateTime;
  73 import java.time.temporal.Chrono;
  74 import java.time.temporal.ChronoLocalDate;
  75 import java.time.temporal.ChronoLocalDateTime;
  76 import java.time.temporal.ISOChrono;


  77 
  78 import org.testng.Assert;
  79 import org.testng.annotations.DataProvider;
  80 import org.testng.annotations.Test;
  81 
  82 /**
  83  * Test.
  84  */
  85 @Test
  86 public class TestMinguoChrono {
  87 

  88     //-----------------------------------------------------------------------
  89     // Chrono.ofName("Minguo")  Lookup by name
  90     //-----------------------------------------------------------------------
  91     @Test(groups={"tck"})
  92     public void test_chrono_byName() {
  93         Chrono<MinguoChrono> c = MinguoChrono.INSTANCE;
  94         Chrono<?> test = Chrono.of("Minguo");
  95         Assert.assertNotNull(test, "The Minguo calendar could not be found byName");
  96         Assert.assertEquals(test.getId(), "Minguo", "ID mismatch");
  97         Assert.assertEquals(test.getCalendarType(), "roc", "Type mismatch");
  98         Assert.assertEquals(test, c);
  99     }
 100 
 101     //-----------------------------------------------------------------------
 102     // creation, toLocalDate()
 103     //-----------------------------------------------------------------------
 104     @DataProvider(name="samples")
 105     Object[][] data_samples() {
 106         return new Object[][] {
 107             {MinguoChrono.INSTANCE.date(1, 1, 1), LocalDate.of(1912, 1, 1)},
 108             {MinguoChrono.INSTANCE.date(1, 1, 2), LocalDate.of(1912, 1, 2)},
 109             {MinguoChrono.INSTANCE.date(1, 1, 3), LocalDate.of(1912, 1, 3)},
 110 
 111             {MinguoChrono.INSTANCE.date(2, 1, 1), LocalDate.of(1913, 1, 1)},
 112             {MinguoChrono.INSTANCE.date(3, 1, 1), LocalDate.of(1914, 1, 1)},
 113             {MinguoChrono.INSTANCE.date(3, 12, 6), LocalDate.of(1914, 12, 6)},
 114             {MinguoChrono.INSTANCE.date(4, 1, 1), LocalDate.of(1915, 1, 1)},
 115             {MinguoChrono.INSTANCE.date(4, 7, 3), LocalDate.of(1915, 7, 3)},
 116             {MinguoChrono.INSTANCE.date(4, 7, 4), LocalDate.of(1915, 7, 4)},
 117             {MinguoChrono.INSTANCE.date(5, 1, 1), LocalDate.of(1916, 1, 1)},
 118             {MinguoChrono.INSTANCE.date(100, 3, 3), LocalDate.of(2011, 3, 3)},
 119             {MinguoChrono.INSTANCE.date(101, 10, 28), LocalDate.of(2012, 10, 28)},
 120             {MinguoChrono.INSTANCE.date(101, 10, 29), LocalDate.of(2012, 10, 29)},





 121         };
 122     }
 123 
 124     @Test(dataProvider="samples", groups={"tck"})
 125     public void test_toLocalDate(ChronoLocalDate<MinguoChrono> minguo, LocalDate iso) {
 126         assertEquals(LocalDate.from(minguo), iso);
 127     }
 128 
 129     @Test(dataProvider="samples", groups={"tck"})
 130     public void test_fromCalendrical(ChronoLocalDate<MinguoChrono> minguo, LocalDate iso) {
 131         assertEquals(MinguoChrono.INSTANCE.date(iso), minguo);
 132     }
 133 
 134     @SuppressWarnings("unused")
 135     @Test(dataProvider="samples", groups={"implementation"})
 136     public void test_MinguoDate(ChronoLocalDate<MinguoChrono> minguoDate, LocalDate iso) {
 137         ChronoLocalDate<MinguoChrono> hd = minguoDate;
 138         ChronoLocalDateTime<MinguoChrono> hdt = hd.atTime(LocalTime.NOON);
 139         ZoneOffset zo = ZoneOffset.ofHours(1);
 140         ChronoZonedDateTime<MinguoChrono> hzdt = hdt.atZone(zo);
 141         hdt = hdt.plus(1, ChronoUnit.YEARS);
 142         hdt = hdt.plus(1, ChronoUnit.MONTHS);
 143         hdt = hdt.plus(1, ChronoUnit.DAYS);
 144         hdt = hdt.plus(1, ChronoUnit.HOURS);
 145         hdt = hdt.plus(1, ChronoUnit.MINUTES);
 146         hdt = hdt.plus(1, ChronoUnit.SECONDS);
 147         hdt = hdt.plus(1, ChronoUnit.NANOS);
 148         ChronoLocalDateTime<MinguoChrono> a2 = hzdt.getDateTime();
 149         ChronoLocalDate<MinguoChrono> a3 = a2.getDate();
 150         ChronoLocalDate<MinguoChrono> a5 = hzdt.getDate();
 151         //System.out.printf(" d: %s, dt: %s; odt: %s; zodt: %s; a4: %s%n", date, hdt, hodt, hzdt, a5);
 152     }
 153 
 154     @Test()
 155     public void test_MinguoChrono() {
 156         ChronoLocalDate<MinguoChrono> h1 = MinguoChrono.ERA_ROC.date(1, 2, 3);
 157         ChronoLocalDate<MinguoChrono> h2 = h1;
 158         ChronoLocalDateTime<MinguoChrono> h3 = h2.atTime(LocalTime.NOON);
 159         @SuppressWarnings("unused")
 160         ChronoZonedDateTime<MinguoChrono> h4 = h3.atZone(ZoneOffset.UTC);
 161     }
 162 
 163     @DataProvider(name="badDates")
 164     Object[][] data_badDates() {
 165         return new Object[][] {
 166             {1912, 0, 0},
 167 
 168             {1912, -1, 1},
 169             {1912, 0, 1},
 170             {1912, 14, 1},
 171             {1912, 15, 1},
 172 
 173             {1912, 1, -1},
 174             {1912, 1, 0},
 175             {1912, 1, 32},
 176             {1912, 2, 29},
 177             {1912, 2, 30},
 178 
 179             {1912, 12, -1},
 180             {1912, 12, 0},
 181             {1912, 12, 32},





 182             };
 183     }
 184 
 185     @Test(dataProvider="badDates", groups={"tck"}, expectedExceptions=DateTimeException.class)
 186     public void test_badDates(int year, int month, int dom) {
 187         MinguoChrono.INSTANCE.date(year, month, dom);







































 188     }
 189 
 190     //-----------------------------------------------------------------------
 191     // with(DateTimeAdjuster)
 192     //-----------------------------------------------------------------------
 193     @Test(groups={"tck"})
 194     public void test_adjust1() {
 195         ChronoLocalDate<MinguoChrono> base = MinguoChrono.INSTANCE.date(2012, 10, 29);
 196         ChronoLocalDate<MinguoChrono> test = base.with(Adjusters.lastDayOfMonth());
 197         assertEquals(test, MinguoChrono.INSTANCE.date(2012, 10, 31));
 198     }
 199 
 200     @Test(groups={"tck"})
 201     public void test_adjust2() {
 202         ChronoLocalDate<MinguoChrono> base = MinguoChrono.INSTANCE.date(1728, 12, 2);
 203         ChronoLocalDate<MinguoChrono> test = base.with(Adjusters.lastDayOfMonth());
 204         assertEquals(test, MinguoChrono.INSTANCE.date(1728, 12, 31));
 205     }
 206 
 207     //-----------------------------------------------------------------------
 208     // MinguoDate.with(Local*)
 209     //-----------------------------------------------------------------------
 210     @Test(groups={"tck"})
 211     public void test_adjust_toLocalDate() {
 212         ChronoLocalDate<MinguoChrono> minguo = MinguoChrono.INSTANCE.date(99, 1, 4);
 213         ChronoLocalDate<MinguoChrono> test = minguo.with(LocalDate.of(2012, 7, 6));
 214         assertEquals(test, MinguoChrono.INSTANCE.date(101, 7, 6));
 215     }
 216 
 217     @Test(groups={"tck"}, expectedExceptions=DateTimeException.class)
 218     public void test_adjust_toMonth() {
 219         ChronoLocalDate<MinguoChrono> minguo = MinguoChrono.INSTANCE.date(1726, 1, 4);
 220         minguo.with(Month.APRIL);
 221     }
 222 
 223     //-----------------------------------------------------------------------
 224     // LocalDate.with(MinguoDate)
 225     //-----------------------------------------------------------------------
 226     @Test(groups={"tck"})
 227     public void test_LocalDate_adjustToMinguoDate() {
 228         ChronoLocalDate<MinguoChrono> minguo = MinguoChrono.INSTANCE.date(101, 10, 29);
 229         LocalDate test = LocalDate.MIN.with(minguo);
 230         assertEquals(test, LocalDate.of(2012, 10, 29));
 231     }
 232 
 233     @Test(groups={"tck"})
 234     public void test_LocalDateTime_adjustToMinguoDate() {
 235         ChronoLocalDate<MinguoChrono> minguo = MinguoChrono.INSTANCE.date(101, 10, 29);
 236         LocalDateTime test = LocalDateTime.MIN.with(minguo);
 237         assertEquals(test, LocalDateTime.of(2012, 10, 29, 0, 0));
 238     }
 239 
 240     //-----------------------------------------------------------------------
 241     // toString()
 242     //-----------------------------------------------------------------------
 243     @DataProvider(name="toString")
 244     Object[][] data_toString() {
 245         return new Object[][] {
 246             {MinguoChrono.INSTANCE.date(1, 1, 1), "Minguo ROC 1-01-01"},
 247             {MinguoChrono.INSTANCE.date(1728, 10, 28), "Minguo ROC 1728-10-28"},
 248             {MinguoChrono.INSTANCE.date(1728, 10, 29), "Minguo ROC 1728-10-29"},
 249             {MinguoChrono.INSTANCE.date(1727, 12, 5), "Minguo ROC 1727-12-05"},
 250             {MinguoChrono.INSTANCE.date(1727, 12, 6), "Minguo ROC 1727-12-06"},
 251         };
 252     }
 253 
 254     @Test(dataProvider="toString", groups={"tck"})
 255     public void test_toString(ChronoLocalDate<MinguoChrono> minguo, String expected) {
 256         assertEquals(minguo.toString(), expected);
 257     }
 258 
 259     //-----------------------------------------------------------------------
 260     // equals()
 261     //-----------------------------------------------------------------------
 262     @Test(groups="tck")
 263     public void test_equals_true() {
 264         assertTrue(MinguoChrono.INSTANCE.equals(MinguoChrono.INSTANCE));
 265     }
 266 
 267     @Test(groups="tck")
 268     public void test_equals_false() {
 269         assertFalse(MinguoChrono.INSTANCE.equals(ISOChrono.INSTANCE));
 270     }
 271 
 272 }
--- EOF ---