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;
023
024import java.util.Enumeration;
025
026import org.junit.jupiter.api.BeforeEach;
027import org.junit.jupiter.api.Test;
028
029import jakarta.servlet.http.HttpServletRequest;
030
031/**
032 * Tests {@link RequestScopeExtractor}.
033 *
034 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
035 * Jakarta EE 9.</p>
036 */
037public class RequestScopeExtractorTest {
038
039    /**
040     * Empty default constructor
041     */
042    public RequestScopeExtractorTest() {
043    }
044
045    /**
046     * The request.
047     */
048    private HttpServletRequest request;
049
050    /**
051     * The extractor to test.
052     */
053    private RequestScopeExtractor extractor;
054
055    /**
056     * Sets up the test.
057     */
058    @BeforeEach
059    public void setUp() {
060        request = createMock(HttpServletRequest.class);
061        extractor = new RequestScopeExtractor(request);
062    }
063
064    /**
065     * Test method for {@link RequestScopeExtractor#setValue(String, Object)}.
066     */
067    @Test
068    public void testSetValue() {
069        request.setAttribute("name", "value");
070
071        replay(request);
072        extractor.setValue("name", "value");
073        verify(request);
074    }
075
076    /**
077     * Test method for {@link RequestScopeExtractor#removeValue(String)}.
078     */
079    @Test
080    public void testRemoveValue() {
081        request.removeAttribute("name");
082
083        replay(request);
084        extractor.removeValue("name");
085        verify(request);
086    }
087
088    /**
089     * Test method for {@link RequestScopeExtractor#getKeys()}.
090     */
091    @Test
092    public void testGetKeys() {
093        Enumeration<String> keys = createMock(Enumeration.class);
094
095        expect(request.getAttributeNames()).andReturn(keys);
096
097        replay(request, keys);
098        assertEquals(keys, extractor.getKeys());
099        verify(request, keys);
100    }
101
102    /**
103     * Test method for {@link RequestScopeExtractor#getValue(String)}.
104     */
105    @Test
106    public void testGetValue() {
107        expect(request.getAttribute("name")).andReturn("value");
108
109        replay(request);
110        assertEquals("value", extractor.getValue("name"));
111        verify(request);
112    }
113}