001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.form; 016 017 import org.apache.tapestry.IBinding; 018 import org.apache.tapestry.IForm; 019 import org.apache.tapestry.IMarkupWriter; 020 import org.apache.tapestry.IRequestCycle; 021 import org.apache.tapestry.spec.ComponentSpecification; 022 import org.apache.tapestry.valid.IValidationDelegate; 023 import org.apache.tapestry.valid.ValidationConstants; 024 import org.apache.tapestry.valid.ValidatorException; 025 import static org.easymock.EasyMock.expect; 026 import static org.easymock.EasyMock.expectLastCall; 027 import org.testng.annotations.Test; 028 029 /** 030 * Tests for the {@link org.apache.tapestry.form.Checkbox} component. 031 * 032 * @author Howard Lewis Ship 033 * @since 4.0 034 */ 035 @Test(sequential=true) 036 public class TestCheckbox extends BaseFormComponentTestCase 037 { 038 039 public void test_Render_Checked() 040 { 041 ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class); 042 043 Checkbox cb = newInstance(Checkbox.class, 044 new Object[]{ "name", "assignedName", "value", Boolean.TRUE, 045 "validatableFieldSupport", vfs }); 046 047 IForm form = newMock(IForm.class); 048 049 IMarkupWriter writer = newBufferWriter(); 050 IRequestCycle cycle = newCycle(); 051 052 IValidationDelegate delegate = newDelegate(); 053 054 expect(cycle.renderStackPush(cb)).andReturn(cb); 055 056 trainGetForm(cycle, form); 057 trainWasPrerendered(form, writer, cb, false); 058 trainGetDelegate(form, delegate); 059 060 delegate.setFormComponent(cb); 061 062 trainGetElementId(form, cb, "barney"); 063 064 trainIsRewinding(form, false); 065 066 expect(cycle.isRewinding()).andReturn(false); 067 068 form.setFormFieldUpdating(true); 069 070 delegate.writePrefix(writer, cycle, cb, null); 071 expect(delegate.isInError()).andReturn(false).anyTimes(); 072 073 vfs.renderContributions(cb, writer, cycle); 074 delegate.writeSuffix(writer, cycle, cb, null); 075 076 delegate.registerForFocus(cb, ValidationConstants.NORMAL_FIELD); 077 078 expect(cycle.renderStackPop()).andReturn(cb); 079 080 replay(); 081 082 cb.render(writer, cycle); 083 084 verify(); 085 086 assertBuffer("<input type=\"checkbox\" name=\"barney\" checked=\"checked\" id=\"barney\" />"); 087 } 088 089 public void test_Render_Disabled() 090 { 091 IForm form = newMock(IForm.class); 092 ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class); 093 094 Checkbox cb = newInstance(Checkbox.class, "name", "assignedName", "disabled", Boolean.TRUE, 095 "form", form, "validatableFieldSupport", vfs); 096 097 IMarkupWriter writer = newBufferWriter(); 098 IRequestCycle cycle = newCycle(); 099 100 IValidationDelegate delegate = newDelegate(); 101 102 trainGetDelegate(form, delegate); 103 delegate.writePrefix(writer, cycle, cb, null); 104 105 expect(delegate.isInError()).andReturn(false); 106 vfs.renderContributions(cb, writer, cycle); 107 delegate.writeSuffix(writer, cycle, cb, null); 108 109 replay(); 110 111 cb.renderFormComponent(writer, cycle); 112 113 verify(); 114 115 assertBuffer("<input type=\"checkbox\" name=\"assignedName\" disabled=\"disabled\" />"); 116 } 117 118 public void test_Render_Informal_Parameters() 119 { 120 IForm form = newMock(IForm.class); 121 ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class); 122 123 Checkbox cb = newInstance(Checkbox.class, "name", "assignedName", "value", Boolean.TRUE, "specification", 124 new ComponentSpecification(), 125 "form", form, "validatableFieldSupport", vfs); 126 127 IMarkupWriter writer = newBufferWriter(); 128 IRequestCycle cycle = newCycle(); 129 130 IBinding binding = newBinding("informal-value"); 131 132 cb.setBinding("informal", binding); 133 134 IValidationDelegate delegate = newDelegate(); 135 136 trainGetDelegate(form, delegate); 137 delegate.writePrefix(writer, cycle, cb, null); 138 139 expect(delegate.isInError()).andReturn(false).anyTimes(); 140 141 vfs.renderContributions(cb, writer, cycle); 142 143 delegate.writeSuffix(writer, cycle, cb, null); 144 145 replay(); 146 147 cb.renderFormComponent(writer, cycle); 148 149 verify(); 150 151 assertBuffer("<input type=\"checkbox\" name=\"assignedName\" checked=\"checked\" informal=\"informal-value\" />"); 152 } 153 154 public void test_Render_With_Id() 155 { 156 IForm form = newMock(IForm.class); 157 ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class); 158 159 Checkbox cb = newInstance(Checkbox.class, 160 "id", "foo", 161 "clientId", "assignedName", 162 "name", "assignedName", 163 "value", Boolean.TRUE, 164 "form", form, 165 "validatableFieldSupport", vfs); 166 167 IMarkupWriter writer = newBufferWriter(); 168 IRequestCycle cycle = newCycle(); 169 170 IValidationDelegate delegate = newDelegate(); 171 trainGetDelegate(form, delegate); 172 173 delegate.writePrefix(writer, cycle, cb, null); 174 175 expect(delegate.isInError()).andReturn(false).anyTimes(); 176 vfs.renderContributions(cb, writer, cycle); 177 delegate.writeSuffix(writer, cycle, cb, null); 178 179 replay(); 180 181 cb.renderFormComponent(writer, cycle); 182 183 verify(); 184 185 assertBuffer("<input type=\"checkbox\" name=\"assignedName\" checked=\"checked\" id=\"assignedName\" />"); 186 } 187 188 public void test_Submit_Null() 189 { 190 ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class); 191 192 Checkbox cb = newInstance(Checkbox.class, 193 "value", Boolean.TRUE, 194 "name", "checkbox", 195 "validatableFieldSupport", vfs); 196 197 IMarkupWriter writer = newWriter(); 198 IRequestCycle cycle = newCycleGetParameter("checkbox", null); 199 200 try 201 { 202 vfs.validate(cb, writer, cycle, null); 203 } 204 catch (ValidatorException e) 205 { 206 unreachable(); 207 } 208 209 replay(); 210 211 cb.rewindFormComponent(writer, cycle); 212 213 verify(); 214 215 assertEquals(false, cb.getValue()); 216 } 217 218 @SuppressWarnings("ALL") 219 public void test_Submit_Validate_Failed() 220 { 221 ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class); 222 IForm form = newMock(IForm.class); 223 224 IValidationDelegate delegate = newDelegate(); 225 226 Checkbox cb = newInstance(Checkbox.class, 227 "form", form, 228 "value", Boolean.FALSE, 229 "name", "checkbox", 230 "validatableFieldSupport", vfs); 231 232 IMarkupWriter writer = newWriter(); 233 IRequestCycle cycle = newCycleGetParameter("checkbox", "foo"); 234 235 ValidatorException exception = new ValidatorException("failed"); 236 237 try 238 { 239 vfs.validate(cb, writer, cycle, "foo"); 240 expectLastCall().andThrow(exception); 241 } 242 catch (ValidatorException e) 243 { 244 unreachable(); 245 } 246 247 expect(form.getDelegate()).andReturn(delegate); 248 delegate.record(exception); 249 250 expect(form.getDelegate()).andReturn(delegate); 251 delegate.recordFieldInputValue("false"); 252 253 replay(); 254 255 cb.rewindFormComponent(writer, cycle); 256 257 verify(); 258 259 assertEquals(false, cb.getValue()); 260 } 261 262 public void test_Submit_Non_Null() 263 { 264 ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class); 265 266 Checkbox cb = newInstance(Checkbox.class, 267 "value", Boolean.FALSE, 268 "name", "checkbox", 269 "validatableFieldSupport", vfs); 270 271 IMarkupWriter writer = newWriter(); 272 IRequestCycle cycle = newCycleGetParameter("checkbox", "foo"); 273 274 try 275 { 276 vfs.validate(cb, writer, cycle, "foo"); 277 } 278 catch (ValidatorException e) 279 { 280 unreachable(); 281 } 282 283 replay(); 284 285 cb.rewindFormComponent(writer, cycle); 286 287 verify(); 288 289 assertEquals(true, cb.getValue()); 290 } 291 }