1/* 2 * Copyright (c) 2003, 2007, 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 4274639 4305280 27 * @summary Tests PropertyDescriptor/PropertyEditorSupport improvements 28 * @author Mark Davidson 29 */ 30 31import java.beans.IntrospectionException; 32import java.beans.PropertyDescriptor; 33import java.beans.PropertyEditor; 34import java.beans.PropertyEditorSupport; 35import java.beans.SimpleBeanInfo; 36 37public class Test4274639 { 38 private static String STRING_PROPERTY = "string"; 39 private static String INTEGER_PROPERTY = "integer"; 40 41 private static String STRING_VALUE = "Test Text"; 42 private static Integer INTEGER_VALUE = 261074; 43 44 public static void main(String[] args) { 45 TestBean bean = new TestBean(STRING_VALUE); 46 if (!STRING_VALUE.equals(bean.getString())) 47 throw new Error("unexpected string property: " + bean.getString()); 48 49 boolean string = false; 50 boolean integer = false; 51 for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(bean.getClass())) { 52 String name = pd.getName(); 53 System.out.println(" - " + name); 54 if (name.equals(STRING_PROPERTY)) { 55 // This tests createPropertyEditor such that the PropertyEditor 56 // returned will have the bean as the source object. 57 Class type = pd.getPropertyEditorClass(); 58 if (!StringEditor.class.equals(type)) 59 throw new Error("unexpected property editor type: " + type); 60 61 PropertyEditor editor = pd.createPropertyEditor(bean); 62 if (editor == null) 63 throw new Error("property editor cannot be created"); 64 65 if (STRING_VALUE != editor.getValue()) 66 throw new Error("unexpected value: " + editor.getValue()); 67 68 Object source = ((PropertyEditorSupport) editor).getSource(); 69 if (source != bean) 70 throw new Error("unexpected source: " + source); 71 72 string = true; 73 } 74 if (name.equals(INTEGER_PROPERTY)) { 75 // This tests createPropertyEditor such that the PropertyEditor 76 // returned will be just a new instance 77 Class type = pd.getPropertyEditorClass(); 78 if (!IntegerEditor.class.equals(type)) 79 throw new Error("unexpected property editor type: " + type); 80 81 PropertyEditor editor = pd.createPropertyEditor(bean); 82 if (editor == null) 83 throw new Error("property editor cannot be created"); 84 85 if (INTEGER_VALUE != editor.getValue()) 86 throw new Error("unexpected value: " + editor.getValue()); 87 88 Object source = ((PropertyEditorSupport) editor).getSource(); 89 if (source != editor) 90 throw new Error("unexpected source: " + source); 91 92 integer = true; 93 } 94 } 95 if (!string) 96 throw new Error("string property is not tested"); 97 98 if (!integer) 99 throw new Error("integer property is not tested"); 100 } 101 102 public static final class TestBean { 103 private String string; 104 private int integer; 105 106 public TestBean() { 107 this.string = "default"; 108 this.integer = 0; 109 } 110 111 public TestBean(String string) { 112 setString(string); 113 } 114 115 public String getString() { 116 return this.string; 117 } 118 119 public void setString(String string) { 120 this.string = string; 121 } 122 123 public int getInteger() { 124 return this.integer; 125 } 126 127 public void setInteger(int integer) { 128 this.integer = integer; 129 } 130 } 131 132 public static final class TestBeanBeanInfo extends SimpleBeanInfo { 133 public PropertyDescriptor[] getPropertyDescriptors() { 134 PropertyDescriptor[] pds = new PropertyDescriptor[2]; 135 try { 136 pds[0] = new PropertyDescriptor(STRING_PROPERTY, TestBean.class); 137 pds[0].setPropertyEditorClass(StringEditor.class); 138 139 pds[1] = new PropertyDescriptor(INTEGER_PROPERTY, TestBean.class); 140 pds[1].setPropertyEditorClass(IntegerEditor.class); 141 142 } 143 catch (IntrospectionException exception) { 144 throw new Error("unexpected error", exception); 145 } 146 return pds; 147 } 148 } 149 150 // Public constructor was added for 4305280 151 public static final class StringEditor extends PropertyEditorSupport { 152 public StringEditor(Object source) { 153 super(source); 154 155 if (source instanceof TestBean) { 156 TestBean test = (TestBean) source; 157 setValue(test.getString()); 158 } 159 } 160 } 161 162 // Will use the default public constructor 163 // that uses this property editor as the source. 164 public static final class IntegerEditor extends PropertyEditorSupport { 165 public Object getValue() { 166 return INTEGER_VALUE; // default value is hard coded 167 } 168 } 169} 170