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.portlet;
018
019import static org.junit.jupiter.api.Assertions.assertEquals;
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertInstanceOf;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertNull;
024
025import java.util.Locale;
026
027import javax.portlet.PortletContext;
028import javax.portlet.PortletRequest;
029import javax.portlet.PortletResponse;
030import javax.portlet.PortletSession;
031
032import org.apache.commons.chain.web.javax.portlet.PortletGetLocaleCommand;
033import org.apache.commons.chain.web.javax.portlet.PortletWebContext;
034import org.junit.jupiter.api.AfterEach;
035import org.junit.jupiter.api.BeforeEach;
036import org.junit.jupiter.api.Test;
037
038/**
039 * Test case for {@link org.apache.commons.chain.web.javax.portlet.PortletGetLocaleCommand}
040 */
041public class PortletGetLocaleCommandTestCase {
042
043    // ----------------------------------------------------- Instance Variables
044
045    protected Locale locale = null;
046
047    /**
048     * Portlet API Objects - Context
049     */
050    protected PortletContext pcontext = null;
051
052    /**
053     * Portlet API Objects - Request
054     */
055    protected PortletRequest request = null;
056
057    /**
058     * Portlet API Objects - Response
059     */
060
061    protected PortletResponse response = null;
062
063    /**
064     * Portlet API Objects - Session
065     */
066    protected PortletSession session = null;
067
068    /**
069     * Chain API Objects - context
070     */
071    protected PortletWebContext context = null;
072
073    /**
074     * Chain API Objects - command
075     */
076    protected PortletGetLocaleCommand command = null;
077
078    // ---------------------------------------------------------- Constructors
079
080    /**
081     * The Default-Constructor for this class.
082     */
083    public PortletGetLocaleCommandTestCase() {
084    }
085
086    // -------------------------------------------------- Overall Test Methods
087
088    /**
089     * Set up instance variables required by this test case.
090     */
091    @BeforeEach
092    public void init() {
093        locale = new Locale("en", "US");
094
095        // Set up Portlet API Objects
096        pcontext = new MockPortletContext();
097        session = new MockPortletSession(pcontext);
098        request = new MockPortletRequest(null, pcontext, session);
099        ((MockPortletRequest) request).setLocale(locale);
100
101        // Set up Chain API Objects
102        context = new PortletWebContext(pcontext, request, response);
103        command = new PortletGetLocaleCommand();
104    }
105
106    /**
107     * Tear down instance variables required by this test case.
108     */
109    @AfterEach
110    public void tearDown() {
111        pcontext = null;
112        session = null;
113        request = null;
114        response = null;
115
116        context = null;
117        command = null;
118    }
119
120    // ------------------------------------------------- Individual Test Methods
121
122    /**
123     * Test configured behavior
124     *
125     * @throws Exception any error
126     */
127    @Test
128    public void testConfigured() throws Exception {
129        command.setLocaleKey("special");
130        assertEquals("special", command.getLocaleKey());
131        check(context, command);
132    }
133
134    /**
135     * Test default behavior
136     *
137     * @throws Exception any error
138     */
139    @Test
140    public void testDefaut() throws Exception {
141        assertEquals("locale", command.getLocaleKey());
142        check(context, command);
143    }
144
145    // --------------------------------------------------------- Support Methods
146
147    protected void check(PortletWebContext context, PortletGetLocaleCommand command)
148            throws Exception {
149
150        String localeKey = command.getLocaleKey();
151        assertNotNull(localeKey);
152        Object value = context.get(localeKey);
153        assertNull(value);
154        boolean result = command.execute(context);
155        assertFalse(result);
156        value = context.get(localeKey);
157        assertNotNull(value);
158        assertInstanceOf(Locale.class, value);
159        assertEquals(locale, value);
160    }
161}