1/* 2 * Copyright (c) 2006, 2008, 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 24import java.beans.PropertyEditor; 25import java.beans.PropertyEditorManager; 26 27final class TestEditor { 28 private final PropertyEditor editor; 29 30 TestEditor(Class type) { 31 System.out.println("Property class: " + type); 32 33 this.editor = PropertyEditorManager.findEditor(type); 34 if (this.editor == null) 35 throw new Error("could not find editor for " + type); 36 37 System.out.println("PropertyEditor class: " + this.editor.getClass()); 38 validate(null, null); 39 } 40 41 void testJava(Object value) { 42 this.editor.setValue(value); 43 44 Object object = execute("Executor", "execute", this.editor.getJavaInitializationString()); 45 46 System.out.println("Property value before: " + value); 47 System.out.println("Property value after: " + object); 48 49 if (!areEqual(value, object)) 50 throw new Error("values are not equal"); 51 } 52 53 void testValue(Object value, String text) { 54 this.editor.setValue(value); 55 validate(value, text); 56 } 57 58 void testText(String text, Object value) { 59 this.editor.setAsText(text); 60 validate(value, text); 61 } 62 63 private void validate(Object value, String text) { 64 if (!areEqual(value, this.editor.getValue())) 65 throw new Error("value should be " + value); 66 67 if (!areEqual(text, this.editor.getAsText())) 68 throw new Error("text should be " + text); 69 } 70 71 private static boolean areEqual(Object object1, Object object2) { 72 return (object1 == null) 73 ? object2 == null 74 : object1.equals(object2); 75 } 76 77 private static Object execute(String classname, String methodname, String value) { 78 String content 79 = "public class " + classname + " {" 80 + " public static Object " + methodname + "() throws Exception {" 81 + " return " + value + ";" 82 + " }" 83 + "}"; 84 85 try { 86 MemoryClassLoader loader = new MemoryClassLoader(); 87 Class type = loader.compile(classname, content); 88 return type.getMethod(methodname).invoke(null); 89 } 90 catch (Exception exception) { 91 throw new Error(exception); 92 } 93 } 94} 95