KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > TestUrlValidator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.valid;
16
17 import java.util.Locale JavaDoc;
18
19 import org.apache.tapestry.IPage;
20 import org.apache.tapestry.form.IFormComponent;
21 import org.apache.tapestry.valid.UrlValidator;
22 import org.apache.tapestry.valid.ValidationConstraint;
23 import org.apache.tapestry.valid.ValidatorException;
24 import org.easymock.MockControl;
25
26 /**
27  * Tests for {@link org.apache.tapestry.valid.EmailValidator}.
28  *
29  * @author Tsvetelin Saykov
30  * @author Jimmy Dyson
31  * @since 3.0
32  */

33
34 public class TestUrlValidator extends BaseValidatorTestCase
35 {
36     private UrlValidator v = new UrlValidator();
37
38     public void testValidUrl() throws ValidatorException
39     {
40         IFormComponent field = newField();
41
42         replayControls();
43
44         Object JavaDoc result = v.toObject(field, "http://www.google.com");
45         assertEquals("http://www.google.com", result);
46
47         verifyControls();
48     }
49
50     public void testInvalidUrl()
51     {
52         IFormComponent field = newField("url");
53
54         replayControls();
55
56         try
57         {
58             v.toObject(field, "fred");
59             unreachable();
60         }
61         catch (ValidatorException ex)
62         {
63             assertEquals(ValidationConstraint.URL_FORMAT, ex.getConstraint());
64             assertEquals("Invalid URL.", ex.getMessage());
65         }
66
67         verifyControls();
68     }
69
70     public void testOverrideInvalidUrlFormatMessage()
71     {
72         IFormComponent field = newField("url");
73
74         replayControls();
75
76         v.setInvalidUrlFormatMessage("Try a valid URL (for {0}), like \"http://www.google.com\"");
77
78         try
79         {
80             v.toObject(field, "fred");
81             unreachable();
82         }
83         catch (ValidatorException ex)
84         {
85             assertEquals("Try a valid URL (for url), like \"http://www.google.com\"", ex
86                     .getMessage());
87         }
88
89         verifyControls();
90     }
91
92     public void testTooShort()
93     {
94         IFormComponent field = newField("short");
95
96         replayControls();
97
98         v.setMinimumLength(20);
99
100         try
101         {
102             v.toObject(field, "http://www.test.com");
103             unreachable();
104         }
105         catch (ValidatorException ex)
106         {
107             assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
108             assertEquals("You must enter at least 20 characters for short.", ex.getMessage());
109         }
110
111         verifyControls();
112     }
113
114     public void testOverrideMinimumLengthMessage()
115     {
116         IFormComponent field = newField("short");
117
118         replayControls();
119
120         v.setMinimumLength(20);
121         v.setMinimumLengthMessage("URLs must be at least 20 characters.");
122
123         try
124         {
125             v.toObject(field, "http://www.test.com");
126             unreachable();
127         }
128         catch (ValidatorException ex)
129         {
130             assertEquals("URLs must be at least 20 characters.", ex.getMessage());
131         }
132
133         verifyControls();
134     }
135
136     public void testDisallowedProtocol()
137     {
138         IPage page = newPage(Locale.ENGLISH);
139         MockControl control = newControl(IFormComponent.class);
140         IFormComponent field = (IFormComponent) control.getMock();
141
142         field.getPage();
143         control.setReturnValue(page);
144
145         replayControls();
146
147         v.setAllowedProtocols("http,https");
148
149         try
150         {
151             v.toObject(field, "ftp://ftp.test.com");
152             unreachable();
153         }
154         catch (ValidatorException ex)
155         {
156             assertEquals(ValidationConstraint.DISALLOWED_PROTOCOL, ex.getConstraint());
157             assertEquals("Disallowed protocol - protocol must be http or https.", ex.getMessage());
158         }
159
160         verifyControls();
161     }
162 }
Popular Tags