1 /*
2  * Copyright (c) 2012, 2016, 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.format;
61 
62 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
63 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
64 import static java.time.temporal.ChronoField.ERA;
65 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
66 import static java.time.temporal.IsoFields.QUARTER_OF_YEAR;
67 import static org.testng.Assert.assertEquals;
68 
69 import java.time.DateTimeException;
70 import java.time.DayOfWeek;
71 import java.time.LocalDate;
72 import java.time.chrono.JapaneseChronology;
73 import java.time.format.DateTimeFormatter;
74 import java.time.format.TextStyle;
75 import java.time.temporal.TemporalField;
76 import java.util.Locale;
77 
78 import org.testng.annotations.DataProvider;
79 import org.testng.annotations.Test;
80 import test.java.time.temporal.MockFieldValue;
81 
82 /**
83  * Test TextPrinterParser.
84  */
85 @Test
86 public class TestTextPrinter extends AbstractTestPrinterParser {
87 
88     //-----------------------------------------------------------------------
89     @Test(expectedExceptions=DateTimeException.class)
test_print_emptyCalendrical()90     public void test_print_emptyCalendrical() throws Exception {
91         getFormatter(DAY_OF_WEEK, TextStyle.FULL).formatTo(EMPTY_DTA, buf);
92     }
93 
test_print_append()94     public void test_print_append() throws Exception {
95         buf.append("EXISTING");
96         getFormatter(DAY_OF_WEEK, TextStyle.FULL).formatTo(LocalDate.of(2012, 4, 18), buf);
97         assertEquals(buf.toString(), "EXISTINGWednesday");
98     }
99 
100     //-----------------------------------------------------------------------
101     @DataProvider(name="print")
provider_dow()102     Object[][] provider_dow() {
103         return new Object[][] {
104             {DAY_OF_WEEK, TextStyle.FULL, 1, "Monday"},
105             {DAY_OF_WEEK, TextStyle.FULL, 2, "Tuesday"},
106             {DAY_OF_WEEK, TextStyle.FULL, 3, "Wednesday"},
107             {DAY_OF_WEEK, TextStyle.FULL, 4, "Thursday"},
108             {DAY_OF_WEEK, TextStyle.FULL, 5, "Friday"},
109             {DAY_OF_WEEK, TextStyle.FULL, 6, "Saturday"},
110             {DAY_OF_WEEK, TextStyle.FULL, 7, "Sunday"},
111 
112             {DAY_OF_WEEK, TextStyle.SHORT, 1, "Mon"},
113             {DAY_OF_WEEK, TextStyle.SHORT, 2, "Tue"},
114             {DAY_OF_WEEK, TextStyle.SHORT, 3, "Wed"},
115             {DAY_OF_WEEK, TextStyle.SHORT, 4, "Thu"},
116             {DAY_OF_WEEK, TextStyle.SHORT, 5, "Fri"},
117             {DAY_OF_WEEK, TextStyle.SHORT, 6, "Sat"},
118             {DAY_OF_WEEK, TextStyle.SHORT, 7, "Sun"},
119 
120             {DAY_OF_WEEK, TextStyle.NARROW, 1, "M"},
121             {DAY_OF_WEEK, TextStyle.NARROW, 2, "T"},
122             {DAY_OF_WEEK, TextStyle.NARROW, 3, "W"},
123             {DAY_OF_WEEK, TextStyle.NARROW, 4, "T"},
124             {DAY_OF_WEEK, TextStyle.NARROW, 5, "F"},
125             {DAY_OF_WEEK, TextStyle.NARROW, 6, "S"},
126             {DAY_OF_WEEK, TextStyle.NARROW, 7, "S"},
127 
128             {DAY_OF_MONTH, TextStyle.FULL, 1, "1"},
129             {DAY_OF_MONTH, TextStyle.FULL, 2, "2"},
130             {DAY_OF_MONTH, TextStyle.FULL, 3, "3"},
131             {DAY_OF_MONTH, TextStyle.FULL, 28, "28"},
132             {DAY_OF_MONTH, TextStyle.FULL, 29, "29"},
133             {DAY_OF_MONTH, TextStyle.FULL, 30, "30"},
134             {DAY_OF_MONTH, TextStyle.FULL, 31, "31"},
135 
136             {DAY_OF_MONTH, TextStyle.SHORT, 1, "1"},
137             {DAY_OF_MONTH, TextStyle.SHORT, 2, "2"},
138             {DAY_OF_MONTH, TextStyle.SHORT, 3, "3"},
139             {DAY_OF_MONTH, TextStyle.SHORT, 28, "28"},
140             {DAY_OF_MONTH, TextStyle.SHORT, 29, "29"},
141             {DAY_OF_MONTH, TextStyle.SHORT, 30, "30"},
142             {DAY_OF_MONTH, TextStyle.SHORT, 31, "31"},
143 
144             {MONTH_OF_YEAR, TextStyle.FULL, 1, "January"},
145             {MONTH_OF_YEAR, TextStyle.FULL, 2, "February"},
146             {MONTH_OF_YEAR, TextStyle.FULL, 3, "March"},
147             {MONTH_OF_YEAR, TextStyle.FULL, 4, "April"},
148             {MONTH_OF_YEAR, TextStyle.FULL, 5, "May"},
149             {MONTH_OF_YEAR, TextStyle.FULL, 6, "June"},
150             {MONTH_OF_YEAR, TextStyle.FULL, 7, "July"},
151             {MONTH_OF_YEAR, TextStyle.FULL, 8, "August"},
152             {MONTH_OF_YEAR, TextStyle.FULL, 9, "September"},
153             {MONTH_OF_YEAR, TextStyle.FULL, 10, "October"},
154             {MONTH_OF_YEAR, TextStyle.FULL, 11, "November"},
155             {MONTH_OF_YEAR, TextStyle.FULL, 12, "December"},
156 
157             {MONTH_OF_YEAR, TextStyle.SHORT, 1, "Jan"},
158             {MONTH_OF_YEAR, TextStyle.SHORT, 2, "Feb"},
159             {MONTH_OF_YEAR, TextStyle.SHORT, 3, "Mar"},
160             {MONTH_OF_YEAR, TextStyle.SHORT, 4, "Apr"},
161             {MONTH_OF_YEAR, TextStyle.SHORT, 5, "May"},
162             {MONTH_OF_YEAR, TextStyle.SHORT, 6, "Jun"},
163             {MONTH_OF_YEAR, TextStyle.SHORT, 7, "Jul"},
164             {MONTH_OF_YEAR, TextStyle.SHORT, 8, "Aug"},
165             {MONTH_OF_YEAR, TextStyle.SHORT, 9, "Sep"},
166             {MONTH_OF_YEAR, TextStyle.SHORT, 10, "Oct"},
167             {MONTH_OF_YEAR, TextStyle.SHORT, 11, "Nov"},
168             {MONTH_OF_YEAR, TextStyle.SHORT, 12, "Dec"},
169 
170             {MONTH_OF_YEAR, TextStyle.NARROW, 1, "J"},
171             {MONTH_OF_YEAR, TextStyle.NARROW, 2, "F"},
172             {MONTH_OF_YEAR, TextStyle.NARROW, 3, "M"},
173             {MONTH_OF_YEAR, TextStyle.NARROW, 4, "A"},
174             {MONTH_OF_YEAR, TextStyle.NARROW, 5, "M"},
175             {MONTH_OF_YEAR, TextStyle.NARROW, 6, "J"},
176             {MONTH_OF_YEAR, TextStyle.NARROW, 7, "J"},
177             {MONTH_OF_YEAR, TextStyle.NARROW, 8, "A"},
178             {MONTH_OF_YEAR, TextStyle.NARROW, 9, "S"},
179             {MONTH_OF_YEAR, TextStyle.NARROW, 10, "O"},
180             {MONTH_OF_YEAR, TextStyle.NARROW, 11, "N"},
181             {MONTH_OF_YEAR, TextStyle.NARROW, 12, "D"},
182 
183             {ERA,           TextStyle.FULL, 0, "Before Christ"},
184             {ERA,           TextStyle.FULL, 1, "Anno Domini"},
185             {ERA,           TextStyle.SHORT, 0, "BC"},
186             {ERA,           TextStyle.SHORT, 1, "AD"},
187             {ERA,           TextStyle.NARROW, 0, "B"},
188             {ERA,           TextStyle.NARROW, 1, "A"},
189 
190             {QUARTER_OF_YEAR, TextStyle.FULL, 1, "1st quarter"},
191             {QUARTER_OF_YEAR, TextStyle.FULL, 2, "2nd quarter"},
192             {QUARTER_OF_YEAR, TextStyle.FULL, 3, "3rd quarter"},
193             {QUARTER_OF_YEAR, TextStyle.FULL, 4, "4th quarter"},
194 
195             {QUARTER_OF_YEAR, TextStyle.SHORT, 1, "Q1"},
196             {QUARTER_OF_YEAR, TextStyle.SHORT, 2, "Q2"},
197             {QUARTER_OF_YEAR, TextStyle.SHORT, 3, "Q3"},
198             {QUARTER_OF_YEAR, TextStyle.SHORT, 4, "Q4"},
199 
200             {QUARTER_OF_YEAR, TextStyle.NARROW, 1, "1"},
201             {QUARTER_OF_YEAR, TextStyle.NARROW, 2, "2"},
202             {QUARTER_OF_YEAR, TextStyle.NARROW, 3, "3"},
203             {QUARTER_OF_YEAR, TextStyle.NARROW, 4, "4"},
204        };
205     }
206 
207     @DataProvider(name="print_DayOfWeekData")
providerDayOfWeekData()208     Object[][] providerDayOfWeekData() {
209         return new Object[][] {
210             // Locale, pattern, expected text, input DayOfWeek
211             {Locale.US, "e",  "1",  DayOfWeek.SUNDAY},
212             {Locale.US, "ee", "01", DayOfWeek.SUNDAY},
213             {Locale.US, "c",  "1",  DayOfWeek.SUNDAY},
214         };
215     }
216 
217     @Test(dataProvider="print")
test_format(TemporalField field, TextStyle style, int value, String expected)218     public void test_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
219         getFormatter(field, style).formatTo(new MockFieldValue(field, value), buf);
220         assertEquals(buf.toString(), expected);
221     }
222 
223     @Test(dataProvider="print_DayOfWeekData")
test_formatDayOfWeek(Locale locale, String pattern, String expected, DayOfWeek dayOfWeek)224     public void test_formatDayOfWeek(Locale locale, String pattern, String expected, DayOfWeek dayOfWeek) {
225         DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale);
226         String text = formatter.format(dayOfWeek);
227         assertEquals(text, expected);
228     }
229 
230     //-----------------------------------------------------------------------
test_toString1()231     public void test_toString1() throws Exception {
232         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).toString(), "Text(MonthOfYear)");
233     }
234 
test_toString2()235     public void test_toString2() throws Exception {
236         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).toString(), "Text(MonthOfYear,SHORT)");
237     }
238 
239 }
240