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