KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > container > AbstractTestSmartContainer


1 package org.bsf.smartValueObject.container;
2
3 import junit.framework.TestCase;
4
5 import java.util.*;
6
7 import org.bsf.smartValueObject.container.SmartContainer;
8 import org.bsf.smartValueObject.TestVO;
9 import org.bsf.smartValueObject.Versionable;
10 import org.bsf.smartValueObject.Version;
11 import org.bsf.smartValueObject.SmartAccess;
12
13 /**
14  * Abstract Testcase for 'smart' containers. Be independent
15  * from actual bytecode modification when possible.
16  *
17  * @see org.bsf.smartValueObject.container.SmartCollection
18  * @see org.bsf.smartValueObject.container.SmartMap
19  * @see org.bsf.smartValueObject.container.SmartSet
20  */

21 public abstract class AbstractTestSmartContainer extends TestCase {
22     /**
23      * A container for logging purposes. The container under test
24      * should be the logic equivalent.
25      */

26     private Collection loggingContainer;
27     /**
28      * The implementation we want to test.
29      */

30     private SmartContainer containerUnderTest;
31
32     /**
33      * Initialize containers for testing.
34      */

35     public void setUp() {
36         loggingContainer = new ArrayList();
37         containerUnderTest = createContainer();
38         assertTrue(containerUnderTest != null);
39     }
40
41     /**
42      * Execute <code>checkIntegrity()</code> after testing.
43      */

44     public void tearDown() {
45         // after tests, do a quick validation if
46
// container under test and verify container
47
// are properly synchronized
48
checkIntegrity();
49     }
50
51     /**
52      * Creates the container to be tested.
53      * @return the container.
54      */

55     protected abstract SmartContainer createContainer();
56
57     /**
58      * Adds an elements to the container under test.
59      * @param t the element to be added.
60      * @return
61      */

62     protected abstract Object JavaDoc addToContainer(TestVO t);
63
64     /**
65      * Removes an element from the container under test.
66      * @param t the element to be removed.
67      * @return
68      */

69     protected abstract Object JavaDoc removeFromContainer(TestVO t);
70
71     /**
72      * Gets a valid VO from the container.
73      * @return a VO.
74      */

75     protected abstract TestVO getOne();
76
77     /**
78      * Gets the size of the logging container.
79      * @return the number of elements in logging container.
80      */

81     protected int getSize() {
82         return loggingContainer.size();
83     }
84
85     /**
86      * Gets random number from 0 - getSize();
87      * @return
88      */

89     protected int randomNumber() {
90         return (getSize() == 0) ? 0 :
91             (int) (Math.random() * getSize()) + 1;
92     }
93
94     /**
95      * Tests if object is contained in logging container.
96      * @param o object to check for.
97      * @return
98      */

99     protected boolean contains(Object JavaDoc o) {
100         return loggingContainer.contains(o);
101     }
102
103     /**
104      * Gets an iterator for the logging container.
105      * @return
106      */

107     protected Iterator iterator() {
108         return loggingContainer.iterator();
109     }
110
111     /**
112      * Clears the logging container.
113      */

114     protected void clear() {
115         loggingContainer.clear();
116     }
117
118     /**
119      * Obtains default <tt>Versionable</tt> implementation.
120      * @return Versionable
121      */

122     protected Versionable getVersionable() {
123         return new Version();
124     }
125
126
127     /**
128      * Deletes one element from the containers.
129      * @return the deleted object.
130      */

131     protected TestVO deleteOne() {
132         TestVO moribund = getOne();
133         _removeFromContainer(moribund);
134         return moribund;
135     }
136
137     /**
138      * Deletes random number of elements from the containers.
139      * @return the deleted objects.
140      */

141     protected TestVO[] deleteMany() {
142         return deleteMany(randomNumber());
143     }
144
145     /**
146      * Deletes specific number of elements from the containers.
147      * @param n number of elements to delete.
148      * @return the deleted objects.
149      */

150     protected TestVO[] deleteMany(int n) {
151         assertTrue("You can't delete more than " + getSize() + " elements", n<=getSize());
152         assertTrue("You must delete at least 1 element", n >= 1);
153         TestVO[] a = new TestVO[n];
154         for (int i = 0; i < n; i++) {
155             TestVO t = getOne();
156             _removeFromContainer(t);
157             a[i] = t;
158         }
159         return a;
160     }
161
162     protected TestVO createOne() {
163         return createOne(false);
164     }
165
166     protected TestVO[] createMany() {
167         return createMany(false);
168     }
169
170     protected TestVO[] createMany(int n) {
171         return createMany(n, false);
172     }
173
174     /**
175      * Creates a VO and add it to the containers.
176      * @return the created object.
177      */

178     protected TestVO createOne(boolean clean) {
179         TestVO t = new TestVO();
180         int id = (int) (Math.random() * Integer.MAX_VALUE);
181         t.setName("New Object no. " + id);
182         t.setId(id);
183
184         if (clean) {
185             t.markClean();
186         }
187         _addToContainer(t);
188
189         return t;
190     }
191
192     /**
193      * Creates a random number of VOs and adds them to the containers.
194      * @return the created objects.
195      */

196     protected TestVO[] createMany(boolean clean) {
197         return createMany((int) (Math.random() * 94) + 5, clean);
198     }
199
200     /**
201      * Creates a specific number of VOs and adds them to the containers.
202      * @param n the number of objects to create.
203      * @return the created objects.
204      */

205     protected TestVO[] createMany(int n, boolean clean) {
206         assertTrue("Use sensible parameters", n > 0 && n < 100);
207         TestVO[] a = new TestVO[n];
208         for (int i=0; i<n; i++) {
209             a[i] = createOne(clean);
210         }
211         return a;
212     }
213
214     /**
215      * Private helper method to add an element to both containers
216      * (logging / test)
217      * @param t
218      */

219     private void _addToContainer(TestVO t) {
220         loggingContainer.add(t);
221         addToContainer(t);
222     }
223
224     /**
225      * Private helper method to remove an element from both containers
226      * (logging / test)
227      * @param t
228      */

229     private void _removeFromContainer(TestVO t) {
230         loggingContainer.remove(t);
231         removeFromContainer(t);
232     }
233
234     /**
235      * Perform integrity checks to make sure the tests are left
236      * in a valid state. The logging container should be
237      * synchronized with the container under test, if tests
238      * have been executed correctly.
239      */

240     private void checkIntegrity() {
241         assertEquals("Container size mismatch",
242                 loggingContainer.size(), containerUnderTest.size());
243
244         int deleted = containerUnderTest.getDeleted();
245         int created = containerUnderTest.getCreated();
246
247         Iterator it = SmartAccess.createdIterator(containerUnderTest);
248         int counter = 0;
249         while (it.hasNext()) {
250             counter++;
251             // loggingContainer should have it, too
252
assertTrue("Added object not found in logging container",
253                     contains(it.next()));
254         }
255         assertEquals("Created objects count mismatch", created, counter);
256
257         it = SmartAccess.deletedIterator(containerUnderTest);
258         counter = 0;
259         while (it.hasNext()) {
260             counter++;
261             // loggingContainer shouldn't have it
262
assertTrue("Deleted object was found in logging container",
263                     !contains(it.next()));
264         }
265         assertEquals("Deleted objects count mismatch", deleted, counter);
266     }
267
268     /**
269      * Helper method, checks for presence of an element in a given array.
270      * @param test element to search.
271      * @param testarray the array to be searched.
272      * @return
273      */

274     protected boolean isInArray(TestVO test, TestVO[] testarray) {
275         for (int i = 0; i < testarray.length; i++) {
276             TestVO testVO = testarray[i];
277             if (test == testVO)
278                 return true;
279         }
280         return false;
281     }
282
283     public void testAddandRemove() {
284         createMany(true);
285         TestVO[] deleted = deleteMany();
286         for (int i = 0; i < deleted.length; i++) {
287             TestVO testVO = deleted[i];
288             assertTrue(testVO.isDeleted());
289         }
290     }
291 }
292
Popular Tags