KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 public class TestSmartMap extends AbstractTestSmartContainer {
18     private SmartMap sm;
19
20     protected SmartContainer createContainer() {
21         sm = new SmartMap(new HashMap JavaDoc(), getVersionable());
22         return sm;
23     }
24
25     protected Object JavaDoc addToContainer(TestVO t) {
26         return sm.put(t, t);
27     }
28
29     protected Object JavaDoc removeFromContainer(TestVO t) {
30         return sm.remove(t);
31     }
32
33     protected TestVO getOne() {
34         assertTrue("Container is empty", sm.size() > 0);
35         TestVO t = (TestVO) sm.values().iterator().next();
36         assertTrue(t != null);
37         return t;
38     }
39
40     public void testSize() {
41         createMany();
42         assertEquals("Incorrect size of map", getSize(), sm.size());
43     }
44
45     public void testSizeAfterDelete() {
46         createMany();
47         deleteMany();
48         testSize();
49     }
50
51     public void testContainsValue() {
52         createMany();
53         for (Iterator JavaDoc it = iterator(); it.hasNext();) {
54             assertTrue("Map doesn't contain all values",
55                     sm.containsValue(it.next()));
56         }
57     }
58
59     public void testContainsValueAfterDelete() {
60         createMany();
61         TestVO[] deleted = deleteMany();
62         testContainsValue();
63
64         for (Iterator JavaDoc it = iterator(); it.hasNext();) {
65             TestVO test = (TestVO) it.next();
66
67             assertTrue("Container has still deleted elements",
68                 !isInArray(test, deleted));
69         }
70     }
71
72     public void testContainsKey() {
73         for (Iterator JavaDoc it = iterator(); it.hasNext();) {
74            TestVO t = (TestVO) it.next();
75            assertTrue("Map doesn't contain all keys",
76                 sm.containsKey(t));
77         }
78     }
79
80     public void testContainsKeyAfterDelete() {
81         createMany();
82         deleteMany();
83         testContainsKey();
84     }
85
86     public void testIsEmpty() {
87         createMany(true); // TODO
88
sm.clear();
89         clear();
90         assertTrue("Map in not empty", sm.isEmpty());
91     }
92
93     public void testGet() {
94         createMany();
95         for (Iterator JavaDoc it = iterator(); it.hasNext();) {
96           TestVO t = (TestVO) it.next();
97           TestVO t_ = (TestVO) sm.get(t);
98           assertTrue("Failed to find entry in map", t_ != null);
99           assertTrue("Incorrect identity in map", t == t_);
100         }
101     }
102
103     public void testPut() {
104         TestVO t = createOne();
105         TestVO t_ = (TestVO) sm.get(t);
106         assertTrue("Failed to put entry in map",
107                 t == t_);
108         assertTrue("Object not flagged as created", t_.isCreated());
109     }
110
111     public void testRemove() {
112         createOne(true);
113         TestVO t = deleteOne();
114         Object JavaDoc o = sm.get(t);
115         assertTrue("Failed to remove entry",
116                 o == null);
117         assertTrue("Object not flagged as deleted", t.isDeleted());
118     }
119
120     public void testKeySet() {
121         createMany();
122         Set JavaDoc keys = sm.keySet();
123         assertTrue("Number of keys doesn't match",
124                 keys.size() == getSize());
125         for (Iterator JavaDoc it = iterator(); it.hasNext();) {
126             TestVO t = (TestVO) it.next();
127             assertTrue("Key not found in keySet",
128                 keys.contains(t));
129         }
130     }
131
132     public void testValues() {
133         createMany();
134         for (Iterator JavaDoc it = sm.values().iterator(); it.hasNext();) {
135             assertTrue(contains(it.next()));
136         }
137     }
138 }
139
Popular Tags