jdk/test/java/beans/Introspector/Test4634390.java
author lana
Tue, 23 May 2017 21:11:42 +0000
changeset 45187 331a6542b5b8
parent 5506 202f599c92aa
permissions -rw-r--r--
Added tag jdk-10+7 for changeset d554736d963e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4634390
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests contract for equals and hashCode methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Mark Davidson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.IndexedPropertyDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.IntrospectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.beans.PropertyDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Test4634390 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final Class[] CLASSES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            JButton.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            JDialog.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            JMenu.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            JPanel.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            JProgressBar.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            JSlider.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            JTable.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            JTree.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            JComboBox.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            JToolBar.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            JTabbedPane.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        for (Class type : CLASSES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            test(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static void test(Class type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            PropertyDescriptor pdCopy = create(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (pdCopy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                // XXX - hack! The Introspector will set the bound property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                // since it assumes that propertyChange event set descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                // infers that all the properties are bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                pdCopy.setBound(pd.isBound());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                String name = pd.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                System.out.println(" - " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (!compare(pd, pdCopy))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    throw new Error("property delegates are not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                if (!pd.equals(pdCopy))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    throw new Error("equals() failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                if (pd.hashCode() != pdCopy.hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    throw new Error("hashCode() failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static PropertyDescriptor create(PropertyDescriptor pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (pd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                return new IndexedPropertyDescriptor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        ipd.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        ipd.getReadMethod(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        ipd.getWriteMethod(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        ipd.getIndexedReadMethod(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        ipd.getIndexedWriteMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                return new PropertyDescriptor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        pd.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        pd.getReadMethod(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        pd.getWriteMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        catch (IntrospectionException exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            exception.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static boolean compare(PropertyDescriptor pd1, PropertyDescriptor pd2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (!compare(pd1.getReadMethod(), pd2.getReadMethod())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.out.println("read methods not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!compare(pd1.getWriteMethod(), pd2.getWriteMethod())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println("write methods not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (pd1.getPropertyType() != pd2.getPropertyType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println("property type not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (pd1.getPropertyEditorClass() != pd2.getPropertyEditorClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println("property editor class not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (pd1.isBound() != pd2.isBound()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.out.println("bound value not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (pd1.isConstrained() != pd2.isConstrained()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            System.out.println("constrained value not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static boolean compare(Method m1, Method m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if ((m1 == null) && (m2 == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if ((m1 == null) || (m2 == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return m1.equals(m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}