1 /*
   2  * Copyright (c) 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 import java.beans.BeanInfo;
  26 import java.beans.BeanProperty;
  27 import java.beans.Introspector;
  28 import java.beans.PropertyDescriptor;
  29 import java.util.Arrays;
  30 
  31 /**
  32  * @test
  33  * @bug 8131342 8131939 8132153 4058433
  34  * @summary Check if BeanProperty annotation works correctly
  35  *          in case of multiple (two) properties. Also check work of
  36  *          BeanProperty "bound" and "required" elements.
  37  * @author a.stepanov
  38  */
  39 
  40 public class TwoPropertiesTest {
  41 
  42     public static class C {
  43 
  44         private int x, y;
  45 
  46         @BeanProperty(
  47                 bound     = false,
  48                 expert    = false,
  49                 hidden    = false,
  50                 preferred = false,
  51                 required  = false,
  52                 visualUpdate = false,
  53                 description = "XX",
  54                 enumerationValues = {"javax.swing.SwingConstants.TOP"}
  55                 )
  56 
  57         public void setX(int i) { x = i; }
  58         public  int getX()      { return x; }
  59 
  60 
  61         @BeanProperty(
  62             bound     = true,
  63             expert    = true,
  64             hidden    = true,
  65             preferred = true,
  66             required  = true,
  67             visualUpdate = true,
  68             description = "YY",
  69             enumerationValues = {"javax.swing.SwingConstants.EAST"}
  70             )
  71         public void setY(int i) { y = i; }
  72         public  int getY()      { return y; }
  73     }
  74 
  75     static void checkEq(String what, Object v, Object ref) throws Exception {
  76 
  77         if ((v != null) && v.equals(ref)) {
  78             System.out.println(what + ": ok (" + ref.toString() + ")");
  79         } else {
  80             throw new Exception(
  81                 "invalid " + what + ", expected: \"" + ref + "\", got: \"" + v + "\"");
  82         }
  83     }
  84 
  85     static void checkEnumEq(String what, Object v, Object ref[]) throws Exception {
  86 
  87         what = "\"" + what + "\"";
  88         if (v == null) {
  89             throw new Exception("null " + what + " enumeration values");
  90         }
  91 
  92         String msg = "invalid " + what + " enumeration values";
  93         if (!(v instanceof Object[])) { throw new Exception(msg); }
  94 
  95         if (Arrays.equals((Object []) v, ref)) {
  96             System.out.println(what + " enumeration values: ok");
  97         } else { throw new Exception(msg); }
  98     }
  99 
 100 
 101     public static void main(String[] args) throws Exception {
 102 
 103         BeanInfo i = Introspector.getBeanInfo(C.class, Object.class);
 104 
 105         PropertyDescriptor[] pds = i.getPropertyDescriptors();
 106         for (PropertyDescriptor pd: pds) {
 107             String name = pd.getName();
 108             System.out.println(name);
 109             switch (name) {
 110                 case "x":
 111                     checkEq(name + " isBound",       pd.isBound(),       false);
 112                     checkEq(name + " isConstrained", pd.isConstrained(), false);
 113                     checkEq(name + " isExpert",      pd.isExpert(),      false);
 114                     checkEq(name + " isHidden",      pd.isHidden(),      false);
 115                     checkEq(name + " isPreferred",   pd.isPreferred(),   false);
 116                     checkEq(name + " required",      pd.getValue("required"),     false);
 117                     checkEq(name + " visualUpdate",  pd.getValue("visualUpdate"), false);
 118 
 119                     checkEq(name + " description",   pd.getShortDescription(), "XX");
 120 
 121                     checkEnumEq(pd.getName(), pd.getValue("enumerationValues"),
 122                         new Object[]{"TOP", 1, "javax.swing.SwingConstants.TOP"});
 123                     System.out.println("");
 124                     break;
 125 
 126                 case "y":
 127 
 128                     checkEq(name + " isBound",       pd.isBound(),       true);
 129                     checkEq(name + " isExpert",      pd.isExpert(),      true);
 130                     checkEq(name + " isHidden",      pd.isHidden(),      true);
 131                     checkEq(name + " isPreferred",   pd.isPreferred(),   true);
 132                     checkEq(name + " required",      pd.getValue("required"),     true);
 133                     checkEq(name + " visualUpdate",  pd.getValue("visualUpdate"), true);
 134 
 135                     checkEq(name + " description",   pd.getShortDescription(), "YY");
 136 
 137                     checkEnumEq(pd.getName(), pd.getValue("enumerationValues"),
 138                         new Object[]{
 139                         "EAST", 3, "javax.swing.SwingConstants.EAST"});
 140                     System.out.println("");
 141                     break;
 142                 default:
 143                     throw new Exception("invalid property descriptor: " + name);
 144             }
 145         }
 146     }
 147 }