001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.chain.web.jakarta.servlet;
018
019import static org.junit.jupiter.api.Assertions.assertEquals;
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022import static org.junit.jupiter.api.Assertions.assertNull;
023
024import java.util.Locale;
025
026import org.junit.jupiter.api.AfterEach;
027import org.junit.jupiter.api.BeforeEach;
028import org.junit.jupiter.api.Test;
029
030import jakarta.servlet.ServletContext;
031import jakarta.servlet.http.HttpServletRequest;
032import jakarta.servlet.http.HttpServletResponse;
033import jakarta.servlet.http.HttpSession;
034
035/**
036 * Test case for {@link org.apache.commons.chain.web.jakarta.servlet.ServletSetLocaleCommand}
037 */
038public class ServletSetLocaleCommandTestCase {
039
040    // ----------------------------------------------------- Instance Variables
041
042    protected Locale locale = null;
043
044    /**
045     * Servlet API Objects - Context
046     */
047    protected ServletContext scontext = null;
048
049    /**
050     * Servlet API Objects - Request
051     */
052    protected HttpServletRequest request = null;
053
054    /**
055     * Servlet API Objects - Response
056     */
057    protected HttpServletResponse response = null;
058
059    /**
060     * Servlet API Objects - Session
061     */
062    protected HttpSession session = null;
063
064    /**
065     * Chain API Objects - context
066     */
067    protected ServletWebContext context = null;
068
069    /**
070     * Chain API Objects - command
071     */
072    protected ServletSetLocaleCommand command = null;
073
074    // ---------------------------------------------------------- Constructors
075
076    /**
077     * The Default-Constructor for this class.
078     */
079    public ServletSetLocaleCommandTestCase() {
080    }
081
082    // -------------------------------------------------- Overall Test Methods
083
084    /**
085     * Set up instance variables required by this test case.
086     */
087    @BeforeEach
088    public void init() {
089        locale = new Locale("en", "US");
090
091        // Set up Servlet API Objects
092        scontext = new MockServletContext();
093        session = new MockHttpSession(scontext);
094        request = new MockHttpServletRequest("/context", "/servlet",
095                                             "/path/info", "a=b&c=d",
096                                             session);
097        response = new MockHttpServletResponse();
098
099        // Set up Chain API Objects
100        context = new ServletWebContext(scontext, request, response);
101        command = new ServletSetLocaleCommand();
102    }
103
104    /**
105     * Tear down instance variables required by this test case.
106     */
107    @AfterEach
108    public void tearDown() {
109        scontext = null;
110        session = null;
111        request = null;
112        response = null;
113
114        context = null;
115        command = null;
116    }
117
118    // ------------------------------------------------- Individual Test Methods
119
120    /**
121     * Test configured behavior
122     *
123     * @throws Exception any error
124     */
125    @Test
126    public void testConfigured() throws Exception {
127        command.setLocaleKey("special");
128        assertEquals("special", command.getLocaleKey());
129        check(context, command);
130    }
131
132    /**
133     * Test default behavior
134     *
135     * @throws Exception any error
136     */
137    @Test
138    public void testDefaut() throws Exception {
139        assertEquals("locale", command.getLocaleKey());
140        check(context, command);
141    }
142
143    // --------------------------------------------------------- Support Methods
144
145    protected void check(ServletWebContext context, ServletSetLocaleCommand command)
146            throws Exception {
147
148        String localeKey = command.getLocaleKey();
149        assertNotNull(localeKey);
150        Object value = context.get(localeKey);
151        assertNull(value);
152        context.put(localeKey, locale);
153        assertNotNull(context.get(localeKey));
154        assertNull(response.getLocale());
155        boolean result = command.execute(context);
156        assertFalse(result);
157        assertNotNull(response.getLocale());
158        assertEquals(locale, response.getLocale());
159    }
160}