Skip to content

Commit ee9bd9b

Browse files
committed
8230136: DateTimeFormatterBuilder.FractionPrinterParser#parse fails to verify minWidth
Reviewed-by: joehw, scolebourne, rriggs
1 parent 4b6f9ed commit ee9bd9b

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public DateTimeFormatterBuilder parseCaseInsensitive() {
306306
/**
307307
* Changes the parse style to be strict for the remainder of the formatter.
308308
* <p>
309-
* Parsing can be strict or lenient - by default its strict.
309+
* Parsing can be strict or lenient - by default it is strict.
310310
* This controls the degree of flexibility in matching the text and sign styles.
311311
* <p>
312312
* When used, this method changes the parsing to be strict from this point onwards.
@@ -325,7 +325,7 @@ public DateTimeFormatterBuilder parseStrict() {
325325
* Changes the parse style to be lenient for the remainder of the formatter.
326326
* Note that case sensitivity is set separately to this method.
327327
* <p>
328-
* Parsing can be strict or lenient - by default its strict.
328+
* Parsing can be strict or lenient - by default it is strict.
329329
* This controls the degree of flexibility in matching the text and sign styles.
330330
* Applications calling this method should typically also call {@link #parseCaseInsensitive()}.
331331
* <p>
@@ -3200,7 +3200,7 @@ public int parse(DateTimeParseContext context, CharSequence text, int position)
32003200
char ch = text.charAt(pos++);
32013201
int digit = context.getDecimalStyle().convertToDigit(ch);
32023202
if (digit < 0) {
3203-
if (pos < minEndPos) {
3203+
if (pos <= minEndPos) {
32043204
return ~position; // need at least min width digits
32053205
}
32063206
pos--;

test/jdk/java/time/test/java/time/format/TestFractionPrinterParser.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -59,6 +59,7 @@
5959
*/
6060
package test.java.time.format;
6161

62+
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
6263
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
6364
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
6465
import static org.testng.Assert.assertEquals;
@@ -77,6 +78,8 @@
7778

7879
/**
7980
* Test FractionPrinterParser.
81+
*
82+
* @bug 8230136
8083
*/
8184
@Test
8285
public class TestFractionPrinterParser extends AbstractTestPrinterParser {
@@ -331,6 +334,24 @@ public void test_parse_nothing(TemporalField field, int min, int max, boolean de
331334
assertEquals(parsed, null);
332335
}
333336

337+
@DataProvider(name="ParseMinWidth")
338+
Object[][] provider_parseMinWidth() {
339+
return new Object[][] {
340+
{MILLI_OF_SECOND, 3, 3, true, ".1x"},
341+
{MILLI_OF_SECOND, 3, 3, true, ".12x"},
342+
{MILLI_OF_SECOND, 3, 3, true, ".1234x"},
343+
};
344+
}
345+
346+
@Test(dataProvider="ParseMinWidth", expectedExceptions=DateTimeException.class)
347+
public void test_parse_minWidth(TemporalField field, int min, int max, boolean decimalPoint, String text) throws Exception {
348+
builder
349+
.appendFraction(field, min, max, decimalPoint)
350+
.appendLiteral("x")
351+
.toFormatter(locale)
352+
.parse(text);
353+
}
354+
334355
//-----------------------------------------------------------------------
335356
public void test_toString() throws Exception {
336357
assertEquals(getFormatter(NANO_OF_SECOND, 3, 6, true).toString(), "Fraction(NanoOfSecond,3,6,DecimalPoint)");

0 commit comments

Comments
 (0)