1   /*
2    *	$Id: TestBean.java,v 1.2 2005/10/21 23:46:11 jstrachan 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.logicblaze.lingo.beans;
22  
23  import org.springframework.beans.factory.BeanFactory;
24  import org.springframework.beans.factory.BeanFactoryAware;
25  
26  import java.util.Collection;
27  import java.util.Date;
28  import java.util.HashMap;
29  import java.util.HashSet;
30  import java.util.LinkedList;
31  import java.util.Map;
32  import java.util.Set;
33  
34  /***
35   * Simple test bean used for testing bean factories,
36   * AOP framework etc.
37   * @author  Rod Johnson
38   * @since 15 April 2001
39   */
40  public class TestBean implements BeanFactoryAware, ITestBean, IOther, Comparable {
41  
42  	private BeanFactory beanFactory;
43  
44  	private boolean postProcessed;
45  
46  	private String name;
47  
48  	private int age;
49  
50  	private ITestBean spouse;
51  
52  	private String touchy;
53  
54  	private String[] stringArray;
55  
56  	private Date date = new Date();
57  
58  	private Float myFloat = new Float(0.0);
59  
60  	private Collection friends = new LinkedList();
61  
62  	private Set someSet = new HashSet();
63  
64  	private Map someMap = new HashMap();
65  
66  	private INestedTestBean doctor = new NestedTestBean();
67  
68  	private INestedTestBean lawyer = new NestedTestBean();
69  
70  	private IndexedTestBean nestedIndexedBean;
71  
72  
73  	public TestBean() {
74  	}
75  
76  	public TestBean(String name, int age) {
77  		this.name = name;
78  		this.age = age;
79  	}
80  
81  
82  	public void setBeanFactory(BeanFactory beanFactory) {
83  		this.beanFactory = beanFactory;
84  	}
85  
86  	public BeanFactory getBeanFactory() {
87  		return beanFactory;
88  	}
89  
90  	public void setPostProcessed(boolean postProcessed) {
91  		this.postProcessed = postProcessed;
92  	}
93  
94  	public boolean isPostProcessed() {
95  		return postProcessed;
96  	}
97  
98  	public String getName() {
99          System.out.println("### getting name: " + name + " for: " + this);
100 		return name;
101 	}
102 
103 	public void setName(String name) {
104 		this.name = name;
105 	}
106 
107 	public int getAge() {
108         System.out.println("### getting age: " + age + " for: " + this);
109 		return age;
110 	}
111 
112 	public void setAge(int age) {
113         System.out.println("### setting age to: " + age + " for: " + this);
114 		this.age = age;
115 	}
116 
117 	public ITestBean getSpouse() {
118 		return spouse;
119 	}
120 
121 	public void setSpouse(ITestBean spouse) {
122 		this.spouse = spouse;
123 	}
124 
125 	public String getTouchy() {
126 		return touchy;
127 	}
128 
129 	public void setTouchy(String touchy) throws Exception {
130 		if (touchy.indexOf('.') != -1) {
131 			throw new Exception("Can't contain a .");
132 		}
133 		if (touchy.indexOf(',') != -1) {
134 			throw new NumberFormatException("Number format exception: contains a ,");
135 		}
136 		this.touchy = touchy;
137 	}
138 
139 	public String[] getStringArray() {
140 		return stringArray;
141 	}
142 
143 	public void setStringArray(String[] stringArray) {
144 		this.stringArray = stringArray;
145 	}
146 
147 	public Date getDate() {
148 		return date;
149 	}
150 
151 	public void setDate(Date date) {
152 		this.date = date;
153 	}
154 
155 	public Float getMyFloat() {
156 		return myFloat;
157 	}
158 
159 	public void setMyFloat(Float myFloat) {
160 		this.myFloat = myFloat;
161 	}
162 
163 	public Collection getFriends() {
164 		return friends;
165 	}
166 
167 	public void setFriends(Collection friends) {
168 		this.friends = friends;
169 	}
170 
171 	public Set getSomeSet() {
172 		return someSet;
173 	}
174 
175 	public void setSomeSet(Set someSet) {
176 		this.someSet = someSet;
177 	}
178 
179 	public Map getSomeMap() {
180 		return someMap;
181 	}
182 
183 	public void setSomeMap(Map someMap) {
184 		this.someMap = someMap;
185 	}
186 
187 	public INestedTestBean getDoctor() {
188 		return doctor;
189 	}
190 
191 	public INestedTestBean getLawyer() {
192 		return lawyer;
193 	}
194 
195 	public void setDoctor(INestedTestBean bean) {
196 		doctor = bean;
197 	}
198 
199 	public void setLawyer(INestedTestBean bean) {
200 		lawyer = bean;
201 	}
202 
203 	public IndexedTestBean getNestedIndexedBean() {
204 		return nestedIndexedBean;
205 	}
206 
207 	public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) {
208 		this.nestedIndexedBean = nestedIndexedBean;
209 	}
210 
211 
212 	/***
213 	 * @see org.logicblaze.lingo.beans.ITestBean#exceptional(Throwable)
214 	 */
215 	public void exceptional(Throwable t) throws Throwable {
216 		if (t != null)
217 			throw t;
218 	}
219 
220 	/***
221 	 * @see org.logicblaze.lingo.beans.ITestBean#returnsThis()
222 	 */
223 	public Object returnsThis() {
224 		return this;
225 	}
226 
227 	/***
228 	 * @see IOther#absquatulate()
229 	 */
230 	public void absquatulate() {
231 	}
232 	
233 	public int haveBirthday() {
234 		return age++;
235 	}
236 
237 
238 	public boolean equals(Object other) {
239 		if (this == other)
240 			return true;
241 
242 		if (other == null || !(other instanceof TestBean))
243 			return false;
244 
245 		TestBean tb2 = (TestBean) other;
246 		if (tb2.age != age)
247 			return false;
248 
249 		if (name == null)
250 			return tb2.name == null;
251 
252 		if (!tb2.name.equals(name))
253 			return false;
254 
255 		return true;
256 	}
257 
258 	public int compareTo(Object other) {
259 		if (this.name != null && other instanceof TestBean) {
260 			return this.name.compareTo(((TestBean) other).getName());
261 		}
262 		else {
263 			return 1;
264 		}
265 	}
266 
267 	public String toString() {
268 		String s = "name=" + name + "; age=" + age + "; touchy=" + touchy;
269 		s += "; spouse={" + (spouse != null ? spouse.getName() : null) + "}";
270 		return s;
271 	}
272 
273 }