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.assertInstanceOf; 022import static org.junit.jupiter.api.Assertions.assertNotNull; 023import static org.junit.jupiter.api.Assertions.assertNull; 024 025import java.util.Locale; 026 027import org.junit.jupiter.api.AfterEach; 028import org.junit.jupiter.api.BeforeEach; 029import org.junit.jupiter.api.Test; 030 031import jakarta.servlet.ServletContext; 032import jakarta.servlet.http.HttpServletRequest; 033import jakarta.servlet.http.HttpServletResponse; 034import jakarta.servlet.http.HttpSession; 035 036/** 037 * Test case for {@link org.apache.commons.chain.web.jakarta.servlet.ServletGetLocaleCommand} 038 */ 039public class ServletGetLocaleCommandTestCase { 040 041 // ----------------------------------------------------- Instance Variables 042 043 protected Locale locale = null; 044 045 /** 046 * Servlet API Objects - Context 047 */ 048 protected ServletContext scontext = null; 049 050 /** 051 * Servlet API Objects - Request 052 */ 053 protected HttpServletRequest request = null; 054 055 /** 056 * Servlet API Objects - Response 057 */ 058 protected HttpServletResponse response = null; 059 060 /** 061 * Servlet API Objects - Session 062 */ 063 protected HttpSession session = null; 064 065 /** 066 * Chain API Objects - context 067 */ 068 protected ServletWebContext context = null; 069 070 /** 071 * Chain API Objects - command 072 */ 073 protected ServletGetLocaleCommand command = null; 074 075 // ---------------------------------------------------------- Constructors 076 077 /** 078 * The Default-Constructor for this class. 079 */ 080 public ServletGetLocaleCommandTestCase() { 081 } 082 083 // -------------------------------------------------- Overall Test Methods 084 085 /** 086 * Set up instance variables required by this test case. 087 */ 088 @BeforeEach 089 public void init() { 090 locale = new Locale("en", "US"); 091 092 // Set up Servlet API Objects 093 scontext = new MockServletContext(); 094 session = new MockHttpSession(scontext); 095 request = new MockHttpServletRequest("/context", "/servlet", 096 "/path/info", "a=b&c=d", 097 session); 098 ((MockHttpServletRequest) request).setLocale(locale); 099 response = new MockHttpServletResponse(); 100 101 // Set up Chain API Objects 102 context = new ServletWebContext(scontext, request, response); 103 command = new ServletGetLocaleCommand(); 104 } 105 106 /** 107 * Tear down instance variables required by this test case. 108 */ 109 @AfterEach 110 public void tearDown() { 111 scontext = 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(ServletWebContext context, ServletGetLocaleCommand 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}