1 15 package org.apache.tapestry.form; 16 17 import java.util.Locale ; 18 19 import org.apache.hivemind.ClassResolver; 20 import org.apache.hivemind.Resource; 21 import org.apache.hivemind.util.ClasspathResource; 22 import org.apache.tapestry.IForm; 23 import org.apache.tapestry.IRequestCycle; 24 import org.apache.tapestry.PageRenderSupport; 25 import org.apache.tapestry.TapestryUtils; 26 import org.apache.tapestry.components.BaseComponentTestCase; 27 import org.apache.tapestry.services.Infrastructure; 28 import org.easymock.MockControl; 29 30 36 public class TestFormComponentContributorContext extends BaseComponentTestCase 37 { 38 private IForm newForm(String name) 39 { 40 MockControl control = newControl(IForm.class); 41 IForm form = (IForm) control.getMock(); 42 43 form.getName(); 44 control.setReturnValue(name); 45 46 return form; 47 } 48 49 private IFormComponent newField(IForm form, String name) 50 { 51 MockControl control = newControl(IFormComponent.class); 52 IFormComponent field = (IFormComponent) control.getMock(); 53 54 field.getForm(); 55 control.setReturnValue(form); 56 57 field.getName(); 58 control.setReturnValue(name); 59 60 return field; 61 } 62 63 private ClassResolver newResolver() 64 { 65 return (ClassResolver) newMock(ClassResolver.class); 66 } 67 68 private Infrastructure newInfrastructure(ClassResolver resolver) 69 { 70 MockControl control = newControl(Infrastructure.class); 71 Infrastructure inf = (Infrastructure) control.getMock(); 72 73 inf.getClassResolver(); 74 control.setReturnValue(resolver); 75 76 return inf; 77 } 78 79 public void testGetFieldDOM() 80 { 81 IForm form = newForm("myform"); 82 IFormComponent field = newField(form, "myfield"); 83 ClassResolver resolver = newResolver(); 84 85 MockControl cyclec = newControl(IRequestCycle.class); 86 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 87 88 Infrastructure inf = newInfrastructure(resolver); 89 90 cycle.getInfrastructure(); 91 cyclec.setReturnValue(inf); 92 93 PageRenderSupport prs = newSupport(); 94 95 trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs); 96 97 replayControls(); 98 99 FormComponentContributorContext context = new FormComponentContributorContextImpl( 100 Locale.ENGLISH, cycle, field); 101 102 assertEquals("document.myform.myfield", context.getFieldDOM()); 103 104 verifyControls(); 105 } 106 107 public void testIncludeClasspathScript() 108 { 109 IForm form = newForm("myform"); 110 IFormComponent field = newField(form, "myfield"); 111 ClassResolver resolver = newResolver(); 112 113 MockControl cyclec = newControl(IRequestCycle.class); 114 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 115 116 Infrastructure inf = newInfrastructure(resolver); 117 118 cycle.getInfrastructure(); 119 cyclec.setReturnValue(inf); 120 121 PageRenderSupport prs = newSupport(); 122 123 Resource expected = new ClasspathResource(resolver, "/foo.js"); 124 125 prs.addExternalScript(expected); 126 127 trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs); 128 129 replayControls(); 130 131 FormComponentContributorContext context = new FormComponentContributorContextImpl( 132 Locale.ENGLISH, cycle, field); 133 134 context.includeClasspathScript("/foo.js"); 135 136 verifyControls(); 137 } 138 139 public void testAddSubmitListener() 140 { 141 142 IForm form = newForm("myform"); 143 IFormComponent field = newField(form, "myfield"); 144 ClassResolver resolver = newResolver(); 145 146 MockControl cyclec = newControl(IRequestCycle.class); 147 IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); 148 149 Infrastructure inf = newInfrastructure(resolver); 150 151 cycle.getInfrastructure(); 152 cyclec.setReturnValue(inf); 153 154 PageRenderSupport prs = newSupport(); 155 156 prs.addInitializationScript("document.myform.events.addSubmitListener(foo);"); 157 158 trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs); 159 160 replayControls(); 161 162 FormComponentContributorContext context = new FormComponentContributorContextImpl( 163 Locale.ENGLISH, cycle, field); 164 165 context.addSubmitListener("foo"); 166 167 verifyControls(); 168 } 169 170 private PageRenderSupport newSupport() 171 { 172 return (PageRenderSupport) newMock(PageRenderSupport.class); 173 } 174 } 175 | Popular Tags |