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 org.testng.Assert.assertSame; 64 import static org.testng.Assert.assertTrue; 65 import static org.testng.Assert.fail; 66 67 import java.time.DateTimeException; 68 import java.time.DayOfWeek; 69 import java.time.Instant; 70 import java.time.LocalDate; 71 import java.time.LocalDateTime; 72 import java.time.LocalTime; 73 import java.time.Month; 74 import java.time.MonthDay; 75 import java.time.OffsetDateTime; 76 import java.time.OffsetTime; 77 import java.time.Year; 78 import java.time.YearMonth; 79 import java.time.ZoneId; 80 import java.time.ZoneOffset; 81 import java.time.ZonedDateTime; 82 import java.time.chrono.Chronology; 83 import java.time.chrono.ThaiBuddhistChronology; 84 import java.time.format.DateTimeFormatter; 85 import java.time.format.DateTimeFormatterBuilder; 86 import java.time.format.DecimalStyle; 87 import java.time.format.SignStyle; 88 import java.time.format.TextStyle; 89 import java.time.temporal.Temporal; 90 import java.time.temporal.TemporalAccessor; 91 import java.util.Locale; 92 import java.util.function.Function; 93 94 import org.testng.annotations.DataProvider; 95 import org.testng.annotations.Test; 96 97 /** 98 * Test DateTimeFormatter. 99 * @bug 8085887 100 */ 101 @Test 102 public class TestDateTimeFormatter { 103 104 @Test test_withLocale_same()105 public void test_withLocale_same() throws Exception { 106 DateTimeFormatter base = 107 new DateTimeFormatterBuilder().appendLiteral("ONE") 108 .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE) 109 .toFormatter(Locale.ENGLISH) 110 .withDecimalStyle(DecimalStyle.STANDARD); 111 DateTimeFormatter test = base.withLocale(Locale.ENGLISH); 112 assertSame(test, base); 113 } 114 115 @Test test_parse_errorMessage()116 public void test_parse_errorMessage() throws Exception { 117 assertGoodErrorDate(DayOfWeek::from, "DayOfWeek"); 118 assertGoodErrorDate(Month::from, "Month"); 119 assertGoodErrorDate(YearMonth::from, "YearMonth"); 120 assertGoodErrorDate(MonthDay::from, "MonthDay"); 121 assertGoodErrorDate(LocalDate::from, "LocalDate"); 122 assertGoodErrorDate(LocalTime::from, "LocalTime"); 123 assertGoodErrorDate(LocalDateTime::from, "LocalDateTime"); 124 assertGoodErrorDate(OffsetTime::from, "OffsetTime"); 125 assertGoodErrorDate(OffsetDateTime::from, "OffsetDateTime"); 126 assertGoodErrorDate(ZonedDateTime::from, "ZonedDateTime"); 127 assertGoodErrorDate(Instant::from, "Instant"); 128 assertGoodErrorDate(ZoneOffset::from, "ZoneOffset"); 129 assertGoodErrorDate(ZoneId::from, "ZoneId"); 130 assertGoodErrorDate(ThaiBuddhistChronology.INSTANCE::date, ""); 131 132 assertGoodErrorTime(DayOfWeek::from, "DayOfWeek"); 133 assertGoodErrorTime(Month::from, "Month"); 134 assertGoodErrorTime(Year::from, "Year"); 135 assertGoodErrorTime(YearMonth::from, "YearMonth"); 136 assertGoodErrorTime(MonthDay::from, "MonthDay"); 137 assertGoodErrorTime(LocalDate::from, "LocalDate"); 138 assertGoodErrorTime(LocalTime::from, "LocalTime"); 139 assertGoodErrorTime(LocalDateTime::from, "LocalDateTime"); 140 assertGoodErrorTime(OffsetTime::from, "OffsetTime"); 141 assertGoodErrorTime(OffsetDateTime::from, "OffsetDateTime"); 142 assertGoodErrorTime(ZonedDateTime::from, "ZonedDateTime"); 143 assertGoodErrorTime(Instant::from, "Instant"); 144 assertGoodErrorTime(ZoneOffset::from, "ZoneOffset"); 145 assertGoodErrorTime(ZoneId::from, "ZoneId"); 146 assertGoodErrorTime(ThaiBuddhistChronology.INSTANCE::date, ""); 147 } 148 assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText)149 private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) { 150 DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd"); 151 TemporalAccessor temporal = f.parse("2010-06-30"); 152 try { 153 function.apply(temporal); 154 fail("Should have failed"); 155 } catch (DateTimeException ex) { 156 String msg = ex.getMessage(); 157 assertTrue(msg.contains(expectedText), msg); 158 assertTrue(msg.contains("Year"), msg); 159 assertTrue(msg.contains("MinuteOfHour"), msg); 160 assertTrue(msg.contains("DayOfMonth"), msg); 161 } 162 } 163 assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText)164 private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) { 165 DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss"); 166 TemporalAccessor temporal = f.parse("11:30:56"); 167 try { 168 function.apply(temporal); 169 fail("Should have failed"); 170 } catch (DateTimeException ex) { 171 String msg = ex.getMessage(); 172 assertTrue(msg.contains(expectedText), msg); 173 assertTrue(msg.contains("HourOfDay"), msg); 174 assertTrue(msg.contains("MonthOfYear"), msg); 175 assertTrue(msg.contains("SecondOfMinute"), msg); 176 } 177 } 178 179 @Test test_parsed_toString_resolvedTime()180 public void test_parsed_toString_resolvedTime() { 181 DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:mm:ss"); 182 TemporalAccessor temporal = f.parse("11:30:56"); 183 String msg = temporal.toString(); 184 assertTrue(msg.contains("11:30:56"), msg); 185 } 186 187 @Test test_parsed_toString_resolvedDate()188 public void test_parsed_toString_resolvedDate() { 189 DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 190 TemporalAccessor temporal = f.parse("2010-06-30"); 191 String msg = temporal.toString(); 192 assertTrue(msg.contains("2010-06-30"), msg); 193 } 194 195 @Test test_parsed_toString_resolvedDateTime()196 public void test_parsed_toString_resolvedDateTime() { 197 DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 198 TemporalAccessor temporal = f.parse("2010-06-30 11:30:56"); 199 String msg = temporal.toString(); 200 assertTrue(msg.contains("2010-06-30"), msg); 201 assertTrue(msg.contains("11:30:56"), msg); 202 } 203 204 @DataProvider(name="nozone_exception_cases") exceptionCases()205 Object[][] exceptionCases() { 206 return new Object[][] { 207 {LocalDateTime.of(2000, 1, 1, 1, 1), "z", "ZoneId"}, 208 {OffsetDateTime.of(2000, 1, 1, 3, 3, 3, 0, ZoneOffset.ofTotalSeconds(60)), "z", "ZoneId"}, 209 }; 210 } 211 212 // Test cases that should throw an exception with a cogent message 213 // containing the Type being queried and the Temporal being queried. 214 @Test(dataProvider="nozone_exception_cases") test_throws_message(Temporal temporal, String pattern, String queryName)215 public void test_throws_message(Temporal temporal, String pattern, String queryName) { 216 DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern); 217 try { 218 fmt.format(temporal); 219 fail("Format using \"" + pattern + "\" with " + 220 temporal + " should have failed"); 221 } catch (DateTimeException dte) { 222 String msg = dte.getMessage(); 223 // Verify message contains the type that is missing and the temporal value 224 assertTrue(msg.contains(queryName), 225 String.format("\"%s\" missing from %s", queryName, msg)); 226 String s = temporal.toString(); 227 assertTrue(msg.contains(s), 228 String.format("\"%s\" missing from %s", s, msg)); 229 } 230 231 } 232 233 // Test cases that should throw an exception with a cogent message when missing the Chronology 234 @Test test_throws_message_chrono()235 public void test_throws_message_chrono() { 236 Chronology chrono = ThaiBuddhistChronology.INSTANCE; 237 DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendZoneId().toFormatter() 238 .withChronology(chrono); 239 LocalTime now = LocalTime.now(); 240 try { 241 fmt.format(now); 242 fail("Format using appendZoneId() should have failed"); 243 } catch (DateTimeException dte) { 244 String msg = dte.getMessage(); 245 // Verify message contains the type that is missing and the temporal value 246 assertTrue(msg.contains("ZoneId"), 247 String.format("\"%s\" missing from %s", "ZoneId", msg)); 248 assertTrue(msg.contains(chrono.toString()), 249 String.format("\"%s\" missing from %s", chrono.toString(), msg)); 250 } 251 252 } 253 254 // Test cases that should throw an exception with a cogent message when missing the ZoneId 255 @Test test_throws_message_zone()256 public void test_throws_message_zone() { 257 ZoneId zone = ZoneId.of("Pacific/Honolulu"); 258 DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendChronologyId().toFormatter() 259 .withZone(zone); 260 LocalTime now = LocalTime.now(); 261 try { 262 fmt.format(now); 263 fail("Format using appendChronologyId() should have failed"); 264 } catch (DateTimeException dte) { 265 String msg = dte.getMessage(); 266 // Verify message contains the type that is missing and the temporal value 267 assertTrue(msg.contains("Chronology"), 268 String.format("\"%s\" missing from %s", "Chronology", msg)); 269 assertTrue(msg.contains(zone.toString()), 270 String.format("\"%s\" missing from %s", zone.toString(), msg)); 271 } 272 273 } 274 275 } 276