|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2012, 2023, 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 |
| -/** |
| 24 | +/* |
25 | 25 | * @test
|
26 | 26 | * @bug 8001209
|
27 | 27 | * @summary Confirm that the values set by setChoices() are not mutable.
|
| 28 | + * @run junit Bug8001209 |
28 | 29 | */
|
29 |
| -import java.text.*; |
| 30 | + |
| 31 | +import java.text.ChoiceFormat; |
| 32 | +import java.text.ParsePosition; |
| 33 | + |
| 34 | +import org.junit.jupiter.api.BeforeAll; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
30 | 38 |
|
31 | 39 | public class Bug8001209 {
|
32 | 40 |
|
33 |
| - public static void main(String[] args) throws Exception { |
34 |
| - boolean err = false; |
| 41 | + // Represents the expected output of formatting the ChoiceFormat |
| 42 | + private static String expectedFormattedOutput; |
| 43 | + private static ChoiceFormat cFmt; |
| 44 | + private static ParsePosition status; |
| 45 | + private static String[] originalSetterArray; |
35 | 46 |
|
36 |
| - // Borrow an example in API doc |
37 |
| - double[] limits = {1,2,3,4,5,6,7}; |
38 |
| - String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; |
39 |
| - ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames); |
40 |
| - ParsePosition status = new ParsePosition(0); |
| 47 | + // Build the original ChoiceFormat to test if it can be mutated |
| 48 | + @BeforeAll |
| 49 | + static void setUpChoiceFormatAndOutput() { |
| 50 | + double[] limits = {1, 2, 3, 4, 5, 6, 7}; |
| 51 | + originalSetterArray = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
| 52 | + // Constructor calls setChoices |
| 53 | + cFmt = new ChoiceFormat(limits, originalSetterArray); |
| 54 | + status = new ParsePosition(0); |
41 | 55 |
|
| 56 | + // Build the expected results of formatting with the original ChoiceFormat |
42 | 57 | StringBuilder before = new StringBuilder();
|
43 | 58 | for (double i = 1.0; i <= 7.0; ++i) {
|
44 | 59 | status.setIndex(0);
|
45 |
| - String s = form.format(i); |
| 60 | + String s = cFmt.format(i); |
46 | 61 | before.append(" ");
|
47 | 62 | before.append(s);
|
48 |
| - before.append(form.parse(form.format(i),status)); |
| 63 | + before.append(cFmt.parse(cFmt.format(i), status)); |
49 | 64 | }
|
50 |
| - String original = before.toString(); |
| 65 | + expectedFormattedOutput = before.toString(); |
| 66 | + } |
51 | 67 |
|
52 |
| - double[] newLimits = form.getLimits(); |
53 |
| - String[] newFormats = (String[])form.getFormats(); |
| 68 | + /* |
| 69 | + * Ensure that mutating the arrays returned by getChoices and getLimits does |
| 70 | + * not affect the internal representation of the ChoiceFormat. |
| 71 | + */ |
| 72 | + @Test |
| 73 | + public void immutableArraysFromGetters() { |
| 74 | + // Modify the array returned by getFormats() -> newFormats |
| 75 | + String[] newFormats = (String[]) cFmt.getFormats(); |
54 | 76 | newFormats[6] = "Doyoubi";
|
55 | 77 | StringBuilder after = new StringBuilder();
|
56 | 78 | for (double i = 1.0; i <= 7.0; ++i) {
|
57 | 79 | status.setIndex(0);
|
58 |
| - String s = form.format(i); |
| 80 | + String s = cFmt.format(i); |
59 | 81 | after.append(" ");
|
60 | 82 | after.append(s);
|
61 |
| - after.append(form.parse(form.format(i),status)); |
62 |
| - } |
63 |
| - if (!original.equals(after.toString())) { |
64 |
| - err = true; |
65 |
| - System.err.println(" Expected:" + before |
66 |
| - + "\n Got: " + after); |
| 83 | + after.append(cFmt.parse(cFmt.format(i), status)); |
67 | 84 | }
|
| 85 | + // Compare the expected results with the new formatted results |
| 86 | + assertEquals(after.toString(), expectedFormattedOutput, |
| 87 | + "Mutating array returned from getter changed internals of ChoiceFormat"); |
| 88 | + } |
68 | 89 |
|
69 |
| - dayOfWeekNames[6] = "Saturday"; |
70 |
| - after = new StringBuilder(); |
| 90 | + /* |
| 91 | + * Ensure that mutating the arrays passed to setChoices/constructor does |
| 92 | + * not affect the internal representation of the ChoiceFormat. |
| 93 | + */ |
| 94 | + @Test |
| 95 | + public void immutableArraysFromSetter() { |
| 96 | + // Modify the array passed to setFormats() -> dayOfWeekNames |
| 97 | + originalSetterArray[6] = "Saturday"; |
| 98 | + StringBuilder after = new StringBuilder(); |
71 | 99 | for (double i = 1.0; i <= 7.0; ++i) {
|
72 | 100 | status.setIndex(0);
|
73 |
| - String s = form.format(i); |
| 101 | + String s = cFmt.format(i); |
74 | 102 | after.append(" ");
|
75 | 103 | after.append(s);
|
76 |
| - after.append(form.parse(form.format(i),status)); |
77 |
| - } |
78 |
| - if (!original.equals(after.toString())) { |
79 |
| - err = true; |
80 |
| - System.err.println(" Expected:" + before |
81 |
| - + "\n Got: " + after); |
82 |
| - } |
83 |
| - |
84 |
| - if (err) { |
85 |
| - throw new RuntimeException("Failed."); |
86 |
| - } else { |
87 |
| - System.out.println("Passed."); |
| 104 | + after.append(cFmt.parse(cFmt.format(i), status)); |
88 | 105 | }
|
| 106 | + // Compare the expected results with the new formatted results |
| 107 | + assertEquals(after.toString(), expectedFormattedOutput, |
| 108 | + "Mutating array passed to setter changed internals of ChoiceFormat"); |
89 | 109 | }
|
90 | 110 | }
|
1 commit comments
openjdk-notifier[bot] commentedon Jan 20, 2025
Review
Issues