001/*
002 * Copyright 2023 Web-Legacy
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.apache.tiles.request.jakarta.servlet.extractor;
017
018import static org.easymock.EasyMock.createMock;
019import static org.easymock.EasyMock.expect;
020import static org.easymock.EasyMock.replay;
021import static org.easymock.EasyMock.verify;
022import static org.junit.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertFalse;
024import static org.junit.jupiter.api.Assertions.assertNotNull;
025import static org.junit.jupiter.api.Assertions.assertNull;
026
027import java.util.Enumeration;
028
029import org.junit.jupiter.api.BeforeEach;
030import org.junit.jupiter.api.Test;
031
032import jakarta.servlet.http.HttpServletRequest;
033import jakarta.servlet.http.HttpSession;
034
035/**
036 * Tests {@link SessionScopeExtractor}.
037 *
038 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
039 * Jakarta EE 9.</p>
040 */
041public class SessionScopeExtractorTest {
042
043    /**
044     * Empty default constructor
045     */
046    public SessionScopeExtractorTest() {
047    }
048
049    /**
050     * The request.
051     */
052    private HttpServletRequest request;
053
054    /**
055     * The session.
056     */
057    private HttpSession session;
058
059    /**
060     * The extractor to test.
061     */
062    private SessionScopeExtractor extractor;
063
064    /**
065     * Sets up the test.
066     */
067    @BeforeEach
068    public void setUp() {
069        request = createMock(HttpServletRequest.class);
070        session = createMock(HttpSession.class);
071        extractor = new SessionScopeExtractor(request);
072    }
073
074    /**
075     * Test method for {@link SessionScopeExtractor#setValue(String, Object)}.
076     */
077    @Test
078    public void testSetValue() {
079        expect(request.getSession()).andReturn(session);
080        session.setAttribute("name", "value");
081
082        replay(request, session);
083        extractor.setValue("name", "value");
084        verify(request, session);
085    }
086
087    /**
088     * Test method for {@link SessionScopeExtractor#removeValue(String)}.
089     */
090    @Test
091    public void testRemoveValue() {
092        expect(request.getSession(false)).andReturn(session);
093        session.removeAttribute("name");
094
095        replay(request, session);
096        extractor.removeValue("name");
097        verify(request, session);
098    }
099
100    /**
101     * Test method for {@link SessionScopeExtractor#getKeys()}.
102     */
103    @Test
104    public void testGetKeys() {
105        Enumeration<String> keys = createMock(Enumeration.class);
106
107        expect(request.getSession(false)).andReturn(session);
108        expect(session.getAttributeNames()).andReturn(keys);
109
110        replay(request, session, keys);
111        assertEquals(keys, extractor.getKeys());
112        verify(request, session, keys);
113    }
114
115    /**
116     * Test method for {@link SessionScopeExtractor#getKeys()}.
117     */
118    @Test
119    public void testGetKeysNoSession() {
120        expect(request.getSession(false)).andReturn(null);
121
122        replay(request, session);
123        Enumeration<String> keys = extractor.getKeys();
124        assertNotNull(keys);
125        assertFalse(keys.hasMoreElements());
126        verify(request, session);
127    }
128
129    /**
130     * Test method for {@link SessionScopeExtractor#getValue(String)}.
131     */
132    @Test
133    public void testGetValue() {
134        expect(request.getSession(false)).andReturn(session);
135        expect(session.getAttribute("name")).andReturn("value");
136
137        replay(request, session);
138        assertEquals("value", extractor.getValue("name"));
139        verify(request, session);
140    }
141
142    /**
143     * Test method for {@link SessionScopeExtractor#getValue(String)}.
144     */
145    @Test
146    public void testGetValueNoSession() {
147        expect(request.getSession(false)).andReturn(null);
148
149        replay(request, session);
150        assertNull(extractor.getValue("name"));
151        verify(request, session);
152    }
153}