001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.engine.state;
016    
017    import org.apache.tapestry.BaseComponentTestCase;
018    import static org.easymock.EasyMock.expect;
019    import org.testng.annotations.Test;
020    
021    /**
022     * Tests for {@link org.apache.tapestry.engine.state.ApplicationStateManagerImpl}.
023     * 
024     * @author Howard M. Lewis Ship
025     * @since 4.0
026     */
027    @Test(sequential = true)
028    public class TestApplicationStateManager extends BaseComponentTestCase
029    {
030        private StateObjectManagerRegistry newRegistry(String name,
031                StateObjectManager manager)
032        {
033            StateObjectManagerRegistry result = newMock(StateObjectManagerRegistry.class);
034            
035            expect(result.get(name)).andReturn(manager);
036    
037            return result;
038        }
039    
040        public void testExistsInCache()
041        {
042            Object stateObject = new Object();
043            
044            StateObjectManager m = newMock(StateObjectManager.class);
045            
046            StateObjectManagerRegistry r = newRegistry("fred", m);
047            
048            expect(m.get()).andReturn(stateObject);
049            
050            replay();
051            
052            ApplicationStateManagerImpl asm = new ApplicationStateManagerImpl();
053            asm.setRegistry(r);
054    
055            assertSame(stateObject, asm.get("fred"));
056    
057            assertEquals(true, asm.exists("fred"));
058    
059            verify();
060        }
061    
062        public void testNotExist()
063        {
064    
065            StateObjectManager m = newMock(StateObjectManager.class);
066    
067            StateObjectManagerRegistry r = newRegistry("barney", m);
068    
069            expect(m.exists()).andReturn(false);
070            
071            replay();
072    
073            ApplicationStateManagerImpl asm = new ApplicationStateManagerImpl();
074            asm.setRegistry(r);
075    
076            assertEquals(false, asm.exists("barney"));
077    
078            verify();
079        }
080    
081        public void testGet()
082        {
083            Object stateObject = new Object();
084    
085            StateObjectManager m = newMock(StateObjectManager.class);
086            
087            StateObjectManagerRegistry r = newMock(StateObjectManagerRegistry.class);
088            
089            expect(r.get("barney")).andReturn(m);
090            
091            expect(m.get()).andReturn(stateObject);
092            
093            replay();
094    
095            ApplicationStateManagerImpl asm = new ApplicationStateManagerImpl();
096            asm.setRegistry(r);
097    
098            assertSame(stateObject, asm.get("barney"));
099    
100            verify();
101    
102            replay();
103    
104            // Note: doesn't affect the SOPM
105    
106            assertSame(stateObject, asm.get("barney"));
107    
108            verify();
109            
110            expect(r.get("barney")).andReturn(m);
111            
112            expect(m.get()).andReturn(stateObject);
113    
114            replay();
115    
116            // Clear the cache
117            asm.passivateService();
118    
119            // This invoked on the SOPM
120            assertSame(stateObject, asm.get("barney"));
121    
122            verify();
123        }
124    
125        public void testFlush()
126        {
127            Object stateObject = new Object();
128    
129            StateObjectManager m = newMock(StateObjectManager.class);
130    
131            StateObjectManagerRegistry r = newMock(StateObjectManagerRegistry.class);
132            
133            expect(r.get("barney")).andReturn(m);
134            
135            expect(m.get()).andReturn(stateObject);
136    
137            replay();
138    
139            ApplicationStateManagerImpl asm = new ApplicationStateManagerImpl();
140            asm.setRegistry(r);
141            
142            assertSame(stateObject, asm.get("barney"));
143            
144            verify();
145    
146            expect(r.get("barney")).andReturn(m);
147    
148            m.store(stateObject);
149    
150            replay();
151    
152            asm.flush();
153    
154            verify();
155        }
156    }