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