1 /* 2 * Copyright (c) 2018, 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 8177552 26 * @summary Checks the equals and hashCode method of CompactNumberFormat 27 * @modules jdk.localedata 28 */ 29 30 import java.text.CompactNumberFormat; 31 import java.text.DecimalFormatSymbols; 32 import java.text.NumberFormat; 33 import java.util.Locale; 34 35 public class EqualityCheck { 36 37 public static void main(String[] args) { 38 testEquality(); 39 testHashCode(); 40 testEqualsAndHashCode(); 41 } 42 43 private static void testEquality() { 44 CompactNumberFormat cnf1 = (CompactNumberFormat) NumberFormat 45 .getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); 46 47 CompactNumberFormat cnf2 = (CompactNumberFormat) NumberFormat 48 .getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); 49 50 // taking a custom compact instance with the same state as 51 // compact number instance of "en_US" locale with SHORT style 52 CompactNumberFormat cnf3 = new CompactNumberFormat("#,##0.###", 53 DecimalFormatSymbols.getInstance(Locale.US), new String[]{"", "", 54 "", "0K", "00K", "000K", "0M", "00M", "000M", "0B", "00B", "000B", 55 "0T", "00T", "000T"}); 56 57 // checking reflexivity 58 if (!cnf1.equals(cnf1)) { 59 throw new RuntimeException("[testEquality() reflexivity FAILED: The compared" 60 + " objects must be equal]"); 61 } 62 63 if (!cnf1.equals(cnf2)) { 64 throw new RuntimeException("[testEquality() FAILED: The compared" 65 + " objects must be equal]"); 66 } 67 68 // checking symmetry, checking equality of two same objects 69 if (!cnf1.equals(cnf2) || !cnf2.equals(cnf1)) { 70 throw new RuntimeException("[testEquality() symmetry FAILED: The compared" 71 + " objects must be equal]"); 72 } 73 74 // checking transitivity, three objects must be equal 75 if (!cnf1.equals(cnf2) || !cnf2.equals(cnf3) || !cnf1.equals(cnf3)) { 76 throw new RuntimeException("[testEquality() transitivity FAILED: The compared" 77 + " objects must be equal]"); 78 } 79 80 // changing the min integer digits of first object; objects must not 81 // be equal 82 cnf1.setMinimumIntegerDigits(5); 83 checkEquals(cnf1, cnf2, false); 84 85 // changing the min integer digits of second object; objects must 86 // be equal 87 cnf2.setMinimumIntegerDigits(5); 88 checkEquals(cnf1, cnf2, true); 89 90 // changing the grouping size of first object; objects must not 91 // be equal 92 cnf1.setGroupingSize(4); 93 checkEquals(cnf1, cnf2, false); 94 95 // changing the grouping size if second object; objects must be equal 96 cnf2.setGroupingSize(4); 97 checkEquals(cnf1, cnf2, true); 98 99 // changing the parseBigDecimal of first object; objects must not 100 // be equal 101 cnf1.setParseBigDecimal(true); 102 checkEquals(cnf1, cnf2, false); 103 104 } 105 106 private static void checkEquals(CompactNumberFormat cnf1, 107 CompactNumberFormat cnf2, boolean mustEqual) { 108 if (cnf1.equals(cnf2) != mustEqual) { 109 if (mustEqual) { 110 throw new RuntimeException("[testEquality() FAILED: The compared" 111 + " objects must be equal]"); 112 } else { 113 throw new RuntimeException("[testEquality() FAILED: The compared" 114 + " objects must not be equal]"); 115 } 116 } 117 } 118 119 private static void testHashCode() { 120 NumberFormat cnf1 = NumberFormat 121 .getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT); 122 NumberFormat cnf2 = NumberFormat 123 .getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT); 124 125 if (cnf1.hashCode() != cnf2.hashCode()) { 126 throw new RuntimeException("[testHashCode() FAILED: hashCode of the" 127 + " compared objects must match]"); 128 } 129 } 130 131 // test the property of equals and hashCode i.e. two equal object must 132 // always have the same hashCode 133 private static void testEqualsAndHashCode() { 134 NumberFormat cnf1 = NumberFormat 135 .getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.SHORT); 136 cnf1.setMinimumIntegerDigits(5); 137 NumberFormat cnf2 = NumberFormat 138 .getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.SHORT); 139 cnf2.setMinimumIntegerDigits(5); 140 if (cnf1.equals(cnf2)) { 141 if (cnf1.hashCode() != cnf2.hashCode()) { 142 throw new RuntimeException("[testEqualsAndHashCode() FAILED: two" 143 + " equal objects must have same hashCode]"); 144 } 145 } else { 146 throw new RuntimeException("[testEqualsAndHashCode() FAILED: The" 147 + " compared objects must be equal]"); 148 } 149 } 150 151 } 152