2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4634390
|
|
27 |
* @summary Tests contract for equals and hashCode methods
|
|
28 |
* @author Mark Davidson
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.beans.IndexedPropertyDescriptor;
|
|
32 |
import java.beans.IntrospectionException;
|
|
33 |
import java.beans.PropertyDescriptor;
|
|
34 |
import java.lang.reflect.Method;
|
|
35 |
import javax.swing.*;
|
|
36 |
|
|
37 |
public class Test4634390 {
|
|
38 |
|
|
39 |
private static final Class[] CLASSES = {
|
|
40 |
JButton.class,
|
|
41 |
JDialog.class,
|
|
42 |
JMenu.class,
|
|
43 |
JPanel.class,
|
|
44 |
JProgressBar.class,
|
|
45 |
JSlider.class,
|
|
46 |
JTable.class,
|
|
47 |
JTree.class,
|
|
48 |
JComboBox.class,
|
|
49 |
JToolBar.class,
|
|
50 |
JTabbedPane.class,
|
|
51 |
};
|
|
52 |
|
|
53 |
public static void main(String[] args) {
|
|
54 |
for (Class type : CLASSES) {
|
|
55 |
test(type);
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
private static void test(Class type) {
|
|
60 |
for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(type)) {
|
|
61 |
PropertyDescriptor pdCopy = create(pd);
|
|
62 |
if (pdCopy != null) {
|
|
63 |
// XXX - hack! The Introspector will set the bound property
|
|
64 |
// since it assumes that propertyChange event set descriptors
|
|
65 |
// infers that all the properties are bound
|
|
66 |
pdCopy.setBound(pd.isBound());
|
|
67 |
|
|
68 |
String name = pd.getName();
|
|
69 |
System.out.println(" - " + name);
|
|
70 |
|
|
71 |
if (!compare(pd, pdCopy))
|
|
72 |
throw new Error("property delegates are not equal");
|
|
73 |
|
|
74 |
if (!pd.equals(pdCopy))
|
|
75 |
throw new Error("equals() failed");
|
|
76 |
|
|
77 |
if (pd.hashCode() != pdCopy.hashCode())
|
|
78 |
throw new Error("hashCode() failed");
|
|
79 |
}
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
private static PropertyDescriptor create(PropertyDescriptor pd) {
|
|
84 |
try {
|
|
85 |
if (pd instanceof IndexedPropertyDescriptor) {
|
|
86 |
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
|
|
87 |
return new IndexedPropertyDescriptor(
|
|
88 |
ipd.getName(),
|
|
89 |
ipd.getReadMethod(),
|
|
90 |
ipd.getWriteMethod(),
|
|
91 |
ipd.getIndexedReadMethod(),
|
|
92 |
ipd.getIndexedWriteMethod());
|
|
93 |
} else {
|
|
94 |
return new PropertyDescriptor(
|
|
95 |
pd.getName(),
|
|
96 |
pd.getReadMethod(),
|
|
97 |
pd.getWriteMethod());
|
|
98 |
}
|
|
99 |
}
|
|
100 |
catch (IntrospectionException exception) {
|
|
101 |
exception.printStackTrace();
|
|
102 |
return null;
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
private static boolean compare(PropertyDescriptor pd1, PropertyDescriptor pd2) {
|
|
107 |
if (!compare(pd1.getReadMethod(), pd2.getReadMethod())) {
|
|
108 |
System.out.println("read methods not equal");
|
|
109 |
return false;
|
|
110 |
}
|
|
111 |
if (!compare(pd1.getWriteMethod(), pd2.getWriteMethod())) {
|
|
112 |
System.out.println("write methods not equal");
|
|
113 |
return false;
|
|
114 |
}
|
|
115 |
if (pd1.getPropertyType() != pd2.getPropertyType()) {
|
|
116 |
System.out.println("property type not equal");
|
|
117 |
return false;
|
|
118 |
}
|
|
119 |
if (pd1.getPropertyEditorClass() != pd2.getPropertyEditorClass()) {
|
|
120 |
System.out.println("property editor class not equal");
|
|
121 |
return false;
|
|
122 |
}
|
|
123 |
if (pd1.isBound() != pd2.isBound()) {
|
|
124 |
System.out.println("bound value not equal");
|
|
125 |
return false;
|
|
126 |
}
|
|
127 |
if (pd1.isConstrained() != pd2.isConstrained()) {
|
|
128 |
System.out.println("constrained value not equal");
|
|
129 |
return false;
|
|
130 |
}
|
|
131 |
return true;
|
|
132 |
}
|
|
133 |
|
|
134 |
private static boolean compare(Method m1, Method m2) {
|
|
135 |
if ((m1 == null) && (m2 == null)) {
|
|
136 |
return true;
|
|
137 |
}
|
|
138 |
if ((m1 == null) || (m2 == null)) {
|
|
139 |
return false;
|
|
140 |
}
|
|
141 |
return m1.equals(m2);
|
|
142 |
}
|
|
143 |
}
|