1/* 2 * Copyright (c) 2007, 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 5004188 27 * @summary Tests how often method equals() is called 28 * @author Sergey Malenkov 29 */ 30 31import java.beans.PropertyChangeEvent; 32import java.beans.PropertyChangeListener; 33import java.beans.PropertyChangeSupport; 34 35public final class TestEquals implements PropertyChangeListener { 36 private static final String PROPERTY = "property"; 37 38 public static void main(String[] args) { 39 TestEquals one = new TestEquals(1); 40 TestEquals two = new TestEquals(2); 41 42 Object source = TestEquals.class; 43 PropertyChangeSupport pcs = new PropertyChangeSupport(source); 44 pcs.addPropertyChangeListener(PROPERTY, one); 45 pcs.addPropertyChangeListener(PROPERTY, two); 46 47 PropertyChangeEvent event = new PropertyChangeEvent(source, PROPERTY, one, two); 48 pcs.firePropertyChange(event); 49 test(one, two, 1); // only one check 50 pcs.firePropertyChange(PROPERTY, one, two); 51 test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent) 52 pcs.fireIndexedPropertyChange(PROPERTY, 1, one, two); 53 test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent) 54 } 55 56 private static void test(TestEquals v1, TestEquals v2, int amount) { 57 int count = v1.count + v2.count; 58 if (amount < count) 59 throw new Error("method equals() is called " + count + " times"); 60 61 v1.count = 0; 62 v2.count = 0; 63 } 64 65 private final int value; 66 private int count; 67 68 private TestEquals(int value) { 69 this.value = value; 70 } 71 72 @Override 73 public boolean equals(Object object) { 74 if (object instanceof TestEquals) { 75 this.count++; 76 TestEquals that = (TestEquals)object; 77 return that.value == this.value; 78 } 79 return false; 80 } 81 82 public void propertyChange(PropertyChangeEvent event) { 83 } 84} 85