1 /*
2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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  *
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.
22  */
23 
24 
25 import java.awt.event.ActionListener;
26 import java.awt.event.MouseListener;
27 import java.beans.BeanDescriptor;
28 import java.beans.BeanInfo;
29 import java.beans.BeanProperty;
30 import java.beans.EventSetDescriptor;
31 import java.beans.IntrospectionException;
32 import java.beans.Introspector;
33 import java.beans.JavaBean;
34 import java.beans.MethodDescriptor;
35 import java.beans.PropertyDescriptor;
36 import java.beans.SimpleBeanInfo;
37 import javax.swing.SwingContainer;
38 import java.util.Arrays;
39 
40 /**
41  * @test
42  * @bug 4058433 8131055
43  * @summary Check if the user-defined bean info
44  *          is not overridden with the annotated one.
45  * @author a.stepanov
46  */
47 
48 
49 public class TestBeanInfoPriority {
50 
51     // ========== test bean (annotations must be ignored!) ==========
52 
53     @JavaBean(
54             description = "annotation-description",
55             defaultProperty = "other",
56             defaultEventSet = "mouse")
57     @SwingContainer(value = false)
58     public static class TestClass {
59 
60         private int    value;
61         private double other;
62 
63         @BeanProperty(
64                 bound     = false,
65                 expert    = false,
66                 hidden    = false,
67                 preferred = false,
68                 required  = false,
69                 visualUpdate = false,
70                 description = "annotation-value",
71                 enumerationValues = {
72                     "javax.swing.SwingConstants.NORTH"}
73                 )
setValue(int v)74         public void setValue(int v) { value = v; }
getValue()75         public  int getValue()      { return value; }
76 
77 
78         @BeanProperty(
79             bound     = true,
80             expert    = true,
81             hidden    = true,
82             preferred = true,
83             required  = true,
84             visualUpdate = true,
85             description = "annotation-other",
86             enumerationValues = {
87                 "javax.swing.SwingConstants.LEFT",
88                 "javax.swing.SwingConstants.RIGHT",
89                 "javax.swing.SwingConstants.CENTER"}
90             )
setOther(double o)91         public   void setOther(double o) { other = o; }
getOther()92         public double getOther()         { return other; }
93 
addActionListener(ActionListener l)94         public void addActionListener(ActionListener l) {}
removeActionListener(ActionListener l)95         public void removeActionListener(ActionListener l) {}
96 
addMouseListener(MouseListener l)97         public void addMouseListener(MouseListener l) {}
removeMouseListener(MouseListener l)98         public void removeMouseListener(MouseListener l) {}
99     }
100 
101     // ========== user-defined bean info ==========
102 
103     public static class TestClassBeanInfo extends SimpleBeanInfo {
104 
105         private static final int iOther = 0;
106         private static final int iValue = 1;
107 
108         private static final int iAction = 0;
109         private static final int iMouse  = 1;
110 
111 
112         @Override
getBeanDescriptor()113         public BeanDescriptor getBeanDescriptor() {
114 
115             BeanDescriptor bd = new BeanDescriptor(TestClass.class, null);
116             bd.setShortDescription("user-defined-description");
117             bd.setValue("isContainer", true);
118             bd.setValue("containerDelegate", "user-defined-delegate");
119 
120             return bd;
121         }
122 
123         @Override
getPropertyDescriptors()124         public PropertyDescriptor[] getPropertyDescriptors() {
125 
126             PropertyDescriptor[] p = new PropertyDescriptor[2];
127 
128             try {
129 
130                 // value
131                 PropertyDescriptor pdValue = new PropertyDescriptor(
132                     "value", TestClass.class, "getValue", "setValue");
133                 pdValue.setBound(true);
134                 pdValue.setConstrained(true);
135                 pdValue.setExpert(true);
136                 pdValue.setHidden(true);
137                 pdValue.setPreferred(true);
138                 pdValue.setValue("required", true);
139                 pdValue.setValue("visualUpdate", true);
140                 pdValue.setShortDescription("user-defined-value");
141                 pdValue.setValue("enumerationValues", new Object[]{
142                         "EAST", 3, "javax.swing.SwingConstants.EAST",
143                         "WEST", 7, "javax.swing.SwingConstants.WEST"});
144                 p[iValue] = pdValue;
145 
146                 // other
147                 PropertyDescriptor pdOther = new PropertyDescriptor(
148                         "other", TestClass.class, "getOther", "setOther");
149                 pdOther.setBound(false);
150                 pdOther.setConstrained(false);
151                 pdOther.setExpert(false);
152                 pdOther.setHidden(false);
153                 pdOther.setPreferred(false);
154                 pdOther.setValue("required", false);
155                 pdOther.setValue("visualUpdate", false);
156                 pdOther.setShortDescription("user-defined-other");
157                 pdOther.setValue("enumerationValues", new Object[]{
158                         "TOP", 1, "javax.swing.SwingConstants.TOP"});
159                 p[iOther] = pdOther;
160 
161             } catch(IntrospectionException e) {
162                 e.printStackTrace();
163             }
164 
165             return p;
166         }
167 
168         @Override
getEventSetDescriptors()169         public EventSetDescriptor[] getEventSetDescriptors() {
170             EventSetDescriptor[] es = new EventSetDescriptor[2];
171             try {
172                 es[iAction] = new EventSetDescriptor(
173                         TestClass.class,
174                         "actionListener",
175                         java.awt.event.ActionListener.class,
176                         new String[] {"actionPerformed"},
177                         "addActionListener",
178                         "removeActionListener");
179                 es[iMouse] = new EventSetDescriptor(
180                         TestClass.class,
181                         "mouseListener",
182                         java.awt.event.MouseListener.class,
183                         new String[] {"mouseClicked", "mousePressed", "mouseReleased", "mouseEntered", "mouseExited"},
184                         "addMouseListener",
185                         "removeMouseListener");
186             } catch(IntrospectionException e) {
187                 e.printStackTrace();
188             }
189             return es;
190         }
191 
192         @Override
getMethodDescriptors()193         public MethodDescriptor[] getMethodDescriptors() {
194             MethodDescriptor[] m = new MethodDescriptor[0];
195             return m;
196         }
197 
198         @Override
getDefaultPropertyIndex()199         public int getDefaultPropertyIndex() { return iValue; } // default: value
200 
201         @Override
getDefaultEventIndex()202         public int getDefaultEventIndex() { return iAction; } // default: action
203 
204         @Override
getIcon(int iconKind)205         public java.awt.Image getIcon(int iconKind) { return null; }
206     }
207 
208     // ========== auxiliary functions ==========
209 
checkEq(String what, Object v, Object ref)210     static void checkEq(String what, Object v, Object ref) throws Exception {
211 
212         if ((v != null) && v.equals(ref)) {
213             System.out.println(what + ": ok (" + ref.toString() + ")");
214         } else {
215             throw new Exception(
216                 "invalid " + what + ", expected: \"" + ref + "\", got: \"" + v + "\"");
217         }
218     }
219 
checkEnumEq(String what, Object v, Object ref[])220     static void checkEnumEq(String what, Object v, Object ref[]) throws Exception {
221 
222         what = "\"" + what + "\"";
223         if (v == null) {
224             throw new Exception("null " + what + " enumeration values");
225         }
226 
227         String msg = "invalid " + what + " enumeration values";
228         if (!(v instanceof Object[])) { throw new Exception(msg); }
229 
230         if (Arrays.equals((Object []) v, ref)) {
231             System.out.println(what + " enumeration values: ok");
232         } else { throw new Exception(msg); }
233     }
234 
235 
236     // ========== test ==========
237 
238 
main(String[] args)239     public static void main(String[] args) throws Exception {
240 
241         BeanInfo i = Introspector.getBeanInfo(TestClass.class, Object.class);
242         BeanDescriptor bd = i.getBeanDescriptor();
243 
244         checkEq("description", bd.getShortDescription(), "user-defined-description");
245         checkEq("default property index", i.getDefaultPropertyIndex(), 1);
246         checkEq("default event index", i.getDefaultEventIndex(), 0);
247 
248         checkEq("isContainer", i.getBeanDescriptor().getValue("isContainer"), true);
249         checkEq("containerDelegate",
250             i.getBeanDescriptor().getValue("containerDelegate"), "user-defined-delegate");
251         System.out.println("");
252 
253         PropertyDescriptor[] pds = i.getPropertyDescriptors();
254         for (PropertyDescriptor pd: pds) {
255             String name = pd.getName();
256             switch (name) {
257                 case "value":
258                     checkEq("\"value\" isBound",       pd.isBound(),       true);
259                     checkEq("\"value\" isConstrained", pd.isConstrained(), true);
260                     checkEq("\"value\" isExpert",      pd.isExpert(),      true);
261                     checkEq("\"value\" isHidden",      pd.isHidden(),      true);
262                     checkEq("\"value\" isPreferred",   pd.isPreferred(),   true);
263                     checkEq("\"value\" required",      pd.getValue("required"),     true);
264                     checkEq("\"value\" visualUpdate",  pd.getValue("visualUpdate"), true);
265 
266                     checkEq("\"value\" description",   pd.getShortDescription(), "user-defined-value");
267 
268                     checkEnumEq(pd.getName(), pd.getValue("enumerationValues"),
269                         new Object[]{
270                         "EAST", 3, "javax.swing.SwingConstants.EAST",
271                         "WEST", 7, "javax.swing.SwingConstants.WEST"});
272                     System.out.println("");
273                     break;
274                 case "other":
275                     checkEq("\"other\" isBound",       pd.isBound(),       false);
276                     checkEq("\"other\" isConstrained", pd.isConstrained(), false);
277                     checkEq("\"other\" isExpert",      pd.isExpert(),      false);
278                     checkEq("\"other\" isHidden",      pd.isHidden(),      false);
279                     checkEq("\"other\" isPreferred",   pd.isPreferred(),   false);
280                     checkEq("\"other\" required",      pd.getValue("required"),     false);
281                     checkEq("\"other\" visualUpdate",  pd.getValue("visualUpdate"), false);
282 
283                     checkEq("\"other\" description",   pd.getShortDescription(), "user-defined-other");
284 
285                     checkEnumEq(pd.getName(), pd.getValue("enumerationValues"),
286                         new Object[]{"TOP", 1, "javax.swing.SwingConstants.TOP"});
287                     System.out.println("");
288                     break;
289                 default:
290                     throw new Exception("invalid property descriptor: " + name);
291             }
292         }
293     }
294 }
295