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.record;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.apache.tapestry.IPage;
021    import org.apache.tapestry.IRequestCycle;
022    import org.testng.annotations.Test;
023    
024    /**
025     * Tests for {@link org.apache.tapestry.record.PageClientPropertyPersistenceScope}.
026     * 
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    @Test
031    public class PageClientPropertyPersistenceScopeTest extends BaseComponentTestCase
032    {
033        protected IRequestCycle newCycle()
034        {
035            return newMock(IRequestCycle.class);
036        }
037    
038        protected IPage newPage()
039        {
040            return newMock(IPage.class);
041        }
042    
043        public void testConstructParameterName()
044        {
045            PageClientPropertyPersistenceScope scope = new PageClientPropertyPersistenceScope();
046    
047            assertEquals("state:MyPage", scope.constructParameterName("MyPage"));
048        }
049    
050        public void testIsParameterForScope()
051        {
052            PageClientPropertyPersistenceScope scope = new PageClientPropertyPersistenceScope();
053    
054            assertEquals(true, scope.isParameterForScope("state:MyPage"));
055            assertEquals(false, scope.isParameterForScope("foo"));
056            assertEquals(false, scope.isParameterForScope("appstate:Foo"));
057        }
058    
059        public void testExtractPageName()
060        {
061            PageClientPropertyPersistenceScope scope = new PageClientPropertyPersistenceScope();
062    
063            assertEquals("MyPage", scope.extractPageName("state:MyPage"));
064        }
065    
066        public void testShouldEncodeState()
067        {
068            IRequestCycle cycle = newCycle();
069            IPage page = newPage();
070    
071            trainGetPage(cycle, page);
072            trainGetPageName(page, "MyPage");
073    
074            replay();
075    
076            PageClientPropertyPersistenceScope scope = new PageClientPropertyPersistenceScope();
077    
078            scope.setRequestCycle(cycle);
079    
080            assertEquals(true, scope.shouldEncodeState(null, "MyPage", null));
081    
082            verify();
083        }
084    
085        public void testShouldEncodeStateDifferentPage()
086        {
087            IRequestCycle cycle = newCycle();
088            IPage page = newPage();
089    
090            trainGetPage(cycle, page);
091            trainGetPageName(page, "MyPage");
092    
093            replay();
094    
095            PageClientPropertyPersistenceScope scope = new PageClientPropertyPersistenceScope();
096    
097            scope.setRequestCycle(cycle);
098    
099            assertEquals(false, scope.shouldEncodeState(null, "OtherPage", null));
100    
101            verify();
102        }
103    
104        public void testShouldEncodeStateNoActivePage()
105        {
106            IRequestCycle cycle = newCycle();
107    
108            trainGetPage(cycle, null);
109    
110            replay();
111    
112            PageClientPropertyPersistenceScope scope = new PageClientPropertyPersistenceScope();
113    
114            scope.setRequestCycle(cycle);
115    
116            assertEquals(true, scope.shouldEncodeState(null, "MyPage", null));
117    
118            verify();
119        }
120    
121        private void trainGetPage(IRequestCycle cycle, IPage page)
122        {
123            expect(cycle.getPage()).andReturn(page);
124        }
125    }