|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | + * @test |
| 25 | + * @bug 8368328 |
| 26 | + * @summary Tests if CompactNumberFormat.clone() creates an independent object |
| 27 | + * @run junit/othervm --add-opens java.base/java.text=ALL-UNNAMED TestClone |
| 28 | + */ |
| 29 | + |
| 30 | +import java.lang.invoke.MethodHandles; |
| 31 | +import java.text.CompactNumberFormat; |
| 32 | +import java.text.DecimalFormat; |
| 33 | +import java.text.DecimalFormatSymbols; |
| 34 | +import java.text.NumberFormat; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.stream.IntStream; |
| 37 | +import java.util.stream.Stream; |
| 38 | + |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.params.ParameterizedTest; |
| 41 | +import org.junit.jupiter.params.provider.Arguments; |
| 42 | +import org.junit.jupiter.params.provider.MethodSource; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 45 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 46 | + |
| 47 | +public class TestClone { |
| 48 | + // Concurrently parse numbers using cloned instances as originally |
| 49 | + // reported in the bug. This test could produce false negative results, |
| 50 | + // depending on the testing environment |
| 51 | + @Test |
| 52 | + void randomAccessTest() { |
| 53 | + var original = |
| 54 | + NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); |
| 55 | + var threads = IntStream.range(0, 10) |
| 56 | + .mapToObj(num -> new Thread(() -> { |
| 57 | + var clone = (NumberFormat) original.clone(); |
| 58 | + for (int i = 0; i < 1000; i++) { |
| 59 | + assertDoesNotThrow(() -> |
| 60 | + assertEquals(num, clone.parse(String.valueOf(num)).intValue())); |
| 61 | + } |
| 62 | + })).toList(); |
| 63 | + threads.forEach(Thread::start); |
| 64 | + threads.forEach(t -> { |
| 65 | + try { |
| 66 | + t.join(); |
| 67 | + } catch (InterruptedException ie) { |
| 68 | + throw new RuntimeException(ie); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + private static Stream<Arguments> referenceFields() throws ClassNotFoundException { |
| 74 | + return Stream.of( |
| 75 | + Arguments.of("compactPatterns", String[].class), |
| 76 | + Arguments.of("symbols", DecimalFormatSymbols.class), |
| 77 | + Arguments.of("decimalFormat", DecimalFormat.class), |
| 78 | + Arguments.of("defaultDecimalFormat", DecimalFormat.class), |
| 79 | + Arguments.of("digitList", Class.forName("java.text.DigitList")) |
| 80 | + ); |
| 81 | + } |
| 82 | + // Explicitly checks if the cloned object has its own references for |
| 83 | + // "compactPatterns", "symbols", "decimalFormat", "defaultDecimalFormat", |
| 84 | + // and "digitList" |
| 85 | + @ParameterizedTest |
| 86 | + @MethodSource("referenceFields") |
| 87 | + void whiteBoxTest(String fieldName, Class<?> type) throws Throwable { |
| 88 | + var original = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); |
| 89 | + var clone = original.clone(); |
| 90 | + var lookup = MethodHandles.privateLookupIn(CompactNumberFormat.class, MethodHandles.lookup()); |
| 91 | + |
| 92 | + assertNotSame(lookup.findGetter(CompactNumberFormat.class, fieldName, type).invoke(original), |
| 93 | + lookup.findGetter(CompactNumberFormat.class, fieldName, type).invoke(clone)); |
| 94 | + } |
| 95 | +} |
0 commit comments