jdk/test/java/beans/PropertyEditor/TestEditor.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 1308 db0929bb4cd4
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1308
diff changeset
     2
 * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1308
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1308
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1308
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.beans.PropertyEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.beans.PropertyEditorManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
final class TestEditor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
    private final PropertyEditor editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
    TestEditor(Class type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
        System.out.println("Property class: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
        this.editor = PropertyEditorManager.findEditor(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
        if (this.editor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
            throw new Error("could not find editor for " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        System.out.println("PropertyEditor class: " + this.editor.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        validate(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    void testJava(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        this.editor.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    44
        Object object = execute("Executor", "execute", this.editor.getJavaInitializationString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.out.println("Property value before: " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.out.println("Property value after: " + object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (!areEqual(value, object))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            throw new Error("values are not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    void testValue(Object value, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.editor.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        validate(value, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    void testText(String text, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.editor.setAsText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        validate(value, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private void validate(Object value, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (!areEqual(value, this.editor.getValue()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new Error("value should be " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (!areEqual(text, this.editor.getAsText()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new Error("text should be " + text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static boolean areEqual(Object object1, Object object2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return (object1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                ? object2 == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                : object1.equals(object2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    77
    private static Object execute(String classname, String methodname, String value) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    78
        String content
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    79
                = "public class " + classname + " {"
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    80
                + "    public static Object " + methodname + "() throws Exception {"
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    81
                + "        return " + value + ";"
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    82
                + "    }"
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    83
                + "}";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    85
        try {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    86
            MemoryClassLoader loader = new MemoryClassLoader();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    87
            Class type = loader.compile(classname, content);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    88
            return type.getMethod(methodname).invoke(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    90
        catch (Exception exception) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents: 2
diff changeset
    91
            throw new Error(exception);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
}