GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / commons / org / codehaus / groovy / grails / validation / UrlConstraintTests.java
blob4bc5f0dcc97893fc48255816f81f7f436ba695af
1 package org.codehaus.groovy.grails.validation;
3 import java.util.ArrayList;
4 import java.util.List;
6 /**
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),
19 "wrong_url",
20 new String[]{"testClass.testURL.url.error", "testClass.testURL.url.invalid"},
21 new Object[]{"testURL", TestClass.class, "wrong_url"}
24 testConstraintPassed(
25 getConstraint("testURL", Boolean.TRUE),
26 "http://www.google.com"
29 testConstraintPassed(
30 getConstraint("testURL", Boolean.TRUE),
31 "https://www.google.com/"
34 testConstraintPassed(
35 getConstraint("testURL", Boolean.TRUE),
36 "https://www.google.com/answers.py?test=1&second=2"
39 testConstraintFailed(
40 getConstraint("testURL", Boolean.TRUE),
41 "http://localhost/tau_gwi_00/clif/cb/19"
44 testConstraintPassed(
45 getConstraint("testURL", "localhost"),
46 "http://localhost/tau_gwi_00/clif/cb/19"
49 testConstraintPassed(
50 getConstraint("testURL", "localhost(:(\\d{1,5}))?"),
51 "http://localhost:8080/tau_gwi_00/clif/cb/19"
54 testConstraintPassed(
55 getConstraint("testURL", ".*\\.localdomain"),
56 "http://localhost.localdomain/myApp/?test=1&second=2"
59 testConstraintPassed(
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'
69 testConstraintPassed(
70 getConstraint("testURL", regexps),
71 "https://localhost/myApp/?test=1&second=2"
74 testConstraintPassed(
75 getConstraint("testURL", regexps),
76 "https://my-machine/myApp/?test=1&second=2"
79 // and fail for 'another-machine'
80 testConstraintFailed(
81 getConstraint("testURL", regexps),
82 "https://another-machine/myApp/?test=1&second=2"
85 // but still pass for IANA TLD's
86 testConstraintPassed(
87 getConstraint("testURL", regexps),
88 "http://www.google.com/"
91 // must always pass when constraint is turned off
92 testConstraintPassed(
93 getConstraint("testURL", Boolean.FALSE),
94 "wrong_url"
97 // must always pass on null values
98 testConstraintPassed(
99 getConstraint("testURL", Boolean.TRUE),
100 null
103 testConstraintDefaultMessage(
104 getConstraint("testURL", Boolean.TRUE),
105 "wrong_url",
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));
120 try {
121 getConstraint("testString", Boolean.TRUE);
122 } catch (IllegalArgumentException iae) {
123 fail("UrlConstraint should allow boolean parameters.");
126 try {
127 getConstraint("testString", "localhost");
128 } catch (IllegalArgumentException iae) {
129 fail("UrlConstraint should allow string parameters.");
132 try {
133 List regexps = new ArrayList();
134 regexps.add("aaa");
135 regexps.add("bbb");
136 getConstraint("testString", regexps);
137 } catch (IllegalArgumentException iae) {
138 fail("UrlConstraint should allow list parameters.");
141 try {
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) {
145 // Great