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.validator; 016 017 import org.apache.tapestry.BaseComponentTestCase; 018 import org.apache.tapestry.form.FormComponentContributorContext; 019 import org.apache.tapestry.form.IFormComponent; 020 import org.apache.tapestry.form.ValidationMessages; 021 import static org.easymock.EasyMock.*; 022 023 /** 024 * Base class for writing {@link org.apache.tapestry.form.validator.Validator} tests. 025 * 026 * @author Howard Lewis Ship 027 * @since 4.0 028 */ 029 public abstract class BaseValidatorTestCase extends BaseComponentTestCase 030 { 031 protected IFormComponent newField(String displayName, boolean disabled) 032 { 033 IFormComponent field = newField(disabled); 034 035 expect(field.getDisplayName()).andReturn(displayName); 036 037 return field; 038 } 039 040 protected IFormComponent newField(String displayName) 041 { 042 IFormComponent field = newMock(IFormComponent.class); 043 checkOrder(field, false); 044 045 expect(field.getDisplayName()).andReturn(displayName); 046 047 return field; 048 } 049 050 protected IFormComponent newField(String displayName, String clientId) 051 { 052 IFormComponent field = newMock(IFormComponent.class); 053 checkOrder(field, false); 054 055 expect(field.getClientId()).andReturn(clientId).anyTimes(); 056 expect(field.getDisplayName()).andReturn(displayName); 057 058 return field; 059 } 060 061 protected IFormComponent newField(String displayName, String clientId, boolean disabled) 062 { 063 IFormComponent field = newField(disabled); 064 065 checkOrder(field, false); 066 067 expect(field.getClientId()).andReturn(clientId).anyTimes(); 068 expect(field.getDisplayName()).andReturn(displayName); 069 070 return field; 071 } 072 073 protected IFormComponent newField(boolean disabled) 074 { 075 IFormComponent field = newField(); 076 expect(field.isDisabled()).andReturn(disabled); 077 078 return field; 079 } 080 081 protected IFormComponent newField() 082 { 083 return newMock(IFormComponent.class); 084 } 085 086 protected ValidationMessages newMessages() 087 { 088 return newMock(ValidationMessages.class); 089 } 090 091 protected ValidationMessages newMessages(String messageOverride, String messageKey, 092 Object[] parameters, String result) 093 { 094 ValidationMessages messages = newMock(ValidationMessages.class); 095 096 trainFormatMessage(messages, messageOverride, messageKey, parameters, result); 097 098 return messages; 099 } 100 101 protected void trainFormatMessage(ValidationMessages messages, 102 String messageOverride, String messageKey, Object[] parameters, String result) 103 { 104 expect(messages.formatValidationMessage(eq(messageOverride), eq(messageKey), aryEq(parameters))) 105 .andReturn(result); 106 } 107 108 protected FormComponentContributorContext newContext() 109 { 110 return newMock(FormComponentContributorContext.class); 111 } 112 113 }