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