1 /*
   2  * Copyright (c) 2014, 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 import java.beans.BeanProperty;
  25 import java.beans.PropertyChangeListener;
  26 import java.beans.PropertyChangeSupport;
  27 import java.beans.PropertyDescriptor;
  28 import java.util.Arrays;
  29 
  30 /**
  31  * @test
  32  * @bug 4058433
  33  * @summary Tests the BeanProperty annotation
  34  * @author Sergey Malenkov
  35  * @library ..
  36  */
  37 public class TestBeanProperty {
  38     public static void main(String[] args) throws Exception {
  39         Class<?>[] types =
  40                 {B.class, BL.class, BLF.class, E.class, H.class, P.class,
  41                  VU.class, D.class, EVD.class, EVE.class, EV.class, EVL.class,
  42                  EVX.class};
  43         for (Class<?> type : types) {
  44             PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, "value");
  45             if (((B.class == type) || (BLF.class == type)) && pd.isBound()) {
  46                 BeanUtils.reportPropertyDescriptor(pd);
  47                 throw new Error("not bound");
  48             }
  49             if ((BL.class == type) == !pd.isBound()) {
  50                 BeanUtils.reportPropertyDescriptor(pd);
  51                 throw new Error("bound");
  52             }
  53             if ((E.class == type) == !pd.isExpert()) {
  54                 BeanUtils.reportPropertyDescriptor(pd);
  55                 throw new Error("expert");
  56             }
  57             if ((H.class == type) == !pd.isHidden()) {
  58                 BeanUtils.reportPropertyDescriptor(pd);
  59                 throw new Error("hidden");
  60             }
  61             if ((P.class == type) == !pd.isPreferred()) {
  62                 BeanUtils.reportPropertyDescriptor(pd);
  63                 throw new Error("preferred");
  64             }
  65             if ((R.class == type) == !Boolean.TRUE.equals(pd.getValue("required"))) {
  66                 BeanUtils.reportPropertyDescriptor(pd);
  67                 throw new Error("required");
  68             }
  69             if ((VU.class == type) == !Boolean.TRUE.equals(pd.getValue("visualUpdate"))) {
  70                 BeanUtils.reportPropertyDescriptor(pd);
  71                 throw new Error("visualUpdate");
  72             }
  73             if ((EV.class == type) == !isEV(pd, "LEFT", 2, "javax.swing.SwingConstants.LEFT", "RIGHT", 4, "javax.swing.SwingConstants.RIGHT")) {
  74                 BeanUtils.reportPropertyDescriptor(pd);
  75                 throw new Error("enumerationValues from another package");
  76             }
  77             if ((EVL.class == type) == !isEV(pd, "ZERO", 0, "ZERO", "ONE", 1, "ONE")) {
  78                 BeanUtils.reportPropertyDescriptor(pd);
  79                 throw new Error("enumerationValues from another package");
  80             }
  81             if ((EVX.class == type) == !isEV(pd, "ZERO", 0, "X.ZERO", "ONE", 1, "X.ONE")) {
  82                 BeanUtils.reportPropertyDescriptor(pd);
  83                 throw new Error("enumerationValues from another package");
  84             }
  85             if (EVD.class == type && !isEV(pd)) {
  86                 BeanUtils.reportPropertyDescriptor(pd);
  87                 throw new Error("EV:"+ pd.getValue("enumerationValues"));
  88             }
  89             if (EVE.class == type && !isEV(pd)) {
  90                 BeanUtils.reportPropertyDescriptor(pd);
  91                 throw new Error("EV:"+ pd.getValue("enumerationValues"));
  92             }
  93         }
  94     }
  95 
  96     private static boolean isEV(PropertyDescriptor pd, Object... expected) {
  97         Object value = pd.getValue("enumerationValues");
  98         return value instanceof Object[] && Arrays.equals((Object[]) value, expected);
  99     }
 100 
 101     public static class B {
 102         private int value;
 103 
 104         public int getValue() {
 105             return this.value;
 106         }
 107 
 108         public void setValue(int value) {
 109             this.value = value;
 110         }
 111     }
 112 
 113     public static class BL {
 114         private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
 115         private int value;
 116 
 117         public int getValue() {
 118             return this.value;
 119         }
 120 
 121         public void setValue(int value) {
 122             this.value = value;
 123         }
 124 
 125         public void addPropertyChangeListener(PropertyChangeListener listener) {
 126             this.pcs.addPropertyChangeListener(listener);
 127         }
 128 
 129         public void removePropertyChangeListener(PropertyChangeListener listener) {
 130             this.pcs.removePropertyChangeListener(listener);
 131         }
 132     }
 133 
 134     public static class BLF {
 135         private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
 136         private int value;
 137 
 138         public int getValue() {
 139             return this.value;
 140         }
 141 
 142         @BeanProperty(bound = false)
 143         public void setValue(int value) {
 144             this.value = value;
 145         }
 146 
 147         public void addPropertyChangeListener(PropertyChangeListener listener) {
 148             this.pcs.addPropertyChangeListener(listener);
 149         }
 150 
 151         public void removePropertyChangeListener(PropertyChangeListener listener) {
 152             this.pcs.removePropertyChangeListener(listener);
 153         }
 154     }
 155 
 156     public static class E {
 157         private int value;
 158 
 159         public int getValue() {
 160             return this.value;
 161         }
 162 
 163         @BeanProperty(expert = true)
 164         public void setValue(int value) {
 165             this.value = value;
 166         }
 167     }
 168 
 169     public static class H {
 170         private int value;
 171 
 172         public int getValue() {
 173             return this.value;
 174         }
 175 
 176         @BeanProperty(hidden = true)
 177         public void setValue(int value) {
 178             this.value = value;
 179         }
 180     }
 181 
 182     public static class P {
 183         private int value;
 184 
 185         public int getValue() {
 186             return this.value;
 187         }
 188 
 189         @BeanProperty(preferred = true)
 190         public void setValue(int value) {
 191             this.value = value;
 192         }
 193     }
 194 
 195     public static class R {
 196         private int value;
 197 
 198         public int getValue() {
 199             return this.value;
 200         }
 201 
 202         @BeanProperty(required = true)
 203         public void setValue(int value) {
 204             this.value = value;
 205         }
 206     }
 207 
 208     public static class VU {
 209         private int value;
 210 
 211         public int getValue() {
 212             return this.value;
 213         }
 214 
 215         @BeanProperty(visualUpdate = true)
 216         public void setValue(int value) {
 217             this.value = value;
 218         }
 219     }
 220 
 221     public static class D {
 222         private int value;
 223 
 224         @BeanProperty(description = "getter")
 225         public int getValue() {
 226             return this.value;
 227         }
 228 
 229         @BeanProperty(description = "setter")
 230         public void setValue(int value) {
 231             this.value = value;
 232         }
 233     }
 234 
 235     public static class EVD {
 236 
 237         private int value;
 238 
 239         public int getValue() {
 240             return value;
 241         }
 242 
 243         @BeanProperty()
 244         public void setValue(int value) {
 245             this.value = value;
 246         }
 247     }
 248 
 249     public static class EVE {
 250 
 251         private int value;
 252 
 253         public int getValue() {
 254             return value;
 255         }
 256 
 257         @BeanProperty(enumerationValues = {})
 258         public void setValue(int value) {
 259             this.value = value;
 260         }
 261     }
 262 
 263     public static class EV {
 264         private int value;
 265 
 266         public int getValue() {
 267             return this.value;
 268         }
 269 
 270         @BeanProperty(enumerationValues = {
 271                 "javax.swing.SwingConstants.LEFT",
 272                 "javax.swing.SwingConstants.RIGHT"})
 273         public void setValue(int value) {
 274             this.value = value;
 275         }
 276     }
 277 
 278     public static class EVL {
 279         private int value;
 280 
 281         public int getValue() {
 282             return this.value;
 283         }
 284 
 285         @BeanProperty(enumerationValues = {"ZERO", "ONE"})
 286         public void setValue(int value) {
 287             this.value = value;
 288         }
 289 
 290         public static int ZERO = 0;
 291         public static int ONE = 1;
 292     }
 293 
 294     public static class EVX {
 295         private int value;
 296 
 297         public int getValue() {
 298             return this.value;
 299         }
 300 
 301         @BeanProperty(enumerationValues = {
 302                 "X.ZERO",
 303                 "X.ONE"})
 304         public void setValue(int value) {
 305             this.value = value;
 306         }
 307     }
 308 
 309     public static interface X {
 310         int ZERO = 0;
 311         int ONE = 1;
 312     }
 313 }