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 java.time.format.*;
  63 
  64 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  65 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  66 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  67 import static org.testng.Assert.assertEquals;
  68 
  69 import java.util.HashMap;
  70 import java.util.Locale;
  71 import java.util.Map;
  72 
  73 import java.time.LocalDateTime;
  74 import java.time.Month;
  75 import java.time.temporal.TemporalField;
  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(groups={"tck"})
  90     public void setUp() {
  91         builder = new DateTimeFormatterBuilder();
  92     }
  93 
  94     //-----------------------------------------------------------------------
  95     @DataProvider(name="printText")
  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", groups={"tck"})
 139     public void test_appendText2arg_print(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.print(dt);
 144         assertEquals(text, expected);
 145     }
 146 
 147     @Test(dataProvider="printText", groups={"tck"})
 148     public void test_appendText1arg_print(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.print(dt);
 154             assertEquals(text, expected);
 155         }
 156     }
 157 
 158     //-----------------------------------------------------------------------
 159     @Test(groups={"tck"})
 160     public void test_print_appendText2arg_french_long() throws Exception {
 161         DateTimeFormatter f = builder.appendText(MONTH_OF_YEAR, TextStyle.FULL).toFormatter(Locale.FRENCH);
 162         LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
 163         String text = f.print(dt);
 164         assertEquals(text, "janvier");
 165     }
 166 
 167     @Test(groups={"tck"})
 168     public void test_print_appendText2arg_french_short() throws Exception {
 169         DateTimeFormatter f = builder.appendText(MONTH_OF_YEAR, TextStyle.SHORT).toFormatter(Locale.FRENCH);
 170         LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
 171         String text = f.print(dt);
 172         assertEquals(text, "janv.");
 173     }
 174 
 175     //-----------------------------------------------------------------------
 176     @Test(groups={"tck"})
 177     public void test_appendTextMap() throws Exception {
 178         Map<Long, String> map = new HashMap<Long, String>();
 179         map.put(1L, "JNY");
 180         map.put(2L, "FBY");
 181         map.put(3L, "MCH");
 182         map.put(4L, "APL");
 183         map.put(5L, "MAY");
 184         map.put(6L, "JUN");
 185         map.put(7L, "JLY");
 186         map.put(8L, "AGT");
 187         map.put(9L, "SPT");
 188         map.put(10L, "OBR");
 189         map.put(11L, "NVR");
 190         map.put(12L, "DBR");
 191         builder.appendText(MONTH_OF_YEAR, map);
 192         DateTimeFormatter f = builder.toFormatter();
 193         LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
 194         for (Month month : Month.values()) {
 195             assertEquals(f.print(dt.with(month)), map.get((long) month.getValue()));
 196         }
 197     }
 198 
 199     @Test(groups={"tck"})
 200     public void test_appendTextMap_DOM() throws Exception {
 201         Map<Long, String> map = new HashMap<Long, String>();
 202         map.put(1L, "1st");
 203         map.put(2L, "2nd");
 204         map.put(3L, "3rd");
 205         builder.appendText(DAY_OF_MONTH, map);
 206         DateTimeFormatter f = builder.toFormatter();
 207         LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
 208         assertEquals(f.print(dt.withDayOfMonth(1)), "1st");
 209         assertEquals(f.print(dt.withDayOfMonth(2)), "2nd");
 210         assertEquals(f.print(dt.withDayOfMonth(3)), "3rd");
 211     }
 212 
 213     @Test(groups={"tck"})
 214     public void test_appendTextMapIncomplete() throws Exception {
 215         Map<Long, String> map = new HashMap<Long, String>();
 216         map.put(1L, "JNY");
 217         builder.appendText(MONTH_OF_YEAR, map);
 218         DateTimeFormatter f = builder.toFormatter();
 219         LocalDateTime dt = LocalDateTime.of(2010, 2, 1, 0, 0);
 220         assertEquals(f.print(dt), "2");
 221     }
 222 
 223 }