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