< prev index next >

test/java/time/tck/java/time/format/TCKLocalizedFieldParser.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 64,92 **** import java.text.ParsePosition; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; import java.time.temporal.WeekFields; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import test.java.time.format.AbstractTestPrinterParser; /** * Test TCKLocalizedFieldParser. */ @Test public class TCKLocalizedFieldParser extends AbstractTestPrinterParser { ! //----------------------------------------------------------------------- @DataProvider(name="FieldPatterns") Object[][] provider_fieldPatterns() { return new Object[][] { {"e", "6", 0, 1, 6}, {"W", "3", 0, 1, 3}, {"w", "29", 0, 2, 29}, {"ww", "29", 0, 2, 29}, {"Y", "2013", 0, 4, 2013}, {"YY", "13", 0, 2, 2013}, --- 64,100 ---- import java.text.ParsePosition; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; + import java.time.format.DateTimeParseException; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; import java.time.temporal.WeekFields; + import java.util.Locale; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import test.java.time.format.AbstractTestPrinterParser; /** * Test TCKLocalizedFieldParser. */ @Test public class TCKLocalizedFieldParser extends AbstractTestPrinterParser { ! WeekFields weekDef = WeekFields.of(Locale.US); ! TemporalField Y = weekDef.weekBasedYear(); ! TemporalField w = weekDef.weekOfWeekBasedYear(); ! TemporalField c = weekDef.dayOfWeek(); //----------------------------------------------------------------------- @DataProvider(name="FieldPatterns") Object[][] provider_fieldPatterns() { return new Object[][] { {"e", "6", 0, 1, 6}, + {"e", "06", 0, 2, 6}, + {"ee", "06", 0, 2, 6}, + {"c", "6", 0, 1 , 6}, {"W", "3", 0, 1, 3}, {"w", "29", 0, 2, 29}, {"ww", "29", 0, 2, 29}, {"Y", "2013", 0, 4, 2013}, {"YY", "13", 0, 2, 2013},
*** 97,106 **** --- 105,115 ---- @Test(dataProvider="FieldPatterns") public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) { WeekFields weekDef = WeekFields.of(locale); TemporalField field = null; switch(pattern.charAt(0)) { + case 'c' : case 'e' : field = weekDef.dayOfWeek(); break; case 'w': field = weekDef.weekOfWeekBasedYear();
*** 200,205 **** --- 209,283 ---- LocalDate result = LocalDate.parse(text, dtf); assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef); } } + //----------------------------------------------------------------------- + @DataProvider(name = "adjacentValuePatterns1") + Object[][] provider_adjacentValuePatterns1() { + return new Object[][] { + {"YYww", Y, w, "1612", 2016, 12}, + {"YYYYww",Y, w, "201612", 2016, 12}, + }; + } + + @Test(dataProvider = "adjacentValuePatterns1") + public void test_adjacentValuePatterns1(String pattern, TemporalField field1, TemporalField field2, + String text, int expected1, int expected2) { + DateTimeFormatter df = new DateTimeFormatterBuilder() + .appendPattern(pattern).toFormatter(Locale.US); + ParsePosition ppos = new ParsePosition(0); + TemporalAccessor parsed = df.parseUnresolved(text, ppos); + assertEquals(parsed.get(field1), expected1); + assertEquals(parsed.get(field2), expected2); + } + + @DataProvider(name = "adjacentValuePatterns2") + Object[][] provider_adjacentValuePatterns2() { + return new Object[][] { + {"YYYYwwc", Y, w, c, "2016121", 2016, 12, 1}, + {"YYYYwwee", Y, w, c, "20161201", 2016, 12, 1}, + }; + } + + @Test(dataProvider = "adjacentValuePatterns2") + public void test_adjacentValuePatterns2(String pattern, TemporalField field1, TemporalField field2, + TemporalField field3, String text, int expected1, int expected2, int expected3) { + DateTimeFormatter df = new DateTimeFormatterBuilder() + .appendPattern(pattern).toFormatter(Locale.US); + ParsePosition ppos = new ParsePosition(0); + TemporalAccessor parsed = df.parseUnresolved(text, ppos); + assertEquals(parsed.get(field1), expected1); + assertEquals(parsed.get(field2), expected2); + assertEquals(parsed.get(field3), expected3); + } + + @Test + public void test_adjacentValuePatterns3() { + String pattern = "yyyyMMddwwc"; + String text = "20120720296"; + DateTimeFormatter df = new DateTimeFormatterBuilder() + .appendPattern(pattern).toFormatter(Locale.US); + ParsePosition ppos = new ParsePosition(0); + TemporalAccessor parsed = df.parseUnresolved(text, ppos); + assertEquals(parsed.get(c), 6); + assertEquals(parsed.get(w), 29); + LocalDate result = LocalDate.parse(text, df); + LocalDate expectedValue = LocalDate.of(2012, 07, 20); + assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern); + } + + @DataProvider(name = "invalidPatterns") + Object[][] provider_invalidPatterns() { + return new Object[][] { + {"W", "01"}, + {"c", "01"}, + {"YYYYwwe", "2016121"}, //e can have variable width(1 or 2) and must come first + {"yyyyMMddwwc", "201207202906"}, // 1 extra digit in the input + }; + } + + @Test(dataProvider = "invalidPatterns", expectedExceptions = DateTimeParseException.class) + public void test_invalidPatterns(String pattern, String value) { + DateTimeFormatter.ofPattern(pattern).parse(value); + } }
< prev index next >