1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.chain.web.servlet;
18
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 import org.apache.commons.chain.Context;
24
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession;
29 import java.util.Locale;
30
31
32
33
34 public class ServletGetLocaleCommandTestCase extends TestCase {
35
36
37
38
39
40
41
42
43
44 public ServletGetLocaleCommandTestCase(String name) {
45 super(name);
46 }
47
48
49
50
51
52 protected Locale locale = null;
53
54
55 protected ServletContext scontext = null;
56 protected HttpServletRequest request = null;
57 protected HttpServletResponse response = null;
58 protected HttpSession session = null;
59
60
61 protected Context context = null;
62 protected ServletGetLocaleCommand command = null;
63
64
65
66
67
68
69
70
71 public void setUp() {
72
73 locale = new Locale("en", "US");
74
75
76 scontext = new MockServletContext();
77 session = new MockHttpSession(scontext);
78 request = new MockHttpServletRequest("/context", "/servlet",
79 "/path/info", "a=b&c=d",
80 session);
81 ((MockHttpServletRequest) request).setLocale(locale);
82 response = new MockHttpServletResponse();
83
84
85 context = new ServletWebContext(scontext, request, response);
86 command = new ServletGetLocaleCommand();
87
88 }
89
90
91
92
93
94 public static Test suite() {
95
96 return (new TestSuite(ServletGetLocaleCommandTestCase.class));
97
98 }
99
100
101
102
103
104 public void tearDown() {
105
106 scontext = null;
107 session = null;
108 request = null;
109 response = null;
110
111 context = null;
112 command = null;
113
114 }
115
116
117
118
119
120
121 public void testConfigured() throws Exception {
122
123 command.setLocaleKey("special");
124 assertEquals("special", command.getLocaleKey());
125 check(context, command);
126
127 }
128
129
130
131 public void testDefaut() throws Exception {
132
133 assertEquals("locale", command.getLocaleKey());
134 check(context, command);
135
136 }
137
138
139
140
141
142 protected void check(Context context, ServletGetLocaleCommand command)
143 throws Exception {
144
145 String localeKey = command.getLocaleKey();
146 assertNotNull(localeKey);
147 Object value = context.get(localeKey);
148 assertNull(value);
149 boolean result = command.execute(context);
150 assertFalse(result);
151 value = context.get(localeKey);
152 assertNotNull(value);
153 assertTrue(value instanceof Locale);
154 assertEquals(locale, (Locale) value);
155
156 }
157
158
159 }