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 static org.easymock.EasyMock.expect; 018 019 import java.util.Collections; 020 021 import org.apache.tapestry.IMarkupWriter; 022 import org.apache.tapestry.IRequestCycle; 023 import org.apache.tapestry.form.FormComponentContributorContext; 024 import org.apache.tapestry.form.IFormComponent; 025 import org.apache.tapestry.form.ValidationMessages; 026 import org.apache.tapestry.json.JSONObject; 027 import org.apache.tapestry.valid.ValidationConstants; 028 import org.apache.tapestry.valid.ValidationConstraint; 029 import org.apache.tapestry.valid.ValidationStrings; 030 import org.apache.tapestry.valid.ValidatorException; 031 import org.testng.annotations.Test; 032 033 /** 034 * Tests for {@link org.apache.tapestry.form.validator.Required}. 035 * 036 * @author Howard Lewis Ship 037 * @since 4.0 038 */ 039 @Test 040 public class TestRequired extends BaseValidatorTestCase 041 { 042 public void test_Validate_Not_Null() throws Exception 043 { 044 IFormComponent field = newField(false); 045 ValidationMessages messages = newMessages(); 046 047 replay(); 048 049 new Required().validate(field, messages, "not null"); 050 051 verify(); 052 } 053 054 public void test_Validate_Null() throws Exception 055 { 056 IFormComponent field = newField("Fred", false); 057 ValidationMessages messages = newMessages( 058 null, 059 ValidationStrings.REQUIRED_FIELD, 060 new Object[] 061 { "Fred" }, 062 "Default Message for Fred."); 063 064 replay(); 065 066 try 067 { 068 new Required().validate(field, messages, null); 069 unreachable(); 070 } 071 catch (ValidatorException ex) 072 { 073 assertEquals("Default Message for Fred.", ex.getMessage()); 074 assertSame(ValidationConstraint.REQUIRED, ex.getConstraint()); 075 } 076 077 verify(); 078 } 079 080 public void test_Validate_Empty_String() throws Exception 081 { 082 IFormComponent field = newField("Fred", false); 083 ValidationMessages messages = newMessages( 084 null, 085 ValidationStrings.REQUIRED_FIELD, 086 new Object[] 087 { "Fred" }, 088 "Default Message for Fred."); 089 090 replay(); 091 092 try 093 { 094 new Required().validate(field, messages, ""); 095 unreachable(); 096 } 097 catch (ValidatorException ex) 098 { 099 assertEquals("Default Message for Fred.", ex.getMessage()); 100 assertSame(ValidationConstraint.REQUIRED, ex.getConstraint()); 101 } 102 103 verify(); 104 } 105 106 public void test_Validate_Empty_Collection() throws Exception 107 { 108 IFormComponent field = newField("Fred", false); 109 ValidationMessages messages = newMessages( 110 null, 111 ValidationStrings.REQUIRED_FIELD, 112 new Object[] 113 { "Fred" }, 114 "Default Message for Fred."); 115 116 replay(); 117 118 try 119 { 120 new Required().validate(field, messages, Collections.EMPTY_LIST); 121 unreachable(); 122 } 123 catch (ValidatorException ex) 124 { 125 assertEquals("Default Message for Fred.", ex.getMessage()); 126 assertSame(ValidationConstraint.REQUIRED, ex.getConstraint()); 127 } 128 129 verify(); 130 } 131 132 public void test_Validate_Null_Custom_Message() throws Exception 133 { 134 IFormComponent field = newField("Fred", false); 135 ValidationMessages messages = newMessages( 136 "custom", 137 ValidationStrings.REQUIRED_FIELD, 138 new Object[] 139 { "Fred" }, 140 "Custom Message for Fred."); 141 142 replay(); 143 144 try 145 { 146 Required required = new Required("message=custom"); 147 148 required.validate(field, messages, null); 149 unreachable(); 150 } 151 catch (ValidatorException ex) 152 { 153 assertEquals("Custom Message for Fred.", ex.getMessage()); 154 assertSame(ValidationConstraint.REQUIRED, ex.getConstraint()); 155 } 156 157 verify(); 158 } 159 160 public void test_Validate_Disabled_Field() throws Exception 161 { 162 IFormComponent field = newField(true); 163 164 replay(); 165 166 new Required().validate(field, null, null); 167 168 verify(); 169 } 170 171 public void test_Render_Contribution() 172 { 173 IMarkupWriter writer = newWriter(); 174 IRequestCycle cycle = newCycle(); 175 JSONObject json = new JSONObject(); 176 177 FormComponentContributorContext context = newMock(FormComponentContributorContext.class); 178 179 IFormComponent field = newField("Fred", "fred", false); 180 181 context.registerForFocus(ValidationConstants.REQUIRED_FIELD); 182 183 expect(context.getProfile()).andReturn(json); 184 185 trainFormatMessage( 186 context, 187 null, 188 ValidationStrings.REQUIRED_FIELD, 189 new Object[] 190 { "Fred" }, 191 "Default\\Message for Fred."); 192 193 replay(); 194 195 new Required().renderContribution(writer, cycle, context, field); 196 197 verify(); 198 199 assertEquals("{\"required\":[\"fred\"],\"fred\":{\"required\":[\"Default\\\\Message for Fred.\"]}}", 200 json.toString()); 201 } 202 203 public void test_Render_Contribution_Disabled() 204 { 205 IMarkupWriter writer = newWriter(); 206 IRequestCycle cycle = newCycle(); 207 JSONObject json = new JSONObject(); 208 209 FormComponentContributorContext context = newMock(FormComponentContributorContext.class); 210 211 IFormComponent field = newField(true); 212 213 replay(); 214 215 new Required().renderContribution(writer, cycle, context, field); 216 217 verify(); 218 219 assertEquals(json.toString(), "{}"); 220 } 221 222 public void testIsRequired() 223 { 224 assertEquals(true, new Required().isRequired()); 225 } 226 }