1 /*
2  * Copyright (c) 2012, 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 8001209
27  * @summary Confirm that the values set by setChoices() are not mutable.
28  */
29 package test.java.text.Format.ChoiceFormat;
30 
31 import java.text.*;
32 
33 public class Bug8001209 {
34 
main(String[] args)35     public static void main(String[] args) throws Exception {
36         boolean err = false;
37 
38         // Borrow an example in API doc
39         double[] limits = {1,2,3,4,5,6,7};
40         String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
41         ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
42         ParsePosition status = new ParsePosition(0);
43 
44         StringBuilder before = new StringBuilder();
45         for (double i = 1.0; i <= 7.0; ++i) {
46             status.setIndex(0);
47             String s = form.format(i);
48             before.append(" ");
49             before.append(s);
50             before.append(form.parse(form.format(i),status));
51         }
52         String original = before.toString();
53 
54         double[] newLimits = form.getLimits();
55         String[] newFormats = (String[])form.getFormats();
56         newFormats[6] = "Doyoubi";
57         StringBuilder after = new StringBuilder();
58         for (double i = 1.0; i <= 7.0; ++i) {
59             status.setIndex(0);
60             String s = form.format(i);
61             after.append(" ");
62             after.append(s);
63             after.append(form.parse(form.format(i),status));
64         }
65         if (!original.equals(after.toString())) {
66             err = true;
67             System.err.println("  Expected:" + before
68                                + "\n  Got:     " + after);
69         }
70 
71         dayOfWeekNames[6] = "Saturday";
72         after = new StringBuilder();
73         for (double i = 1.0; i <= 7.0; ++i) {
74             status.setIndex(0);
75             String s = form.format(i);
76             after.append(" ");
77             after.append(s);
78             after.append(form.parse(form.format(i),status));
79         }
80         if (!original.equals(after.toString())) {
81             err = true;
82             System.err.println("  Expected:" + before
83                                + "\n  Got:     " + after);
84         }
85 
86         if (err) {
87             throw new RuntimeException("Failed.");
88         } else {
89             System.out.println("Passed.");
90         }
91     }
92 }
93