Path: blob/master/test/jdk/java/beans/PropertyChangeSupport/TestEquals.java
41152 views
/*1* Copyright (c) 2007, 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*/2223/*24* @test25* @bug 500418826* @summary Tests how often method equals() is called27* @author Sergey Malenkov28*/2930import java.beans.PropertyChangeEvent;31import java.beans.PropertyChangeListener;32import java.beans.PropertyChangeSupport;3334public final class TestEquals implements PropertyChangeListener {35private static final String PROPERTY = "property";3637public static void main(String[] args) {38TestEquals one = new TestEquals(1);39TestEquals two = new TestEquals(2);4041Object source = TestEquals.class;42PropertyChangeSupport pcs = new PropertyChangeSupport(source);43pcs.addPropertyChangeListener(PROPERTY, one);44pcs.addPropertyChangeListener(PROPERTY, two);4546PropertyChangeEvent event = new PropertyChangeEvent(source, PROPERTY, one, two);47pcs.firePropertyChange(event);48test(one, two, 1); // only one check49pcs.firePropertyChange(PROPERTY, one, two);50test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)51pcs.fireIndexedPropertyChange(PROPERTY, 1, one, two);52test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)53}5455private static void test(TestEquals v1, TestEquals v2, int amount) {56int count = v1.count + v2.count;57if (amount < count)58throw new Error("method equals() is called " + count + " times");5960v1.count = 0;61v2.count = 0;62}6364private final int value;65private int count;6667private TestEquals(int value) {68this.value = value;69}7071@Override72public boolean equals(Object object) {73if (object instanceof TestEquals) {74this.count++;75TestEquals that = (TestEquals)object;76return that.value == this.value;77}78return false;79}8081public void propertyChange(PropertyChangeEvent event) {82}83}848586