< prev index next >

test/jdk/java/text/Format/ChoiceFormat/Bug8001209.java

Print this page

  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 import java.text.*;







 30 
 31 public class Bug8001209 {
 32 
 33     public static void main(String[] args) throws Exception {
 34         boolean err = false;



 35 
 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);



 41 

 42         StringBuilder before = new StringBuilder();
 43         for (double i = 1.0; i <= 7.0; ++i) {
 44             status.setIndex(0);
 45             String s = form.format(i);
 46             before.append(" ");
 47             before.append(s);
 48             before.append(form.parse(form.format(i),status));
 49         }
 50         String original = before.toString();

 51 
 52         double[] newLimits = form.getLimits();
 53         String[] newFormats = (String[])form.getFormats();






 54         newFormats[6] = "Doyoubi";
 55         StringBuilder after = new StringBuilder();
 56         for (double i = 1.0; i <= 7.0; ++i) {
 57             status.setIndex(0);
 58             String s = form.format(i);
 59             after.append(" ");
 60             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);
 67         }




 68 
 69         dayOfWeekNames[6] = "Saturday";
 70         after = new StringBuilder();







 71         for (double i = 1.0; i <= 7.0; ++i) {
 72             status.setIndex(0);
 73             String s = form.format(i);
 74             after.append(" ");
 75             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.");
 88         }



 89     }
 90 }

  1 /*
  2  * Copyright (c) 2012, 2023, 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  * @run junit Bug8001209
 29  */
 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;
 38 
 39 public class Bug8001209 {
 40 
 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;
 46 
 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);
 55 
 56         // Build the expected results of formatting with the original ChoiceFormat
 57         StringBuilder before = new StringBuilder();
 58         for (double i = 1.0; i <= 7.0; ++i) {
 59             status.setIndex(0);
 60             String s = cFmt.format(i);
 61             before.append(" ");
 62             before.append(s);
 63             before.append(cFmt.parse(cFmt.format(i), status));
 64         }
 65         expectedFormattedOutput = before.toString();
 66     }
 67 
 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();
 76         newFormats[6] = "Doyoubi";
 77         StringBuilder after = new StringBuilder();
 78         for (double i = 1.0; i <= 7.0; ++i) {
 79             status.setIndex(0);
 80             String s = cFmt.format(i);
 81             after.append(" ");
 82             after.append(s);
 83             after.append(cFmt.parse(cFmt.format(i), status));





 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     }
 89 
 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();
 99         for (double i = 1.0; i <= 7.0; ++i) {
100             status.setIndex(0);
101             String s = cFmt.format(i);
102             after.append(" ");
103             after.append(s);
104             after.append(cFmt.parse(cFmt.format(i), status));











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");
109     }
110 }
< prev index next >