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