test/java/time/test/java/time/format/TestTextPrinter.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 test.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 java.time.temporal.ChronoField.ERA;


  68 import static org.testng.Assert.assertEquals;
  69 
  70 import java.util.Locale;
  71 
  72 import java.time.DateTimeException;

  73 import java.time.LocalDate;
  74 import java.time.temporal.TemporalField;
  75 import java.time.chrono.JapaneseChronology;
  76 import test.java.time.temporal.MockFieldValue;



  77 
  78 import org.testng.annotations.DataProvider;
  79 import org.testng.annotations.Test;

  80 
  81 /**
  82  * Test TextPrinterParser.
  83  */
  84 @Test(groups={"implementation"})
  85 public class TestTextPrinter extends AbstractTestPrinterParser {


  86 
  87     //-----------------------------------------------------------------------
  88     @Test(expectedExceptions=DateTimeException.class)
  89     public void test_print_emptyCalendrical() throws Exception {
  90         getFormatter(DAY_OF_WEEK, TextStyle.FULL).formatTo(EMPTY_DTA, buf);
  91     }
  92 
  93     public void test_print_append() throws Exception {
  94         buf.append("EXISTING");
  95         getFormatter(DAY_OF_WEEK, TextStyle.FULL).formatTo(LocalDate.of(2012, 4, 18), buf);
  96         assertEquals(buf.toString(), "EXISTINGWednesday");
  97     }
  98 
  99     //-----------------------------------------------------------------------
 100     @DataProvider(name="print")
 101     Object[][] provider_dow() {
 102         return new Object[][] {
 103             {DAY_OF_WEEK, TextStyle.FULL, 1, "Monday"},
 104             {DAY_OF_WEEK, TextStyle.FULL, 2, "Tuesday"},
 105             {DAY_OF_WEEK, TextStyle.FULL, 3, "Wednesday"},


 149             {MONTH_OF_YEAR, TextStyle.FULL, 7, "July"},
 150             {MONTH_OF_YEAR, TextStyle.FULL, 8, "August"},
 151             {MONTH_OF_YEAR, TextStyle.FULL, 9, "September"},
 152             {MONTH_OF_YEAR, TextStyle.FULL, 10, "October"},
 153             {MONTH_OF_YEAR, TextStyle.FULL, 11, "November"},
 154             {MONTH_OF_YEAR, TextStyle.FULL, 12, "December"},
 155 
 156             {MONTH_OF_YEAR, TextStyle.SHORT, 1, "Jan"},
 157             {MONTH_OF_YEAR, TextStyle.SHORT, 2, "Feb"},
 158             {MONTH_OF_YEAR, TextStyle.SHORT, 3, "Mar"},
 159             {MONTH_OF_YEAR, TextStyle.SHORT, 4, "Apr"},
 160             {MONTH_OF_YEAR, TextStyle.SHORT, 5, "May"},
 161             {MONTH_OF_YEAR, TextStyle.SHORT, 6, "Jun"},
 162             {MONTH_OF_YEAR, TextStyle.SHORT, 7, "Jul"},
 163             {MONTH_OF_YEAR, TextStyle.SHORT, 8, "Aug"},
 164             {MONTH_OF_YEAR, TextStyle.SHORT, 9, "Sep"},
 165             {MONTH_OF_YEAR, TextStyle.SHORT, 10, "Oct"},
 166             {MONTH_OF_YEAR, TextStyle.SHORT, 11, "Nov"},
 167             {MONTH_OF_YEAR, TextStyle.SHORT, 12, "Dec"},
 168 













 169             {ERA,           TextStyle.FULL, 0, "Before Christ"},
 170             {ERA,           TextStyle.FULL, 1, "Anno Domini"},
 171             {ERA,           TextStyle.SHORT, 0, "BC"},
 172             {ERA,           TextStyle.SHORT, 1, "AD"},
 173             {ERA,           TextStyle.NARROW, 0, "B"},
 174             {ERA,           TextStyle.NARROW, 1, "A"},





























 175        };
 176     }
 177 
 178     @DataProvider(name="print_JapaneseChronology")
 179     Object[][] provider_japaneseEra() {
 180        return new Object[][] {
 181             {ERA,           TextStyle.FULL, 2, "Heisei"}, // Note: CLDR doesn't define "wide" Japanese era names.
 182             {ERA,           TextStyle.SHORT, 2, "Heisei"},
 183             {ERA,           TextStyle.NARROW, 2, "H"},
 184        };
 185     };
 186 













 187     @Test(dataProvider="print")
 188     public void test_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
 189         getFormatter(field, style).formatTo(new MockFieldValue(field, value), buf);
 190         assertEquals(buf.toString(), expected);
 191     }
 192 







 193     @Test(dataProvider="print_JapaneseChronology")
 194     public void test_formatJapaneseEra(TemporalField field, TextStyle style, int value, String expected) throws Exception {
 195         LocalDate ld = LocalDate.of(2013, 1, 31);
 196         getFormatter(field, style).withChronology(JapaneseChronology.INSTANCE).formatTo(ld, buf);
 197         assertEquals(buf.toString(), expected);
 198     }
 199 






 200     //-----------------------------------------------------------------------
 201     public void test_print_french_long() throws Exception {
 202         getFormatter(MONTH_OF_YEAR, TextStyle.FULL).withLocale(Locale.FRENCH).formatTo(LocalDate.of(2012, 1, 1), buf);
 203         assertEquals(buf.toString(), "janvier");
 204     }
 205 
 206     public void test_print_french_short() throws Exception {
 207         getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH).formatTo(LocalDate.of(2012, 1, 1), buf);
 208         assertEquals(buf.toString(), "janv.");
 209     }
 210 
 211     //-----------------------------------------------------------------------
 212     public void test_toString1() throws Exception {
 213         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).toString(), "Text(MonthOfYear)");
 214     }
 215 
 216     public void test_toString2() throws Exception {
 217         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).toString(), "Text(MonthOfYear,SHORT)");
 218     }
 219 


  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.DAY_OF_WEEK;

  64 import static java.time.temporal.ChronoField.ERA;
  65 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  66 import static java.time.temporal.IsoFields.QUARTER_OF_YEAR;
  67 import static org.testng.Assert.assertEquals;
  68 


  69 import java.time.DateTimeException;
  70 import java.time.DayOfWeek;
  71 import java.time.LocalDate;

  72 import java.time.chrono.JapaneseChronology;
  73 import java.time.format.DateTimeFormatter;
  74 import java.time.format.TextStyle;
  75 import java.time.temporal.TemporalField;
  76 import java.util.Locale;
  77 
  78 import org.testng.annotations.DataProvider;
  79 import org.testng.annotations.Test;
  80 import test.java.time.temporal.MockFieldValue;
  81 
  82 /**
  83  * Test TextPrinterParser.
  84  */
  85 @Test
  86 public class TestTextPrinter extends AbstractTestPrinterParser {
  87     static final Locale RUSSIAN = new Locale("ru");
  88     static final Locale FINNISH = new Locale("fi");
  89 
  90     //-----------------------------------------------------------------------
  91     @Test(expectedExceptions=DateTimeException.class)
  92     public void test_print_emptyCalendrical() throws Exception {
  93         getFormatter(DAY_OF_WEEK, TextStyle.FULL).formatTo(EMPTY_DTA, buf);
  94     }
  95 
  96     public void test_print_append() throws Exception {
  97         buf.append("EXISTING");
  98         getFormatter(DAY_OF_WEEK, TextStyle.FULL).formatTo(LocalDate.of(2012, 4, 18), buf);
  99         assertEquals(buf.toString(), "EXISTINGWednesday");
 100     }
 101 
 102     //-----------------------------------------------------------------------
 103     @DataProvider(name="print")
 104     Object[][] provider_dow() {
 105         return new Object[][] {
 106             {DAY_OF_WEEK, TextStyle.FULL, 1, "Monday"},
 107             {DAY_OF_WEEK, TextStyle.FULL, 2, "Tuesday"},
 108             {DAY_OF_WEEK, TextStyle.FULL, 3, "Wednesday"},


 152             {MONTH_OF_YEAR, TextStyle.FULL, 7, "July"},
 153             {MONTH_OF_YEAR, TextStyle.FULL, 8, "August"},
 154             {MONTH_OF_YEAR, TextStyle.FULL, 9, "September"},
 155             {MONTH_OF_YEAR, TextStyle.FULL, 10, "October"},
 156             {MONTH_OF_YEAR, TextStyle.FULL, 11, "November"},
 157             {MONTH_OF_YEAR, TextStyle.FULL, 12, "December"},
 158 
 159             {MONTH_OF_YEAR, TextStyle.SHORT, 1, "Jan"},
 160             {MONTH_OF_YEAR, TextStyle.SHORT, 2, "Feb"},
 161             {MONTH_OF_YEAR, TextStyle.SHORT, 3, "Mar"},
 162             {MONTH_OF_YEAR, TextStyle.SHORT, 4, "Apr"},
 163             {MONTH_OF_YEAR, TextStyle.SHORT, 5, "May"},
 164             {MONTH_OF_YEAR, TextStyle.SHORT, 6, "Jun"},
 165             {MONTH_OF_YEAR, TextStyle.SHORT, 7, "Jul"},
 166             {MONTH_OF_YEAR, TextStyle.SHORT, 8, "Aug"},
 167             {MONTH_OF_YEAR, TextStyle.SHORT, 9, "Sep"},
 168             {MONTH_OF_YEAR, TextStyle.SHORT, 10, "Oct"},
 169             {MONTH_OF_YEAR, TextStyle.SHORT, 11, "Nov"},
 170             {MONTH_OF_YEAR, TextStyle.SHORT, 12, "Dec"},
 171 
 172             {MONTH_OF_YEAR, TextStyle.NARROW, 1, "J"},
 173             {MONTH_OF_YEAR, TextStyle.NARROW, 2, "F"},
 174             {MONTH_OF_YEAR, TextStyle.NARROW, 3, "M"},
 175             {MONTH_OF_YEAR, TextStyle.NARROW, 4, "A"},
 176             {MONTH_OF_YEAR, TextStyle.NARROW, 5, "M"},
 177             {MONTH_OF_YEAR, TextStyle.NARROW, 6, "J"},
 178             {MONTH_OF_YEAR, TextStyle.NARROW, 7, "J"},
 179             {MONTH_OF_YEAR, TextStyle.NARROW, 8, "A"},
 180             {MONTH_OF_YEAR, TextStyle.NARROW, 9, "S"},
 181             {MONTH_OF_YEAR, TextStyle.NARROW, 10, "O"},
 182             {MONTH_OF_YEAR, TextStyle.NARROW, 11, "N"},
 183             {MONTH_OF_YEAR, TextStyle.NARROW, 12, "D"},
 184 
 185             {ERA,           TextStyle.FULL, 0, "Before Christ"},
 186             {ERA,           TextStyle.FULL, 1, "Anno Domini"},
 187             {ERA,           TextStyle.SHORT, 0, "BC"},
 188             {ERA,           TextStyle.SHORT, 1, "AD"},
 189             {ERA,           TextStyle.NARROW, 0, "B"},
 190             {ERA,           TextStyle.NARROW, 1, "A"},
 191 
 192             {QUARTER_OF_YEAR, TextStyle.FULL, 1, "1st quarter"},
 193             {QUARTER_OF_YEAR, TextStyle.FULL, 2, "2nd quarter"},
 194             {QUARTER_OF_YEAR, TextStyle.FULL, 3, "3rd quarter"},
 195             {QUARTER_OF_YEAR, TextStyle.FULL, 4, "4th quarter"},
 196 
 197             {QUARTER_OF_YEAR, TextStyle.SHORT, 1, "Q1"},
 198             {QUARTER_OF_YEAR, TextStyle.SHORT, 2, "Q2"},
 199             {QUARTER_OF_YEAR, TextStyle.SHORT, 3, "Q3"},
 200             {QUARTER_OF_YEAR, TextStyle.SHORT, 4, "Q4"},
 201 
 202             {QUARTER_OF_YEAR, TextStyle.NARROW, 1, "1"},
 203             {QUARTER_OF_YEAR, TextStyle.NARROW, 2, "2"},
 204             {QUARTER_OF_YEAR, TextStyle.NARROW, 3, "3"},
 205             {QUARTER_OF_YEAR, TextStyle.NARROW, 4, "4"},
 206        };
 207     }
 208 
 209     @DataProvider(name="print_DayOfWeekData")
 210     Object[][] providerDayOfWeekData() {
 211         return new Object[][] {
 212             // Locale, pattern, expected text, input DayOfWeek
 213             {Locale.US, "e",  "1",  DayOfWeek.SUNDAY},
 214             {Locale.US, "ee", "01", DayOfWeek.SUNDAY},
 215             {Locale.US, "c",  "1",  DayOfWeek.SUNDAY},
 216 
 217             {Locale.UK, "e",  "1",  DayOfWeek.MONDAY},
 218             {Locale.UK, "ee", "01", DayOfWeek.MONDAY},
 219             {Locale.UK, "c",  "1",  DayOfWeek.MONDAY},
 220         };
 221     }
 222 
 223     @DataProvider(name="print_JapaneseChronology")
 224     Object[][] provider_japaneseEra() {
 225        return new Object[][] {
 226             {ERA,           TextStyle.FULL, 2, "Heisei"}, // Note: CLDR doesn't define "wide" Japanese era names.
 227             {ERA,           TextStyle.SHORT, 2, "Heisei"},
 228             {ERA,           TextStyle.NARROW, 2, "H"},
 229        };
 230     };
 231 
 232     // Test data is dependent on localized resources.
 233     @DataProvider(name="print_standalone")
 234     Object[][] provider_StandaloneNames() {
 235         return new Object[][] {
 236             // standalone names for 2013-01-01 (Tue)
 237             // Locale, TemporalField, TextStyle, expected text
 238             {RUSSIAN, MONTH_OF_YEAR, TextStyle.FULL_STANDALONE,  "\u042f\u043d\u0432\u0430\u0440\u044c"},
 239             {RUSSIAN, MONTH_OF_YEAR, TextStyle.SHORT_STANDALONE, "\u042f\u043d\u0432."},
 240             {FINNISH, DAY_OF_WEEK,   TextStyle.FULL_STANDALONE,  "tiistai"},
 241             {FINNISH, DAY_OF_WEEK,   TextStyle.SHORT_STANDALONE, "ti"},
 242         };
 243     }
 244 
 245     @Test(dataProvider="print")
 246     public void test_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
 247         getFormatter(field, style).formatTo(new MockFieldValue(field, value), buf);
 248         assertEquals(buf.toString(), expected);
 249     }
 250 
 251     @Test(dataProvider="print_DayOfWeekData")
 252     public void test_formatDayOfWeek(Locale locale, String pattern, String expected, DayOfWeek dayOfWeek) {
 253         DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale);
 254         String text = formatter.format(dayOfWeek);
 255         assertEquals(text, expected);
 256     }
 257 
 258     @Test(dataProvider="print_JapaneseChronology")
 259     public void test_formatJapaneseEra(TemporalField field, TextStyle style, int value, String expected) throws Exception {
 260         LocalDate ld = LocalDate.of(2013, 1, 31);
 261         getFormatter(field, style).withChronology(JapaneseChronology.INSTANCE).formatTo(ld, buf);
 262         assertEquals(buf.toString(), expected);
 263     }
 264 
 265     @Test(dataProvider="print_standalone")
 266     public void test_standaloneNames(Locale locale, TemporalField field, TextStyle style, String expected) {
 267         getFormatter(field, style).withLocale(locale).formatTo(LocalDate.of(2013, 1, 1), buf);
 268         assertEquals(buf.toString(), expected);
 269     }
 270 
 271     //-----------------------------------------------------------------------
 272     public void test_print_french_long() throws Exception {
 273         getFormatter(MONTH_OF_YEAR, TextStyle.FULL).withLocale(Locale.FRENCH).formatTo(LocalDate.of(2012, 1, 1), buf);
 274         assertEquals(buf.toString(), "janvier");
 275     }
 276 
 277     public void test_print_french_short() throws Exception {
 278         getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH).formatTo(LocalDate.of(2012, 1, 1), buf);
 279         assertEquals(buf.toString(), "janv.");
 280     }
 281 
 282     //-----------------------------------------------------------------------
 283     public void test_toString1() throws Exception {
 284         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).toString(), "Text(MonthOfYear)");
 285     }
 286 
 287     public void test_toString2() throws Exception {
 288         assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).toString(), "Text(MonthOfYear,SHORT)");
 289     }
 290