View Javadoc
1   /*
2    * Copyright 2002-2012 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.tests.sample.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<Object> {
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  	protected ITestBean[] spouses;
62  
63  	private String touchy;
64  
65  	private String[] stringArray;
66  
67  	private Integer[] someIntegerArray;
68  
69  	private Integer[][] nestedIntegerArray;
70  
71  	private int[] someIntArray;
72  
73  	private int[][] nestedIntArray;
74  
75  	private Date date = new Date();
76  
77  	private Float myFloat = new Float(0.0);
78  
79  	private Collection<? super Object> friends = new LinkedList<>();
80  
81  	private Set<?> someSet = new HashSet<>();
82  
83  	private Map<?, ?> someMap = new HashMap<>();
84  
85  	private List<?> someList = new ArrayList<>();
86  
87  	private Properties someProperties = new Properties();
88  
89  	private INestedTestBean doctor = new NestedTestBean();
90  
91  	private INestedTestBean lawyer = new NestedTestBean();
92  
93  	private IndexedTestBean nestedIndexedBean;
94  
95  	private boolean destroyed;
96  
97  	private Number someNumber;
98  
99  	private Colour favouriteColour;
100 
101 	private Boolean someBoolean;
102 
103 	private List<?> otherColours;
104 
105 	private List<?> pets;
106 
107 
108 	public TestBean() {
109 	}
110 
111 	public TestBean(String name) {
112 		this.name = name;
113 	}
114 
115 	public TestBean(ITestBean spouse) {
116 		this.spouses = new ITestBean[] {spouse};
117 	}
118 
119 	public TestBean(String name, int age) {
120 		this.name = name;
121 		this.age = age;
122 	}
123 
124 	public TestBean(ITestBean spouse, Properties someProperties) {
125 		this.spouses = new ITestBean[] {spouse};
126 		this.someProperties = someProperties;
127 	}
128 
129 	public TestBean(List<?> someList) {
130 		this.someList = someList;
131 	}
132 
133 	public TestBean(Set<?> someSet) {
134 		this.someSet = someSet;
135 	}
136 
137 	public TestBean(Map<?, ?> someMap) {
138 		this.someMap = someMap;
139 	}
140 
141 	public TestBean(Properties someProperties) {
142 		this.someProperties = someProperties;
143 	}
144 
145 
146 	@Override
147 	public void setBeanName(String beanName) {
148 		this.beanName = beanName;
149 	}
150 
151 	public String getBeanName() {
152 		return beanName;
153 	}
154 
155 	@Override
156 	public void setBeanFactory(BeanFactory beanFactory) {
157 		this.beanFactory = beanFactory;
158 	}
159 
160 	public BeanFactory getBeanFactory() {
161 		return beanFactory;
162 	}
163 
164 	public void setPostProcessed(boolean postProcessed) {
165 		this.postProcessed = postProcessed;
166 	}
167 
168 	public boolean isPostProcessed() {
169 		return postProcessed;
170 	}
171 
172 	@Override
173 	public String getName() {
174 		return name;
175 	}
176 
177 	@Override
178 	public void setName(String name) {
179 		this.name = name;
180 	}
181 
182 	public String getSex() {
183 		return sex;
184 	}
185 
186 	public void setSex(String sex) {
187 		this.sex = sex;
188 		if (this.name == null) {
189 			this.name = sex;
190 		}
191 	}
192 
193 	@Override
194 	public int getAge() {
195 		return age;
196 	}
197 
198 	@Override
199 	public void setAge(int age) {
200 		this.age = age;
201 	}
202 
203 	public boolean isJedi() {
204 		return jedi;
205 	}
206 
207 	public void setJedi(boolean jedi) {
208 		this.jedi = jedi;
209 	}
210 
211 	@Override
212 	public ITestBean getSpouse() {
213 		return (spouses != null ? spouses[0] : null);
214 	}
215 
216 	@Override
217 	public void setSpouse(ITestBean spouse) {
218 		this.spouses = new ITestBean[] {spouse};
219 	}
220 
221 	@Override
222 	public ITestBean[] getSpouses() {
223 		return spouses;
224 	}
225 
226 	public String getTouchy() {
227 		return touchy;
228 	}
229 
230 	public void setTouchy(String touchy) throws Exception {
231 		if (touchy.indexOf('.') != -1) {
232 			throw new Exception("Can't contain a .");
233 		}
234 		if (touchy.indexOf(',') != -1) {
235 			throw new NumberFormatException("Number format exception: contains a ,");
236 		}
237 		this.touchy = touchy;
238 	}
239 
240 	public String getCountry() {
241 		return country;
242 	}
243 
244 	public void setCountry(String country) {
245 		this.country = country;
246 	}
247 
248 	@Override
249 	public String[] getStringArray() {
250 		return stringArray;
251 	}
252 
253 	@Override
254 	public void setStringArray(String[] stringArray) {
255 		this.stringArray = stringArray;
256 	}
257 
258 	@Override
259 	public Integer[] getSomeIntegerArray() {
260 		return someIntegerArray;
261 	}
262 
263 	@Override
264 	public void setSomeIntegerArray(Integer[] someIntegerArray) {
265 		this.someIntegerArray = someIntegerArray;
266 	}
267 
268 	@Override
269 	public Integer[][] getNestedIntegerArray() {
270 		return nestedIntegerArray;
271 	}
272 
273 	@Override
274 	public void setNestedIntegerArray(Integer[][] nestedIntegerArray) {
275 		this.nestedIntegerArray = nestedIntegerArray;
276 	}
277 
278 	@Override
279 	public int[] getSomeIntArray() {
280 		return someIntArray;
281 	}
282 
283 	@Override
284 	public void setSomeIntArray(int[] someIntArray) {
285 		this.someIntArray = someIntArray;
286 	}
287 
288 	@Override
289 	public int[][] getNestedIntArray() {
290 		return nestedIntArray;
291 	}
292 
293 	@Override
294 	public void setNestedIntArray(int[][] nestedIntArray) {
295 		this.nestedIntArray = nestedIntArray;
296 	}
297 
298 	public Date getDate() {
299 		return date;
300 	}
301 
302 	public void setDate(Date date) {
303 		this.date = date;
304 	}
305 
306 	public Float getMyFloat() {
307 		return myFloat;
308 	}
309 
310 	public void setMyFloat(Float myFloat) {
311 		this.myFloat = myFloat;
312 	}
313 
314 	public Collection<? super Object> getFriends() {
315 		return friends;
316 	}
317 
318 	public void setFriends(Collection<? super Object> friends) {
319 		this.friends = friends;
320 	}
321 
322 	public Set<?> getSomeSet() {
323 		return someSet;
324 	}
325 
326 	public void setSomeSet(Set<?> someSet) {
327 		this.someSet = someSet;
328 	}
329 
330 	public Map<?, ?> getSomeMap() {
331 		return someMap;
332 	}
333 
334 	public void setSomeMap(Map<?, ?> someMap) {
335 		this.someMap = someMap;
336 	}
337 
338 	public List<?> getSomeList() {
339 		return someList;
340 	}
341 
342 	public void setSomeList(List<?> someList) {
343 		this.someList = someList;
344 	}
345 
346 	public Properties getSomeProperties() {
347 		return someProperties;
348 	}
349 
350 	public void setSomeProperties(Properties someProperties) {
351 		this.someProperties = someProperties;
352 	}
353 
354 	@Override
355 	public INestedTestBean getDoctor() {
356 		return doctor;
357 	}
358 
359 	public void setDoctor(INestedTestBean doctor) {
360 		this.doctor = doctor;
361 	}
362 
363 	@Override
364 	public INestedTestBean getLawyer() {
365 		return lawyer;
366 	}
367 
368 	public void setLawyer(INestedTestBean lawyer) {
369 		this.lawyer = lawyer;
370 	}
371 
372 	public Number getSomeNumber() {
373 		return someNumber;
374 	}
375 
376 	public void setSomeNumber(Number someNumber) {
377 		this.someNumber = someNumber;
378 	}
379 
380 	public Colour getFavouriteColour() {
381 		return favouriteColour;
382 	}
383 
384 	public void setFavouriteColour(Colour favouriteColour) {
385 		this.favouriteColour = favouriteColour;
386 	}
387 
388 	public Boolean getSomeBoolean() {
389 		return someBoolean;
390 	}
391 
392 	public void setSomeBoolean(Boolean someBoolean) {
393 		this.someBoolean = someBoolean;
394 	}
395 
396 	@Override
397 	public IndexedTestBean getNestedIndexedBean() {
398 		return nestedIndexedBean;
399 	}
400 
401 	public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) {
402 		this.nestedIndexedBean = nestedIndexedBean;
403 	}
404 
405 	public List<?> getOtherColours() {
406 		return otherColours;
407 	}
408 
409 	public void setOtherColours(List<?> otherColours) {
410 		this.otherColours = otherColours;
411 	}
412 
413 	public List<?> getPets() {
414 		return pets;
415 	}
416 
417 	public void setPets(List<?> pets) {
418 		this.pets = pets;
419 	}
420 
421 
422 	/**
423 	 * @see org.springframework.tests.sample.beans.ITestBean#exceptional(Throwable)
424 	 */
425 	@Override
426 	public void exceptional(Throwable t) throws Throwable {
427 		if (t != null) {
428 			throw t;
429 		}
430 	}
431 
432 	@Override
433 	public void unreliableFileOperation() throws IOException {
434 		throw new IOException();
435 	}
436 	/**
437 	 * @see org.springframework.tests.sample.beans.ITestBean#returnsThis()
438 	 */
439 	@Override
440 	public Object returnsThis() {
441 		return this;
442 	}
443 
444 	/**
445 	 * @see org.springframework.tests.sample.beans.IOther#absquatulate()
446 	 */
447 	@Override
448 	public void absquatulate() {
449 	}
450 
451 	@Override
452 	public int haveBirthday() {
453 		return age++;
454 	}
455 
456 
457 	public void destroy() {
458 		this.destroyed = true;
459 	}
460 
461 	public boolean wasDestroyed() {
462 		return destroyed;
463 	}
464 
465 
466 	@Override
467 	public boolean equals(Object other) {
468 		if (this == other) {
469 			return true;
470 		}
471 		if (other == null || !(other instanceof TestBean)) {
472 			return false;
473 		}
474 		TestBean tb2 = (TestBean) other;
475 		return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age);
476 	}
477 
478 	@Override
479 	public int hashCode() {
480 		return this.age;
481 	}
482 
483 	@Override
484 	public int compareTo(Object other) {
485 		if (this.name != null && other instanceof TestBean) {
486 			return this.name.compareTo(((TestBean) other).getName());
487 		}
488 		else {
489 			return 1;
490 		}
491 	}
492 
493 	@Override
494 	public String toString() {
495 		return this.name;
496 	}
497 
498 }