Path: blob/master/test/jdk/java/beans/Introspector/4058433/TestBeanInfoPriority.java
41152 views
/*1* Copyright (c) 2015, 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*/222324import java.awt.event.ActionListener;25import java.awt.event.MouseListener;26import java.beans.BeanDescriptor;27import java.beans.BeanInfo;28import java.beans.BeanProperty;29import java.beans.EventSetDescriptor;30import java.beans.IntrospectionException;31import java.beans.Introspector;32import java.beans.JavaBean;33import java.beans.MethodDescriptor;34import java.beans.PropertyDescriptor;35import java.beans.SimpleBeanInfo;36import javax.swing.SwingContainer;37import java.util.Arrays;3839/**40* @test41* @bug 4058433 813105542* @summary Check if the user-defined bean info43* is not overridden with the annotated one.44* @author a.stepanov45*/464748public class TestBeanInfoPriority {4950// ========== test bean (annotations must be ignored!) ==========5152@JavaBean(53description = "annotation-description",54defaultProperty = "other",55defaultEventSet = "mouse")56@SwingContainer(value = false)57public static class TestClass {5859private int value;60private double other;6162@BeanProperty(63bound = false,64expert = false,65hidden = false,66preferred = false,67required = false,68visualUpdate = false,69description = "annotation-value",70enumerationValues = {71"javax.swing.SwingConstants.NORTH"}72)73public void setValue(int v) { value = v; }74public int getValue() { return value; }757677@BeanProperty(78bound = true,79expert = true,80hidden = true,81preferred = true,82required = true,83visualUpdate = true,84description = "annotation-other",85enumerationValues = {86"javax.swing.SwingConstants.LEFT",87"javax.swing.SwingConstants.RIGHT",88"javax.swing.SwingConstants.CENTER"}89)90public void setOther(double o) { other = o; }91public double getOther() { return other; }9293public void addActionListener(ActionListener l) {}94public void removeActionListener(ActionListener l) {}9596public void addMouseListener(MouseListener l) {}97public void removeMouseListener(MouseListener l) {}98}99100// ========== user-defined bean info ==========101102public static class TestClassBeanInfo extends SimpleBeanInfo {103104private static final int iOther = 0;105private static final int iValue = 1;106107private static final int iAction = 0;108private static final int iMouse = 1;109110111@Override112public BeanDescriptor getBeanDescriptor() {113114BeanDescriptor bd = new BeanDescriptor(TestClass.class, null);115bd.setShortDescription("user-defined-description");116bd.setValue("isContainer", true);117bd.setValue("containerDelegate", "user-defined-delegate");118119return bd;120}121122@Override123public PropertyDescriptor[] getPropertyDescriptors() {124125PropertyDescriptor[] p = new PropertyDescriptor[2];126127try {128129// value130PropertyDescriptor pdValue = new PropertyDescriptor(131"value", TestClass.class, "getValue", "setValue");132pdValue.setBound(true);133pdValue.setConstrained(true);134pdValue.setExpert(true);135pdValue.setHidden(true);136pdValue.setPreferred(true);137pdValue.setValue("required", true);138pdValue.setValue("visualUpdate", true);139pdValue.setShortDescription("user-defined-value");140pdValue.setValue("enumerationValues", new Object[]{141"EAST", 3, "javax.swing.SwingConstants.EAST",142"WEST", 7, "javax.swing.SwingConstants.WEST"});143p[iValue] = pdValue;144145// other146PropertyDescriptor pdOther = new PropertyDescriptor(147"other", TestClass.class, "getOther", "setOther");148pdOther.setBound(false);149pdOther.setConstrained(false);150pdOther.setExpert(false);151pdOther.setHidden(false);152pdOther.setPreferred(false);153pdOther.setValue("required", false);154pdOther.setValue("visualUpdate", false);155pdOther.setShortDescription("user-defined-other");156pdOther.setValue("enumerationValues", new Object[]{157"TOP", 1, "javax.swing.SwingConstants.TOP"});158p[iOther] = pdOther;159160} catch(IntrospectionException e) {161e.printStackTrace();162}163164return p;165}166167@Override168public EventSetDescriptor[] getEventSetDescriptors() {169EventSetDescriptor[] es = new EventSetDescriptor[2];170try {171es[iAction] = new EventSetDescriptor(172TestClass.class,173"actionListener",174java.awt.event.ActionListener.class,175new String[] {"actionPerformed"},176"addActionListener",177"removeActionListener");178es[iMouse] = new EventSetDescriptor(179TestClass.class,180"mouseListener",181java.awt.event.MouseListener.class,182new String[] {"mouseClicked", "mousePressed", "mouseReleased", "mouseEntered", "mouseExited"},183"addMouseListener",184"removeMouseListener");185} catch(IntrospectionException e) {186e.printStackTrace();187}188return es;189}190191@Override192public MethodDescriptor[] getMethodDescriptors() {193MethodDescriptor[] m = new MethodDescriptor[0];194return m;195}196197@Override198public int getDefaultPropertyIndex() { return iValue; } // default: value199200@Override201public int getDefaultEventIndex() { return iAction; } // default: action202203@Override204public java.awt.Image getIcon(int iconKind) { return null; }205}206207// ========== auxiliary functions ==========208209static void checkEq(String what, Object v, Object ref) throws Exception {210211if ((v != null) && v.equals(ref)) {212System.out.println(what + ": ok (" + ref.toString() + ")");213} else {214throw new Exception(215"invalid " + what + ", expected: \"" + ref + "\", got: \"" + v + "\"");216}217}218219static void checkEnumEq(String what, Object v, Object ref[]) throws Exception {220221what = "\"" + what + "\"";222if (v == null) {223throw new Exception("null " + what + " enumeration values");224}225226String msg = "invalid " + what + " enumeration values";227if (!(v instanceof Object[])) { throw new Exception(msg); }228229if (Arrays.equals((Object []) v, ref)) {230System.out.println(what + " enumeration values: ok");231} else { throw new Exception(msg); }232}233234235// ========== test ==========236237238public static void main(String[] args) throws Exception {239240BeanInfo i = Introspector.getBeanInfo(TestClass.class, Object.class);241BeanDescriptor bd = i.getBeanDescriptor();242243checkEq("description", bd.getShortDescription(), "user-defined-description");244checkEq("default property index", i.getDefaultPropertyIndex(), 1);245checkEq("default event index", i.getDefaultEventIndex(), 0);246247checkEq("isContainer", i.getBeanDescriptor().getValue("isContainer"), true);248checkEq("containerDelegate",249i.getBeanDescriptor().getValue("containerDelegate"), "user-defined-delegate");250System.out.println("");251252PropertyDescriptor[] pds = i.getPropertyDescriptors();253for (PropertyDescriptor pd: pds) {254String name = pd.getName();255switch (name) {256case "value":257checkEq("\"value\" isBound", pd.isBound(), true);258checkEq("\"value\" isConstrained", pd.isConstrained(), true);259checkEq("\"value\" isExpert", pd.isExpert(), true);260checkEq("\"value\" isHidden", pd.isHidden(), true);261checkEq("\"value\" isPreferred", pd.isPreferred(), true);262checkEq("\"value\" required", pd.getValue("required"), true);263checkEq("\"value\" visualUpdate", pd.getValue("visualUpdate"), true);264265checkEq("\"value\" description", pd.getShortDescription(), "user-defined-value");266267checkEnumEq(pd.getName(), pd.getValue("enumerationValues"),268new Object[]{269"EAST", 3, "javax.swing.SwingConstants.EAST",270"WEST", 7, "javax.swing.SwingConstants.WEST"});271System.out.println("");272break;273case "other":274checkEq("\"other\" isBound", pd.isBound(), false);275checkEq("\"other\" isConstrained", pd.isConstrained(), false);276checkEq("\"other\" isExpert", pd.isExpert(), false);277checkEq("\"other\" isHidden", pd.isHidden(), false);278checkEq("\"other\" isPreferred", pd.isPreferred(), false);279checkEq("\"other\" required", pd.getValue("required"), false);280checkEq("\"other\" visualUpdate", pd.getValue("visualUpdate"), false);281282checkEq("\"other\" description", pd.getShortDescription(), "user-defined-other");283284checkEnumEq(pd.getName(), pd.getValue("enumerationValues"),285new Object[]{"TOP", 1, "javax.swing.SwingConstants.TOP"});286System.out.println("");287break;288default:289throw new Exception("invalid property descriptor: " + name);290}291}292}293}294295296