KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > TestBean


1 /*
2  * $Id: TestBean.java,v 1.21 2005/06/19 22:06:47 robharrop Exp $
3  */

4
5 /*
6  * Copyright 2002-2005 the original author or authors.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.springframework.beans;
22
23 import java.util.Collection JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
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,
38  * AOP framework etc.
39  *
40  * @author Rod Johnson
41  * @since 15 April 2001
42  */

43 public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOther, Comparable JavaDoc {
44
45     private String JavaDoc beanName;
46
47     private BeanFactory beanFactory;
48
49     private boolean postProcessed;
50
51     private String JavaDoc name;
52
53     private int age;
54
55     private ITestBean spouse;
56
57     private String JavaDoc touchy;
58
59     private String JavaDoc[] stringArray;
60
61     private Date JavaDoc date = new Date JavaDoc();
62
63     private Float JavaDoc myFloat = new Float JavaDoc(0.0);
64
65     private Collection JavaDoc friends = new LinkedList JavaDoc();
66
67     private Set JavaDoc someSet = new HashSet JavaDoc();
68
69     private Map JavaDoc someMap = new HashMap JavaDoc();
70
71     private INestedTestBean doctor = new NestedTestBean();
72
73     private INestedTestBean lawyer = new NestedTestBean();
74
75     private IndexedTestBean nestedIndexedBean;
76
77     private boolean destroyed = false;
78
79     private Number JavaDoc someNumber;
80
81
82     public TestBean() {
83     }
84
85     public TestBean(String JavaDoc name, int age) {
86         this.name = name;
87         this.age = age;
88     }
89
90
91     public void setBeanName(String JavaDoc beanName) {
92         this.beanName = beanName;
93     }
94
95     public String JavaDoc getBeanName() {
96         return beanName;
97     }
98
99     public void setBeanFactory(BeanFactory beanFactory) {
100         this.beanFactory = beanFactory;
101     }
102
103     public BeanFactory getBeanFactory() {
104         return beanFactory;
105     }
106
107     public void setPostProcessed(boolean postProcessed) {
108         this.postProcessed = postProcessed;
109     }
110
111     public boolean isPostProcessed() {
112         return postProcessed;
113     }
114
115     public String JavaDoc getName() {
116         return name;
117     }
118
119     public void setName(String JavaDoc name) {
120         this.name = name;
121     }
122
123     public int getAge() {
124         return age;
125     }
126
127     public void setAge(int age) {
128         this.age = age;
129     }
130
131     public ITestBean getSpouse() {
132         return spouse;
133     }
134
135     public void setSpouse(ITestBean spouse) {
136         this.spouse = spouse;
137     }
138
139     public String JavaDoc getTouchy() {
140         return touchy;
141     }
142
143     public void setTouchy(String JavaDoc touchy) throws Exception JavaDoc {
144         if (touchy.indexOf('.') != -1) {
145             throw new Exception JavaDoc("Can't contain a .");
146         }
147         if (touchy.indexOf(',') != -1) {
148             throw new NumberFormatException JavaDoc("Number format exception: contains a ,");
149         }
150         this.touchy = touchy;
151     }
152
153     public String JavaDoc[] getStringArray() {
154         return stringArray;
155     }
156
157     public void setStringArray(String JavaDoc[] stringArray) {
158         this.stringArray = stringArray;
159     }
160
161     public Date JavaDoc getDate() {
162         return date;
163     }
164
165     public void setDate(Date JavaDoc date) {
166         this.date = date;
167     }
168
169     public Float JavaDoc getMyFloat() {
170         return myFloat;
171     }
172
173     public void setMyFloat(Float JavaDoc myFloat) {
174         this.myFloat = myFloat;
175     }
176
177     public Collection JavaDoc getFriends() {
178         return friends;
179     }
180
181     public void setFriends(Collection JavaDoc friends) {
182         this.friends = friends;
183     }
184
185     public Set JavaDoc getSomeSet() {
186         return someSet;
187     }
188
189     public void setSomeSet(Set JavaDoc someSet) {
190         this.someSet = someSet;
191     }
192
193     public Map JavaDoc getSomeMap() {
194         return someMap;
195     }
196
197     public void setSomeMap(Map JavaDoc someMap) {
198         this.someMap = someMap;
199     }
200
201     public INestedTestBean getDoctor() {
202         return doctor;
203     }
204
205     public INestedTestBean getLawyer() {
206         return lawyer;
207     }
208
209     public void setDoctor(INestedTestBean bean) {
210         doctor = bean;
211     }
212
213     public void setLawyer(INestedTestBean bean) {
214         lawyer = bean;
215     }
216
217     public Number JavaDoc getSomeNumber() {
218         return someNumber;
219     }
220
221     public void setSomeNumber(Number JavaDoc someNumber) {
222         this.someNumber = someNumber;
223     }
224
225     public IndexedTestBean getNestedIndexedBean() {
226         return nestedIndexedBean;
227     }
228
229     public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) {
230         this.nestedIndexedBean = nestedIndexedBean;
231     }
232
233
234     /**
235      * @see ITestBean#exceptional(Throwable)
236      */

237     public void exceptional(Throwable JavaDoc t) throws Throwable JavaDoc {
238         if (t != null) {
239             throw t;
240         }
241     }
242
243     /**
244      * @see ITestBean#returnsThis()
245      */

246     public Object JavaDoc returnsThis() {
247         return this;
248     }
249
250     /**
251      * @see IOther#absquatulate()
252      */

253     public void absquatulate() {
254     }
255
256     public int haveBirthday() {
257         return age++;
258     }
259
260
261     public void destroy() {
262         this.destroyed = true;
263     }
264
265     public boolean wasDestroyed() {
266         return destroyed;
267     }
268
269
270     public boolean equals(Object JavaDoc other) {
271         if (this == other) {
272             return true;
273         }
274         if (other == null || !(other instanceof TestBean)) {
275             return false;
276         }
277         TestBean tb2 = (TestBean) other;
278         return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age);
279     }
280
281     public int hashCode() {
282         return this.age;
283     }
284
285     public int compareTo(Object JavaDoc other) {
286         if (this.name != null && other instanceof TestBean) {
287             return this.name.compareTo(((TestBean) other).getName());
288         }
289         else {
290             return 1;
291         }
292     }
293
294     public String JavaDoc toString() {
295         String JavaDoc s = "name=" + name + "; age=" + age + "; touchy=" + touchy;
296         s += "; spouse={" + (spouse != null ? spouse.getName() : null) + "}";
297         return s;
298     }
299
300 }
301
Popular Tags