View Javadoc

1   /*
2    * $Id: CheckboxInterceptorTest.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  package org.apache.struts2.interceptor;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.struts2.StrutsTestCase;
28  
29  import com.opensymphony.xwork2.ActionContext;
30  import com.opensymphony.xwork2.mock.MockActionInvocation;
31  
32  /***
33   * Unit test for ChecboxInterceptor. 
34   */
35  public class CheckboxInterceptorTest extends StrutsTestCase {
36  
37      private CheckboxInterceptor interceptor;
38      private MockActionInvocation ai;
39      private Map<String, String> param;
40      
41      protected void setUp() throws Exception {
42      	super.setUp();
43      	param = new HashMap<String, String>();
44      	
45      	interceptor = new CheckboxInterceptor();
46      	ai = new MockActionInvocation();
47      	ai.setInvocationContext(ActionContext.getContext());
48      	ActionContext.getContext().setParameters(param);
49      }
50  	
51  	public void testNoParam() throws Exception {
52  		interceptor.init();
53  		interceptor.intercept(ai);
54  		interceptor.destroy();
55  
56  		assertEquals(0, param.size());
57  	}
58  
59  	public void testPassthroughOne() throws Exception {
60  		param.put("user", "batman");
61  		interceptor.init();
62  		interceptor.intercept(ai);
63  		interceptor.destroy();
64  		
65  		assertEquals(1, param.size());
66  	}
67  
68  	public void testPassthroughTwo() throws Exception {
69  		param.put("user", "batman");
70  		param.put("email", "batman@comic.org");
71  		interceptor.init();
72  		interceptor.intercept(ai);
73  		interceptor.destroy();
74  		
75  		assertEquals(2, param.size());
76  	}
77  
78  	public void testOneCheckboxTrue() throws Exception {
79  		param.put("user", "batman");
80  		param.put("email", "batman@comic.org");
81  		param.put("superpower", "true");
82  		param.put("__checkbox_superpower", "true");
83  		assertTrue(param.containsKey("__checkbox_superpower"));
84  
85  		interceptor.init();
86  		interceptor.intercept(ai);
87  		interceptor.destroy();
88  		
89  		assertFalse(param.containsKey("__checkbox_superpower"));
90  		assertEquals(3, param.size()); // should be 3 as __checkbox_ should be removed
91  		assertEquals("true", param.get("superpower"));
92  	}
93  
94  	public void testOneCheckboxNoValue() throws Exception {
95  		param.put("user", "batman");
96  		param.put("email", "batman@comic.org");
97  		param.put("__checkbox_superpower", "false");
98  		assertTrue(param.containsKey("__checkbox_superpower"));
99  
100 		interceptor.init();
101 		interceptor.intercept(ai);
102 		interceptor.destroy();
103 		
104 		assertFalse(param.containsKey("__checkbox_superpower"));
105 		assertEquals(3, param.size()); // should be 3 as __checkbox_ should be removed
106 		assertEquals("false", param.get("superpower"));
107 	}
108 
109 	public void testOneCheckboxNoValueDifferentDefault() throws Exception {
110 		param.put("user", "batman");
111 		param.put("email", "batman@comic.org");
112 		param.put("__checkbox_superpower", "false");
113 		assertTrue(param.containsKey("__checkbox_superpower"));
114 
115 		interceptor.setUncheckedValue("off");
116 		interceptor.init();
117 		interceptor.intercept(ai);
118 		interceptor.destroy();
119 		
120 		assertFalse(param.containsKey("__checkbox_superpower"));
121 		assertEquals(3, param.size()); // should be 3 as __checkbox_ should be removed
122 		assertEquals("off", param.get("superpower"));
123 	}
124 
125 	public void testTwoCheckboxMixed() throws Exception {
126 		param.put("user", "batman");
127 		param.put("email", "batman@comic.org");
128 		param.put("__checkbox_superpower", "true");
129 		param.put("superpower", "yes");
130 		param.put("__checkbox_cool", "no");
131 		assertTrue(param.containsKey("__checkbox_superpower"));
132 		assertTrue(param.containsKey("__checkbox_cool"));
133 
134 		interceptor.init();
135 		interceptor.intercept(ai);
136 		interceptor.destroy();
137 		
138 		assertFalse(param.containsKey("__checkbox_superpower"));
139 		assertFalse(param.containsKey("__checkbox_cool"));
140 		assertEquals(4, param.size()); // should be 4 as __checkbox_ should be removed
141 		assertEquals("yes", param.get("superpower"));
142 		assertEquals("false", param.get("cool")); // will use false as default and not 'no'
143 	}
144 
145 	public void testTwoCheckboxMixedWithDifferentDefault() throws Exception {
146 		param.put("user", "batman");
147 		param.put("email", "batman@comic.org");
148 		param.put("__checkbox_superpower", "true");
149 		param.put("superpower", "yes");
150 		param.put("__checkbox_cool", "no");
151 		assertTrue(param.containsKey("__checkbox_superpower"));
152 		assertTrue(param.containsKey("__checkbox_cool"));
153 
154 		interceptor.setUncheckedValue("no");
155 		interceptor.init();
156 		interceptor.intercept(ai);
157 		interceptor.destroy();
158 		
159 		assertFalse(param.containsKey("__checkbox_superpower"));
160 		assertFalse(param.containsKey("__checkbox_cool"));
161 		assertEquals(4, param.size()); // should be 4 as __checkbox_ should be removed
162 		assertEquals("yes", param.get("superpower"));
163 		assertEquals("no", param.get("cool"));
164 	}
165 	
166 }