KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > tags > StateManagerTest


1 package com.tonbeller.jpivot.tags;
2
3 import junit.framework.TestCase;
4
5 import com.tonbeller.jpivot.tags.StateManager.State;
6
7 /**
8  * @author av
9  * @since 15.02.2005
10  */

11 public class StateManagerTest extends TestCase {
12   State globalVisible;
13   int globalShowCount;
14
15   MyState a1;
16   MyState a2;
17   MyState a3;
18   MyState b1;
19   MyState b2;
20   MyState c1;
21   MyState c2;
22
23   class MyState implements State {
24     String JavaDoc name;
25     String JavaDoc value;
26     int initializeCount;
27     int destroyCount;
28     int hideCount;
29     int showCount;
30
31     public MyState(String JavaDoc name, String JavaDoc value) {
32       this.name = name;
33       this.value = value;
34     }
35
36     public String JavaDoc getName() {
37       return name;
38     }
39
40     public void initialize() {
41       assertHidden();
42       assertDestroyed();
43       ++initializeCount;
44       assertInitialized();
45     }
46
47     public void destroy() {
48       assertHidden();
49       assertInitialized();
50       ++destroyCount;
51       assertDestroyed();
52     }
53
54     public void show() {
55       assertInitialized();
56       assertHidden();
57       ++ showCount;
58       assertVisible();
59       
60       assertNull("only one state may be visible at once", globalVisible);
61       globalVisible = this;
62       ++ globalShowCount;
63     }
64     
65     public void hide() {
66       assertInitialized();
67       assertVisible();
68       ++ hideCount;
69       assertHidden();
70
71       assertEquals("state must be visible to be hidden", globalVisible, this);
72       globalVisible = null;
73     }
74
75     void assertVisible() {
76       assertTrue("visible", showCount == hideCount + 1);
77     }
78     void assertHidden() {
79       assertTrue("hidden", showCount == hideCount);
80     }
81     void assertDestroyed() {
82       assertTrue("initializeCount == destroyCount", initializeCount == destroyCount);
83     }
84     void assertInitialized() {
85       assertTrue("initializeCount == destroyCount + 1", initializeCount == destroyCount + 1);
86     }
87   }
88
89   void assertVisible(State s) {
90     assertEquals("visible state ", s, globalVisible);
91   }
92
93   protected void setUp() throws Exception JavaDoc {
94     super.setUp();
95     a1 = new MyState("A", "a1");
96     a2 = new MyState("A", "a2");
97     a3 = new MyState("A", "a3");
98     b1 = new MyState("B", "b1");
99     b2 = new MyState("B", "b2");
100     c1 = new MyState("C", "c1");
101     c2 = new MyState("C", "c2");
102   }
103
104   /**
105    * multiple initializeAndShow() with the same State Name "A"
106    * @throws Exception
107    */

108   public void testStackedReplace() throws Exception JavaDoc {
109     StateManager s = new StackStateManager();
110     s.initializeAndShow(a1);
111     assertVisible(a1);
112     a1.assertInitialized();
113     s.initializeAndShow(a2);
114     assertVisible(a2);
115     a1.assertDestroyed();
116     a2.assertInitialized();
117     s.initializeAndShow(a3);
118     assertVisible(a3);
119     a2.assertDestroyed();
120     a3.assertInitialized();
121     s.destroyAll();
122     a3.assertDestroyed();
123     assertVisible(null);
124   }
125
126   /**
127    * multiple initializeAndShow() with the different state names
128    * @throws Exception
129    */

130   public void testStackedStacked() throws Exception JavaDoc {
131     StateManager s = new StackStateManager();
132     s.initializeAndShow(a1);
133     assertVisible(a1);
134     a1.assertInitialized();
135
136     // push b1 on top of a1
137
s.initializeAndShow(b1);
138     assertVisible(b1);
139     a1.assertInitialized();
140     b1.assertInitialized();
141
142     // push c1 on top of a1, b1
143
s.initializeAndShow(c1);
144     assertVisible(c1);
145     a1.assertInitialized();
146     b1.assertInitialized();
147     c1.assertInitialized();
148
149     // remove c1, b1, a1 and make a2 current
150
s.initializeAndShow(a2);
151     assertVisible(a2);
152     a1.assertDestroyed();
153     b1.assertDestroyed();
154     c1.assertDestroyed();
155     a2.assertInitialized();
156
157     // clear all
158
s.destroyAll();
159     assertVisible(null);
160   }
161
162   public void testStackedCurrentByName() throws Exception JavaDoc {
163     StateManager s = new StackStateManager();
164     s.initializeAndShow(a1);
165     s.initializeAndShow(b1);
166     s.initializeAndShow(c1);
167
168     // initializeAndShow must be called only if the state changes
169
globalShowCount = 0;
170
171     // illegal name is ignored
172
s.showByName("xxx");
173     assertEquals(0, globalShowCount);
174     a1.assertInitialized();
175     b1.assertInitialized();
176     c1.assertInitialized();
177
178     // C is already current
179
s.showByName("C");
180     assertEquals(0, globalShowCount);
181     c1.assertInitialized();
182     c1.assertVisible();
183     b1.assertInitialized();
184     b1.assertHidden();
185     a1.assertHidden();
186     a1.assertInitialized();
187     
188     s.showByName("B");
189     c1.assertHidden();
190     c1.assertDestroyed();
191     b1.assertInitialized();
192     b1.assertVisible();
193     a1.assertInitialized();
194     a1.assertHidden();
195     assertEquals(1, globalShowCount);
196
197     // "B" again does not change anything
198
s.showByName("B");
199     assertEquals(1, globalShowCount);
200     c1.assertHidden();
201     c1.assertDestroyed();
202     b1.assertInitialized();
203     b1.assertVisible();
204     a1.assertInitialized();
205     a1.assertHidden();
206
207     s.showByName("A");
208     assertEquals(2, globalShowCount);
209     c1.assertDestroyed();
210     c1.assertHidden();
211     b1.assertDestroyed();
212     b1.assertHidden();
213     a1.assertInitialized();
214     a1.assertVisible();
215     s.showByName("A");
216     assertEquals(2, globalShowCount);
217   }
218
219   public void testStackInitializeDestroy() throws Exception JavaDoc {
220     StateManager s = new StackStateManager();
221     s.initializeAndShow(a1);
222     assertVisible(a1);
223     assertEquals(1, a1.initializeCount);
224     assertEquals(0, a1.destroyCount);
225
226     s.initializeAndShow(a1);
227     assertVisible(a1);
228     assertEquals(2, a1.initializeCount);
229     assertEquals(1, a1.destroyCount);
230   }
231   
232   public void testStackedDestroyByNameCurrent() throws Exception JavaDoc {
233     StateManager s = new StackStateManager();
234     s.initializeAndShow(a1);
235     s.initializeAndShow(b1);
236     s.initializeAndShow(c1);
237     
238     s.destroyByName("C");
239     a1.assertInitialized();
240     a1.assertHidden();
241     b1.assertInitialized();
242     b1.assertVisible();
243     assertEquals("initialize() called once", 1, b1.initializeCount);
244     assertEquals("show() called twice", 2, b1.showCount);
245     c1.assertDestroyed();
246     c1.assertHidden();
247   }
248   
249   public void testStackedDestroyByNameNotCurrent() throws Exception JavaDoc {
250     StateManager s = new StackStateManager();
251     s.initializeAndShow(a1);
252     s.initializeAndShow(b1);
253     s.initializeAndShow(c1);
254     
255     s.destroyByName("B");
256     a1.assertInitialized();
257     a1.assertVisible();
258     b1.assertDestroyed();
259     b1.assertHidden();
260     c1.assertDestroyed();
261     c1.assertHidden();
262   }
263
264   public void testPageStateManager() throws Exception JavaDoc {
265     StateManager s = new PageStateManager();
266     s.initializeAndShow(a1);
267     assertVisible(a1);
268     a1.assertInitialized();
269     s.initializeAndShow(b1);
270     assertVisible(b1);
271     a1.assertInitialized();
272     b1.assertInitialized();
273     s.initializeAndShow(c1);
274     assertVisible(c1);
275     a1.assertInitialized();
276     b1.assertInitialized();
277     c1.assertInitialized();
278     s.initializeAndShow(a2);
279     assertVisible(a2);
280     a1.assertDestroyed();
281     a2.assertInitialized();
282     b1.assertInitialized();
283     c1.assertInitialized();
284     s.destroyAll();
285     a2.assertDestroyed();
286     b1.assertDestroyed();
287     c1.assertDestroyed();
288     assertVisible(null);
289   }
290
291   public void testPageCurrentByName() throws Exception JavaDoc {
292     StateManager s = new PageStateManager();
293     s.initializeAndShow(a1);
294     s.initializeAndShow(b1);
295     s.initializeAndShow(c1);
296
297     // initializeAndShow must be called only if the state changes
298
globalShowCount = 0;
299     s.showByName("C");
300     assertEquals(0, globalShowCount);
301     a1.assertHidden();
302     a1.assertInitialized();
303     b1.assertHidden();
304     b1.assertInitialized();
305     c1.assertVisible();
306     c1.assertInitialized();
307     
308     s.showByName("B");
309     assertEquals(1, globalShowCount);
310     a1.assertHidden();
311     a1.assertInitialized();
312     b1.assertVisible();
313     b1.assertInitialized();
314     c1.assertHidden();
315     c1.assertInitialized();
316
317     s.showByName("B");
318     assertEquals(1, globalShowCount);
319     a1.assertHidden();
320     a1.assertInitialized();
321     b1.assertVisible();
322     b1.assertInitialized();
323     c1.assertHidden();
324     c1.assertInitialized();
325
326     s.showByName("A");
327     assertEquals(2, globalShowCount);
328     a1.assertVisible();
329     a1.assertInitialized();
330     b1.assertHidden();
331     b1.assertInitialized();
332     c1.assertHidden();
333     c1.assertInitialized();
334
335     s.showByName("A");
336     assertEquals(2, globalShowCount);
337     a1.assertVisible();
338     a1.assertInitialized();
339     b1.assertHidden();
340     b1.assertInitialized();
341     c1.assertHidden();
342     c1.assertInitialized();
343   }
344
345   // destroys the current state
346
public void testPageDestroyByNameCurrent() throws Exception JavaDoc {
347     StateManager s = new PageStateManager();
348     s.initializeAndShow(a1);
349     s.initializeAndShow(b1);
350     s.initializeAndShow(c1);
351     
352     s.destroyByName("C");
353     a1.assertInitialized();
354     a1.assertHidden();
355     b1.assertInitialized();
356     b1.assertHidden();
357     c1.assertDestroyed();
358     c1.assertHidden();
359     
360     s.showByName("B");
361     b1.assertInitialized();
362     assertEquals("initialize() called once", 1, b1.initializeCount);
363     b1.assertVisible();
364     assertEquals("show() called twice", 2, b1.showCount);
365    }
366   // destroy the not-current state
367
public void testPageDestroyByNameNotCurrent() throws Exception JavaDoc {
368     StateManager s = new PageStateManager();
369     s.initializeAndShow(a1);
370     s.initializeAndShow(b1);
371     s.initializeAndShow(c1);
372     
373     s.destroyByName("B");
374     a1.assertInitialized();
375     a1.assertHidden();
376     b1.assertDestroyed();
377     b1.assertHidden();
378     c1.assertInitialized();
379     c1.assertVisible();
380    }
381 }
382
Popular Tags