Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestEquality.java
41153 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 8177552 822275625* @summary Checks the equals and hashCode method of CompactNumberFormat26* @modules jdk.localedata27* @run testng/othervm TestEquality28*29*/3031import java.text.CompactNumberFormat;32import java.text.DecimalFormatSymbols;33import java.text.NumberFormat;34import java.util.Locale;35import org.testng.annotations.Test;3637public class TestEquality {3839@Test40public void testEquality() {41CompactNumberFormat cnf1 = (CompactNumberFormat) NumberFormat42.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);4344CompactNumberFormat cnf2 = (CompactNumberFormat) NumberFormat45.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);4647// A custom compact instance with the same state as48// compact number instance of "en_US" locale with SHORT style49String decimalPattern = "#,##0.###";50String[] compactPatterns = new String[]{51"",52"",53"",54"{one:0K other:0K}",55"{one:00K other:00K}",56"{one:000K other:000K}",57"{one:0M other:0M}",58"{one:00M other:00M}",59"{one:000M other:000M}",60"{one:0B other:0B}",61"{one:00B other:00B}",62"{one:000B other:000B}",63"{one:0T other:0T}",64"{one:00T other:00T}",65"{one:000T other:000T}"66};67DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);68CompactNumberFormat cnf3 =69new CompactNumberFormat(decimalPattern, symbols, compactPatterns, "one:i = 1 and v = 0");7071// A compact instance created with different decimalPattern than cnf372CompactNumberFormat cnf4 = new CompactNumberFormat("#,#0.0#", symbols, compactPatterns);7374// A compact instance created with different format symbols than cnf375CompactNumberFormat cnf5 = new CompactNumberFormat(decimalPattern,76DecimalFormatSymbols.getInstance(Locale.JAPAN), compactPatterns);7778// A compact instance created with different compact patterns than cnf379CompactNumberFormat cnf6 = new CompactNumberFormat(decimalPattern,80symbols, new String[]{"", "", "", "0K", "00K", "000K"});8182// Checking reflexivity83if (!cnf1.equals(cnf1)) {84throw new RuntimeException("[testEquality() reflexivity FAILED: The compared"85+ " objects must be equal]");86}8788// Checking symmetry, checking equality of two same objects89if (!cnf1.equals(cnf2) || !cnf2.equals(cnf1)) {90throw new RuntimeException("[testEquality() symmetry FAILED: The compared"91+ " objects must be equal]");92}9394// Checking transitivity, three objects must be equal95if (!cnf1.equals(cnf2) || !cnf2.equals(cnf3) || !cnf1.equals(cnf3)) {96throw new RuntimeException("[testEquality() transitivity FAILED: The compared"97+ " objects must be equal]");98}99100// Objects must not be equal as the decimalPattern is different101checkEquals(cnf3, cnf4, false, "1st", "different decimal pattern");102103// Objects must not be equal as the format symbols instance is different104checkEquals(cnf3, cnf5, false, "2nd", "different format symbols");105106// Objects must not be equal as the compact patters are different107checkEquals(cnf3, cnf6, false, "3rd", "different compact patterns");108109// Changing the min integer digits of first object; objects must not110// be equal111cnf1.setMinimumIntegerDigits(5);112checkEquals(cnf1, cnf2, false, "4th", "different min integer digits");113114// Changing the min integer digits of second object; objects must115// be equal116cnf2.setMinimumIntegerDigits(5);117checkEquals(cnf1, cnf2, true, "5th", "");118119// Changing the grouping size of first object; objects must not120// be equal121cnf1.setGroupingSize(4);122checkEquals(cnf1, cnf2, false, "6th", "different grouping size");123124// Changing the grouping size if second object; objects must be equal125cnf2.setGroupingSize(4);126checkEquals(cnf1, cnf2, true, "7th", "");127128// Changing the parseBigDecimal of first object; objects must not129// be equal130cnf1.setParseBigDecimal(true);131checkEquals(cnf1, cnf2, false, "8th", "different parse big decimal");132133}134135private void checkEquals(CompactNumberFormat cnf1, CompactNumberFormat cnf2,136boolean mustEqual, String nthComparison, String message) {137if (cnf1.equals(cnf2) != mustEqual) {138if (mustEqual) {139throw new RuntimeException("[testEquality() " + nthComparison140+ " comparison FAILED: The compared objects must be equal]");141} else {142throw new RuntimeException("[testEquality() " + nthComparison143+ " comparison FAILED: The compared objects must"144+ " not be equal because of " + message + "]");145}146}147}148149@Test150public void testHashCode() {151NumberFormat cnf1 = NumberFormat152.getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);153NumberFormat cnf2 = NumberFormat154.getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);155156if (cnf1.hashCode() != cnf2.hashCode()) {157throw new RuntimeException("[testHashCode() FAILED: hashCode of the"158+ " compared objects must match]");159}160}161162// Test the property of equals and hashCode i.e. two equal object must163// always have the same hashCode164@Test165public void testEqualsAndHashCode() {166NumberFormat cnf1 = NumberFormat167.getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.SHORT);168cnf1.setMinimumIntegerDigits(5);169NumberFormat cnf2 = NumberFormat170.getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.SHORT);171cnf2.setMinimumIntegerDigits(5);172if (cnf1.equals(cnf2)) {173if (cnf1.hashCode() != cnf2.hashCode()) {174throw new RuntimeException("[testEqualsAndHashCode() FAILED: two"175+ " equal objects must have same hashCode]");176}177} else {178throw new RuntimeException("[testEqualsAndHashCode() FAILED: The"179+ " compared objects must be equal]");180}181}182183}184185186