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