Path: blob/master/test/jdk/java/beans/Introspector/Test4634390.java
41149 views
/*1* Copyright (c) 2003, 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 463439026* @summary Tests contract for equals and hashCode methods27* @author Mark Davidson28*/2930import java.beans.IndexedPropertyDescriptor;31import java.beans.IntrospectionException;32import java.beans.PropertyDescriptor;33import java.lang.reflect.Method;34import javax.swing.*;3536public class Test4634390 {3738private static final Class[] CLASSES = {39JButton.class,40JDialog.class,41JMenu.class,42JPanel.class,43JProgressBar.class,44JSlider.class,45JTable.class,46JTree.class,47JComboBox.class,48JToolBar.class,49JTabbedPane.class,50};5152public static void main(String[] args) {53for (Class type : CLASSES) {54test(type);55}56}5758private static void test(Class type) {59for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(type)) {60PropertyDescriptor pdCopy = create(pd);61if (pdCopy != null) {62// XXX - hack! The Introspector will set the bound property63// since it assumes that propertyChange event set descriptors64// infers that all the properties are bound65pdCopy.setBound(pd.isBound());6667String name = pd.getName();68System.out.println(" - " + name);6970if (!compare(pd, pdCopy))71throw new Error("property delegates are not equal");7273if (!pd.equals(pdCopy))74throw new Error("equals() failed");7576if (pd.hashCode() != pdCopy.hashCode())77throw new Error("hashCode() failed");78}79}80}8182private static PropertyDescriptor create(PropertyDescriptor pd) {83try {84if (pd instanceof IndexedPropertyDescriptor) {85IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;86return new IndexedPropertyDescriptor(87ipd.getName(),88ipd.getReadMethod(),89ipd.getWriteMethod(),90ipd.getIndexedReadMethod(),91ipd.getIndexedWriteMethod());92} else {93return new PropertyDescriptor(94pd.getName(),95pd.getReadMethod(),96pd.getWriteMethod());97}98}99catch (IntrospectionException exception) {100exception.printStackTrace();101return null;102}103}104105private static boolean compare(PropertyDescriptor pd1, PropertyDescriptor pd2) {106if (!compare(pd1.getReadMethod(), pd2.getReadMethod())) {107System.out.println("read methods not equal");108return false;109}110if (!compare(pd1.getWriteMethod(), pd2.getWriteMethod())) {111System.out.println("write methods not equal");112return false;113}114if (pd1.getPropertyType() != pd2.getPropertyType()) {115System.out.println("property type not equal");116return false;117}118if (pd1.getPropertyEditorClass() != pd2.getPropertyEditorClass()) {119System.out.println("property editor class not equal");120return false;121}122if (pd1.isBound() != pd2.isBound()) {123System.out.println("bound value not equal");124return false;125}126if (pd1.isConstrained() != pd2.isConstrained()) {127System.out.println("constrained value not equal");128return false;129}130return true;131}132133private static boolean compare(Method m1, Method m2) {134if ((m1 == null) && (m2 == null)) {135return true;136}137if ((m1 == null) || (m2 == null)) {138return false;139}140return m1.equals(m2);141}142}143144145