test/java/time/tck/java/time/format/TCKDateTimeTextPrinting.java

Print this page




  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"},


 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_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", groups={"tck"})
 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(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.format(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.format(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.format(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.format(dt.withDayOfMonth(1)), "1st");
 209         assertEquals(f.format(dt.withDayOfMonth(2)), "2nd");
 210         assertEquals(f.format(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.format(dt), "2");
 221     }
 222 
 223 }


  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
  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"},


 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")
 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")
 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
 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.format(dt);
 164         assertEquals(text, "janvier");
 165     }
 166 
 167     @Test
 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.format(dt);
 172         assertEquals(text, "janv.");
 173     }
 174 
 175     //-----------------------------------------------------------------------
 176     @Test
 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.format(dt.with(month)), map.get((long) month.getValue()));
 196         }
 197     }
 198 
 199     @Test
 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.format(dt.withDayOfMonth(1)), "1st");
 209         assertEquals(f.format(dt.withDayOfMonth(2)), "2nd");
 210         assertEquals(f.format(dt.withDayOfMonth(3)), "3rd");
 211     }
 212 
 213     @Test
 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.format(dt), "2");
 221     }
 222 
 223 }