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) 2009-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 tck.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.MONTH_OF_YEAR; 65 import static org.testng.Assert.assertEquals; 66 67 import java.time.LocalDateTime; 68 import java.time.Month; 69 import java.time.format.DateTimeFormatter; 70 import java.time.format.DateTimeFormatterBuilder; 71 import java.time.format.TextStyle; 72 import java.time.temporal.TemporalField; 73 import java.util.HashMap; 74 import java.util.Locale; 75 import java.util.Map; 76 77 import org.testng.annotations.BeforeMethod; 78 import org.testng.annotations.DataProvider; 79 import org.testng.annotations.Test; 80 81 /** 82 * Test text printing. 83 */ 84 @Test 85 public class TCKDateTimeTextPrinting { 86 87 private DateTimeFormatterBuilder builder; 88 89 @BeforeMethod setUp()90 public void setUp() { 91 builder = new DateTimeFormatterBuilder(); 92 } 93 94 //----------------------------------------------------------------------- 95 @DataProvider(name="printText") data_text()96 Object[][] data_text() { 97 return new Object[][] { 98 {DAY_OF_WEEK, TextStyle.FULL, 1, "Monday"}, 99 {DAY_OF_WEEK, TextStyle.FULL, 2, "Tuesday"}, 100 {DAY_OF_WEEK, TextStyle.FULL, 3, "Wednesday"}, 101 {DAY_OF_WEEK, TextStyle.FULL, 4, "Thursday"}, 102 {DAY_OF_WEEK, TextStyle.FULL, 5, "Friday"}, 103 {DAY_OF_WEEK, TextStyle.FULL, 6, "Saturday"}, 104 {DAY_OF_WEEK, TextStyle.FULL, 7, "Sunday"}, 105 106 {DAY_OF_WEEK, TextStyle.SHORT, 1, "Mon"}, 107 {DAY_OF_WEEK, TextStyle.SHORT, 2, "Tue"}, 108 {DAY_OF_WEEK, TextStyle.SHORT, 3, "Wed"}, 109 {DAY_OF_WEEK, TextStyle.SHORT, 4, "Thu"}, 110 {DAY_OF_WEEK, TextStyle.SHORT, 5, "Fri"}, 111 {DAY_OF_WEEK, TextStyle.SHORT, 6, "Sat"}, 112 {DAY_OF_WEEK, TextStyle.SHORT, 7, "Sun"}, 113 114 {DAY_OF_MONTH, TextStyle.FULL, 1, "1"}, 115 {DAY_OF_MONTH, TextStyle.FULL, 2, "2"}, 116 {DAY_OF_MONTH, TextStyle.FULL, 3, "3"}, 117 {DAY_OF_MONTH, TextStyle.FULL, 28, "28"}, 118 {DAY_OF_MONTH, TextStyle.FULL, 29, "29"}, 119 {DAY_OF_MONTH, TextStyle.FULL, 30, "30"}, 120 {DAY_OF_MONTH, TextStyle.FULL, 31, "31"}, 121 122 {DAY_OF_MONTH, TextStyle.SHORT, 1, "1"}, 123 {DAY_OF_MONTH, TextStyle.SHORT, 2, "2"}, 124 {DAY_OF_MONTH, TextStyle.SHORT, 3, "3"}, 125 {DAY_OF_MONTH, TextStyle.SHORT, 28, "28"}, 126 {DAY_OF_MONTH, TextStyle.SHORT, 29, "29"}, 127 {DAY_OF_MONTH, TextStyle.SHORT, 30, "30"}, 128 {DAY_OF_MONTH, TextStyle.SHORT, 31, "31"}, 129 130 {MONTH_OF_YEAR, TextStyle.FULL, 1, "January"}, 131 {MONTH_OF_YEAR, TextStyle.FULL, 12, "December"}, 132 133 {MONTH_OF_YEAR, TextStyle.SHORT, 1, "Jan"}, 134 {MONTH_OF_YEAR, TextStyle.SHORT, 12, "Dec"}, 135 }; 136 } 137 138 @Test(dataProvider="printText") test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected)139 public void test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception { 140 DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH); 141 LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0); 142 dt = dt.with(field, value); 143 String text = f.format(dt); 144 assertEquals(text, expected); 145 } 146 147 @Test(dataProvider="printText") test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected)148 public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception { 149 if (style == TextStyle.FULL) { 150 DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH); 151 LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0); 152 dt = dt.with(field, value); 153 String text = f.format(dt); 154 assertEquals(text, expected); 155 } 156 } 157 158 //----------------------------------------------------------------------- 159 @Test test_appendTextMap()160 public void test_appendTextMap() throws Exception { 161 Map<Long, String> map = new HashMap<Long, String>(); 162 map.put(1L, "JNY"); 163 map.put(2L, "FBY"); 164 map.put(3L, "MCH"); 165 map.put(4L, "APL"); 166 map.put(5L, "MAY"); 167 map.put(6L, "JUN"); 168 map.put(7L, "JLY"); 169 map.put(8L, "AGT"); 170 map.put(9L, "SPT"); 171 map.put(10L, "OBR"); 172 map.put(11L, "NVR"); 173 map.put(12L, "DBR"); 174 builder.appendText(MONTH_OF_YEAR, map); 175 DateTimeFormatter f = builder.toFormatter(); 176 LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0); 177 for (Month month : Month.values()) { 178 assertEquals(f.format(dt.with(month)), map.get((long) month.getValue())); 179 } 180 } 181 182 @Test test_appendTextMap_DOM()183 public void test_appendTextMap_DOM() throws Exception { 184 Map<Long, String> map = new HashMap<Long, String>(); 185 map.put(1L, "1st"); 186 map.put(2L, "2nd"); 187 map.put(3L, "3rd"); 188 builder.appendText(DAY_OF_MONTH, map); 189 DateTimeFormatter f = builder.toFormatter(); 190 LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0); 191 assertEquals(f.format(dt.withDayOfMonth(1)), "1st"); 192 assertEquals(f.format(dt.withDayOfMonth(2)), "2nd"); 193 assertEquals(f.format(dt.withDayOfMonth(3)), "3rd"); 194 } 195 196 @Test test_appendTextMapIncomplete()197 public void test_appendTextMapIncomplete() throws Exception { 198 Map<Long, String> map = new HashMap<Long, String>(); 199 map.put(1L, "JNY"); 200 builder.appendText(MONTH_OF_YEAR, map); 201 DateTimeFormatter f = builder.toFormatter(); 202 LocalDateTime dt = LocalDateTime.of(2010, 2, 1, 0, 0); 203 assertEquals(f.format(dt), "2"); 204 } 205 206 } 207