001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.chain.web.servlet;
018    
019    
020    import junit.framework.Test;
021    import junit.framework.TestCase;
022    import junit.framework.TestSuite;
023    import org.apache.commons.chain.Context;
024    
025    import javax.servlet.ServletContext;
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    import javax.servlet.http.HttpSession;
029    import java.util.Locale;
030    
031    
032    // Test case for org.apache.commons.chain.web.servlet.ServletGetLocaleCommand
033    
034    public class ServletGetLocaleCommandTestCase extends TestCase {
035    
036    
037        // ---------------------------------------------------------- Constructors
038    
039        /**
040         * Construct a new instance of this test case.
041         *
042         * @param name Name of the test case
043         */
044        public ServletGetLocaleCommandTestCase(String name) {
045            super(name);
046        }
047    
048    
049        // ----------------------------------------------------- Instance Variables
050    
051    
052        protected Locale locale = null;
053    
054        // Servlet API Objects
055        protected ServletContext scontext = null;
056        protected HttpServletRequest request = null;
057        protected HttpServletResponse response = null;
058        protected HttpSession session = null;
059    
060        // Chain API Objects
061        protected Context context = null;
062        protected ServletGetLocaleCommand command = null;
063    
064    
065        // -------------------------------------------------- Overall Test Methods
066    
067    
068        /**
069         * Set up instance variables required by this test case.
070         */
071        public void setUp() {
072    
073        locale = new Locale("en", "US");
074    
075        // Set up Servlet API Objects
076            scontext = new MockServletContext();
077            session = new MockHttpSession(scontext);
078            request = new MockHttpServletRequest("/context", "/servlet",
079                                                 "/path/info", "a=b&c=d",
080                                                 session);
081        ((MockHttpServletRequest) request).setLocale(locale);
082            response = new MockHttpServletResponse();
083    
084        // Set up Chain API Objects
085            context = new ServletWebContext(scontext, request, response);
086        command = new ServletGetLocaleCommand();
087    
088        }
089    
090    
091        /**
092         * Return the tests included in this test suite.
093         */
094        public static Test suite() {
095    
096            return (new TestSuite(ServletGetLocaleCommandTestCase.class));
097    
098        }
099    
100    
101        /**
102         * Tear down instance variables required by this test case.
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        // ------------------------------------------------- Individual Test Methods
118    
119    
120        // Test configured behavior
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        // Test default behavior
131        public void testDefaut() throws Exception {
132    
133        assertEquals("locale", command.getLocaleKey());
134        check(context, command);
135    
136        }
137    
138    
139        // --------------------------------------------------------- Support Methods
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    }