001// Copyright 2006, 2007, 2008, 2024 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
015package org.apache.tapestry5.internal.test;
016
017import org.apache.tapestry5.commons.util.CollectionFactory;
018import org.apache.tapestry5.http.services.Session;
019import org.apache.tapestry5.ioc.internal.util.InternalUtils;
020
021import static org.apache.tapestry5.commons.util.CollectionFactory.newList;
022
023import java.util.List;
024import java.util.Map;
025
026public class PageTesterSession implements Session
027{
028    private final Map<String, Object> attributes = CollectionFactory.newMap();
029
030    @Override
031    public List<String> getAttributeNames()
032    {
033        return InternalUtils.sortedKeys(attributes);
034    }
035
036    @Override
037    public List<String> getAttributeNames(Session.LockMode lockMode)
038    {
039        return getAttributeNames();
040    }
041
042    @Override
043    public List<String> getAttributeNames(String prefix)
044    {
045        List<String> result = newList();
046
047        for (String name : getAttributeNames())
048            if (name.startsWith(prefix)) result.add(name);
049
050        return result;
051    }
052
053    @Override
054    public List<String> getAttributeNames(String prefix, LockMode lockMode)
055    {
056        return getAttributeNames(prefix);
057    }
058
059    @Override
060    public Object getAttribute(String name)
061    {
062        return attributes.get(name);
063    }
064
065    @Override
066    public Object getAttribute(String name, LockMode lockMode)
067    {
068        return getAttribute(name);
069    }
070
071    @Override
072    public void setAttribute(String name, Object value)
073    {
074        if (value == null)
075        {
076            attributes.remove(name);
077        }
078        else
079        {
080            attributes.put(name, value);
081        }
082    }
083
084    @Override
085    public boolean containsAttribute(String name)
086    {
087        return attributes.containsKey(name);
088    }
089
090    @Override
091    public boolean containsAttribute(String name, Session.LockMode lockMode)
092    {
093        return containsAttribute(name);
094    }
095
096    private void nyi(String name)
097    {
098        throw new IllegalStateException(String.format("%s.%s() is not yet implemented.", getClass()
099                .getName(), name));
100    }
101
102    @Override
103    public int getMaxInactiveInterval()
104    {
105        nyi("getMaxInativeInterval");
106
107        return 0;
108    }
109
110    @Override
111    public void invalidate()
112    {
113        nyi("invalidate");
114    }
115
116    @Override
117    public boolean isInvalidated()
118    {
119        return false;
120    }
121
122    @Override
123    public void restoreDirtyObjects()
124    {
125    }
126
127    @Override
128    public void setMaxInactiveInterval(int seconds)
129    {
130        nyi("setMaxInactiveInterval");
131    }
132}