1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.testing;
39
40 import com.gargoylesoftware.base.util.DetailedNullPointerException;
41 import java.beans.PropertyChangeListener;
42 import java.beans.PropertyChangeSupport;
43 import java.io.Serializable;
44
45 /***
46 * A simple bean class that can be used for testing purposes. All
47 * properties fire PropertyChangeEvents when changed.
48 *
49 * @version $Revision: 1.4 $
50 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51 */
52 public class SampleBean implements Serializable {
53
54
55 private static final long serialVersionUID = 2000;
56
57 private final PropertyChangeSupport propertyChangeSupport_;
58
59 private String name_;
60 private int age_;
61
62 /***
63 * Create a new instance.
64 */
65 public SampleBean() {
66 this( "", 0 );
67 }
68
69 /***
70 * Create a new instance.
71 * @param name The name.
72 * @param age The age.
73 */
74 public SampleBean( final String name, final int age ) {
75 propertyChangeSupport_ = new PropertyChangeSupport(this);
76 name_ = name;
77 age_ = age;
78 }
79
80 /***
81 * Add a property change listener
82 * @param listener The listener
83 */
84 public void addPropertyChangeListener( final PropertyChangeListener listener ) {
85 assertNotNull("listener", listener);
86 propertyChangeSupport_.addPropertyChangeListener(listener);
87 }
88
89 /***
90 * Remove a property change listener
91 * @param listener The listener
92 */
93 public void removePropertyChangeListener( final PropertyChangeListener listener ) {
94 assertNotNull("listener", listener);
95 propertyChangeSupport_.removePropertyChangeListener(listener);
96 }
97
98 /***
99 * Return the name.
100 * @return The name
101 */
102 public String getName() {
103 return name_;
104 }
105
106 /***
107 * Set the name.
108 * @param name The new name.
109 */
110 public void setName( final String name ) {
111 assertNotNull("name", name);
112
113 final String oldValue = name_;
114 name_ = name;
115 propertyChangeSupport_.firePropertyChange( "name", oldValue, name_ );
116 }
117
118 /***
119 * Return the age.
120 * @return The age.
121 */
122 public int getAge() {
123 return age_;
124 }
125
126 /***
127 * Set the age.
128 * @param age The new age.
129 */
130 public void setAge( final int age ) {
131
132 final int oldValue = age_;
133 age_ = age;
134 propertyChangeSupport_.firePropertyChange( "age", oldValue, age_ );
135 }
136
137
138 /***
139 * Verify that the specified value is not null. If it is then throw an exception
140 *
141 * @param fieldName The name of the field to check
142 * @param fieldValue The value of the field to check
143 * @exception DetailedNullPointerException If fieldValue is null
144 */
145 protected final void assertNotNull( final String fieldName, final Object fieldValue )
146 throws DetailedNullPointerException {
147
148 if( fieldValue == null ) {
149 throw new DetailedNullPointerException(fieldName);
150 }
151 }
152 }
153