1/* 2 * Copyright (c) 2010, 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 * @test 26 * @bug 4498236 27 * @summary Tests toString methods 28 * @author Sergey Malenkov 29 */ 30 31import java.awt.event.KeyEvent; 32import java.awt.event.KeyListener; 33import java.beans.BeanDescriptor; 34import java.beans.EventSetDescriptor; 35import java.beans.FeatureDescriptor; 36import java.beans.IndexedPropertyChangeEvent; 37import java.beans.IndexedPropertyDescriptor; 38import java.beans.MethodDescriptor; 39import java.beans.ParameterDescriptor; 40import java.beans.PropertyChangeEvent; 41import java.beans.PropertyDescriptor; 42import java.lang.reflect.Method; 43 44public class Test4498236 { 45 46 public static void main(String[] args) throws Exception { 47 PropertyChangeEvent event = new PropertyChangeEvent("source", null, null, null); 48 event.setPropagationId("id"); 49 test("[propertyName=null; oldValue=null; newValue=null; propagationId=id; source=source]", event); 50 test("[propertyName=name; oldValue=old; newValue=new; propagationId=null; source=source]", 51 new PropertyChangeEvent("source", "name", "old", "new") 52 ); 53 test("[propertyName=array; index=5; oldValue=old; newValue=new; propagationId=null; source=source]", 54 new IndexedPropertyChangeEvent("source", "array", "old", "new", 5) 55 ); 56 FeatureDescriptor fd = new FeatureDescriptor(); 57 fd.setName("n"); 58 fd.setDisplayName("dn"); 59 fd.setShortDescription("sd"); 60 fd.setPreferred(true); 61 fd.setHidden(true); 62 fd.setExpert(true); 63 fd.setValue("first", "value"); 64 test("[name=n; displayName=dn; shortDescription=sd; preferred; hidden; expert; values={first=value}]", fd); 65 test("[name=String; beanClass=class java.lang.String]", 66 new BeanDescriptor(String.class) 67 ); 68 test("[name=Object; beanClass=class java.lang.Object; customizerClass=class java.lang.String]", 69 new BeanDescriptor(Object.class, String.class) 70 ); 71 test("[name=Object; beanClass=class java.lang.Object; customizerClass=class java.lang.String]", 72 new BeanDescriptor(Object.class, String.class) 73 ); 74 test("[name=equals; method=public boolean java.lang.Object.equals(java.lang.Object)]", 75 new MethodDescriptor(Object.class.getMethod("equals", Object.class)) 76 ); 77 test("[name=equals; method=public boolean java.lang.Object.equals(java.lang.Object); parameterDescriptors={java.beans.ParameterDescriptor[name=null]}]", 78 new MethodDescriptor(Object.class.getMethod("equals", Object.class), new ParameterDescriptor[] { 79 new ParameterDescriptor() 80 }) 81 ); 82 Class type = KeyListener.class; 83 String[] names = { "keyTyped", "keyPressed", "keyReleased" }; 84 Method[] methods = new Method[names.length]; 85 for (int i = 0; i < names.length; i++) { 86 methods[i] = type.getMethod(names[i], KeyEvent.class); 87 } 88 test("[name=key; inDefaultEventSet; listenerType=interface java.awt.event.KeyListener; getListenerMethod=public java.awt.event.KeyListener Test4498236.getKeyListeners(); addListenerMethod=public void Test4498236.addKeyListener(java.awt.event.KeyListener); removeListenerMethod=public void Test4498236.removeKeyListener(java.awt.event.KeyListener)]", 89 new EventSetDescriptor(Test4498236.class, "key", type, names[0]) 90 ); 91 test("[name=$$$; inDefaultEventSet; listenerType=interface java.awt.event.KeyListener; addListenerMethod=public void Test4498236.add(java.awt.event.KeyListener); removeListenerMethod=public void Test4498236.remove(java.awt.event.KeyListener)]", 92 new EventSetDescriptor(Test4498236.class, "$$$", type, names, "add", "remove") 93 ); 94 test("[name=$$$; inDefaultEventSet; listenerType=interface java.awt.event.KeyListener; getListenerMethod=public java.awt.event.KeyListener Test4498236.get(); addListenerMethod=public void Test4498236.add(java.awt.event.KeyListener); removeListenerMethod=public void Test4498236.remove(java.awt.event.KeyListener)]", 95 new EventSetDescriptor(Test4498236.class, "$$$", type, names, "add", "remove", "get") 96 ); 97 test("[name=$$$; inDefaultEventSet; listenerType=interface java.awt.event.KeyListener; addListenerMethod=public void Test4498236.add(java.awt.event.KeyListener); removeListenerMethod=public void Test4498236.remove(java.awt.event.KeyListener)]", 98 new EventSetDescriptor("$$$", type, methods, Test4498236.class.getMethod("add", type), Test4498236.class.getMethod("remove", type)) 99 ); 100 test("[name=$$$; inDefaultEventSet; listenerType=interface java.awt.event.KeyListener; getListenerMethod=public java.awt.event.KeyListener Test4498236.get(); addListenerMethod=public void Test4498236.add(java.awt.event.KeyListener); removeListenerMethod=public void Test4498236.remove(java.awt.event.KeyListener)]", 101 new EventSetDescriptor("$$$", type, methods, Test4498236.class.getMethod("add", type), Test4498236.class.getMethod("remove", type), Test4498236.class.getMethod("get")) 102 ); 103 test("[name=value; propertyType=boolean; readMethod=public boolean Test4498236.isValue(); writeMethod=public void Test4498236.setValue(boolean)]", 104 new PropertyDescriptor("value", Test4498236.class) 105 ); 106 test("[name=$$$]", 107 new PropertyDescriptor("$$$", Test4498236.class, null, null) 108 ); 109 test("[name=$$$; propertyType=boolean; readMethod=public boolean Test4498236.getValue()]", 110 new PropertyDescriptor("$$$", Test4498236.class, "getValue", null) 111 ); 112 test("[name=$$$; propertyType=boolean; readMethod=public boolean Test4498236.getValue(); writeMethod=public void Test4498236.setValue(boolean)]", 113 new PropertyDescriptor("$$$", Test4498236.class, "getValue", "setValue") 114 ); 115 test("[name=$$$]", 116 new PropertyDescriptor("$$$", null, null) 117 ); 118 test("[name=$$$; propertyType=boolean; readMethod=public boolean Test4498236.getValue()]", 119 new PropertyDescriptor("$$$", Test4498236.class.getMethod("getValue"), null) 120 ); 121 test("[name=$$$; propertyType=boolean; readMethod=public boolean Test4498236.getValue(); writeMethod=public void Test4498236.setValue(boolean)]", 122 new PropertyDescriptor("$$$", Test4498236.class.getMethod("getValue"), Test4498236.class.getMethod("setValue", boolean.class)) 123 ); 124 test("[name=index; propertyType=class [I; readMethod=public int[] Test4498236.getIndex(); writeMethod=public void Test4498236.setIndex(int[]); indexedPropertyType=int; indexedReadMethod=public int Test4498236.getIndex(int); indexedWriteMethod=public void Test4498236.setIndex(int,int)]", 125 new IndexedPropertyDescriptor("index", Test4498236.class) 126 ); 127 test("[name=$$$; propertyType=class [I; readMethod=public int[] Test4498236.getIndex(); writeMethod=public void Test4498236.setIndex(int[]); indexedPropertyType=int; indexedReadMethod=public int Test4498236.getIndex(int); indexedWriteMethod=public void Test4498236.setIndex(int,int)]", 128 new IndexedPropertyDescriptor("$$$", Test4498236.class, "getIndex", "setIndex", "getIndex", "setIndex") 129 ); 130 test("[name=$$$; propertyType=class [I; readMethod=public int[] Test4498236.getIndex(); writeMethod=public void Test4498236.setIndex(int[]); indexedPropertyType=int; indexedReadMethod=public int Test4498236.getIndex(int); indexedWriteMethod=public void Test4498236.setIndex(int,int)]", 131 new IndexedPropertyDescriptor("$$$", Test4498236.class.getMethod("getIndex"), Test4498236.class.getMethod("setIndex", new int[0].getClass()), Test4498236.class.getMethod("getIndex", int.class), Test4498236.class.getMethod("setIndex", int.class, int.class) ) 132 ); 133 } 134 135 public void addKeyListener(KeyListener listener) { 136 add(listener); 137 } 138 139 public void removeKeyListener(KeyListener listener) { 140 remove(listener); 141 } 142 143 public KeyListener getKeyListeners() { 144 return null; 145 } 146 147 public void add(KeyListener listener) { 148 } 149 150 public void remove(KeyListener listener) { 151 } 152 153 public KeyListener get() { 154 return null; 155 } 156 157 public boolean isValue() { 158 return true; 159 } 160 161 public boolean getValue() { 162 return true; 163 } 164 165 public void setValue(boolean value) { 166 } 167 168 public int[] getIndex() { 169 return null; 170 } 171 172 public int getIndex(int index) { 173 return 0; 174 } 175 176 public void setIndex(int index, int value) { 177 } 178 179 public void setIndex(int[] value) { 180 } 181 182 private static void test(String expected, Object object) { 183 String actual = object.toString(); 184 if (!actual.equals(object.getClass().getName() + expected)) { 185 throw new Error(actual); 186 } 187 } 188} 189