Path: blob/master/test/jdk/java/beans/PropertyEditor/TestEditor.java
41149 views
/*1* Copyright (c) 2006, 2008, 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*/2223import java.beans.PropertyEditor;24import java.beans.PropertyEditorManager;2526final class TestEditor {27private final PropertyEditor editor;2829TestEditor(Class type) {30System.out.println("Property class: " + type);3132this.editor = PropertyEditorManager.findEditor(type);33if (this.editor == null)34throw new Error("could not find editor for " + type);3536System.out.println("PropertyEditor class: " + this.editor.getClass());37validate(null, null);38}3940void testJava(Object value) {41this.editor.setValue(value);4243Object object = execute("Executor", "execute", this.editor.getJavaInitializationString());4445System.out.println("Property value before: " + value);46System.out.println("Property value after: " + object);4748if (!areEqual(value, object))49throw new Error("values are not equal");50}5152void testValue(Object value, String text) {53this.editor.setValue(value);54validate(value, text);55}5657void testText(String text, Object value) {58this.editor.setAsText(text);59validate(value, text);60}6162private void validate(Object value, String text) {63if (!areEqual(value, this.editor.getValue()))64throw new Error("value should be " + value);6566if (!areEqual(text, this.editor.getAsText()))67throw new Error("text should be " + text);68}6970private static boolean areEqual(Object object1, Object object2) {71return (object1 == null)72? object2 == null73: object1.equals(object2);74}7576private static Object execute(String classname, String methodname, String value) {77String content78= "public class " + classname + " {"79+ " public static Object " + methodname + "() throws Exception {"80+ " return " + value + ";"81+ " }"82+ "}";8384try {85MemoryClassLoader loader = new MemoryClassLoader();86Class type = loader.compile(classname, content);87return type.getMethod(methodname).invoke(null);88}89catch (Exception exception) {90throw new Error(exception);91}92}93}949596