1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  // Test case for org.apache.commons.chain.web.servlet.ServletSetLocaleCommand
33  
34  public class ServletSetLocaleCommandTestCase extends TestCase {
35  
36  
37      // ---------------------------------------------------------- Constructors
38  
39      /**
40       * Construct a new instance of this test case.
41       *
42       * @param name Name of the test case
43       */
44      public ServletSetLocaleCommandTestCase(String name) {
45          super(name);
46      }
47  
48  
49      // ----------------------------------------------------- Instance Variables
50  
51  
52      protected Locale locale = null;
53  
54      // Servlet API Objects
55      protected ServletContext scontext = null;
56      protected HttpServletRequest request = null;
57      protected HttpServletResponse response = null;
58      protected HttpSession session = null;
59  
60      // Chain API Objects
61      protected Context context = null;
62      protected ServletGetLocaleCommand command = null;
63  
64  
65      // -------------------------------------------------- Overall Test Methods
66  
67  
68      /**
69       * Set up instance variables required by this test case.
70       */
71      public void setUp() {
72  
73      locale = new Locale("en", "US");
74  
75      // Set up Servlet API Objects
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          response = new MockHttpServletResponse();
82  
83      // Set up Chain API Objects
84          context = new ServletWebContext(scontext, request, response);
85      command = new ServletGetLocaleCommand();
86  
87      }
88  
89  
90      /**
91       * Return the tests included in this test suite.
92       */
93      public static Test suite() {
94  
95          return (new TestSuite(ServletGetLocaleCommandTestCase.class));
96  
97      }
98  
99  
100     /**
101      * Tear down instance variables required by this test case.
102      */
103     public void tearDown() {
104 
105         scontext = null;
106         session = null;
107         request = null;
108         response = null;
109 
110         context = null;
111     command = null;
112 
113     }
114 
115 
116     // ------------------------------------------------- Individual Test Methods
117 
118 
119     // Test configured behavior
120     public void testConfigured() throws Exception {
121 
122     command.setLocaleKey("special");
123     assertEquals("special", command.getLocaleKey());
124     check(context, command);
125 
126     }
127 
128 
129     // Test default behavior
130     public void testDefaut() throws Exception {
131 
132     assertEquals("locale", command.getLocaleKey());
133     check(context, command);
134 
135     }
136 
137 
138     // --------------------------------------------------------- Support Methods
139 
140 
141     protected void check(Context context, ServletGetLocaleCommand command)
142     throws Exception {
143 
144     String localeKey = command.getLocaleKey();
145     assertNotNull(localeKey);
146     Object value = context.get(localeKey);
147     assertNull(value);
148     context.put(localeKey, locale);
149     assertNotNull(context.get(localeKey));
150     assertNull(response.getLocale());
151     boolean result = command.execute(context);
152     assertFalse(result);
153     assertNotNull(response.getLocale());
154     assertEquals(locale, response.getLocale());
155 
156     }
157 
158 
159 }