KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.bsf.smartValueObject.container;
2
3 import org.bsf.smartValueObject.container.SmartCollection;
4 import org.bsf.smartValueObject.container.SmartContainer;
5 import org.bsf.smartValueObject.container.AbstractTestSmartContainer;
6 import org.bsf.smartValueObject.TestVO;
7 import org.bsf.smartValueObject.Versionable;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 /**
13  * Testcase for SmartCollection.
14  *
15  * @see org.bsf.smartValueObject.container.SmartCollection
16  */

17 public class TestSmartCollection extends AbstractTestSmartContainer {
18     private SmartCollection sc;
19
20     protected SmartContainer createContainer() {
21         sc = new SmartCollection(new ArrayList JavaDoc(), getVersionable());
22         return sc;
23     }
24
25     protected Object JavaDoc addToContainer(TestVO t) {
26        assertTrue(sc.add(t));
27        return t;
28     }
29
30     protected Object JavaDoc removeFromContainer(TestVO t) {
31         assertTrue(sc.remove(t));
32         return t;
33     }
34
35     protected TestVO getOne() {
36         assertTrue("Container is empty", sc.size() > 0);
37         TestVO t = (TestVO) sc.iterator().next();
38         assertTrue(t != null);
39         return t;
40     }
41
42     public void testSize() {
43         createMany();
44         assertEquals("Collection has incorrect size",
45                 getSize(), sc.size());
46     }
47
48     public void testContains() {
49         createMany();
50
51         for (Iterator JavaDoc it = iterator(); it.hasNext();) {
52             Object JavaDoc o = it.next();
53             assertTrue(sc.contains(o));
54         }
55     }
56
57     public void testIterator() {
58         createMany();
59
60         Iterator JavaDoc it = sc.iterator();
61         while (it.hasNext()) {
62             Object JavaDoc o = it.next();
63             assertTrue(contains(o));
64         }
65     }
66
67     public void testDelete() {
68         TestVO alive = createOne(true);
69         TestVO dead = deleteOne();
70
71         assertTrue(alive == dead);
72         assertTrue("Object is not flagged as deleted",
73                 dead.isDeleted());
74         assertTrue("Objet is still in collection",
75                 !sc.contains(dead));
76     }
77
78     public void testDelete2() {
79         createMany(true);
80
81         int initialSize = getSize();
82         TestVO[] deads = deleteMany();
83         int n = deads.length;
84
85         assertEquals("Collection has incorrect size afer deletion",
86                 initialSize - n, sc.size());
87         for (int i = 0; i < deads.length; i++) {
88             TestVO dead = deads[i];
89
90             assertTrue("Object is not flagged as deleted",
91                     dead.isDeleted());
92             assertTrue("Object is still in collection",
93                     !sc.contains(dead));
94         }
95     }
96
97     public void testCreate() {
98         TestVO t[] = createMany();
99
100         for (int i = 0; i < t.length; i++) {
101             TestVO testVO = t[i];
102             _testCreate(testVO);
103         }
104     }
105
106     private void _testCreate(TestVO t) {
107         assertTrue("Object is not flagged created",
108                 t.isCreated());
109         assertTrue("Object is not in collection",
110                 sc.contains(t));
111     }
112
113     public void testIsEmpty() {
114         createMany();
115         deleteMany(getSize());
116         assertTrue("Collection is not flagged empty",
117                 sc.isEmpty());
118     }
119
120     public void testToArray() {
121         createMany();
122         deleteMany();
123
124         Object JavaDoc[] oarray = sc.toArray();
125         assertEquals(getSize(), oarray.length);
126
127         for (int i = 0; i < oarray.length; i++) {
128             Object JavaDoc o = oarray[i];
129             assertTrue(o instanceof Versionable);
130             // everyone's still alive ?
131
assertTrue(! ((Versionable) o).isDeleted() );
132         }
133     }
134
135     public void testToArray2() {
136         createMany();
137         deleteMany();
138
139         Object JavaDoc[] oarray = new Object JavaDoc[getSize()];
140         sc.toArray(oarray);
141         assertEquals(getSize(), oarray.length);
142
143         for (int i = 0; i < oarray.length; i++) {
144             Object JavaDoc o = oarray[i];
145             assertTrue(o instanceof Versionable);
146             // everyone's still alive ?
147
assertTrue(! ((Versionable) o).isDeleted() );
148         }
149     }
150
151     public void testSizeAfterDelete() {
152         createMany();
153         deleteMany();
154
155         testSize();
156     }
157
158     public void testIteratorAfterDelete() {
159         createMany();
160
161         TestVO dead = deleteOne();
162         Iterator JavaDoc it = sc.iterator();
163         while (it.hasNext()) {
164             TestVO a = (TestVO) it.next();
165             assertTrue(contains(a));
166             assertTrue(a != dead);
167         }
168     }
169 }
170
Popular Tags