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.ServletGetLocaleCommand
030:
031: public class ServletGetLocaleCommandTestCase 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 ServletGetLocaleCommandTestCase(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: ((MockHttpServletRequest) request).setLocale(locale);
073: response = new MockHttpServletResponse();
074:
075: // Set up Chain API Objects
076: context = new ServletWebContext(scontext, request, response);
077: command = new ServletGetLocaleCommand();
078:
079: }
080:
081: /**
082: * Return the tests included in this test suite.
083: */
084: public static Test suite() {
085:
086: return (new TestSuite(ServletGetLocaleCommandTestCase.class));
087:
088: }
089:
090: /**
091: * Tear down instance variables required by this test case.
092: */
093: public void tearDown() {
094:
095: scontext = null;
096: session = null;
097: request = null;
098: response = null;
099:
100: context = null;
101: command = null;
102:
103: }
104:
105: // ------------------------------------------------- Individual Test Methods
106:
107: // Test configured behavior
108: public void testConfigured() throws Exception {
109:
110: command.setLocaleKey("special");
111: assertEquals("special", command.getLocaleKey());
112: check(context, command);
113:
114: }
115:
116: // Test default behavior
117: public void testDefaut() throws Exception {
118:
119: assertEquals("locale", command.getLocaleKey());
120: check(context, command);
121:
122: }
123:
124: // --------------------------------------------------------- Support Methods
125:
126: protected void check(Context context,
127: ServletGetLocaleCommand command) throws Exception {
128:
129: String localeKey = command.getLocaleKey();
130: assertNotNull(localeKey);
131: Object value = context.get(localeKey);
132: assertNull(value);
133: boolean result = command.execute(context);
134: assertFalse(result);
135: value = context.get(localeKey);
136: assertNotNull(value);
137: assertTrue(value instanceof Locale);
138: assertEquals(locale, (Locale) value);
139:
140: }
141:
142: }
|