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;
023import static org.junit.jupiter.api.Assertions.assertInstanceOf;
024import static org.junit.jupiter.api.Assertions.assertNotNull;
025import static org.junit.jupiter.api.Assertions.assertNull;
026
027import java.io.IOException;
028import java.net.URL;
029import java.util.Collection;
030import java.util.Locale;
031
032import org.apache.tiles.request.ApplicationResource;
033import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
034import org.apache.tiles.request.collection.ScopeMap;
035import org.junit.jupiter.api.BeforeEach;
036import org.junit.jupiter.api.Test;
037
038import jakarta.servlet.ServletContext;
039
040/**
041 * Tests {@link ServletApplicationContext}.
042 *
043 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
044 * Jakarta EE 9.</p>
045 */
046public class ServletApplicationContextTest {
047
048    /**
049     * Empty default constructor
050     */
051    public ServletApplicationContextTest() {
052    }
053
054    /**
055     * The servlet context.
056     */
057    private ServletContext servletContext;
058
059    /**
060     * The application context to test.
061     */
062    private ServletApplicationContext context;
063
064    /**
065     * Sets up the test.
066     */
067    @BeforeEach
068    public void setUp() {
069        servletContext = createMock(ServletContext.class);
070        context = new ServletApplicationContext(servletContext);
071    }
072
073    /**
074     * Test method for {@link ServletApplicationContext#getContext()}.
075     */
076    @Test
077    public void testGetContext() {
078        replay(servletContext);
079        assertEquals(servletContext, context.getContext());
080        verify(servletContext);
081    }
082
083    /**
084     * Test method for {@link ServletApplicationContext#getApplicationScope()}.
085     */
086    @Test
087    public void testGetApplicationScope() {
088        replay(servletContext);
089        assertInstanceOf(ScopeMap.class, context.getApplicationScope());
090        verify(servletContext);
091    }
092
093    /**
094     * Test method for {@link ServletApplicationContext#getInitParams()}.
095     */
096    @Test
097    public void testGetInitParams() {
098        replay(servletContext);
099        assertInstanceOf(ReadOnlyEnumerationMap.class, context.getInitParams());
100        verify(servletContext);
101    }
102
103    /**
104     * Test method for {@link ServletApplicationContext#getResource(String)}.
105     *
106     * @throws IOException If something goes wrong.
107     */
108    @Test
109    public void testGetResource() throws IOException {
110        URL url = new URL("file:///servletContext/my/path.html");
111        URL urlFr = new URL("file:///servletContext/my/path_fr.html");
112        expect(servletContext.getResource("/my/path.html")).andReturn(url);
113        expect(servletContext.getResource("/my/path_fr.html")).andReturn(urlFr);
114        expect(servletContext.getResource("/null/path.html")).andReturn(null);
115
116        replay(servletContext);
117        ApplicationResource resource = context.getResource("/my/path.html");
118        assertNotNull(resource);
119        assertEquals("/my/path.html", resource.getLocalePath());
120        assertEquals("/my/path.html", resource.getPath());
121        assertEquals(Locale.ROOT, resource.getLocale());
122        ApplicationResource resourceFr = context.getResource(resource, Locale.FRENCH);
123        assertNotNull(resourceFr);
124        assertEquals("/my/path_fr.html", resourceFr.getLocalePath());
125        assertEquals("/my/path.html", resourceFr.getPath());
126        assertEquals(Locale.FRENCH, resourceFr.getLocale());
127        ApplicationResource nullResource = context.getResource("/null/path.html");
128        assertNull(nullResource);
129        verify(servletContext);
130    }
131
132    /**
133     * Test method for {@link ServletApplicationContext#getResources(String)}.
134     *
135     * @throws IOException If something goes wrong.
136     */
137    @Test
138    public void testGetResources() throws IOException {
139        URL url = new URL("file:///servletContext/my/path");
140        expect(servletContext.getResource("/my/path")).andReturn(url);
141
142        replay(servletContext);
143        Collection<ApplicationResource> resources = context.getResources("/my/path");
144        assertEquals(1, resources.size());
145        assertEquals("/my/path", resources.iterator().next().getLocalePath());
146        verify(servletContext);
147    }
148}