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.portlet;
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.portlet.PortletContext;
26  import javax.portlet.PortletRequest;
27  import javax.portlet.PortletResponse;
28  import javax.portlet.PortletSession;
29  import java.util.Locale;
30  
31  
32  // Test case for org.apache.commons.chain.web.portlet.PortletGetLocaleCommand
33  
34  public class PortletGetLocaleCommandTestCase 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 PortletGetLocaleCommandTestCase(String name) {
45          super(name);
46      }
47  
48  
49      // ----------------------------------------------------- Instance Variables
50  
51  
52      protected Locale locale = null;
53  
54      // Portlet API Objects
55      protected PortletContext pcontext = null;
56      protected PortletRequest request = null;
57      protected PortletResponse response = null;
58      protected PortletSession session = null;
59  
60      // Chain API Objects
61      protected Context context = null;
62      protected PortletGetLocaleCommand 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 Portlet API Objects
76          pcontext = new MockPortletContext();
77          session = new MockPortletSession(pcontext);
78          request = new MockPortletRequest(null, pcontext, session);
79          ((MockPortletRequest) request).setLocale(locale);
80  
81          // Set up Chain API Objects
82          context = new PortletWebContext(pcontext, request, response);
83          command = new PortletGetLocaleCommand();
84  
85      }
86  
87  
88      /**
89       * Return the tests included in this test suite.
90       */
91      public static Test suite() {
92  
93          return (new TestSuite(PortletGetLocaleCommandTestCase.class));
94  
95      }
96  
97  
98      /**
99       * Tear down instance variables required by this test case.
100      */
101     public void tearDown() {
102 
103         pcontext = null;
104         session = null;
105         request = null;
106         response = null;
107 
108         context = null;
109         command = null;
110 
111     }
112 
113 
114     // ------------------------------------------------- Individual Test Methods
115 
116 
117     // Test configured behavior
118     public void testConfigured() throws Exception {
119 
120         command.setLocaleKey("special");
121         assertEquals("special", command.getLocaleKey());
122         check(context, command);
123 
124     }
125 
126 
127     // Test default behavior
128     public void testDefaut() throws Exception {
129 
130         assertEquals("locale", command.getLocaleKey());
131         check(context, command);
132 
133     }
134 
135 
136     // --------------------------------------------------------- Support Methods
137 
138 
139     protected void check(Context context, PortletGetLocaleCommand command)
140         throws Exception {
141 
142         String localeKey = command.getLocaleKey();
143         assertNotNull(localeKey);
144         Object value = context.get(localeKey);
145         assertNull(value);
146         boolean result = command.execute(context);
147         assertFalse(result);
148         value = context.get(localeKey);
149         assertNotNull(value);
150         assertTrue(value instanceof Locale);
151         assertEquals(locale, (Locale) value);
152 
153     }
154 
155 
156 }