KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > state > TestApplicationStateManager


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.engine.state;
16
17 import org.apache.hivemind.test.HiveMindTestCase;
18 import org.easymock.MockControl;
19
20 /**
21  * Tests for {@link org.apache.tapestry.engine.state.ApplicationStateManagerImpl}.
22  *
23  * @author Howard M. Lewis Ship
24  * @since 4.0
25  */

26 public class TestApplicationStateManager extends HiveMindTestCase
27 {
28     private StateObjectManagerRegistry newRegistry(String JavaDoc name,
29             StateObjectManager manager)
30     {
31         MockControl c = newControl(StateObjectManagerRegistry.class);
32         StateObjectManagerRegistry result = (StateObjectManagerRegistry) c
33                 .getMock();
34
35         result.get(name);
36         c.setReturnValue(manager);
37
38         return result;
39     }
40
41     public void testExistsInCache()
42     {
43         Object JavaDoc stateObject = new Object JavaDoc();
44
45         MockControl c = newControl(StateObjectManager.class);
46         StateObjectManager m = (StateObjectManager) c.getMock();
47
48         m.get();
49         c.setReturnValue(stateObject);
50
51         StateObjectManagerRegistry r = newRegistry("fred", m);
52
53         replayControls();
54
55         ApplicationStateManagerImpl asm = new ApplicationStateManagerImpl();
56         asm.setRegistry(r);
57
58         assertSame(stateObject, asm.get("fred"));
59
60         assertEquals(true, asm.exists("fred"));
61
62         verifyControls();
63     }
64
65     public void testNotExist()
66     {
67
68         MockControl c = newControl(StateObjectManager.class);
69         StateObjectManager m = (StateObjectManager) c.getMock();
70
71         m.exists();
72         c.setReturnValue(false);
73
74         StateObjectManagerRegistry r = newRegistry("barney", m);
75
76         replayControls();
77
78         ApplicationStateManagerImpl asm = new ApplicationStateManagerImpl();
79         asm.setRegistry(r);
80
81         assertEquals(false, asm.exists("barney"));
82
83         verifyControls();
84     }
85
86     public void testGet()
87     {
88         Object JavaDoc stateObject = new Object JavaDoc();
89
90         MockControl c = newControl(StateObjectManager.class);
91         StateObjectManager m = (StateObjectManager) c.getMock();
92
93         m.get();
94         c.setReturnValue(stateObject);
95
96         MockControl rc = newControl(StateObjectManagerRegistry.class);
97         StateObjectManagerRegistry r = (StateObjectManagerRegistry) rc
98                 .getMock();
99
100         r.get("barney");
101         rc.setReturnValue(m);
102
103         replayControls();
104
105         ApplicationStateManagerImpl asm = new ApplicationStateManagerImpl();
106         asm.setRegistry(r);
107
108         assertSame(stateObject, asm.get("barney"));
109
110         verifyControls();
111
112         replayControls();
113
114         // Note: doesn't affect the SOPM
115

116         assertSame(stateObject, asm.get("barney"));
117
118         verifyControls();
119
120         r.get("barney");
121         rc.setReturnValue(m);
122
123         m.get();
124         c.setReturnValue(stateObject);
125
126         replayControls();
127
128         // Clear the cache
129
asm.passivateService();
130
131         // This invoked on the SOPM
132
assertSame(stateObject, asm.get("barney"));
133
134         verifyControls();
135     }
136
137     public void testFlush()
138     {
139         Object JavaDoc stateObject = new Object JavaDoc();
140
141         MockControl c = newControl(StateObjectManager.class);
142         StateObjectManager m = (StateObjectManager) c.getMock();
143
144         m.get();
145         c.setReturnValue(stateObject);
146
147         MockControl rc = newControl(StateObjectManagerRegistry.class);
148         StateObjectManagerRegistry r = (StateObjectManagerRegistry) rc
149                 .getMock();
150
151         r.get("barney");
152         rc.setReturnValue(m);
153
154         replayControls();
155
156         ApplicationStateManagerImpl asm = new ApplicationStateManagerImpl();
157         asm.setRegistry(r);
158
159         assertSame(stateObject, asm.get("barney"));
160
161         verifyControls();
162
163         r.get("barney");
164         rc.setReturnValue(m);
165
166         m.store(stateObject);
167
168         replayControls();
169
170         asm.flush();
171
172         verifyControls();
173     }
174 }
Popular Tags