Search in sources :

Example 61 with ParsePosition

use of java.text.ParsePosition in project jdk8u_jdk by JetBrains.

the class TestReducedParser method test_reducedWithLateChronoChangeTwice.

@Test
public void test_reducedWithLateChronoChangeTwice() {
    DateTimeFormatter df = new DateTimeFormatterBuilder().appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1)).appendLiteral(" ").appendChronologyId().appendLiteral(" ").appendChronologyId().toFormatter();
    int expected = 2044;
    String input = "44 ThaiBuddhist ISO";
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    assertEquals(pos.getIndex(), input.length(), "Input not parsed completely: " + pos);
    assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)");
    int actual = parsed.get(YEAR);
    assertEquals(actual, expected, String.format("Wrong date parsed, chrono: %s, input: %s", parsed.query(TemporalQueries.chronology()), input));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 62 with ParsePosition

use of java.text.ParsePosition in project jdk8u_jdk by JetBrains.

the class TestReducedParser method test_parseLenient_baseDate.

@Test(dataProvider = "ParseLenientSensitive")
public void test_parseLenient_baseDate(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos, Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(false);
    TemporalAccessor parsed = getFormatterBaseDate(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), lenient.parseLen, "error case parse position");
        assertEquals(parsed, lenient.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), lenient.parseLen, "parse position");
        assertParsed(parsed, YEAR, lenient.parseVal != null ? (long) lenient.parseVal : null);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 63 with ParsePosition

use of java.text.ParsePosition in project jdk8u_jdk by JetBrains.

the class TestReducedParser method test_parseStrict.

//-----------------------------------------------------------------------
// Parsing tests for strict mode
//-----------------------------------------------------------------------
@Test(dataProvider = "ParseLenientSensitive")
public void test_parseStrict(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos, Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), strict.parseLen, "error case parse position");
        assertEquals(parsed, strict.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), strict.parseLen, "parse position");
        assertParsed(parsed, YEAR, strict.parseVal != null ? (long) strict.parseVal : null);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 64 with ParsePosition

use of java.text.ParsePosition in project jdk8u_jdk by JetBrains.

the class TestFractionPrinterParser method test_reverseParse_preceededByNonDigit.

//    @Test(dataProvider="Nanos")
//    public void test_reverseParse_followedByNonDigit_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception {
//        FractionPrinterParser pp = new FractionPrinterParser(NANO_OF_SECOND, minWidth, maxWidth, false);
//        int newPos = pp.parse(parseContext, result + " ", (result.startsWith(".") ? 1 : 0));
//        assertEquals(newPos, result.length());
//        int expectedValue = fixParsedValue(maxWidth, value);
//        assertParsed(parseContext, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
//    }
@Test(dataProvider = "Nanos")
public void test_reverseParse_preceededByNonDigit(int minWidth, int maxWidth, int value, String result) throws Exception {
    ParsePosition pos = new ParsePosition(1);
    int expectedValue = fixParsedValue(maxWidth, value);
    TemporalAccessor parsed = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseUnresolved(" " + result, pos);
    assertEquals(pos.getIndex(), result.length() + 1);
    assertParsed(parsed, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 65 with ParsePosition

use of java.text.ParsePosition in project jdk8u_jdk by JetBrains.

the class TestTextParser method test_parseDayOfWeekText.

@Test(dataProvider = "parseDayOfWeekText")
public void test_parseDayOfWeekText(Locale locale, String pattern, String input, DayOfWeek expected) {
    DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale);
    ParsePosition pos = new ParsePosition(0);
    assertEquals(DayOfWeek.from(formatter.parse(input, pos)), expected);
    assertEquals(pos.getIndex(), input.length());
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Aggregations

ParsePosition (java.text.ParsePosition)694 Test (org.junit.Test)250 Date (java.util.Date)213 TemporalAccessor (java.time.temporal.TemporalAccessor)163 SimpleDateFormat (java.text.SimpleDateFormat)117 DateTimeFormatter (java.time.format.DateTimeFormatter)94 Test (org.testng.annotations.Test)88 ParseException (java.text.ParseException)71 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)49 SimpleDateFormat (android.icu.text.SimpleDateFormat)39 FieldPosition (java.text.FieldPosition)32 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)25 Calendar (android.icu.util.Calendar)23 ULocale (android.icu.util.ULocale)23 NumberFormat (java.text.NumberFormat)23 Calendar (java.util.Calendar)23 Format (java.text.Format)21 DecimalFormat (java.text.DecimalFormat)19 GregorianCalendar (android.icu.util.GregorianCalendar)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)17