1 package org
.codehaus
.groovy
.grails
.validation
;
3 import java
.util
.ArrayList
;
7 * Test cases for 'url' constraint.
9 * @author Sergey Nebolsin (<a href="mailto:nebolsin@gmail.com"/>)
11 public class UrlConstraintTests
extends AbstractConstraintTests
{
12 protected Class
getConstraintClass() {
13 return UrlConstraint
.class;
16 public void testValidation() {
17 testConstraintMessageCodes(
18 getConstraint("testURL", Boolean
.TRUE
),
20 new String
[]{"testClass.testURL.url.error", "testClass.testURL.url.invalid"},
21 new Object
[]{"testURL", TestClass
.class, "wrong_url"}
25 getConstraint("testURL", Boolean
.TRUE
),
26 "http://www.google.com"
30 getConstraint("testURL", Boolean
.TRUE
),
31 "https://www.google.com/"
35 getConstraint("testURL", Boolean
.TRUE
),
36 "https://www.google.com/answers.py?test=1&second=2"
40 getConstraint("testURL", Boolean
.TRUE
),
41 "http://localhost/tau_gwi_00/clif/cb/19"
45 getConstraint("testURL", "localhost"),
46 "http://localhost/tau_gwi_00/clif/cb/19"
50 getConstraint("testURL", "localhost(:(\\d{1,5}))?"),
51 "http://localhost:8080/tau_gwi_00/clif/cb/19"
55 getConstraint("testURL", ".*\\.localdomain"),
56 "http://localhost.localdomain/myApp/?test=1&second=2"
60 getConstraint("testURL", ".*\\.localdomain"),
61 "http://mytest.localdomain/myApp/?test=1&second=2"
64 List regexps
= new ArrayList();
65 regexps
.add("localhost");
66 regexps
.add("my-machine");
68 // now should pass for 'localhost' and 'my-machine'
70 getConstraint("testURL", regexps
),
71 "https://localhost/myApp/?test=1&second=2"
75 getConstraint("testURL", regexps
),
76 "https://my-machine/myApp/?test=1&second=2"
79 // and fail for 'another-machine'
81 getConstraint("testURL", regexps
),
82 "https://another-machine/myApp/?test=1&second=2"
85 // but still pass for IANA TLD's
87 getConstraint("testURL", regexps
),
88 "http://www.google.com/"
91 // must always pass when constraint is turned off
93 getConstraint("testURL", Boolean
.FALSE
),
97 // must always pass on null values
99 getConstraint("testURL", Boolean
.TRUE
),
103 testConstraintDefaultMessage(
104 getConstraint("testURL", Boolean
.TRUE
),
106 "Property [{0}] of class [{1}] with value [{2}] is not a valid URL"
111 public void testCreation() {
112 UrlConstraint constraint
= (UrlConstraint
) getConstraint("testString", Boolean
.FALSE
);
113 assertEquals(ConstrainedProperty
.URL_CONSTRAINT
, constraint
.getName());
114 assertTrue(constraint
.supports(String
.class));
115 assertFalse(constraint
.supports(Float
.class));
116 assertFalse(constraint
.supports(Double
.class));
117 assertFalse(constraint
.supports(Object
.class));
118 assertFalse(constraint
.supports(null));
121 getConstraint("testString", Boolean
.TRUE
);
122 } catch (IllegalArgumentException iae
) {
123 fail("UrlConstraint should allow boolean parameters.");
127 getConstraint("testString", "localhost");
128 } catch (IllegalArgumentException iae
) {
129 fail("UrlConstraint should allow string parameters.");
133 List regexps
= new ArrayList();
136 getConstraint("testString", regexps
);
137 } catch (IllegalArgumentException iae
) {
138 fail("UrlConstraint should allow list parameters.");
142 getConstraint("testString", new Double(1.0));
143 fail("UrlConstraint must throw an exception for non-boolean, non-string and non-list parameters.");
144 } catch (IllegalArgumentException iae
) {