View Javadoc

1   /*
2    * $Id: CheckboxInterceptorTest.java 655721 2008-05-13 03:14:03Z mrdon $
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, Object> param;
40      
41      protected void setUp() throws Exception {
42      	super.setUp();
43      	param = new HashMap<String, Object>();
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", ((String[])param.get("superpower"))[0]);
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", ((String[])param.get("superpower"))[0]);
123 	}
124 
125     public void testTwoCheckboxNoValue() throws Exception {
126 		param.put("user", "batman");
127 		param.put("email", "batman@comic.org");
128 		param.put("__checkbox_superpower", new String[]{"true","true"});
129 
130 		interceptor.init();
131 		interceptor.intercept(ai);
132 		interceptor.destroy();
133 
134 		assertFalse(param.containsKey("__checkbox_superpower"));
135 		assertEquals(2, param.size()); // should be 2 as __checkbox_ should be removed
136 		assertNull(param.get("superpower"));
137     }
138 
139     public void testTwoCheckboxMixed() throws Exception {
140 		param.put("user", "batman");
141 		param.put("email", "batman@comic.org");
142 		param.put("__checkbox_superpower", "true");
143 		param.put("superpower", "yes");
144 		param.put("__checkbox_cool", "no");
145 		assertTrue(param.containsKey("__checkbox_superpower"));
146 		assertTrue(param.containsKey("__checkbox_cool"));
147 
148 		interceptor.init();
149 		interceptor.intercept(ai);
150 		interceptor.destroy();
151 		
152 		assertFalse(param.containsKey("__checkbox_superpower"));
153 		assertFalse(param.containsKey("__checkbox_cool"));
154 		assertEquals(4, param.size()); // should be 4 as __checkbox_ should be removed
155 		assertEquals("yes", param.get("superpower"));
156 		assertEquals("false", ((String[])param.get("cool"))[0]); // will use false as default and not 'no'
157 	}
158 
159 	public void testTwoCheckboxMixedWithDifferentDefault() throws Exception {
160 		param.put("user", "batman");
161 		param.put("email", "batman@comic.org");
162 		param.put("__checkbox_superpower", "true");
163 		param.put("superpower", "yes");
164 		param.put("__checkbox_cool", "no");
165 		assertTrue(param.containsKey("__checkbox_superpower"));
166 		assertTrue(param.containsKey("__checkbox_cool"));
167 
168 		interceptor.setUncheckedValue("no");
169 		interceptor.init();
170 		interceptor.intercept(ai);
171 		interceptor.destroy();
172 		
173 		assertFalse(param.containsKey("__checkbox_superpower"));
174 		assertFalse(param.containsKey("__checkbox_cool"));
175 		assertEquals(4, param.size()); // should be 4 as __checkbox_ should be removed
176 		assertEquals("yes", param.get("superpower"));
177 		assertEquals("no", ((String[])param.get("cool"))[0]);
178 	}
179 	
180 }