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 static org.easymock.EasyMock.aryEq; 018 import static org.easymock.EasyMock.checkOrder; 019 import static org.easymock.EasyMock.eq; 020 import static org.easymock.EasyMock.expect; 021 022 import java.util.Locale; 023 024 import org.apache.hivemind.util.ClasspathResource; 025 import org.apache.tapestry.IEngine; 026 import org.apache.tapestry.IForm; 027 import org.apache.tapestry.IMarkupWriter; 028 import org.apache.tapestry.IPage; 029 import org.apache.tapestry.IRequestCycle; 030 import org.apache.tapestry.PageRenderSupport; 031 import org.apache.tapestry.junit.TapestryTestCase; 032 033 /** 034 * Abstract test case for {@link FormComponentContributor}. 035 * 036 * @author Paul Ferraro 037 * @since 4.0 038 */ 039 public abstract class FormComponentContributorTestCase extends TapestryTestCase 040 { 041 // Paul, 042 // 043 // Think you missed the newControl() and newMock() methods inherited from HiveMindTestCase. 044 // Those exist to eliminate the need for all this stuff. Instead, you create newFoo() methods 045 // that 046 // create and initialize a Foo instance. 047 // -- Howard 048 049 protected IFormComponent _component = newMock(IFormComponent.class); 050 051 protected IPage _page = newPage(); 052 053 protected IRequestCycle _cycle = newCycle(); 054 055 protected IForm _form = newMock(IForm.class); 056 057 protected IEngine _engine = newMock(IEngine.class); 058 059 protected PageRenderSupport _pageRenderSupport = newPageRenderSupport(); 060 061 protected void addScript(String script) 062 { 063 expect(_cycle.getEngine()).andReturn(_engine); 064 065 expect(_cycle.getAttribute("org.apache.tapestry.PageRenderSupport")) 066 .andReturn(_pageRenderSupport); 067 068 _pageRenderSupport.addExternalScript(new ClasspathResource(null, script)); 069 } 070 071 protected IFormComponent newField(String displayName) 072 { 073 IFormComponent field = newMock(IFormComponent.class); 074 075 checkOrder(field, false); 076 077 expect(field.getDisplayName()).andReturn(displayName).anyTimes(); 078 079 return field; 080 } 081 082 protected IFormComponent newField(String displayName, String clientId, int count) 083 { 084 IFormComponent field = newMock(IFormComponent.class); 085 checkOrder(field, false); 086 087 expect(field.getDisplayName()).andReturn(displayName); 088 089 expect(field.getClientId()).andReturn(clientId).anyTimes(); 090 091 return field; 092 } 093 094 protected IFormComponent newFieldWithClientId(String clientId) 095 { 096 IFormComponent field = newMock(IFormComponent.class); 097 098 expect(field.getClientId()).andReturn(clientId); 099 100 return field; 101 } 102 103 104 protected void trainBuildMessage(ValidationMessages messages, 105 String overrideMessage, String key, Object[] parameters, String result) 106 { 107 expect(messages.formatValidationMessage(eq(overrideMessage), eq(key), aryEq(parameters))) 108 .andReturn(result); 109 } 110 111 protected void trainGetLocale(ValidationMessages messages, Locale locale) 112 { 113 expect(messages.getLocale()).andReturn(locale).anyTimes(); 114 } 115 116 protected IFormComponent newField() 117 { 118 return newMock(IFormComponent.class); 119 } 120 121 protected IMarkupWriter newWriter() 122 { 123 return newMock(IMarkupWriter.class); 124 } 125 126 protected IRequestCycle newCycle() 127 { 128 return newMock(IRequestCycle.class); 129 } 130 131 protected ValidationMessages newValidationMessages(Locale locale) 132 { 133 ValidationMessages messages = newMock(ValidationMessages.class); 134 135 expect(messages.getLocale()).andReturn(locale).anyTimes(); 136 137 return messages; 138 } 139 140 protected void trainTrim(FormComponentContributorContext context, String fieldId) 141 { 142 context.addSubmitHandler("function (event) { Tapestry.trim_field_value('" + fieldId 143 + "'); }"); 144 } 145 }