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.hivemind.ApplicationRuntimeException; 018 import org.apache.hivemind.Location; 019 import org.apache.hivemind.Resource; 020 import org.apache.tapestry.*; 021 import org.apache.tapestry.engine.DirectServiceParameter; 022 import org.apache.tapestry.engine.IEngineService; 023 import org.apache.tapestry.engine.ILink; 024 import org.apache.tapestry.valid.IValidationDelegate; 025 import static org.easymock.EasyMock.*; 026 import org.testng.annotations.Test; 027 028 import java.util.Arrays; 029 import java.util.List; 030 import java.util.Map; 031 032 /** 033 * Tests for {@link org.apache.tapestry.form.LinkSubmit} 034 */ 035 @Test 036 public class LinkSubmitTest extends BaseComponentTestCase 037 { 038 private class ScriptFixture implements IScript 039 { 040 public void execute(IRequestCycle cycle, IScriptProcessor processor, Map symbols) 041 { 042 assertNotNull(cycle); 043 assertNotNull(processor); 044 assertNotNull(symbols); 045 046 symbols.put("href", "HREF"); 047 } 048 049 public void execute(IComponent target, IRequestCycle cycle, IScriptProcessor processor, Map symbols) 050 { 051 assertNotNull(cycle); 052 assertNotNull(processor); 053 assertNotNull(symbols); 054 055 symbols.put("href", "HREF"); 056 } 057 058 public Resource getScriptResource() 059 { 060 return null; 061 } 062 063 } 064 065 public void test_Render_Normal() 066 { 067 IMarkupWriter writer = newBufferWriter(); 068 IRequestCycle cycle = newCycle(); 069 IForm form = newForm(); 070 071 LinkSubmit linkSubmit = newInstance(LinkSubmit.class, 072 "form", form, 073 "name", "fred_1", 074 "id", "fred_id", 075 "clientId", "fred_1", 076 "submitType", FormConstants.SUBMIT_NORMAL); 077 078 expect(form.getClientId()).andReturn("form"); 079 trainResponseBuilder(cycle, writer); 080 linkSubmit.addBody(newBody()); 081 082 replay(); 083 084 linkSubmit.renderFormComponent(writer, cycle); 085 086 verify(); 087 088 assertBuffer("<a href=\"javascript:tapestry.form.submit('form', 'fred_1');\" id=\"fred_1\">BODY</a>"); 089 } 090 091 public void test_Render_Disabled() 092 { 093 IMarkupWriter writer = newBufferWriter(); 094 IRequestCycle cycle = newCycle(); 095 096 IForm form = newForm(); 097 098 LinkSubmit linkSubmit = newInstance(LinkSubmit.class, 099 "disabled", Boolean.TRUE, 100 "form", form, 101 "name", "fred_1", 102 "id", "fred_id", 103 "submitType", FormConstants.SUBMIT_NORMAL); 104 linkSubmit.addBody(newBody()); 105 106 trainResponseBuilder(cycle, writer); 107 108 replay(); 109 110 linkSubmit.renderFormComponent(writer, cycle); 111 112 verify(); 113 114 assertBuffer("BODY"); 115 } 116 117 public void test_Render_Submit_Bindings_True() 118 { 119 IMarkupWriter writer = newBufferWriter(); 120 IRequestCycle cycle = newCycle(); 121 122 IEngineService engine = newMock(IEngineService.class); 123 ILink link = newMock(ILink.class); 124 125 IForm form = newForm(); 126 List updates = Arrays.asList("comp1", "comp2"); 127 128 LinkSubmit linkSubmit = newInstance(LinkSubmit.class, 129 "updateComponents", updates, 130 "form", form, 131 "name", "submitMe", 132 "clientId", "submitMe", 133 "submitType", FormConstants.SUBMIT_NORMAL, 134 "directService", engine); 135 136 expect(form.getClientId()).andReturn("form"); 137 linkSubmit.addBody(newBody()); 138 139 expect(engine.getLink(eq(true), isA(DirectServiceParameter.class))).andReturn(link); 140 expect(link.getURL()).andReturn("http://submit"); 141 142 trainResponseBuilder(cycle, writer); 143 144 replay(); 145 146 linkSubmit.renderFormComponent(writer, cycle); 147 148 verify(); 149 150 assertBuffer("<a href=\"http://submit\" " + 151 "onClick=\"tapestry.form.submit('form', 'submitMe'," + 152 "{async:true,json:false,url:this.href}); return false;\" id=\"submitMe\">BODY</a>"); 153 } 154 155 public void test_Prepare_Normal() 156 { 157 IRequestCycle cycle = newCycle(); 158 159 trainGetAttribute(cycle, LinkSubmit.ATTRIBUTE_NAME, null); 160 161 LinkSubmit linkSubmit = (LinkSubmit) newInstance(LinkSubmit.class); 162 163 cycle.setAttribute(LinkSubmit.ATTRIBUTE_NAME, linkSubmit); 164 165 replay(); 166 167 linkSubmit.prepareForRender(cycle); 168 169 verify(); 170 } 171 172 public void test_Prepare_Conflict() 173 { 174 IRequestCycle cycle = newCycle(); 175 IPage page = newPage("MyPage"); 176 Location bloc = newLocation(); 177 Location floc = newLocation(); 178 IComponent existing = newComponent("MyPage/barney", bloc); 179 180 trainGetAttribute(cycle, LinkSubmit.ATTRIBUTE_NAME, existing); 181 182 trainGetIdPath(page, null); 183 184 LinkSubmit linkSubmit = newInstance(LinkSubmit.class, "id", "fred", "page", page, "container", page, "location", floc); 185 186 replay(); 187 188 try 189 { 190 linkSubmit.prepareForRender(cycle); 191 unreachable(); 192 } 193 catch (ApplicationRuntimeException ex) 194 { 195 assert ex.getMessage() 196 .indexOf("LinkSubmit MyPage/fred may not be enclosed by another LinkSubmit ") > -1; 197 198 assertSame(linkSubmit, ex.getComponent()); 199 assertSame(floc, ex.getLocation()); 200 } 201 202 verify(); 203 } 204 205 public void test_Cleanup_After_Render() 206 { 207 IRequestCycle cycle = newCycle(); 208 209 cycle.removeAttribute(LinkSubmit.ATTRIBUTE_NAME); 210 211 replay(); 212 213 LinkSubmit linkSubmit = (LinkSubmit) newInstance(LinkSubmit.class); 214 215 linkSubmit.cleanupAfterRender(cycle); 216 217 verify(); 218 } 219 220 public void test_Is_Clicked() 221 { 222 IRequestCycle cycle = newCycle(); 223 224 trainGetParameter(cycle, FormConstants.SUBMIT_NAME_PARAMETER, "fred"); 225 226 replay(); 227 228 LinkSubmit linkSubmit = (LinkSubmit) newInstance(LinkSubmit.class); 229 230 assertEquals(true, linkSubmit.isClicked(cycle, "fred")); 231 232 verify(); 233 } 234 235 public void test_Is_Not_Clicked() 236 { 237 IRequestCycle cycle = newCycle(); 238 239 trainGetParameter(cycle, FormConstants.SUBMIT_NAME_PARAMETER, null); 240 241 replay(); 242 243 LinkSubmit linkSubmit = (LinkSubmit) newInstance(LinkSubmit.class); 244 245 assertEquals(false, linkSubmit.isClicked(cycle, "fred")); 246 247 verify(); 248 } 249 250 public void test_Rewind() 251 { 252 IMarkupWriter writer = newWriter(); 253 IRequestCycle cycle = newCycle(); 254 IRender body = newRender(); 255 IForm form = newForm(); 256 IValidationDelegate delegate = newDelegate(); 257 258 LinkSubmit linkSubmit = newInstance(LinkSubmit.class, "name", "fred", "form", form); 259 linkSubmit.addBody(body); 260 261 trainGetForm(cycle, form); 262 263 trainWasPrerendered(form, writer, linkSubmit, false); 264 265 trainGetDelegate(form, delegate); 266 267 delegate.setFormComponent(linkSubmit); 268 269 trainGetElementId(form, linkSubmit, "fred"); 270 271 trainIsRewinding(form, true); 272 273 // Finally, code inside LinkSubmit ... 274 275 trainGetParameter(cycle, FormConstants.SUBMIT_NAME_PARAMETER, null); 276 277 // ... and back to AbstractFormComponent 278 279 trainResponseBuilder(cycle, writer); 280 281 body.render(writer, cycle); 282 283 replay(); 284 285 linkSubmit.renderComponent(writer, cycle); 286 287 verify(); 288 } 289 290 private void trainIsRewinding(IForm form, boolean isRewindind) 291 { 292 expect(form.isRewinding()).andReturn(isRewindind); 293 } 294 295 protected void trainGetElementId(IForm form, IFormComponent field, String name) 296 { 297 expect(form.getElementId(field)).andReturn(name); 298 } 299 300 protected void trainGetDelegate(IForm form, IValidationDelegate delegate) 301 { 302 expect(form.getDelegate()).andReturn(delegate); 303 } 304 305 protected void trainWasPrerendered(IForm form, IMarkupWriter writer, IFormComponent field, 306 boolean wasPrendered) 307 { 308 expect(form.wasPrerendered(writer, field)).andReturn(wasPrendered); 309 } 310 311 protected void trainGetForm(IRequestCycle cycle, IForm form) 312 { 313 trainGetAttribute(cycle, TapestryUtils.FORM_ATTRIBUTE, form); 314 } 315 316 protected IValidationDelegate newDelegate() 317 { 318 return newMock(IValidationDelegate.class); 319 } 320 321 }