• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.MONTH_OF_YEAR;
71 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
72 import static java.time.temporal.ChronoField.YEAR;
73 import static org.testng.Assert.assertEquals;
74 import static org.testng.Assert.fail;
75 
76 import java.time.DateTimeException;
77 import java.time.LocalDate;
78 import java.time.format.DateTimeFormatter;
79 import java.time.format.DateTimeFormatterBuilder;
80 import java.time.temporal.TemporalAccessor;
81 import java.time.temporal.TemporalField;
82 
83 import org.testng.annotations.DataProvider;
84 import org.testng.annotations.Test;
85 
86 /**
87  * Test.
88  */
89 public class TestDateTimeBuilderCombinations {
90 
91     @DataProvider(name = "combine")
data_combine()92     Object[][] data_combine() {
93         return new Object[][] {
94             {YEAR, 2012, MONTH_OF_YEAR, 6, DAY_OF_MONTH, 3, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
95             {PROLEPTIC_MONTH, 2012 * 12 + 6 - 1, DAY_OF_MONTH, 3, null, null, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
96             {YEAR, 2012, ALIGNED_WEEK_OF_YEAR, 6, DAY_OF_WEEK, 3, null, null, LocalDate.class, LocalDate.of(2012, 2, 8)},
97             {YEAR, 2012, DAY_OF_YEAR, 155, null, null, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
98 //            {ERA, 1, YEAR_OF_ERA, 2012, DAY_OF_YEAR, 155, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
99             {YEAR, 2012, MONTH_OF_YEAR, 6, null, null, null, null, LocalDate.class, null},
100             {EPOCH_DAY, 12, null, null, null, null, null, null, LocalDate.class, LocalDate.of(1970, 1, 13)},
101         };
102     }
103 
104     @Test(dataProvider = "combine")
test_derive(final TemporalField field1, final Number value1, final TemporalField field2, final Number value2, final TemporalField field3, final Number value3, final TemporalField field4, final Number value4, Class<?> query, Object expectedVal)105     public void test_derive(final TemporalField field1, final Number value1,
106                             final TemporalField field2, final Number value2,
107                             final TemporalField field3, final Number value3,
108                             final TemporalField field4, final Number value4,
109                             Class<?> query, Object expectedVal) {
110         // mock for testing that does not fully comply with TemporalAccessor contract
111         TemporalAccessor test = new TemporalAccessor() {
112             @Override
113             public boolean isSupported(TemporalField field) {
114                 return field == field1 || field == field2 || field == field3 || field == field4;
115             }
116             @Override
117             public long getLong(TemporalField field) {
118                 if (field == field1) {
119                     return value1.longValue();
120                 }
121                 if (field == field2) {
122                     return value2.longValue();
123                 }
124                 if (field == field3) {
125                     return value3.longValue();
126                 }
127                 if (field == field4) {
128                     return value4.longValue();
129                 }
130                 throw new DateTimeException("Unsupported");
131             }
132         };
133         String str = "";
134         DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
135         dtfb.appendValue(field1).appendLiteral('-');
136         str += value1 + "-";
137         if (field2 != null) {
138             dtfb.appendValue(field2).appendLiteral('-');
139             str += value2 + "-";
140         }
141         if (field3 != null) {
142             dtfb.appendValue(field3).appendLiteral('-');
143             str += value3 + "-";
144         }
145         if (field4 != null) {
146             dtfb.appendValue(field4).appendLiteral('-');
147             str += value4 + "-";
148         }
149         TemporalAccessor parsed = dtfb.toFormatter().parse(str);
150         if (query == LocalDate.class) {
151             if (expectedVal != null) {
152                 assertEquals(parsed.query(LocalDate::from), expectedVal);
153             } else {
154                 try {
155                     parsed.query(LocalDate::from);
156                     fail();
157                 } catch (DateTimeException ex) {
158                     // expected
159                 }
160             }
161         } else {
162             throw new IllegalArgumentException();
163         }
164     }
165 
166     //-----------------------------------------------------------------------
167     @DataProvider(name = "normalized")
data_normalized()168     Object[][] data_normalized() {
169         return new Object[][] {
170             {YEAR, 2127, YEAR, 2127},
171             {MONTH_OF_YEAR, 12, MONTH_OF_YEAR, 12},
172             {DAY_OF_YEAR, 127, DAY_OF_YEAR, 127},
173             {DAY_OF_MONTH, 23, DAY_OF_MONTH, 23},
174             {DAY_OF_WEEK, 127, DAY_OF_WEEK, 127L},
175             {ALIGNED_WEEK_OF_YEAR, 23, ALIGNED_WEEK_OF_YEAR, 23},
176             {ALIGNED_DAY_OF_WEEK_IN_YEAR, 4, ALIGNED_DAY_OF_WEEK_IN_YEAR, 4L},
177             {ALIGNED_WEEK_OF_MONTH, 4, ALIGNED_WEEK_OF_MONTH, 4},
178             {ALIGNED_DAY_OF_WEEK_IN_MONTH, 3, ALIGNED_DAY_OF_WEEK_IN_MONTH, 3},
179             {PROLEPTIC_MONTH, 27, PROLEPTIC_MONTH, null},
180             {PROLEPTIC_MONTH, 27, YEAR, 2},
181             {PROLEPTIC_MONTH, 27, MONTH_OF_YEAR, 4},
182         };
183     }
184 
185     @Test(dataProvider = "normalized")
test_normalized(final TemporalField field1, final Number value1, TemporalField expectedField, Number expectedVal)186     public void test_normalized(final TemporalField field1, final Number value1, TemporalField expectedField, Number expectedVal) {
187         // mock for testing that does not fully comply with TemporalAccessor contract
188         TemporalAccessor test = new TemporalAccessor() {
189             @Override
190             public boolean isSupported(TemporalField field) {
191                 return field == field1;
192             }
193             @Override
194             public long getLong(TemporalField field) {
195                 if (field == field1) {
196                     return value1.longValue();
197                 }
198                 throw new DateTimeException("Unsupported");
199             }
200         };
201         DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).toFormatter();
202         String str = value1.toString();
203         TemporalAccessor temporal = f.parse(str);
204         if (expectedVal != null) {
205             assertEquals(temporal.getLong(expectedField), expectedVal.longValue());
206         } else {
207             assertEquals(temporal.isSupported(expectedField), false);
208         }
209     }
210 
211 }
212