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