001    // Copyright 2004, 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.test.mock;
016    
017    import javax.servlet.ServletContext;
018    import javax.servlet.http.HttpSession;
019    import javax.servlet.http.HttpSessionBindingEvent;
020    import javax.servlet.http.HttpSessionBindingListener;
021    import javax.servlet.http.HttpSessionContext;
022    
023    /**
024     *  Mock implementation of {@link javax.servlet.http.HttpSession}.
025     *
026     *
027     *  @author Howard Lewis Ship
028     *  @since 4.0
029     */
030    
031    public class MockSession extends AttributeHolder implements HttpSession
032    {
033        private MockContext _context;
034        private String _id;
035    
036        public MockSession(MockContext context, String id)
037        {
038            _context = context;
039            _id = id;
040        }
041    
042        public long getCreationTime()
043        {
044            return 0;
045        }
046    
047        public String getId()
048        {
049            return _id;
050        }
051    
052        public long getLastAccessedTime()
053        {
054            return 0;
055        }
056    
057        public ServletContext getServletContext()
058        {
059            return _context;
060        }
061    
062        public void setMaxInactiveInterval(int arg0)
063        {
064        }
065    
066        public int getMaxInactiveInterval()
067        {
068            return 0;
069        }
070    
071        public HttpSessionContext getSessionContext()
072        {
073            return null;
074        }
075    
076        public Object getValue(String name)
077        {
078            return getAttribute(name);
079        }
080    
081        public String[] getValueNames()
082        {
083            return getAttributeNamesArray();
084        }
085    
086        public void putValue(String name, Object value)
087        {
088            setAttribute(name, value);
089        }
090    
091        public void removeValue(String name)
092        {
093            removeAttribute(name);
094        }
095    
096        public void invalidate()
097        {
098        }
099    
100        public boolean isNew()
101        {
102            return false;
103        }
104    
105        public void setAttribute(String name, Object value)
106        {
107            super.setAttribute(name, value);
108    
109            if (value instanceof HttpSessionBindingListener)
110            {
111                HttpSessionBindingListener listener = (HttpSessionBindingListener) value;
112                HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, name);
113    
114                listener.valueBound(event);
115            }
116        }
117    
118    }