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) 2008-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.temporal;
  61 
  62 import static java.time.temporal.ChronoUnit.DAYS;
  63 import static java.time.temporal.ChronoUnit.MONTHS;
  64 import static java.time.temporal.ChronoUnit.NANOS;
  65 import static java.time.temporal.ChronoUnit.SECONDS;
  66 import static java.time.temporal.ChronoUnit.YEARS;
  67 import static org.testng.Assert.assertEquals;
  68 import static org.testng.Assert.assertSame;
  69 
  70 import java.io.ByteArrayOutputStream;
  71 import java.io.DataOutputStream;
  72 import java.io.IOException;
  73 
  74 import java.time.LocalDate;
  75 import java.time.Period;
  76 import java.time.temporal.ISOFields;
  77 import java.time.temporal.SimplePeriod;
  78 import java.time.temporal.TemporalUnit;
  79 
  80 import org.testng.annotations.DataProvider;
  81 import org.testng.annotations.Test;
  82 import tck.java.time.AbstractTCKTest;
  83 
  84 /**
  85  * Test.
  86  */
  87 @Test
  88 public class TCKSimplePeriod extends AbstractTCKTest {
  89 
  90     private static final SimplePeriod TEST_12_MONTHS = SimplePeriod.of(12, MONTHS);
  91 
  92     //-----------------------------------------------------------------------
  93     @Test(dataProvider="samples")
  94     public void test_serialization(long amount, TemporalUnit unit) throws ClassNotFoundException, IOException {
  95         SimplePeriod test = SimplePeriod.of(amount, unit);
  96         assertSerializable(test);
  97     }
  98 
  99     @Test
 100     public void test_serialization_format_zoneOffset() throws Exception {
 101         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 102         try (DataOutputStream dos = new DataOutputStream(baos) ) {
 103             dos.writeByte(10);
 104             dos.writeLong(12);
 105         }
 106         byte[] bytes = baos.toByteArray();
 107         assertSerializedBySer(TEST_12_MONTHS, bytes, new byte[0]);
 108     }
 109 
 110     //-----------------------------------------------------------------------
 111     // of(long, TenmporalUnit)
 112     //-----------------------------------------------------------------------
 113     @DataProvider(name="samples")
 114     Object[][] data_samples() {
 115         return new Object[][] {
 116                 {0, YEARS},
 117                 {1, YEARS},
 118                 {-1, YEARS},
 119                 {2, MONTHS},
 120                 {-2, MONTHS},
 121                 {43, ISOFields.WEEK_BASED_YEARS},
 122                 {Long.MAX_VALUE, NANOS},
 123                 {Long.MIN_VALUE, NANOS},
 124         };
 125     }
 126 
 127     @Test(dataProvider="samples")
 128     public void factory_of(long amount, TemporalUnit unit) {
 129         SimplePeriod test = SimplePeriod.of(amount, unit);
 130         assertEquals(test.getAmount(), amount);
 131         assertEquals(test.getUnit(), unit);
 132     }
 133 
 134     //-----------------------------------------------------------------------
 135     // addTo()
 136     //-----------------------------------------------------------------------
 137     @DataProvider(name="addTo")
 138     Object[][] data_addTo() {
 139         return new Object[][] {
 140             {SimplePeriod.of(0, DAYS),  date(2012, 6, 30), date(2012, 6, 30)},
 141 
 142             {SimplePeriod.of(1, DAYS),  date(2012, 6, 30), date(2012, 7, 1)},
 143             {SimplePeriod.of(-1, DAYS),  date(2012, 6, 30), date(2012, 6, 29)},
 144 
 145             {SimplePeriod.of(2, DAYS),  date(2012, 6, 30), date(2012, 7, 2)},
 146             {SimplePeriod.of(-2, DAYS),  date(2012, 6, 30), date(2012, 6, 28)},
 147 
 148             {SimplePeriod.of(3, MONTHS),  date(2012, 5, 31), date(2012, 8, 31)},
 149             {SimplePeriod.of(4, MONTHS),  date(2012, 5, 31), date(2012, 9, 30)},
 150             {SimplePeriod.of(-3, MONTHS),  date(2012, 5, 31), date(2012, 2, 29)},
 151             {SimplePeriod.of(-4, MONTHS),  date(2012, 5, 31), date(2012, 1, 31)},
 152         };
 153     }
 154 
 155     @Test(dataProvider="addTo")
 156     public void test_addTo(SimplePeriod period, LocalDate baseDate, LocalDate expected) {
 157         assertEquals(period.addTo(baseDate), expected);
 158     }
 159 
 160     @Test(dataProvider="addTo")
 161     public void test_addTo_usingLocalDatePlus(SimplePeriod period, LocalDate baseDate, LocalDate expected) {
 162         assertEquals(baseDate.plus(period), expected);
 163     }
 164 
 165     @Test(expectedExceptions=NullPointerException.class)
 166     public void test_addTo_nullZero() {
 167         SimplePeriod.of(0, DAYS).addTo(null);
 168     }
 169 
 170     @Test(expectedExceptions=NullPointerException.class)
 171     public void test_addTo_nullNonZero() {
 172         SimplePeriod.of(2, DAYS).addTo(null);
 173     }
 174 
 175     //-----------------------------------------------------------------------
 176     // subtractFrom()
 177     //-----------------------------------------------------------------------
 178     @DataProvider(name="subtractFrom")
 179     Object[][] data_subtractFrom() {
 180         return new Object[][] {
 181             {SimplePeriod.of(0, DAYS),  date(2012, 6, 30), date(2012, 6, 30)},
 182 
 183             {SimplePeriod.of(1, DAYS),  date(2012, 6, 30), date(2012, 6, 29)},
 184             {SimplePeriod.of(-1, DAYS),  date(2012, 6, 30), date(2012, 7, 1)},
 185 
 186             {SimplePeriod.of(2, DAYS),  date(2012, 6, 30), date(2012, 6, 28)},
 187             {SimplePeriod.of(-2, DAYS),  date(2012, 6, 30), date(2012, 7, 2)},
 188 
 189             {SimplePeriod.of(3, MONTHS),  date(2012, 5, 31), date(2012, 2, 29)},
 190             {SimplePeriod.of(4, MONTHS),  date(2012, 5, 31), date(2012, 1, 31)},
 191             {SimplePeriod.of(-3, MONTHS),  date(2012, 5, 31), date(2012, 8, 31)},
 192             {SimplePeriod.of(-4, MONTHS),  date(2012, 5, 31), date(2012, 9, 30)},
 193         };
 194     }
 195 
 196     @Test(dataProvider="subtractFrom")
 197     public void test_subtractFrom(SimplePeriod period, LocalDate baseDate, LocalDate expected) {
 198         assertEquals(period.subtractFrom(baseDate), expected);
 199     }
 200 
 201     @Test(dataProvider="subtractFrom")
 202     public void test_subtractFrom_usingLocalDateMinus(SimplePeriod period, LocalDate baseDate, LocalDate expected) {
 203         assertEquals(baseDate.minus(period), expected);
 204     }
 205 
 206     @Test(expectedExceptions=NullPointerException.class)
 207     public void test_subtractFrom_nullZero() {
 208         SimplePeriod.of(0, DAYS).subtractFrom(null);
 209     }
 210 
 211     @Test(expectedExceptions=NullPointerException.class)
 212     public void test_subtractFrom_nullNonZero() {
 213         SimplePeriod.of(2, DAYS).subtractFrom(null);
 214     }
 215 
 216     //-----------------------------------------------------------------------
 217     // abs()
 218     //-----------------------------------------------------------------------
 219     @Test(dataProvider="samples")
 220     public void test_abs(long amount, TemporalUnit unit) {
 221         SimplePeriod test = SimplePeriod.of(amount, unit);
 222         if (amount >= 0) {
 223             assertSame(test.abs(), test);  // spec requires assertSame
 224         } else if (amount == Long.MIN_VALUE) {
 225             // ignore, separately tested
 226         } else {
 227             assertEquals(test.abs(), SimplePeriod.of(-amount, unit));
 228         }
 229     }
 230 
 231     @Test(expectedExceptions=ArithmeticException.class)
 232     public void test_abs_minValue() {
 233         SimplePeriod.of(Long.MIN_VALUE, SECONDS).abs();
 234     }
 235 
 236     //-----------------------------------------------------------------------
 237     // equals() / hashCode()
 238     //-----------------------------------------------------------------------
 239     @Test(dataProvider="samples")
 240     public void test_equals(long amount, TemporalUnit unit) {
 241         SimplePeriod test1 = SimplePeriod.of(amount, unit);
 242         SimplePeriod test2 = SimplePeriod.of(amount, unit);
 243         assertEquals(test1, test2);
 244     }
 245 
 246     @Test(dataProvider="samples")
 247     public void test_equals_self(long amount, TemporalUnit unit) {
 248         SimplePeriod test = SimplePeriod.of(amount, unit);
 249         assertEquals(test.equals(test), true);
 250     }
 251 
 252     public void test_equals_null() {
 253         assertEquals(TEST_12_MONTHS.equals(null), false);
 254     }
 255 
 256     public void test_equals_otherClass() {
 257         Period test = Period.of(1, 2, 3, 4, 5, 6);
 258         assertEquals(test.equals(""), false);
 259     }
 260 
 261     //-----------------------------------------------------------------------
 262     public void test_hashCode() {
 263         SimplePeriod test5 = SimplePeriod.of(5, DAYS);
 264         SimplePeriod test6 = SimplePeriod.of(6, DAYS);
 265         SimplePeriod test5M = SimplePeriod.of(5, MONTHS);
 266         SimplePeriod test5Y = SimplePeriod.of(5, YEARS);
 267         assertEquals(test5.hashCode() == test5.hashCode(), true);
 268         assertEquals(test5.hashCode() == test6.hashCode(), false);
 269         assertEquals(test5.hashCode() == test5M.hashCode(), false);
 270         assertEquals(test5.hashCode() == test5Y.hashCode(), false);
 271     }
 272 
 273     //-----------------------------------------------------------------------
 274     // toString()
 275     //-----------------------------------------------------------------------
 276     @Test(dataProvider="samples")
 277     public void test_toString(long amount, TemporalUnit unit) {
 278         SimplePeriod test = SimplePeriod.of(amount, unit);
 279         assertEquals(test.toString(), amount + " " + unit.getName());
 280     }
 281 
 282     private static LocalDate date(int y, int m, int d) {
 283         return LocalDate.of(y, m, d);
 284     }
 285 
 286 }