1 15 package org.apache.tapestry.components; 16 17 import org.apache.hivemind.impl.DefaultClassResolver; 18 import org.apache.tapestry.IBinding; 19 import org.apache.tapestry.IForm; 20 import org.apache.tapestry.IMarkupWriter; 21 import org.apache.tapestry.IRender; 22 import org.apache.tapestry.IRequestCycle; 23 import org.apache.tapestry.TapestryUtils; 24 import org.apache.tapestry.services.DataSqueezer; 25 import org.apache.tapestry.spec.IComponentSpecification; 26 import org.apache.tapestry.util.io.DataSqueezerImpl; 27 import org.easymock.MockControl; 28 29 37 public class TestIfElse extends BaseComponentTestCase 38 { 39 private IRender newRender(IMarkupWriter writer, IRequestCycle cycle) 40 { 41 IRender render = (IRender) newMock(IRender.class); 42 43 render.render(writer, cycle); 44 45 return render; 46 } 47 48 public void testRenderPlainTrue() 49 { 50 MockControl cyclec = newControl(IRequestCycle.class); 51 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 52 53 cycle.isRewinding(); 54 cyclec.setReturnValue(false); 55 56 cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); 57 cyclec.setReturnValue(null); 58 59 cycle.setAttribute(IfBean.IF_VALUE_ATTRIBUTE, Boolean.TRUE); 60 61 cycle.getAttribute(IfBean.IF_VALUE_ATTRIBUTE); 62 cyclec.setReturnValue(Boolean.TRUE); 63 64 IMarkupWriter writer = newWriter(); 65 IRender body = newRender(writer, cycle); 66 IRender body2 = newRender(); 67 68 replayControls(); 69 70 71 IfBean conditional = (IfBean) newInstance(IfBean.class, new Object [] 72 { "condition", Boolean.TRUE }); 73 conditional.addBody(body); 74 75 conditional.render(writer, cycle); 76 77 ElseBean reverse = (ElseBean) newInstance(ElseBean.class); 78 reverse.addBody(body2); 79 reverse.render(writer, cycle); 80 81 verifyControls(); 82 } 83 84 public void testRenderPlainFalse() 85 { 86 MockControl cyclec = newControl(IRequestCycle.class); 87 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 88 89 cycle.isRewinding(); 90 cyclec.setReturnValue(false); 91 92 cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); 93 cyclec.setReturnValue(null); 94 95 cycle.setAttribute(IfBean.IF_VALUE_ATTRIBUTE, Boolean.FALSE); 96 97 cycle.getAttribute(IfBean.IF_VALUE_ATTRIBUTE); 98 cyclec.setReturnValue(Boolean.FALSE); 99 100 cycle.isRewinding(); 101 cyclec.setReturnValue(false); 102 103 IMarkupWriter writer = newWriter(); 104 IRender body = newRender(); 105 IRender body2 = newRender(writer, cycle); 106 107 replayControls(); 108 109 110 IfBean conditional = (IfBean) newInstance(IfBean.class, new Object [] 111 { "condition", Boolean.FALSE }); 112 conditional.addBody(body); 113 conditional.render(writer, cycle); 114 115 ElseBean reverse = (ElseBean) newInstance(ElseBean.class); 116 reverse.addBody(body2); 117 reverse.render(writer, cycle); 118 119 verifyControls(); 120 } 121 122 public void testRenderInFormTrue() 123 { 124 IfBean conditional = (IfBean) newInstance(TestIfBean.class, 125 new Object [] { "condition", Boolean.TRUE }); 126 127 MockControl formc = newControl(IForm.class); 128 IForm form = (IForm) formc.getMock(); 129 130 MockControl cyclec = newControl(IRequestCycle.class); 131 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 132 133 cycle.isRewinding(); 134 cyclec.setReturnValue(false); 135 136 cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); 137 cyclec.setReturnValue(form); 138 139 form.getElementId(conditional); 140 formc.setReturnValue("If"); 141 142 form.addHiddenValue("If", "T"); 143 144 cycle.setAttribute(IfBean.IF_VALUE_ATTRIBUTE, Boolean.TRUE); 145 146 cycle.getAttribute(IfBean.IF_VALUE_ATTRIBUTE); 147 cyclec.setReturnValue(Boolean.TRUE); 148 149 IMarkupWriter writer = newWriter(); 150 IRender body = newRender(writer, cycle); 151 IRender body2 = newRender(); 152 153 replayControls(); 154 155 156 conditional.addBody(body); 157 conditional.render(writer, cycle); 158 159 ElseBean reverse = (ElseBean) newInstance(ElseBean.class); 160 reverse.addBody(body2); 161 reverse.render(writer, cycle); 162 163 verifyControls(); 164 } 165 166 public void testRenderInFormFalse() 167 { 168 IfBean conditional = (IfBean) newInstance(TestIfBean.class, 169 new Object [] { "condition", Boolean.FALSE }); 170 171 MockControl cyclec = newControl(IRequestCycle.class); 172 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 173 174 MockControl formc = newControl(IForm.class); 175 IForm form = (IForm) formc.getMock(); 176 177 cycle.isRewinding(); 178 cyclec.setReturnValue(false); 179 180 cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); 181 cyclec.setReturnValue(form); 182 183 form.getElementId(conditional); 184 formc.setReturnValue("If"); 185 186 form.addHiddenValue("If", "F"); 187 188 cycle.setAttribute(IfBean.IF_VALUE_ATTRIBUTE, Boolean.FALSE); 189 190 cycle.getAttribute(IfBean.IF_VALUE_ATTRIBUTE); 191 cyclec.setReturnValue(Boolean.FALSE); 192 193 cycle.isRewinding(); 194 cyclec.setReturnValue(false); 195 196 IMarkupWriter writer = newWriter(); 197 IRender body = newRender(); 198 IRender body2 = newRender(writer, cycle); 199 200 replayControls(); 201 202 203 conditional.addBody(body); 204 conditional.render(writer, cycle); 205 206 ElseBean reverse = (ElseBean) newInstance(ElseBean.class); 207 reverse.addBody(body2); 208 reverse.render(writer, cycle); 209 210 verifyControls(); 211 } 212 213 public void testIgnoreElementWhenRewindingTrue() 214 { 215 IfBean conditional = (IfBean) newInstance(TestIfBean.class, 216 new Object [] { "condition", Boolean.TRUE, "element", "div" }); 217 218 MockControl formc = newControl(IForm.class); 219 IForm form = (IForm) formc.getMock(); 220 221 MockControl cyclec = newControl(IRequestCycle.class); 222 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 223 224 cycle.isRewinding(); 225 cyclec.setReturnValue(true); 226 227 cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); 228 cyclec.setReturnValue(form); 229 230 form.getElementId(conditional); 231 formc.setReturnValue("If"); 232 233 cycle.getParameter("If"); 234 cyclec.setReturnValue("T"); 235 236 cycle.setAttribute(IfBean.IF_VALUE_ATTRIBUTE, Boolean.TRUE); 237 238 cycle.getAttribute(IfBean.IF_VALUE_ATTRIBUTE); 239 cyclec.setReturnValue(Boolean.TRUE); 240 241 IMarkupWriter writer = newWriter(); 242 243 IRender body = newRender(writer, cycle); 244 IRender body2 = newRender(); 245 246 replayControls(); 247 248 conditional.addBody(body); 249 conditional.render(writer, cycle); 250 251 ElseBean reverse = (ElseBean) newInstance(ElseBean.class); 252 reverse.addBody(body2); 253 reverse.render(writer, cycle); 254 255 verifyControls(); 256 } 257 258 public void testIgnoreElementWhenRewindingFalse() 259 { 260 IfBean conditional = (IfBean) newInstance(TestIfBean.class, 261 new Object [] { "condition", Boolean.TRUE, "element", "div" }); 262 263 MockControl formc = newControl(IForm.class); 264 IForm form = (IForm) formc.getMock(); 265 266 MockControl cyclec = newControl(IRequestCycle.class); 267 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 268 269 cycle.isRewinding(); 270 cyclec.setReturnValue(true); 271 272 cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); 273 cyclec.setReturnValue(form); 274 275 form.getElementId(conditional); 276 formc.setReturnValue("If"); 277 278 cycle.getParameter("If"); 279 cyclec.setReturnValue("F"); 280 281 cycle.setAttribute(IfBean.IF_VALUE_ATTRIBUTE, Boolean.FALSE); 282 283 cycle.getAttribute(IfBean.IF_VALUE_ATTRIBUTE); 284 cyclec.setReturnValue(Boolean.FALSE); 285 286 cycle.isRewinding(); 287 cyclec.setReturnValue(true); 288 289 IMarkupWriter writer = newWriter(); 290 291 IRender body = newRender(); 292 IRender body2 = newRender(writer, cycle); 293 294 replayControls(); 295 296 conditional.addBody(body); 297 conditional.render(writer, cycle); 298 299 ElseBean reverse = (ElseBean) newInstance(ElseBean.class); 300 reverse.addBody(body2); 301 reverse.render(writer, cycle); 302 303 verifyControls(); 304 } 305 306 public void testElement() 307 { 308 IBinding informal = newBinding("informal-value"); 309 IComponentSpecification spec = newSpec("informal", null); 310 311 MockControl cyclec = newControl(IRequestCycle.class); 312 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 313 314 cycle.isRewinding(); 315 cyclec.setReturnValue(false); 316 317 cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); 318 cyclec.setReturnValue(null); 319 320 IMarkupWriter writer = newWriter(); 321 IRender body = newRender(writer, cycle); 322 323 writer.begin("div"); 324 writer.attribute("informal", "informal-value"); 325 326 writer.end("div"); 327 328 cycle.setAttribute(IfBean.IF_VALUE_ATTRIBUTE, Boolean.TRUE); 329 330 331 replayControls(); 332 333 IfBean conditional = (IfBean) newInstance(TestIfBean.class, new Object [] 334 { "condition", Boolean.TRUE, "element", "div", "specification", spec }); 335 conditional.addBody(body); 336 conditional.setBinding("informal", informal); 337 338 conditional.render(writer, cycle); 339 340 verifyControls(); 341 } 342 343 public static abstract class TestIfBean extends IfBean 344 { 345 public TestIfBean() { 346 } 347 348 public DataSqueezer getDataSqueezer() { 349 return new DataSqueezerImpl(new DefaultClassResolver()); 350 } 351 } 352 } | Popular Tags |