001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
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: */
016: package org.apache.commons.chain.web.servlet;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021: import org.apache.commons.chain.Context;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026: import javax.servlet.http.HttpSession;
027: import java.util.Locale;
028:
029: // Test case for org.apache.commons.chain.web.servlet.ServletSetLocaleCommand
030:
031: public class ServletSetLocaleCommandTestCase extends TestCase {
032:
033: // ---------------------------------------------------------- Constructors
034:
035: /**
036: * Construct a new instance of this test case.
037: *
038: * @param name Name of the test case
039: */
040: public ServletSetLocaleCommandTestCase(String name) {
041: super (name);
042: }
043:
044: // ----------------------------------------------------- Instance Variables
045:
046: protected Locale locale = null;
047:
048: // Servlet API Objects
049: protected ServletContext scontext = null;
050: protected HttpServletRequest request = null;
051: protected HttpServletResponse response = null;
052: protected HttpSession session = null;
053:
054: // Chain API Objects
055: protected Context context = null;
056: protected ServletGetLocaleCommand command = null;
057:
058: // -------------------------------------------------- Overall Test Methods
059:
060: /**
061: * Set up instance variables required by this test case.
062: */
063: public void setUp() {
064:
065: locale = new Locale("en", "US");
066:
067: // Set up Servlet API Objects
068: scontext = new MockServletContext();
069: session = new MockHttpSession(scontext);
070: request = new MockHttpServletRequest("/context", "/servlet",
071: "/path/info", "a=b&c=d", session);
072: response = new MockHttpServletResponse();
073:
074: // Set up Chain API Objects
075: context = new ServletWebContext(scontext, request, response);
076: command = new ServletGetLocaleCommand();
077:
078: }
079:
080: /**
081: * Return the tests included in this test suite.
082: */
083: public static Test suite() {
084:
085: return (new TestSuite(ServletGetLocaleCommandTestCase.class));
086:
087: }
088:
089: /**
090: * Tear down instance variables required by this test case.
091: */
092: public void tearDown() {
093:
094: scontext = null;
095: session = null;
096: request = null;
097: response = null;
098:
099: context = null;
100: command = null;
101:
102: }
103:
104: // ------------------------------------------------- Individual Test Methods
105:
106: // Test configured behavior
107: public void testConfigured() throws Exception {
108:
109: command.setLocaleKey("special");
110: assertEquals("special", command.getLocaleKey());
111: check(context, command);
112:
113: }
114:
115: // Test default behavior
116: public void testDefaut() throws Exception {
117:
118: assertEquals("locale", command.getLocaleKey());
119: check(context, command);
120:
121: }
122:
123: // --------------------------------------------------------- Support Methods
124:
125: protected void check(Context context,
126: ServletGetLocaleCommand command) throws Exception {
127:
128: String localeKey = command.getLocaleKey();
129: assertNotNull(localeKey);
130: Object value = context.get(localeKey);
131: assertNull(value);
132: context.put(localeKey, locale);
133: assertNotNull(context.get(localeKey));
134: assertNull(response.getLocale());
135: boolean result = command.execute(context);
136: assertFalse(result);
137: assertNotNull(response.getLocale());
138: assertEquals(locale, response.getLocale());
139:
140: }
141:
142: }
|