1 /*
   2  * Copyright (c) 2010, 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 BeanInfo finder
  28  * @author Sergey Malenkov
  29  */
  30 
  31 import beans.FirstBean;
  32 import beans.FirstBeanBeanInfo;
  33 import beans.SecondBean;
  34 import beans.ThirdBean;
  35 
  36 import infos.SecondBeanBeanInfo;
  37 import infos.ThirdBeanBeanInfo;
  38 
  39 import java.beans.BeanInfo;
  40 import java.beans.Introspector;
  41 import java.lang.reflect.Method;
  42 
  43 public class TestBeanInfo implements Runnable {
  44 
  45     private static final String[] SEARCH_PATH = { "infos" }; // NON-NLS: package name
  46 
  47     public static void main(String[] args) throws InterruptedException {
  48         TestBeanInfo test = new TestBeanInfo();
  49         test.run();
  50         // the following tests fails on previous build
  51         ThreadGroup group = new ThreadGroup("$$$"); // NON-NLS: unique thread name
  52         Thread thread = new Thread(group, test);
  53         thread.start();
  54         thread.join();
  55     }
  56 
  57     private static void test(Class<?> type, Class<? extends BeanInfo> expected) {
  58         BeanInfo actual;
  59         try {
  60             actual = Introspector.getBeanInfo(type);
  61             type = actual.getClass();
  62             Method method = type.getDeclaredMethod("getTargetBeanInfo"); // NON-NLS: method name
  63             method.setAccessible(true);
  64             actual = (BeanInfo) method.invoke(actual);
  65         }
  66         catch (Exception exception) {
  67             throw new Error("unexpected error", exception);
  68         }
  69         if ((actual == null) && (expected != null)) {
  70             throw new Error("expected info is not found");
  71         }
  72         if ((actual != null) && !actual.getClass().equals(expected)) {
  73             throw new Error("found unexpected info");
  74         }
  75     }
  76 
  77     private boolean passed;
  78 
  79     public void run() {
  80         Introspector.flushCaches();
  81 
  82         test(FirstBean.class, FirstBeanBeanInfo.class);
  83         test(SecondBean.class, null);
  84         test(ThirdBean.class, null);
  85         test(ThirdBeanBeanInfo.class, ThirdBeanBeanInfo.class);
  86 
  87         Introspector.setBeanInfoSearchPath(SEARCH_PATH);
  88         Introspector.flushCaches();
  89 
  90         test(FirstBean.class, FirstBeanBeanInfo.class);
  91         test(SecondBean.class, SecondBeanBeanInfo.class);
  92         test(ThirdBean.class, null);
  93         test(ThirdBeanBeanInfo.class, ThirdBeanBeanInfo.class);
  94     }
  95 }