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.web;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import java.util.List;
020    
021    import org.testng.annotations.Test;
022    
023    import javax.servlet.http.HttpSession;
024    
025    /**
026     * Tests for {@link org.apache.tapestry.web.ServletWebSession}.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    @Test
032    public class ServletWebSessionTest extends BaseWebTestCase
033    {
034        public void testGetAttributeNames()
035        {
036            HttpSession session = newSession();
037    
038            expect(session.getAttributeNames()).andReturn(newEnumeration());
039    
040            replay();
041    
042            WebSession ws = new ServletWebSession(session);
043    
044            List l = ws.getAttributeNames();
045    
046            checkList(l);
047    
048            verify();
049        }
050    
051        private HttpSession newSession()
052        {
053            return newMock(HttpSession.class);
054        }
055    
056        public void testGetAttribute()
057        {
058            Object attribute = new Object();
059    
060            HttpSession session = newSession();
061    
062            expect(session.getAttribute("attr")).andReturn(attribute);
063    
064            replay();
065    
066            WebSession ws = new ServletWebSession(session);
067    
068            assertSame(attribute, ws.getAttribute("attr"));
069    
070            verify();
071        }
072    
073        public void testSetAttribute()
074        {
075            Object attribute = new Object();
076    
077            HttpSession session = newSession();
078    
079            session.setAttribute("name", attribute);
080    
081            replay();
082    
083            WebSession ws = new ServletWebSession(session);
084    
085            ws.setAttribute("name", attribute);
086    
087            verify();
088        }
089    
090        public void testSetAttributeToNull()
091        {
092            HttpSession session = newSession();
093    
094            session.removeAttribute("tonull");
095    
096            replay();
097    
098            WebSession ws = new ServletWebSession(session);
099    
100            ws.setAttribute("tonull", null);
101    
102            verify();
103        }
104    
105        public void testGetId()
106        {
107            HttpSession session = newSession();
108    
109            expect(session.getId()).andReturn("abc");
110    
111            replay();
112    
113            WebSession ws = new ServletWebSession(session);
114    
115            assertEquals("abc", ws.getId());
116    
117            verify();
118        }
119    
120        public void testInvalidate()
121        {
122            HttpSession session = newSession();
123    
124            session.invalidate();
125    
126            replay();
127    
128            WebSession ws = new ServletWebSession(session);
129    
130            ws.invalidate();
131    
132            verify();
133        }
134    }