1 /**
   2  * Copyright (c) 2009, 2015, 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 6380849
  27  * @summary Tests PropertyEditor finder
  28  * @author Sergey Malenkov
  29  * @modules java.desktop/com.sun.beans.editors
  30  * @compile -XDignore.symbol.file TestPropertyEditor.java
  31  * @run main TestPropertyEditor
  32  * @key headful
  33  */
  34 
  35 import editors.SecondBeanEditor;
  36 import editors.ThirdBeanEditor;
  37 
  38 import java.awt.Color;
  39 import java.awt.Font;
  40 import java.beans.PropertyEditor;
  41 import java.beans.PropertyEditorManager;
  42 
  43 import com.sun.beans.editors.BooleanEditor;
  44 import com.sun.beans.editors.ByteEditor;
  45 import com.sun.beans.editors.ColorEditor;
  46 import com.sun.beans.editors.DoubleEditor;
  47 import com.sun.beans.editors.EnumEditor;
  48 import com.sun.beans.editors.FloatEditor;
  49 import com.sun.beans.editors.FontEditor;
  50 import com.sun.beans.editors.IntegerEditor;
  51 import com.sun.beans.editors.LongEditor;
  52 import com.sun.beans.editors.ShortEditor;
  53 import com.sun.beans.editors.StringEditor;
  54 
  55 public class TestPropertyEditor implements Runnable {
  56 
  57     private enum Enumeration {
  58         FIRST, SECOND, THIRD
  59     }
  60 
  61     private static final String[] SEARCH_PATH = { "editors" }; // NON-NLS: package name
  62 
  63     public static void main(String[] args) throws InterruptedException {
  64         TestPropertyEditor test = new TestPropertyEditor();
  65         test.run();
  66         // the following tests fails on previous build
  67         ThreadGroup group = new ThreadGroup("$$$"); // NON-NLS: unique thread name
  68         Thread thread = new Thread(group, test);
  69         thread.start();
  70         thread.join();
  71     }
  72 
  73     private static void test(Class<?> type, Class<? extends PropertyEditor> expected) {
  74         PropertyEditor actual = PropertyEditorManager.findEditor(type);
  75         if ((actual == null) && (expected != null)) {
  76             throw new Error("expected editor is not found");
  77         }
  78         if ((actual != null) && !actual.getClass().equals(expected)) {
  79             throw new Error("found unexpected editor");
  80         }
  81     }
  82 
  83     public void run() {
  84         PropertyEditorManager.registerEditor(ThirdBean.class, ThirdBeanEditor.class);
  85 
  86         test(FirstBean.class, FirstBeanEditor.class);
  87         test(SecondBean.class, null);
  88         test(ThirdBean.class, ThirdBeanEditor.class);
  89         // test editors for default primitive types
  90         test(Byte.TYPE, ByteEditor.class);
  91         test(Short.TYPE, ShortEditor.class);
  92         test(Integer.TYPE, IntegerEditor.class);
  93         test(Long.TYPE, LongEditor.class);
  94         test(Boolean.TYPE, BooleanEditor.class);
  95         test(Float.TYPE, FloatEditor.class);
  96         test(Double.TYPE, DoubleEditor.class);
  97         // test editors for default object types
  98         test(Byte.class, ByteEditor.class);
  99         test(Short.class, ShortEditor.class);
 100         test(Integer.class, IntegerEditor.class);
 101         test(Long.class, LongEditor.class);
 102         test(Boolean.class, BooleanEditor.class);
 103         test(Float.class, FloatEditor.class);
 104         test(Double.class, DoubleEditor.class);
 105         test(String.class, StringEditor.class);
 106         test(Color.class, ColorEditor.class);
 107         test(Font.class, FontEditor.class);
 108         test(Enumeration.class, EnumEditor.class);
 109 
 110         PropertyEditorManager.registerEditor(ThirdBean.class, null);
 111         PropertyEditorManager.setEditorSearchPath(SEARCH_PATH);
 112 
 113         test(FirstBean.class, FirstBeanEditor.class);
 114         test(SecondBean.class, SecondBeanEditor.class);
 115         test(ThirdBean.class, ThirdBeanEditor.class);
 116         // test editors for default primitive types
 117         test(Byte.TYPE, ByteEditor.class);
 118         test(Short.TYPE, ShortEditor.class);
 119         test(Integer.TYPE, IntegerEditor.class);
 120         test(Long.TYPE, LongEditor.class);
 121         test(Boolean.TYPE, BooleanEditor.class);
 122         test(Float.TYPE, FloatEditor.class);
 123         test(Double.TYPE, DoubleEditor.class);
 124         // test editors for default object types
 125         test(Byte.class, null);
 126         test(Short.class, null);
 127         test(Integer.class, null);
 128         test(Long.class, null);
 129         test(Boolean.class, null);
 130         test(Float.class, null);
 131         test(Double.class, null);
 132         test(String.class, null);
 133         test(Color.class, null);
 134         test(Font.class, null);
 135         test(Enumeration.class, EnumEditor.class);
 136     }
 137 }