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.format;
61 
62 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
63 import static java.time.temporal.ChronoField.HOUR_OF_DAY;
64 import static org.testng.Assert.assertEquals;
65 import static org.testng.Assert.fail;
66 
67 import java.time.DateTimeException;
68 import java.time.LocalDate;
69 import java.time.format.SignStyle;
70 
71 import org.testng.annotations.DataProvider;
72 import org.testng.annotations.Test;
73 import test.java.time.temporal.MockFieldValue;
74 
75 /**
76  * Test SimpleNumberPrinterParser.
77  */
78 @Test
79 public class TestNumberPrinter extends AbstractTestPrinterParser {
80 
81     //-----------------------------------------------------------------------
82     @Test(expectedExceptions=DateTimeException.class)
test_print_emptyCalendrical()83     public void test_print_emptyCalendrical() throws Exception {
84         getFormatter(DAY_OF_MONTH, 1, 2, SignStyle.NEVER).formatTo(EMPTY_DTA, buf);
85     }
86 
test_print_append()87     public void test_print_append() throws Exception {
88         buf.append("EXISTING");
89         getFormatter(DAY_OF_MONTH, 1, 2, SignStyle.NEVER).formatTo(LocalDate.of(2012, 1, 3), buf);
90         assertEquals(buf.toString(), "EXISTING3");
91     }
92 
93     //-----------------------------------------------------------------------
94     @DataProvider(name="Pad")
provider_pad()95     Object[][] provider_pad() {
96         return new Object[][] {
97             {1, 1, -10, null},
98             {1, 1, -9, "9"},
99             {1, 1, -1, "1"},
100             {1, 1, 0, "0"},
101             {1, 1, 3, "3"},
102             {1, 1, 9, "9"},
103             {1, 1, 10, null},
104 
105             {1, 2, -100, null},
106             {1, 2, -99, "99"},
107             {1, 2, -10, "10"},
108             {1, 2, -9, "9"},
109             {1, 2, -1, "1"},
110             {1, 2, 0, "0"},
111             {1, 2, 3, "3"},
112             {1, 2, 9, "9"},
113             {1, 2, 10, "10"},
114             {1, 2, 99, "99"},
115             {1, 2, 100, null},
116 
117             {2, 2, -100, null},
118             {2, 2, -99, "99"},
119             {2, 2, -10, "10"},
120             {2, 2, -9, "09"},
121             {2, 2, -1, "01"},
122             {2, 2, 0, "00"},
123             {2, 2, 3, "03"},
124             {2, 2, 9, "09"},
125             {2, 2, 10, "10"},
126             {2, 2, 99, "99"},
127             {2, 2, 100, null},
128 
129             {1, 3, -1000, null},
130             {1, 3, -999, "999"},
131             {1, 3, -100, "100"},
132             {1, 3, -99, "99"},
133             {1, 3, -10, "10"},
134             {1, 3, -9, "9"},
135             {1, 3, -1, "1"},
136             {1, 3, 0, "0"},
137             {1, 3, 3, "3"},
138             {1, 3, 9, "9"},
139             {1, 3, 10, "10"},
140             {1, 3, 99, "99"},
141             {1, 3, 100, "100"},
142             {1, 3, 999, "999"},
143             {1, 3, 1000, null},
144 
145             {2, 3, -1000, null},
146             {2, 3, -999, "999"},
147             {2, 3, -100, "100"},
148             {2, 3, -99, "99"},
149             {2, 3, -10, "10"},
150             {2, 3, -9, "09"},
151             {2, 3, -1, "01"},
152             {2, 3, 0, "00"},
153             {2, 3, 3, "03"},
154             {2, 3, 9, "09"},
155             {2, 3, 10, "10"},
156             {2, 3, 99, "99"},
157             {2, 3, 100, "100"},
158             {2, 3, 999, "999"},
159             {2, 3, 1000, null},
160 
161             {3, 3, -1000, null},
162             {3, 3, -999, "999"},
163             {3, 3, -100, "100"},
164             {3, 3, -99, "099"},
165             {3, 3, -10, "010"},
166             {3, 3, -9, "009"},
167             {3, 3, -1, "001"},
168             {3, 3, 0, "000"},
169             {3, 3, 3, "003"},
170             {3, 3, 9, "009"},
171             {3, 3, 10, "010"},
172             {3, 3, 99, "099"},
173             {3, 3, 100, "100"},
174             {3, 3, 999, "999"},
175             {3, 3, 1000, null},
176 
177             {1, 10, Integer.MAX_VALUE - 1, "2147483646"},
178             {1, 10, Integer.MAX_VALUE, "2147483647"},
179             {1, 10, Integer.MIN_VALUE + 1, "2147483647"},
180             {1, 10, Integer.MIN_VALUE, "2147483648"},
181        };
182     }
183 
184     @Test(dataProvider="Pad")
test_pad_NOT_NEGATIVE(int minPad, int maxPad, long value, String result)185     public void test_pad_NOT_NEGATIVE(int minPad, int maxPad, long value, String result) throws Exception {
186         try {
187             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.NOT_NEGATIVE).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
188             if (result == null || value < 0) {
189                 fail("Expected exception");
190             }
191             assertEquals(buf.toString(), result);
192         } catch (DateTimeException ex) {
193             if (result == null || value < 0) {
194                 assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
195             } else {
196                 throw ex;
197             }
198         }
199     }
200 
201     @Test(dataProvider="Pad")
test_pad_NEVER(int minPad, int maxPad, long value, String result)202     public void test_pad_NEVER(int minPad, int maxPad, long value, String result) throws Exception {
203         try {
204             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.NEVER).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
205             if (result == null) {
206                 fail("Expected exception");
207             }
208             assertEquals(buf.toString(), result);
209         } catch (DateTimeException ex) {
210             if (result != null) {
211                 throw ex;
212             }
213             assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
214         }
215     }
216 
217     @Test(dataProvider="Pad")
test_pad_NORMAL(int minPad, int maxPad, long value, String result)218     public void test_pad_NORMAL(int minPad, int maxPad, long value, String result) throws Exception {
219         try {
220             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.NORMAL).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
221             if (result == null) {
222                 fail("Expected exception");
223             }
224             assertEquals(buf.toString(), (value < 0 ? "-" + result : result));
225         } catch (DateTimeException ex) {
226             if (result != null) {
227                 throw ex;
228             }
229             assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
230         }
231     }
232 
233     @Test(dataProvider="Pad")
234     public void test_pad_ALWAYS(int minPad, int maxPad, long value, String result) throws Exception {
235         try {
236             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.ALWAYS).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
237             if (result == null) {
238                 fail("Expected exception");
239             }
240             assertEquals(buf.toString(), (value < 0 ? "-" + result : "+" + result));
241         } catch (DateTimeException ex) {
242             if (result != null) {
243                 throw ex;
244             }
245             assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
246         }
247     }
248 
249     @Test(dataProvider="Pad")
250     public void test_pad_EXCEEDS_PAD(int minPad, int maxPad, long value, String result) throws Exception {
251         try {
252             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.EXCEEDS_PAD).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
253             if (result == null) {
254                 fail("Expected exception");
255                 return;  // unreachable
256             }
257             if (result.length() > minPad || value < 0) {
258                 result = (value < 0 ? "-" + result : "+" + result);
259             }
260             assertEquals(buf.toString(), result);
261         } catch (DateTimeException ex) {
262             if (result != null) {
263                 throw ex;
264             }
265             assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
266         }
267     }
268 
269     //-----------------------------------------------------------------------
270     public void test_toString1() throws Exception {
271         assertEquals(getFormatter(HOUR_OF_DAY, 1, 19, SignStyle.NORMAL).toString(), "Value(HourOfDay)");
272     }
273 
274     public void test_toString2() throws Exception {
275         assertEquals(getFormatter(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE).toString(), "Value(HourOfDay,2)");
276     }
277 
278     public void test_toString3() throws Exception {
279         assertEquals(getFormatter(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE).toString(), "Value(HourOfDay,1,2,NOT_NEGATIVE)");
280     }
281 
282 }
283