|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8324665 |
| 27 | + * @summary Checks if SPACE_SEPARATOR are correctly parsed in lenient mode |
| 28 | + * @run junit LenientSpaceParsingTest |
| 29 | + */ |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.Arguments; |
| 33 | +import org.junit.jupiter.params.provider.MethodSource; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 35 | + |
| 36 | +import java.text.ParseException; |
| 37 | +import java.text.SimpleDateFormat; |
| 38 | +import java.time.format.DateTimeFormatterBuilder; |
| 39 | +import java.time.format.DateTimeParseException; |
| 40 | +import java.util.stream.Stream; |
| 41 | + |
| 42 | +public class LenientSpaceParsingTest { |
| 43 | + @MethodSource |
| 44 | + private static Stream<Arguments> strictSpaces() { |
| 45 | + // input, pattern |
| 46 | + return Stream.of( |
| 47 | + Arguments.of("00\u002000", "H\u0020m"), |
| 48 | + Arguments.of("00\u202f00", "H\u202fm"), |
| 49 | + Arguments.of("00\u00a000", "H\u00a0m"), |
| 50 | + Arguments.of("00\u0020\u202f\u0020\u00a000", "H\u0020\u202f\u0020\u00a0m") |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + @MethodSource |
| 55 | + private static Stream<Arguments> lenientSpaces() { |
| 56 | + // input, pattern |
| 57 | + return Stream.of( |
| 58 | + Arguments.of("00\u002000", "H\u202fm"), |
| 59 | + Arguments.of("00\u202f00", "H\u0020m"), |
| 60 | + Arguments.of("00\u00a000", "H\u0020m"), |
| 61 | + Arguments.of("00\u002000", "H\u00a0m"), |
| 62 | + Arguments.of("00\u0020\u202f\u0020\u00a000", "H\u0020\u0020\u0020\u0020m"), |
| 63 | + Arguments.of("00\u0020\u202f\u0020\u00a000", "H\u202f\u00a0\u202f\u00a0m") |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + @MethodSource |
| 68 | + private static Stream<Arguments> nonSpaces() { |
| 69 | + // input, pattern |
| 70 | + return Stream.of( |
| 71 | + Arguments.of("00a00", "H\u202fm"), |
| 72 | + Arguments.of("00a00", "H\u00a0m"), |
| 73 | + Arguments.of("00a00", "H\u0020m"), |
| 74 | + Arguments.of("00aa00", "H\u0020\u0020m"), |
| 75 | + Arguments.of("00aa00", "H\u00a0\u202fm") |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + @ParameterizedTest |
| 80 | + @MethodSource({"strictSpaces", "lenientSpaces"}) |
| 81 | + public void checkDateTimeFormatter_Lenient(String input, String pattern) { |
| 82 | + new DateTimeFormatterBuilder().parseLenient().appendPattern(pattern).toFormatter().parse(input); |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest |
| 86 | + @MethodSource("nonSpaces") |
| 87 | + public void checkDateTimeFormatter_Lenient_Exception(String input, String pattern) { |
| 88 | + var dtf = new DateTimeFormatterBuilder().parseLenient().appendPattern(pattern).toFormatter(); |
| 89 | + assertThrows(DateTimeParseException.class, () -> { |
| 90 | + dtf.parse(input); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @ParameterizedTest |
| 95 | + @MethodSource("strictSpaces") |
| 96 | + public void checkDateTimeFormatter_Strict(String input, String pattern) { |
| 97 | + new DateTimeFormatterBuilder().parseStrict().appendPattern(pattern).toFormatter().parse(input); |
| 98 | + } |
| 99 | + |
| 100 | + @ParameterizedTest |
| 101 | + @MethodSource({"lenientSpaces", "nonSpaces"}) |
| 102 | + public void checkDateTimeFormatter_Strict_Exception(String input, String pattern) { |
| 103 | + var dtf = new DateTimeFormatterBuilder().parseStrict().appendPattern(pattern).toFormatter(); |
| 104 | + assertThrows(DateTimeParseException.class, () -> { |
| 105 | + dtf.parse(input); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + @ParameterizedTest |
| 110 | + @MethodSource({"strictSpaces", "lenientSpaces"}) |
| 111 | + public void checkSimpleDateFormat_Lenient(String input, String pattern) throws ParseException { |
| 112 | + new SimpleDateFormat(pattern).parse(input); |
| 113 | + } |
| 114 | + |
| 115 | + @ParameterizedTest |
| 116 | + @MethodSource("nonSpaces") |
| 117 | + public void checkSimpleDateFormat_Lenient_Exception(String input, String pattern) { |
| 118 | + var sdf = new SimpleDateFormat(pattern); |
| 119 | + assertThrows(ParseException.class, () -> { |
| 120 | + sdf.parse(input); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + @ParameterizedTest |
| 125 | + @MethodSource("strictSpaces") |
| 126 | + public void checkSimpleDateFormat_Strict(String input, String pattern) throws ParseException { |
| 127 | + var sdf = new SimpleDateFormat(pattern); |
| 128 | + sdf.setLenient(false); |
| 129 | + sdf.parse(input); |
| 130 | + } |
| 131 | + |
| 132 | + @ParameterizedTest |
| 133 | + @MethodSource({"lenientSpaces", "nonSpaces"}) |
| 134 | + public void checkSimpleDateFormat_Strict_Exception(String input, String pattern) { |
| 135 | + var sdf = new SimpleDateFormat(pattern); |
| 136 | + sdf.setLenient(false); |
| 137 | + assertThrows(ParseException.class, () -> { |
| 138 | + sdf.parse(input); |
| 139 | + }); |
| 140 | + } |
| 141 | +} |
0 commit comments