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;
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.io.IOException;
025
026import org.apache.tiles.request.ApplicationAccess;
027import org.apache.tiles.request.ApplicationContext;
028import org.junit.jupiter.api.Test;
029
030import jakarta.servlet.ServletContext;
031import jakarta.servlet.ServletException;
032
033/**
034 * Tests {@link ServletUtil}.
035 *
036 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for Jakarta EE 9.</p>
037 */
038public class ServletUtilTest {
039
040    /**
041     * Empty default constructor
042     */
043    public ServletUtilTest() {
044    }
045
046    /**
047     * Test method for {@link ServletUtil#wrapServletException(ServletException, String)}.
048     */
049    @Test
050    public void testWrapServletException() {
051        ServletException servletException = new ServletException();
052        IOException exception = ServletUtil.wrapServletException(servletException, "my message");
053        assertEquals(servletException, exception.getCause());
054        assertEquals("my message", exception.getMessage());
055    }
056
057    /**
058     * Test method for {@link ServletUtil#wrapServletException(ServletException, String)}.
059     */
060    @Test
061    public void testWrapServletExceptionWithCause() {
062        Throwable cause = createMock(Throwable.class);
063
064        replay(cause);
065        ServletException servletException = new ServletException(cause);
066        IOException exception = ServletUtil.wrapServletException(servletException, "my message");
067        assertEquals(cause, exception.getCause());
068        assertEquals("my message", exception.getMessage());
069        verify(cause);
070    }
071
072    /**
073     * Test method for {@link ServletUtil#getApplicationContext(ServletContext)}.
074     */
075    @Test
076    public void testGetApplicationContext() {
077        ServletContext servletContext = createMock(ServletContext.class);
078        ApplicationContext applicationContext = createMock(ApplicationContext.class);
079
080        expect(servletContext.getAttribute(ApplicationAccess
081                .APPLICATION_CONTEXT_ATTRIBUTE)).andReturn(applicationContext);
082
083        replay(servletContext, applicationContext);
084        assertEquals(applicationContext, ServletUtil.getApplicationContext(servletContext));
085        verify(servletContext, applicationContext);
086    }
087}