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 test.java.time.temporal;
  61 
  62 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
  63 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
  64 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
  65 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
  66 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  67 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  68 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
  69 import static java.time.temporal.ChronoField.EPOCH_DAY;
  70 import static java.time.temporal.ChronoField.EPOCH_MONTH;
  71 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  72 import static java.time.temporal.ChronoField.YEAR;
  73 import static org.testng.Assert.assertEquals;
  74 
  75 import java.time.LocalDate;
  76 import java.time.format.DateTimeBuilder;
  77 import java.time.temporal.TemporalField;
  78 
  79 import org.testng.annotations.DataProvider;
  80 import org.testng.annotations.Test;
  81 
  82 /**
  83  * Test.
  84  */
  85 public class TestDateTimeBuilderCombinations {
  86 
  87     @DataProvider(name = "combine")
  88     Object[][] data_combine() {
  89         return new Object[][] {
  90             {YEAR, 2012, MONTH_OF_YEAR, 6, DAY_OF_MONTH, 3, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  91             {EPOCH_MONTH, (2012 - 1970) * 12 + 6 - 1, DAY_OF_MONTH, 3, null, null, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  92             {YEAR, 2012, ALIGNED_WEEK_OF_YEAR, 6, DAY_OF_WEEK, 3, null, null, LocalDate.class, LocalDate.of(2012, 2, 8)},
  93             {YEAR, 2012, DAY_OF_YEAR, 155, null, null, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  94 //            {ERA, 1, YEAR_OF_ERA, 2012, DAY_OF_YEAR, 155, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  95             {YEAR, 2012, MONTH_OF_YEAR, 6, null, null, null, null, LocalDate.class, null},
  96             {EPOCH_DAY, 12, null, null, null, null, null, null, LocalDate.class, LocalDate.of(1970, 1, 13)},
  97         };
  98     }
  99 
 100     @Test(dataProvider = "combine")
 101     public void test_derive(TemporalField field1, Number value1, TemporalField field2, Number value2,
 102             TemporalField field3, Number value3, TemporalField field4, Number value4, Class<?> query, Object expectedVal) {
 103         DateTimeBuilder builder = new DateTimeBuilder(field1, value1.longValue());
 104         if (field2 != null) {
 105             builder.addFieldValue(field2, value2.longValue());
 106         }
 107         if (field3 != null) {
 108             builder.addFieldValue(field3, value3.longValue());
 109         }
 110         if (field4 != null) {
 111             builder.addFieldValue(field4, value4.longValue());
 112         }
 113         builder.resolve();
 114         assertEquals(builder.extract((Class<?>) query), expectedVal);
 115     }
 116 
 117     //-----------------------------------------------------------------------
 118     @DataProvider(name = "normalized")
 119     Object[][] data_normalized() {
 120         return new Object[][] {
 121             {YEAR, 2127, null, null, null, null, YEAR, 2127},
 122             {MONTH_OF_YEAR, 12, null, null, null, null, MONTH_OF_YEAR, 12},
 123             {DAY_OF_YEAR, 127, null, null, null, null, DAY_OF_YEAR, 127},
 124             {DAY_OF_MONTH, 23, null, null, null, null, DAY_OF_MONTH, 23},
 125             {DAY_OF_WEEK, 127, null, null, null, null, DAY_OF_WEEK, 127L},
 126             {ALIGNED_WEEK_OF_YEAR, 23, null, null, null, null, ALIGNED_WEEK_OF_YEAR, 23},
 127             {ALIGNED_DAY_OF_WEEK_IN_YEAR, 4, null, null, null, null, ALIGNED_DAY_OF_WEEK_IN_YEAR, 4L},
 128             {ALIGNED_WEEK_OF_MONTH, 4, null, null, null, null, ALIGNED_WEEK_OF_MONTH, 4},
 129             {ALIGNED_DAY_OF_WEEK_IN_MONTH, 3, null, null, null, null, ALIGNED_DAY_OF_WEEK_IN_MONTH, 3},
 130             {EPOCH_MONTH, 15, null, null, null, null, EPOCH_MONTH, null},
 131             {EPOCH_MONTH, 15, null, null, null, null, YEAR, 1971},
 132             {EPOCH_MONTH, 15, null, null, null, null, MONTH_OF_YEAR, 4},
 133         };
 134     }
 135 
 136     @Test(dataProvider = "normalized")
 137     public void test_normalized(TemporalField field1, Number value1, TemporalField field2, Number value2,
 138             TemporalField field3, Number value3, TemporalField query, Number expectedVal) {
 139         DateTimeBuilder builder = new DateTimeBuilder(field1, value1.longValue());
 140         if (field2 != null) {
 141             builder.addFieldValue(field2, value2.longValue());
 142         }
 143         if (field3 != null) {
 144             builder.addFieldValue(field3, value3.longValue());
 145         }
 146         builder.resolve();
 147         if (expectedVal != null) {
 148             assertEquals(builder.getLong(query), expectedVal.longValue());
 149         } else {
 150             assertEquals(builder.containsFieldValue(query), false);
 151         }
 152     }
 153 
 154     //-----------------------------------------------------------------------
 155     // TODO: maybe reinstate
 156 //    public void test_split() {
 157 //        DateTimeBuilder builder = new DateTimeBuilder();
 158 //        builder.addCalendrical(LocalDateTime.of(2012, 6, 30, 12, 30));
 159 //        builder.addCalendrical(ZoneOffset.ofHours(2));
 160 //        builder.resolve();
 161 //        assertEquals(builder.build(LocalDate.class), LocalDate.of(2012, 6, 30));
 162 //        assertEquals(builder.build(LocalTime.class), LocalTime.of(12, 30));
 163 //        assertEquals(builder.build(ZoneOffset.class), ZoneOffset.ofHours(2));
 164 //
 165 //        assertEquals(builder.build(LocalDateTime.class), LocalDateTime.of(2012, 6, 30, 12, 30));
 166 //        assertEquals(builder.build(OffsetDate.class), OffsetDate.of(LocalDate.of(2012, 6, 30), ZoneOffset.ofHours(2)));
 167 //        assertEquals(builder.build(OffsetTime.class), OffsetTime.of(LocalTime.of(12, 30), ZoneOffset.ofHours(2)));
 168 ////        assertEquals(builder.build(OffsetDateTime.class), OffsetDateTime.of(2012, 6, 30, 12, 30, ZoneOffset.ofHours(2)));
 169 //    }
 170 
 171 }