0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 5004188
0N/A * @summary Tests how often method equals() is called
0N/A * @author Sergey Malenkov
0N/A */
0N/A
0N/Aimport java.beans.PropertyChangeEvent;
0N/Aimport java.beans.PropertyVetoException;
0N/Aimport java.beans.VetoableChangeListener;
0N/Aimport java.beans.VetoableChangeSupport;
0N/A
0N/Apublic final class TestEquals implements VetoableChangeListener {
0N/A private static final String PROPERTY = "property";
0N/A
0N/A public static void main(String[] args) throws PropertyVetoException {
0N/A TestEquals one = new TestEquals(1);
0N/A TestEquals two = new TestEquals(2);
0N/A
0N/A Object source = TestEquals.class;
0N/A VetoableChangeSupport vcs = new VetoableChangeSupport(source);
0N/A vcs.addVetoableChangeListener(PROPERTY, one);
0N/A vcs.addVetoableChangeListener(PROPERTY, two);
0N/A
0N/A PropertyChangeEvent event = new PropertyChangeEvent(source, PROPERTY, one, two);
0N/A vcs.fireVetoableChange(event);
0N/A test(one, two, 1); // only one check
0N/A vcs.fireVetoableChange(PROPERTY, one, two);
0N/A test(one, two, 2); // because it invokes fireVetoableChange(PropertyChangeEvent)
0N/A }
0N/A
0N/A private static void test(TestEquals v1, TestEquals v2, int amount) {
0N/A int count = v1.count + v2.count;
0N/A if (amount < count)
0N/A throw new Error("method equals() is called " + count + " times");
0N/A
0N/A v1.count = 0;
0N/A v2.count = 0;
0N/A }
0N/A
0N/A private final int value;
0N/A private int count;
0N/A
0N/A private TestEquals(int value) {
0N/A this.value = value;
0N/A }
0N/A
0N/A @Override
0N/A public boolean equals(Object object) {
0N/A if (object instanceof TestEquals) {
0N/A this.count++;
0N/A TestEquals that = (TestEquals)object;
0N/A return that.value == this.value;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A public void vetoableChange(PropertyChangeEvent event) {
0N/A }
0N/A}