|
5 | 5 |
|
6 | 6 | import java.nio.charset.StandardCharsets; |
7 | 7 | import java.text.DateFormat; |
| 8 | +import java.util.ArrayList; |
8 | 9 | import java.util.Date; |
| 10 | +import java.util.List; |
9 | 11 | import java.util.Locale; |
10 | 12 | import java.util.Set; |
11 | 13 |
|
12 | 14 | import javax.servlet.http.HttpServletRequest; |
13 | 15 | import javax.validation.ConstraintViolation; |
| 16 | +import javax.validation.Valid; |
14 | 17 | import javax.validation.Validation; |
15 | 18 | import javax.validation.Validator; |
16 | 19 |
|
@@ -192,6 +195,33 @@ public void testValidURI() { |
192 | 195 | assertViolations(bean, 1); |
193 | 196 | } |
194 | 197 |
|
| 198 | + @Test |
| 199 | + public void testValidString() { |
| 200 | + Person person = new Person("John"); |
| 201 | + assertViolations(person, 0); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + public void testInvalidStringType() { |
| 206 | + Person person = new Person("John<script>"); |
| 207 | + assertViolations(person, 1); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void testInvalidStringLength() { |
| 212 | + Person person = new Person("John John John John John John John"); |
| 213 | + assertViolations(person, 1); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void testInvalidStringCascading() { |
| 218 | + Person person = new Person("John John John John John John John"); |
| 219 | + List<Person> people = new ArrayList<>(); |
| 220 | + people.add(person); |
| 221 | + Department department = new Department(people); |
| 222 | + assertViolations(department, 1); |
| 223 | + } |
| 224 | + |
195 | 225 | @Test |
196 | 226 | public void testValidHTTPRequestParameterSet() { |
197 | 227 | MockHttpServletRequest validRequest = new MockHttpServletRequest(); |
@@ -382,6 +412,24 @@ private static class UriBean { |
382 | 412 | } |
383 | 413 | } |
384 | 414 |
|
| 415 | + private static class Person { |
| 416 | + @ValidString(context = "name", type = "SafeString", maxLength = 32, allowNull = false) |
| 417 | + private String name; |
| 418 | + |
| 419 | + Person(String name) { |
| 420 | + this.name = name; |
| 421 | + } |
| 422 | + } |
| 423 | + |
| 424 | + private static class Department { |
| 425 | + @Valid |
| 426 | + private List<Person> people; |
| 427 | + |
| 428 | + Department(List<Person> people) { |
| 429 | + this.people = people; |
| 430 | + } |
| 431 | + } |
| 432 | + |
385 | 433 | private static class HttpRequestBean { |
386 | 434 | @ValidHTTPRequestParameterSet( |
387 | 435 | context = "params", |
|
0 commit comments