test/java/time/test/java/time/format/TestTextParser.java
Print this page
*** 57,78 ****
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.java.time.format;
- import java.time.format.*;
-
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.text.ParsePosition;
! import java.util.Locale;
! import java.time.format.DateTimeBuilder;
import java.time.temporal.TemporalField;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
--- 57,77 ----
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.java.time.format;
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.text.ParsePosition;
! import java.time.format.TextStyle;
! import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
+ import java.util.Locale;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
*** 91,143 ****
}
@Test(dataProvider="error")
public void test_parse_error(TemporalField field, TextStyle style, String text, int pos, Class<?> expected) {
try {
! getFormatter(field, style).parseToBuilder(text, new ParsePosition(pos));
} catch (RuntimeException ex) {
assertTrue(expected.isInstance(ex));
}
}
//-----------------------------------------------------------------------
public void test_parse_midStr() throws Exception {
ParsePosition pos = new ParsePosition(3);
assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.FULL)
! .parseToBuilder("XxxMondayXxx", pos)
.getLong(DAY_OF_WEEK), 1L);
assertEquals(pos.getIndex(), 9);
}
public void test_parse_remainderIgnored() throws Exception {
ParsePosition pos = new ParsePosition(0);
assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.SHORT)
! .parseToBuilder("Wednesday", pos)
.getLong(DAY_OF_WEEK), 3L);
assertEquals(pos.getIndex(), 3);
}
//-----------------------------------------------------------------------
public void test_parse_noMatch1() throws Exception {
ParsePosition pos = new ParsePosition(0);
! DateTimeBuilder dtb =
! getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseToBuilder("Munday", pos);
assertEquals(pos.getErrorIndex(), 0);
}
public void test_parse_noMatch2() throws Exception {
ParsePosition pos = new ParsePosition(3);
! DateTimeBuilder dtb =
! getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseToBuilder("Monday", pos);
assertEquals(pos.getErrorIndex(), 3);
}
public void test_parse_noMatch_atEnd() throws Exception {
ParsePosition pos = new ParsePosition(6);
! DateTimeBuilder dtb =
! getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseToBuilder("Monday", pos);
assertEquals(pos.getErrorIndex(), 6);
}
//-----------------------------------------------------------------------
@DataProvider(name="parseText")
Object[][] provider_text() {
--- 90,145 ----
}
@Test(dataProvider="error")
public void test_parse_error(TemporalField field, TextStyle style, String text, int pos, Class<?> expected) {
try {
! getFormatter(field, style).parseUnresolved(text, new ParsePosition(pos));
} catch (RuntimeException ex) {
assertTrue(expected.isInstance(ex));
}
}
//-----------------------------------------------------------------------
public void test_parse_midStr() throws Exception {
ParsePosition pos = new ParsePosition(3);
assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.FULL)
! .parseUnresolved("XxxMondayXxx", pos)
.getLong(DAY_OF_WEEK), 1L);
assertEquals(pos.getIndex(), 9);
}
public void test_parse_remainderIgnored() throws Exception {
ParsePosition pos = new ParsePosition(0);
assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.SHORT)
! .parseUnresolved("Wednesday", pos)
.getLong(DAY_OF_WEEK), 3L);
assertEquals(pos.getIndex(), 3);
}
//-----------------------------------------------------------------------
public void test_parse_noMatch1() throws Exception {
ParsePosition pos = new ParsePosition(0);
! TemporalAccessor parsed =
! getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Munday", pos);
assertEquals(pos.getErrorIndex(), 0);
+ assertEquals(parsed, null);
}
public void test_parse_noMatch2() throws Exception {
ParsePosition pos = new ParsePosition(3);
! TemporalAccessor parsed =
! getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos);
assertEquals(pos.getErrorIndex(), 3);
+ assertEquals(parsed, null);
}
public void test_parse_noMatch_atEnd() throws Exception {
ParsePosition pos = new ParsePosition(6);
! TemporalAccessor parsed =
! getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos);
assertEquals(pos.getErrorIndex(), 6);
+ assertEquals(parsed, null);
}
//-----------------------------------------------------------------------
@DataProvider(name="parseText")
Object[][] provider_text() {
*** 182,341 ****
}
@Test(dataProvider="parseText")
public void test_parseText(TemporalField field, TextStyle style, int value, String input) throws Exception {
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(field, style).parseToBuilder(input, pos).getLong(field), (long) value);
assertEquals(pos.getIndex(), input.length());
}
@Test(dataProvider="parseNumber")
public void test_parseNumber(TemporalField field, TextStyle style, int value, String input) throws Exception {
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(field, style).parseToBuilder(input, pos).getLong(field), (long) value);
assertEquals(pos.getIndex(), input.length());
}
//-----------------------------------------------------------------------
@Test(dataProvider="parseText")
public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
setCaseSensitive(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(field, style).parseToBuilder(input.toUpperCase(), pos);
assertEquals(pos.getErrorIndex(), 0);
}
@Test(dataProvider="parseText")
public void test_parse_strict_caseInsensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
setCaseSensitive(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(field, style).parseToBuilder(input.toUpperCase(), pos).getLong(field), (long) value);
assertEquals(pos.getIndex(), input.length());
}
//-----------------------------------------------------------------------
@Test(dataProvider="parseText")
public void test_parse_strict_caseSensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
setCaseSensitive(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(field, style).parseToBuilder(input.toLowerCase(), pos);
assertEquals(pos.getErrorIndex(), 0);
}
@Test(dataProvider="parseText")
public void test_parse_strict_caseInsensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
setCaseSensitive(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(field, style).parseToBuilder(input.toLowerCase(), pos).getLong(field), (long) value);
assertEquals(pos.getIndex(), input.length());
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public void test_parse_full_strict_full_match() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("January", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 7);
}
public void test_parse_full_strict_short_noMatch() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("Janua", pos);
assertEquals(pos.getErrorIndex(), 0);
}
public void test_parse_full_strict_number_noMatch() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("1", pos);
assertEquals(pos.getErrorIndex(), 0);
}
//-----------------------------------------------------------------------
public void test_parse_short_strict_full_match() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("January", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 3);
}
public void test_parse_short_strict_short_match() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 3);
}
public void test_parse_short_strict_number_noMatch() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("1", pos);
assertEquals(pos.getErrorIndex(), 0);
}
//-----------------------------------------------------------------------
public void test_parse_french_short_strict_full_noMatch() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH)
! .parseToBuilder("janvier", pos);
assertEquals(pos.getErrorIndex(), 0);
}
public void test_parse_french_short_strict_short_match() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH)
! .parseToBuilder("janv.", pos)
.getLong(MONTH_OF_YEAR),
1L);
assertEquals(pos.getIndex(), 5);
}
//-----------------------------------------------------------------------
public void test_parse_full_lenient_full_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("January.", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 7);
}
public void test_parse_full_lenient_short_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 3);
}
public void test_parse_full_lenient_number_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseToBuilder("1", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 1);
}
//-----------------------------------------------------------------------
public void test_parse_short_lenient_full_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("January", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 7);
}
public void test_parse_short_lenient_short_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 3);
}
public void test_parse_short_lenient_number_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseToBuilder("1", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 1);
}
}
--- 184,343 ----
}
@Test(dataProvider="parseText")
public void test_parseText(TemporalField field, TextStyle style, int value, String input) throws Exception {
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(field, style).parseUnresolved(input, pos).getLong(field), (long) value);
assertEquals(pos.getIndex(), input.length());
}
@Test(dataProvider="parseNumber")
public void test_parseNumber(TemporalField field, TextStyle style, int value, String input) throws Exception {
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(field, style).parseUnresolved(input, pos).getLong(field), (long) value);
assertEquals(pos.getIndex(), input.length());
}
//-----------------------------------------------------------------------
@Test(dataProvider="parseText")
public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
setCaseSensitive(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(field, style).parseUnresolved(input.toUpperCase(), pos);
assertEquals(pos.getErrorIndex(), 0);
}
@Test(dataProvider="parseText")
public void test_parse_strict_caseInsensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
setCaseSensitive(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(field, style).parseUnresolved(input.toUpperCase(), pos).getLong(field), (long) value);
assertEquals(pos.getIndex(), input.length());
}
//-----------------------------------------------------------------------
@Test(dataProvider="parseText")
public void test_parse_strict_caseSensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
setCaseSensitive(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(field, style).parseUnresolved(input.toLowerCase(), pos);
assertEquals(pos.getErrorIndex(), 0);
}
@Test(dataProvider="parseText")
public void test_parse_strict_caseInsensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
setCaseSensitive(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(field, style).parseUnresolved(input.toLowerCase(), pos).getLong(field), (long) value);
assertEquals(pos.getIndex(), input.length());
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public void test_parse_full_strict_full_match() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 7);
}
public void test_parse_full_strict_short_noMatch() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("Janua", pos);
assertEquals(pos.getErrorIndex(), 0);
}
public void test_parse_full_strict_number_noMatch() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("1", pos);
assertEquals(pos.getErrorIndex(), 0);
}
//-----------------------------------------------------------------------
public void test_parse_short_strict_full_match() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 3);
}
public void test_parse_short_strict_short_match() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 3);
}
public void test_parse_short_strict_number_noMatch() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
! getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("1", pos);
assertEquals(pos.getErrorIndex(), 0);
}
//-----------------------------------------------------------------------
public void test_parse_french_short_strict_full_noMatch() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH)
! .parseUnresolved("janvier", pos);
assertEquals(pos.getErrorIndex(), 0);
}
public void test_parse_french_short_strict_short_match() throws Exception {
setStrict(true);
ParsePosition pos = new ParsePosition(0);
assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH)
! .parseUnresolved("janv.", pos)
.getLong(MONTH_OF_YEAR),
1L);
assertEquals(pos.getIndex(), 5);
}
//-----------------------------------------------------------------------
public void test_parse_full_lenient_full_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("January.", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 7);
}
public void test_parse_full_lenient_short_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 3);
}
public void test_parse_full_lenient_number_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.FULL).parseUnresolved("1", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 1);
}
//-----------------------------------------------------------------------
public void test_parse_short_lenient_full_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("January", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 7);
}
public void test_parse_short_lenient_short_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("Janua", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 3);
}
public void test_parse_short_lenient_number_match() throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
! assertEquals(getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).parseUnresolved("1", pos).getLong(MONTH_OF_YEAR), 1L);
assertEquals(pos.getIndex(), 1);
}
}