1 /* 2 * Copyright 2002-2008 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.beans; 18 19 import java.io.IOException; 20 import java.util.ArrayList; 21 import java.util.Collection; 22 import java.util.Date; 23 import java.util.HashMap; 24 import java.util.HashSet; 25 import java.util.LinkedList; 26 import java.util.List; 27 import java.util.Map; 28 import java.util.Properties; 29 import java.util.Set; 30 31 import org.springframework.beans.factory.BeanFactory; 32 import org.springframework.beans.factory.BeanFactoryAware; 33 import org.springframework.beans.factory.BeanNameAware; 34 import org.springframework.util.ObjectUtils; 35 36 /** 37 * Simple test bean used for testing bean factories, the AOP framework etc. 38 * 39 * @author Rod Johnson 40 * @author Juergen Hoeller 41 * @since 15 April 2001 42 */ 43 public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOther, Comparable { 44 45 private String beanName; 46 47 private String country; 48 49 private BeanFactory beanFactory; 50 51 private boolean postProcessed; 52 53 private String name; 54 55 private String sex; 56 57 private int age; 58 59 private boolean jedi; 60 61 private ITestBean[] spouses; 62 63 private String touchy; 64 65 private String[] stringArray; 66 67 private Integer[] someIntegerArray; 68 69 private Date date = new Date(); 70 71 private Float myFloat = new Float(0.0); 72 73 private Collection friends = new LinkedList(); 74 75 private Set someSet = new HashSet(); 76 77 private Map someMap = new HashMap(); 78 79 private List someList = new ArrayList(); 80 81 private Properties someProperties = new Properties(); 82 83 private INestedTestBean doctor = new NestedTestBean(); 84 85 private INestedTestBean lawyer = new NestedTestBean(); 86 87 private IndexedTestBean nestedIndexedBean; 88 89 private boolean destroyed; 90 91 private Number someNumber; 92 93 private Colour favouriteColour; 94 95 private Boolean someBoolean; 96 97 private List otherColours; 98 99 private List pets; 100 101 TestBean()102 public TestBean() { 103 } 104 TestBean(String name)105 public TestBean(String name) { 106 this.name = name; 107 } 108 TestBean(ITestBean spouse)109 public TestBean(ITestBean spouse) { 110 this.spouses = new ITestBean[] {spouse}; 111 } 112 TestBean(String name, int age)113 public TestBean(String name, int age) { 114 this.name = name; 115 this.age = age; 116 } 117 TestBean(ITestBean spouse, Properties someProperties)118 public TestBean(ITestBean spouse, Properties someProperties) { 119 this.spouses = new ITestBean[] {spouse}; 120 this.someProperties = someProperties; 121 } 122 TestBean(List someList)123 public TestBean(List someList) { 124 this.someList = someList; 125 } 126 TestBean(Set someSet)127 public TestBean(Set someSet) { 128 this.someSet = someSet; 129 } 130 TestBean(Map someMap)131 public TestBean(Map someMap) { 132 this.someMap = someMap; 133 } 134 TestBean(Properties someProperties)135 public TestBean(Properties someProperties) { 136 this.someProperties = someProperties; 137 } 138 139 setBeanName(String beanName)140 public void setBeanName(String beanName) { 141 this.beanName = beanName; 142 } 143 getBeanName()144 public String getBeanName() { 145 return beanName; 146 } 147 setBeanFactory(BeanFactory beanFactory)148 public void setBeanFactory(BeanFactory beanFactory) { 149 this.beanFactory = beanFactory; 150 } 151 getBeanFactory()152 public BeanFactory getBeanFactory() { 153 return beanFactory; 154 } 155 setPostProcessed(boolean postProcessed)156 public void setPostProcessed(boolean postProcessed) { 157 this.postProcessed = postProcessed; 158 } 159 isPostProcessed()160 public boolean isPostProcessed() { 161 return postProcessed; 162 } 163 getName()164 public String getName() { 165 return name; 166 } 167 setName(String name)168 public void setName(String name) { 169 this.name = name; 170 } 171 getSex()172 public String getSex() { 173 return sex; 174 } 175 setSex(String sex)176 public void setSex(String sex) { 177 this.sex = sex; 178 if (this.name == null) { 179 this.name = sex; 180 } 181 } 182 getAge()183 public int getAge() { 184 return age; 185 } 186 setAge(int age)187 public void setAge(int age) { 188 this.age = age; 189 } 190 isJedi()191 public boolean isJedi() { 192 return jedi; 193 } 194 setJedi(boolean jedi)195 public void setJedi(boolean jedi) { 196 this.jedi = jedi; 197 } 198 getSpouse()199 public ITestBean getSpouse() { 200 return (spouses != null ? spouses[0] : null); 201 } 202 setSpouse(ITestBean spouse)203 public void setSpouse(ITestBean spouse) { 204 this.spouses = new ITestBean[] {spouse}; 205 } 206 getSpouses()207 public ITestBean[] getSpouses() { 208 return spouses; 209 } 210 getTouchy()211 public String getTouchy() { 212 return touchy; 213 } 214 setTouchy(String touchy)215 public void setTouchy(String touchy) throws Exception { 216 if (touchy.indexOf('.') != -1) { 217 throw new Exception("Can't contain a ."); 218 } 219 if (touchy.indexOf(',') != -1) { 220 throw new NumberFormatException("Number format exception: contains a ,"); 221 } 222 this.touchy = touchy; 223 } 224 getCountry()225 public String getCountry() { 226 return country; 227 } 228 setCountry(String country)229 public void setCountry(String country) { 230 this.country = country; 231 } 232 getStringArray()233 public String[] getStringArray() { 234 return stringArray; 235 } 236 setStringArray(String[] stringArray)237 public void setStringArray(String[] stringArray) { 238 this.stringArray = stringArray; 239 } 240 getSomeIntegerArray()241 public Integer[] getSomeIntegerArray() { 242 return someIntegerArray; 243 } 244 setSomeIntegerArray(Integer[] someIntegerArray)245 public void setSomeIntegerArray(Integer[] someIntegerArray) { 246 this.someIntegerArray = someIntegerArray; 247 } 248 getDate()249 public Date getDate() { 250 return date; 251 } 252 setDate(Date date)253 public void setDate(Date date) { 254 this.date = date; 255 } 256 getMyFloat()257 public Float getMyFloat() { 258 return myFloat; 259 } 260 setMyFloat(Float myFloat)261 public void setMyFloat(Float myFloat) { 262 this.myFloat = myFloat; 263 } 264 getFriends()265 public Collection getFriends() { 266 return friends; 267 } 268 setFriends(Collection friends)269 public void setFriends(Collection friends) { 270 this.friends = friends; 271 } 272 getSomeSet()273 public Set getSomeSet() { 274 return someSet; 275 } 276 setSomeSet(Set someSet)277 public void setSomeSet(Set someSet) { 278 this.someSet = someSet; 279 } 280 getSomeMap()281 public Map getSomeMap() { 282 return someMap; 283 } 284 setSomeMap(Map someMap)285 public void setSomeMap(Map someMap) { 286 this.someMap = someMap; 287 } 288 getSomeList()289 public List getSomeList() { 290 return someList; 291 } 292 setSomeList(List someList)293 public void setSomeList(List someList) { 294 this.someList = someList; 295 } 296 getSomeProperties()297 public Properties getSomeProperties() { 298 return someProperties; 299 } 300 setSomeProperties(Properties someProperties)301 public void setSomeProperties(Properties someProperties) { 302 this.someProperties = someProperties; 303 } 304 getDoctor()305 public INestedTestBean getDoctor() { 306 return doctor; 307 } 308 setDoctor(INestedTestBean doctor)309 public void setDoctor(INestedTestBean doctor) { 310 this.doctor = doctor; 311 } 312 getLawyer()313 public INestedTestBean getLawyer() { 314 return lawyer; 315 } 316 setLawyer(INestedTestBean lawyer)317 public void setLawyer(INestedTestBean lawyer) { 318 this.lawyer = lawyer; 319 } 320 getSomeNumber()321 public Number getSomeNumber() { 322 return someNumber; 323 } 324 setSomeNumber(Number someNumber)325 public void setSomeNumber(Number someNumber) { 326 this.someNumber = someNumber; 327 } 328 getFavouriteColour()329 public Colour getFavouriteColour() { 330 return favouriteColour; 331 } 332 setFavouriteColour(Colour favouriteColour)333 public void setFavouriteColour(Colour favouriteColour) { 334 this.favouriteColour = favouriteColour; 335 } 336 getSomeBoolean()337 public Boolean getSomeBoolean() { 338 return someBoolean; 339 } 340 setSomeBoolean(Boolean someBoolean)341 public void setSomeBoolean(Boolean someBoolean) { 342 this.someBoolean = someBoolean; 343 } 344 getNestedIndexedBean()345 public IndexedTestBean getNestedIndexedBean() { 346 return nestedIndexedBean; 347 } 348 setNestedIndexedBean(IndexedTestBean nestedIndexedBean)349 public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) { 350 this.nestedIndexedBean = nestedIndexedBean; 351 } 352 getOtherColours()353 public List getOtherColours() { 354 return otherColours; 355 } 356 setOtherColours(List otherColours)357 public void setOtherColours(List otherColours) { 358 this.otherColours = otherColours; 359 } 360 getPets()361 public List getPets() { 362 return pets; 363 } 364 setPets(List pets)365 public void setPets(List pets) { 366 this.pets = pets; 367 } 368 369 370 /** 371 * @see org.springframework.beans.ITestBean#exceptional(Throwable) 372 */ exceptional(Throwable t)373 public void exceptional(Throwable t) throws Throwable { 374 if (t != null) { 375 throw t; 376 } 377 } 378 unreliableFileOperation()379 public void unreliableFileOperation() throws IOException { 380 throw new IOException(); 381 } 382 /** 383 * @see org.springframework.beans.ITestBean#returnsThis() 384 */ returnsThis()385 public Object returnsThis() { 386 return this; 387 } 388 389 /** 390 * @see org.springframework.beans.IOther#absquatulate() 391 */ absquatulate()392 public void absquatulate() { 393 } 394 haveBirthday()395 public int haveBirthday() { 396 return age++; 397 } 398 399 destroy()400 public void destroy() { 401 this.destroyed = true; 402 } 403 wasDestroyed()404 public boolean wasDestroyed() { 405 return destroyed; 406 } 407 408 equals(Object other)409 public boolean equals(Object other) { 410 if (this == other) { 411 return true; 412 } 413 if (other == null || !(other instanceof TestBean)) { 414 return false; 415 } 416 TestBean tb2 = (TestBean) other; 417 return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age); 418 } 419 hashCode()420 public int hashCode() { 421 return this.age; 422 } 423 compareTo(Object other)424 public int compareTo(Object other) { 425 if (this.name != null && other instanceof TestBean) { 426 return this.name.compareTo(((TestBean) other).getName()); 427 } 428 else { 429 return 1; 430 } 431 } 432 toString()433 public String toString() { 434 return this.name; 435 } 436 437 }