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