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 InitParameterExtractor}.
033 *
034 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
035 * Jakarta EE 9.</p>
036 */
037public class InitParameterExtractorTest {
038
039    /**
040     * Empty default constructor
041     */
042    public InitParameterExtractorTest() {
043    }
044
045    /**
046     * The servlet context.
047     */
048    private ServletContext context;
049
050    /**
051     * The extractor to test.
052     */
053    private InitParameterExtractor extractor;
054
055    /**
056     * Sets up the test.
057     */
058    @BeforeEach
059    public void setUp() {
060        context = createMock(ServletContext.class);
061        extractor = new InitParameterExtractor(context);
062    }
063
064    /**
065     * Test method for {@link InitParameterExtractor#getKeys()}.
066     */
067    @Test
068    public void testGetKeys() {
069        Enumeration<String> keys = createMock(Enumeration.class);
070
071        expect(context.getInitParameterNames()).andReturn(keys);
072
073        replay(context, keys);
074        assertEquals(keys, extractor.getKeys());
075        verify(context, keys);
076    }
077
078    /**
079     * Test method for {@link InitParameterExtractor#getValue(String)}.
080     */
081    @Test
082    public void testGetValue() {
083        expect(context.getInitParameter("name")).andReturn("value");
084
085        replay(context);
086        assertEquals("value", extractor.getValue("name"));
087        verify(context);
088    }
089}