|
1 | 1 | /* |
2 | | - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -/** |
25 | | - * @test |
26 | | - * @bug 4109023 4153060 4153061 |
27 | | - * @summary test ParsePosition and FieldPosition |
28 | | - * @run junit PositionTest |
29 | | - */ |
30 | 24 | /* |
31 | 25 | (C) Copyright Taligent, Inc. 1996 - All Rights Reserved |
32 | 26 | (C) Copyright IBM Corp. 1996 - All Rights Reserved |
|
39 | 33 | Taligent is a registered trademark of Taligent, Inc. |
40 | 34 | */ |
41 | 35 |
|
42 | | -import java.text.*; |
43 | | -import java.io.*; |
| 36 | +/* |
| 37 | + * @test |
| 38 | + * @bug 4109023 4153060 4153061 8366400 |
| 39 | + * @summary test ParsePosition and FieldPosition |
| 40 | + * @run junit PositionTest |
| 41 | + */ |
44 | 42 |
|
45 | 43 | import org.junit.jupiter.api.Test; |
46 | 44 |
|
| 45 | +import java.text.DecimalFormat; |
| 46 | +import java.text.FieldPosition; |
| 47 | +import java.text.NumberFormat; |
| 48 | +import java.text.ParsePosition; |
| 49 | + |
| 50 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 51 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertNull; |
47 | 53 | import static org.junit.jupiter.api.Assertions.fail; |
48 | 54 |
|
49 | 55 | public class PositionTest { |
50 | 56 |
|
| 57 | + // Parsing text which contains un-parseable data, but the index |
| 58 | + // begins at the valid portion. Ensure PP is properly updated. |
| 59 | + @Test |
| 60 | + public void modifiedPositionTest() { |
| 61 | + var df = new DecimalFormat("YY#"); |
| 62 | + df.setStrict(false); // Lenient by default, set for test explicitness |
| 63 | + var pp = new ParsePosition(9); |
| 64 | + assertEquals(123L, assertDoesNotThrow(() -> df.parse("FOOBARBAZYY123", pp))); |
| 65 | + assertEquals(-1, pp.getErrorIndex()); |
| 66 | + assertEquals(14, pp.getIndex()); |
| 67 | + } |
| 68 | + |
| 69 | + // Clearly invalid index value that could not work under any scenarios |
| 70 | + // Specifically, ensuring no SIOOBE during affix matching |
| 71 | + @Test |
| 72 | + public void invalidPositionParseTest() { |
| 73 | + var df = new DecimalFormat(); |
| 74 | + df.setStrict(false); // Lenient by default, set for test explicitness |
| 75 | + assertNull(assertDoesNotThrow(() -> df.parse("1", new ParsePosition(-1)))); |
| 76 | + assertNull(assertDoesNotThrow(() -> df.parse("1", new ParsePosition(Integer.MAX_VALUE)))); |
| 77 | + } |
| 78 | + |
| 79 | + // When prefix matching, position + affix length is greater than parsed String length |
| 80 | + // Ensure we do not index out of bounds of the length of the parsed String |
| 81 | + @Test |
| 82 | + public void prefixMatchingTest() { |
| 83 | + var df = new DecimalFormat("ZZZ#;YYY#"); |
| 84 | + df.setStrict(false); // Lenient by default, set for test explicitness |
| 85 | + // 0 + 3 > 2 = (pos + prefix > text) |
| 86 | + assertNull(assertDoesNotThrow(() -> df.parse("Z1", new ParsePosition(0)))); |
| 87 | + assertNull(assertDoesNotThrow(() -> df.parse("Y1", new ParsePosition(0)))); |
| 88 | + } |
| 89 | + |
| 90 | + // When suffix matching, position + affix length is greater than parsed String length |
| 91 | + // Ensure we do not index out of bounds of the length of the parsed String |
| 92 | + @Test |
| 93 | + public void suffixMatchingTest() { |
| 94 | + var df = new DecimalFormat("#ZZ;#YY"); |
| 95 | + df.setStrict(false); // Lenient by default, set for test explicitness |
| 96 | + // Matches prefix properly first. Then 3 + 2 > 4 = (pos + suffix > text) |
| 97 | + assertNull(assertDoesNotThrow(() -> df.parse("123Z", new ParsePosition(0)))); |
| 98 | + assertNull(assertDoesNotThrow(() -> df.parse("123Y", new ParsePosition(0)))); |
| 99 | + } |
| 100 | + |
51 | 101 | @Test |
52 | 102 | public void TestParsePosition() { |
53 | 103 | ParsePosition pp1 = new ParsePosition(0); |
|
0 commit comments