1 /*
   2  * Copyright (c) 1999, 2016, 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     (this test doesn't have an at-test tag because it's run by a shell
  26     script instead of directly by the test harness)
  27 */
  28 
  29 /*
  30  *
  31  *
  32  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  33  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  34  *
  35  * Portions copyright (c) 2007 Sun Microsystems, Inc.
  36  * All Rights Reserved.
  37  *
  38  * The original version of this source code and documentation
  39  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  40  * subsidiary of IBM. These materials are provided under terms
  41  * of a License Agreement between Taligent and Sun. This technology
  42  * is protected by multiple US and International patents.
  43  *
  44  * This notice and attribution to Taligent may not be removed.
  45  * Taligent is a registered trademark of Taligent, Inc.
  46  *
  47  * Permission to use, copy, modify, and distribute this software
  48  * and its documentation for NON-COMMERCIAL purposes and without
  49  * fee is hereby granted provided that this copyright notice
  50  * appears in all copies. Please refer to the file "copyright.html"
  51  * for further important copyright and licensing information.
  52  *
  53  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  54  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  55  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  56  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  57  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  58  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  59  *
  60  */
  61 package java.text;
  62 import sun.text.IntHashtable;
  63 
  64 
  65 /**
  66  * This class tests some internal hashCode() functions.
  67  * Bug #4170614 complained that we had two iternal classes that
  68  * break the invariant that if a.equals(b) than a.hashCode() ==
  69  * b.hashCode().  This is because these classes overrode equals()
  70  * but not hashCode().  These are both purely internal classes, and
  71  * the library itself doesn't actually call hashCode(), so this isn't
  72  * actually causing anyone problems yet.  But if these classes are
  73  * ever exposed in the API, their hashCode() methods need to work right.
  74  * PatternEntry will never be exposed in the API, but IntHashtable
  75  * might be.  This is a shell test to allow us to access classes that
  76  * are declared package private.
  77  * @author Richard Gillam
  78  */
  79 public class Bug4170614Test {
  80     public static void main(String[] args) throws Exception {
  81         testIntHashtable();
  82         testPatternEntry();
  83     }
  84 
  85 
  86     public static void testIntHashtable() throws Exception {
  87         IntHashtable fred = new IntHashtable();
  88         fred.put(1, 10);
  89         fred.put(2, 20);
  90         fred.put(3, 30);
  91 
  92         IntHashtable barney = new IntHashtable();
  93         barney.put(1, 10);
  94         barney.put(3, 30);
  95         barney.put(2, 20);
  96 
  97         IntHashtable homer = new IntHashtable();
  98         homer.put(3, 30);
  99         homer.put(1, 10);
 100         homer.put(7, 900);
 101 
 102         if (fred.equals(barney)) {
 103             System.out.println("fred.equals(barney)");
 104         }
 105         else {
 106             System.out.println("!fred.equals(barney)");
 107         }
 108         System.out.println("fred.hashCode() == " + fred.hashCode());
 109         System.out.println("barney.hashCode() == " + barney.hashCode());
 110 
 111         if (!fred.equals(barney)) {
 112             throw new Exception("equals() failed on two hashtables that are equal");
 113         }
 114 
 115         if (fred.hashCode() != barney.hashCode()) {
 116            throw new Exception("hashCode() failed on two hashtables that are equal");
 117         }
 118 
 119         System.out.println();
 120         if (fred.equals(homer)) {
 121             System.out.println("fred.equals(homer)");
 122         }
 123         else {
 124             System.out.println("!fred.equals(homer)");
 125         }
 126         System.out.println("fred.hashCode() == " + fred.hashCode());
 127         System.out.println("homer.hashCode() == " + homer.hashCode());
 128 
 129         if (fred.equals(homer)) {
 130             throw new Exception("equals() failed on two hashtables that are not equal");
 131         }
 132 
 133         if (fred.hashCode() == homer.hashCode()) {
 134             throw new Exception("hashCode() failed on two hashtables that are not equal");
 135         }
 136 
 137         System.out.println();
 138         System.out.println("testIntHashtable() passed.\n");
 139     }
 140 
 141     public static void testPatternEntry() throws Exception {
 142         PatternEntry fred = new PatternEntry(1,
 143                                              new StringBuffer("hello"),
 144                                              new StringBuffer("up"));
 145         PatternEntry barney = new PatternEntry(1,
 146                                                new StringBuffer("hello"),
 147                                                new StringBuffer("down"));
 148         // (equals() only considers the "chars" field, so fred and barney are equal)
 149         PatternEntry homer = new PatternEntry(1,
 150                                               new StringBuffer("goodbye"),
 151                                               new StringBuffer("up"));
 152 
 153         if (fred.equals(barney)) {
 154             System.out.println("fred.equals(barney)");
 155         }
 156         else {
 157             System.out.println("!fred.equals(barney)");
 158         }
 159         System.out.println("fred.hashCode() == " + fred.hashCode());
 160         System.out.println("barney.hashCode() == " + barney.hashCode());
 161 
 162         if (!fred.equals(barney)) {
 163             throw new Exception("equals() failed on two hashtables that are equal");
 164         }
 165 
 166         if (fred.hashCode() != barney.hashCode()) {
 167            throw new Exception("hashCode() failed on two hashtables that are equal");
 168         }
 169 
 170         System.out.println();
 171         if (fred.equals(homer)) {
 172             System.out.println("fred.equals(homer)");
 173         }
 174         else {
 175             System.out.println("!fred.equals(homer)");
 176         }
 177         System.out.println("fred.hashCode() == " + fred.hashCode());
 178         System.out.println("homer.hashCode() == " + homer.hashCode());
 179 
 180         if (fred.equals(homer)) {
 181             throw new Exception("equals() failed on two hashtables that are not equal");
 182         }
 183 
 184         if (fred.hashCode() == homer.hashCode()) {
 185             throw new Exception("hashCode() failed on two hashtables that are not equal");
 186         }
 187 
 188         System.out.println();
 189         System.out.println("testPatternEntry() passed.\n");
 190     }
 191 }