001 // Copyright 2004, 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.valid; 016 017 import static org.easymock.EasyMock.expect; 018 019 import java.util.Locale; 020 021 import org.apache.tapestry.IPage; 022 import org.apache.tapestry.form.IFormComponent; 023 import org.testng.annotations.AfterMethod; 024 import org.testng.annotations.Test; 025 026 /** 027 * Tests for {@link org.apache.tapestry.valid.EmailValidator}. 028 * 029 * @author Tsvetelin Saykov 030 * @author Jimmy Dyson 031 * @since 3.0 032 */ 033 @Test(sequential=true) 034 public class TestUrlValidator extends BaseValidatorTestCase 035 { 036 private UrlValidator v = new UrlValidator(); 037 038 @AfterMethod(alwaysRun=true) 039 public void reset() 040 { 041 v.setClientScriptingEnabled(false); 042 v.setDisallowedProtocolMessage(null); 043 v.setInvalidUrlFormatMessage(null); 044 v.setMinimumLength(-1); 045 v.setMinimumLengthMessage(null); 046 v.setRequired(false); 047 v.setRequiredMessage(null); 048 } 049 050 public void testValidUrl() throws ValidatorException 051 { 052 IFormComponent field = newField(); 053 054 replay(); 055 056 Object result = v.toObject(field, "http://www.google.com"); 057 assertEquals("http://www.google.com", result); 058 059 verify(); 060 } 061 062 public void testInvalidUrl() 063 { 064 IFormComponent field = newField("url"); 065 066 replay(); 067 068 try 069 { 070 v.toObject(field, "fred"); 071 unreachable(); 072 } 073 catch (ValidatorException ex) 074 { 075 assertEquals(ValidationConstraint.URL_FORMAT, ex.getConstraint()); 076 assertEquals("Invalid URL.", ex.getMessage()); 077 } 078 079 verify(); 080 } 081 082 public void testOverrideInvalidUrlFormatMessage() 083 { 084 IFormComponent field = newField("url"); 085 086 replay(); 087 088 v.setInvalidUrlFormatMessage("Try a valid URL (for {0}), like \"http://www.google.com\""); 089 090 try 091 { 092 v.toObject(field, "fred"); 093 unreachable(); 094 } 095 catch (ValidatorException ex) 096 { 097 assertEquals("Try a valid URL (for url), like \"http://www.google.com\"", ex 098 .getMessage()); 099 } 100 101 verify(); 102 } 103 104 public void testTooShort() 105 { 106 IFormComponent field = newField("short"); 107 108 replay(); 109 110 v.setMinimumLength(20); 111 112 try 113 { 114 v.toObject(field, "http://www.test.com"); 115 unreachable(); 116 } 117 catch (ValidatorException ex) 118 { 119 assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint()); 120 assertEquals("You must enter at least 20 characters for short.", ex.getMessage()); 121 } 122 123 verify(); 124 } 125 126 public void testOverrideMinimumLengthMessage() 127 { 128 IFormComponent field = newField("short"); 129 130 replay(); 131 132 v.setMinimumLength(20); 133 v.setMinimumLengthMessage("URLs must be at least 20 characters."); 134 135 try 136 { 137 v.toObject(field, "http://www.test.com"); 138 unreachable(); 139 } 140 catch (ValidatorException ex) 141 { 142 assertEquals("URLs must be at least 20 characters.", ex.getMessage()); 143 } 144 145 verify(); 146 } 147 148 public void testDisallowedProtocol() 149 { 150 IPage page = newPage(Locale.ENGLISH); 151 IFormComponent field = newMock(IFormComponent.class); 152 153 expect(field.getPage()).andReturn(page); 154 155 replay(); 156 157 v.setAllowedProtocols("http,https"); 158 159 try 160 { 161 v.toObject(field, "ftp://ftp.test.com"); 162 unreachable(); 163 } 164 catch (ValidatorException ex) 165 { 166 assertEquals(ValidationConstraint.DISALLOWED_PROTOCOL, ex.getConstraint()); 167 assertEquals("Disallowed protocol - protocol must be http or https.", ex.getMessage()); 168 } 169 170 verify(); 171 } 172 }