KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > servlet > ServletSetLocaleCommandTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.chain.web.servlet;
17
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 import org.apache.commons.chain.Context;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28 import java.util.Locale JavaDoc;
29
30
31 // Test case for org.apache.commons.chain.web.servlet.ServletSetLocaleCommand
32

33 public class ServletSetLocaleCommandTestCase extends TestCase {
34
35
36     // ---------------------------------------------------------- Constructors
37

38     /**
39      * Construct a new instance of this test case.
40      *
41      * @param name Name of the test case
42      */

43     public ServletSetLocaleCommandTestCase(String JavaDoc name) {
44         super(name);
45     }
46
47
48     // ----------------------------------------------------- Instance Variables
49

50
51     protected Locale JavaDoc locale = null;
52
53     // Servlet API Objects
54
protected ServletContext JavaDoc scontext = null;
55     protected HttpServletRequest JavaDoc request = null;
56     protected HttpServletResponse JavaDoc response = null;
57     protected HttpSession JavaDoc session = null;
58
59     // Chain API Objects
60
protected Context context = null;
61     protected ServletGetLocaleCommand command = null;
62
63
64     // -------------------------------------------------- Overall Test Methods
65

66
67     /**
68      * Set up instance variables required by this test case.
69      */

70     public void setUp() {
71
72     locale = new Locale JavaDoc("en", "US");
73
74     // Set up Servlet API Objects
75
scontext = new MockServletContext();
76         session = new MockHttpSession(scontext);
77         request = new MockHttpServletRequest("/context", "/servlet",
78                                              "/path/info", "a=b&c=d",
79                                              session);
80         response = new MockHttpServletResponse();
81
82     // Set up Chain API Objects
83
context = new ServletWebContext(scontext, request, response);
84     command = new ServletGetLocaleCommand();
85
86     }
87
88
89     /**
90      * Return the tests included in this test suite.
91      */

92     public static Test suite() {
93
94         return (new TestSuite(ServletGetLocaleCommandTestCase.class));
95
96     }
97
98
99     /**
100      * Tear down instance variables required by this test case.
101      */

102     public void tearDown() {
103
104         scontext = null;
105         session = null;
106         request = null;
107         response = null;
108
109         context = null;
110     command = null;
111
112     }
113
114
115     // ------------------------------------------------- Individual Test Methods
116

117
118     // Test configured behavior
119
public void testConfigured() throws Exception JavaDoc {
120
121     command.setLocaleKey("special");
122     assertEquals("special", command.getLocaleKey());
123     check(context, command);
124
125     }
126
127
128     // Test default behavior
129
public void testDefaut() throws Exception JavaDoc {
130
131     assertEquals("locale", command.getLocaleKey());
132     check(context, command);
133
134     }
135
136
137     // --------------------------------------------------------- Support Methods
138

139
140     protected void check(Context context, ServletGetLocaleCommand command)
141     throws Exception JavaDoc {
142
143     String JavaDoc localeKey = command.getLocaleKey();
144     assertNotNull(localeKey);
145     Object JavaDoc value = context.get(localeKey);
146     assertNull(value);
147     context.put(localeKey, locale);
148     assertNotNull(context.get(localeKey));
149     assertNull(response.getLocale());
150     boolean result = command.execute(context);
151     assertFalse(result);
152     assertNotNull(response.getLocale());
153     assertEquals(locale, response.getLocale());
154
155     }
156
157
158 }
159
Popular Tags