View Javadoc

1   /*
2    * $Id: MessageStoreInterceptorTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  package org.apache.struts2.interceptor;
24  
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import com.opensymphony.xwork2.ActionContext;
30  import com.opensymphony.xwork2.mock.MockActionInvocation;
31  
32  import org.apache.struts2.StrutsTestCase;
33  
34  /***
35   * Unit test for MultiselectInterceptor.
36   */
37  public class MultiselectInterceptorTest extends StrutsTestCase {
38  
39      private MultiselectInterceptor interceptor;
40      private MockActionInvocation ai;
41      private Map<String, Object> param;
42  
43      protected void setUp() throws Exception {
44          super.setUp();
45          param = new HashMap<String, Object>();
46  
47          interceptor = new MultiselectInterceptor();
48          ai = new MockActionInvocation();
49          ai.setInvocationContext(ActionContext.getContext());
50          ActionContext.getContext().setParameters(param);
51      }
52  
53      public void testNoParam() throws Exception {
54          interceptor.init();
55          interceptor.intercept(ai);
56          interceptor.destroy();
57  
58          assertEquals(0, param.size());
59      }
60  
61      public void testPassthroughOne() throws Exception {
62          param.put("user", "batman");
63          interceptor.init();
64          interceptor.intercept(ai);
65          interceptor.destroy();
66  
67          assertEquals(1, param.size());
68      }
69  
70      public void testPassthroughTwo() throws Exception {
71          param.put("user", "batman");
72          param.put("email", "batman@comic.org");
73          interceptor.init();
74          interceptor.intercept(ai);
75          interceptor.destroy();
76  
77          assertEquals(2, param.size());
78      }
79  
80      public void testSelectedMultiselect() throws Exception {
81          param.put("user", "batman");
82          param.put("email", "batman@comic.org");
83          param.put("superpower", "robin");
84          param.put("__multiselect_superpower", "");
85          assertTrue(param.containsKey("__multiselect_superpower"));
86  
87          interceptor.init();
88          interceptor.intercept(ai);
89          interceptor.destroy();
90  
91          assertFalse(param.containsKey("__multiselect_superpower"));
92          assertEquals(3, param.size()); // should be 3 as __multiselect_ should be removed
93          assertEquals("robin", param.get("superpower"));
94      }
95  
96      public void testMultiselectNoValue() throws Exception {
97          param.put("user", "batman");
98          param.put("email", "batman@comic.org");
99          param.put("__multiselect_superpower", "");
100         assertTrue(param.containsKey("__multiselect_superpower"));
101 
102         interceptor.init();
103         interceptor.intercept(ai);
104         interceptor.destroy();
105 
106         assertFalse(param.containsKey("__multiselect_superpower"));
107         assertEquals(3, param.size()); // should be 3 as __multiselect_ should be removed
108         assertEquals(0, ((String[]) param.get("superpower")).length);
109     }
110 
111     public void testTwoMultiselect() throws Exception {
112         param.put("user", "batman");
113         param.put("email", "batman@comic.org");
114         param.put("__multiselect_superpower", "");
115         param.put("superpower", "yes");
116         param.put("__multiselect_cool", "");
117         assertTrue(param.containsKey("__multiselect_superpower"));
118         assertTrue(param.containsKey("__multiselect_cool"));
119 
120         interceptor.init();
121         interceptor.intercept(ai);
122         interceptor.destroy();
123 
124         assertFalse(param.containsKey("__multiselect_superpower"));
125         assertFalse(param.containsKey("__multiselect_cool"));
126         assertEquals(4, param.size()); // should be 4 as __multiselect_ should be removed
127         assertEquals("yes", param.get("superpower"));
128         assertEquals(0, ((String[]) param.get("cool")).length);
129     }
130 
131 }