|
| 1 | +/* |
| 2 | + * Hibernate Validator, declare and validate application constraints |
| 3 | + * |
| 4 | + * License: Apache License, Version 2.0 |
| 5 | + * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. |
| 6 | + */ |
| 7 | +package org.hibernate.validator.test.constraints.records; |
| 8 | + |
| 9 | +import java.lang.reflect.Constructor; |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import jakarta.validation.ConstraintViolation; |
| 14 | +import jakarta.validation.ConstraintViolationException; |
| 15 | +import jakarta.validation.Validator; |
| 16 | +import jakarta.validation.constraints.NotBlank; |
| 17 | +import jakarta.validation.constraints.Positive; |
| 18 | + |
| 19 | +import org.hibernate.validator.test.constraints.annotations.AbstractConstrainedTest; |
| 20 | +import org.hibernate.validator.testutils.ValidatorUtil; |
| 21 | + |
| 22 | +import org.testng.Assert; |
| 23 | +import org.testng.annotations.Test; |
| 24 | + |
| 25 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertNoViolations; |
| 26 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat; |
| 27 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Jan Schatteman |
| 31 | + */ |
| 32 | +public class RecordConstrainedTest extends AbstractConstrainedTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testRecord() { |
| 36 | + PersonRecord r = new PersonRecord( "David", 15 ); |
| 37 | + Set<ConstraintViolation<PersonRecord>> violations = validator.validate( r ); |
| 38 | + assertNoViolations( violations ); |
| 39 | + |
| 40 | + r = new PersonRecord( null, 15 ); |
| 41 | + violations = validator.validate( r ); |
| 42 | + assertThat( violations ).containsOnlyViolations( violationOf( NotBlank.class ).withProperty( "name" ) ); |
| 43 | + assertThat( violations ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 44 | + |
| 45 | + r = new PersonRecord( "", 15 ); |
| 46 | + violations = validator.validate( r ); |
| 47 | + assertThat( violations ).containsOnlyViolations( violationOf( NotBlank.class ).withProperty( "name" ) ); |
| 48 | + assertThat( violations ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 49 | + |
| 50 | + r = new PersonRecord( " ", 15 ); |
| 51 | + violations = validator.validate( r ); |
| 52 | + assertThat( violations ).containsOnlyViolations( violationOf( NotBlank.class ).withProperty( "name" ) ); |
| 53 | + assertThat( violations ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 54 | + |
| 55 | + r = new PersonRecord( "David", 0 ); |
| 56 | + violations = validator.validate( r ); |
| 57 | + assertThat( violations ).containsOnlyViolations( violationOf( Positive.class ).withProperty( "age" ) ); |
| 58 | + assertThat( violations ).containsOnlyViolations( violationOf( Positive.class ).withMessage( "Age has to be a strictly positive integer" ) ); |
| 59 | + |
| 60 | + r = new PersonRecord( "David", -15 ); |
| 61 | + violations = validator.validate( r ); |
| 62 | + assertThat( violations ).containsOnlyViolations( violationOf( Positive.class ).withProperty( "age" ) ); |
| 63 | + assertThat( violations ).containsOnlyViolations( violationOf( Positive.class ).withMessage( "Age has to be a strictly positive integer" ) ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testExplicitConstructorRecord() { |
| 68 | + try { |
| 69 | + new ConstructorValidationRecord( "David", 15 ); |
| 70 | + } |
| 71 | + catch (ConstraintViolationException e) { |
| 72 | + Assert.fail( "No violations expected" ); |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + new ConstructorValidationRecord( null, 15 ); |
| 77 | + } |
| 78 | + catch (ConstraintViolationException e) { |
| 79 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + new ConstructorValidationRecord( "", 15 ); |
| 84 | + } |
| 85 | + catch (ConstraintViolationException e) { |
| 86 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + new ConstructorValidationRecord( " ", 15 ); |
| 91 | + } |
| 92 | + catch (ConstraintViolationException e) { |
| 93 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + new ConstructorValidationRecord( "David", 0 ); |
| 98 | + } |
| 99 | + catch (ConstraintViolationException e) { |
| 100 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( Positive.class ).withMessage( "Age has to be a strictly positive integer" ) ); |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + new ConstructorValidationRecord( "David", -15 ); |
| 105 | + } |
| 106 | + catch (ConstraintViolationException e) { |
| 107 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( Positive.class ).withMessage( "Age has to be a strictly positive integer" ) ); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testCompactConstructorRecord() { |
| 113 | + try { |
| 114 | + new CompactConstructorValidationRecord( "David" ); |
| 115 | + } |
| 116 | + catch (ConstraintViolationException e) { |
| 117 | + Assert.fail( "No violations expected" ); |
| 118 | + } |
| 119 | + |
| 120 | + try { |
| 121 | + new CompactConstructorValidationRecord( null ); |
| 122 | + } |
| 123 | + catch (ConstraintViolationException e) { |
| 124 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 125 | + } |
| 126 | + |
| 127 | + try { |
| 128 | + new CompactConstructorValidationRecord( "" ); |
| 129 | + } |
| 130 | + catch (ConstraintViolationException e) { |
| 131 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 132 | + } |
| 133 | + |
| 134 | + try { |
| 135 | + new CompactConstructorValidationRecord( " " ); |
| 136 | + } |
| 137 | + catch (ConstraintViolationException e) { |
| 138 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( NotBlank.class ).withMessage( "Name cannot be null or empty" ) ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testRecordMethodValidation() { |
| 144 | + try { |
| 145 | + new MethodValidationRecord( 50 ); |
| 146 | + } |
| 147 | + catch (ConstraintViolationException e) { |
| 148 | + assertThat( e.getConstraintViolations() ).containsOnlyViolations( violationOf( Positive.class ) ); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private record PersonRecord(@NotBlank(message = "Name cannot be null or empty") String name, @Positive(message = "Age has to be a strictly positive integer") int age) { |
| 153 | + } |
| 154 | + |
| 155 | + private record ConstructorValidationRecord(String name, int age) implements ConstructorValidator { |
| 156 | + private ConstructorValidationRecord(@NotBlank(message = "Name cannot be null or empty") String name, @Positive(message = "Age has to be a strictly positive integer") int age) { |
| 157 | + validate( name, age ); |
| 158 | + this.name = name; |
| 159 | + this.age = age; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private record CompactConstructorValidationRecord( |
| 164 | + @NotBlank(message = "Name cannot be null or empty") String name) implements ConstructorValidator { |
| 165 | + private CompactConstructorValidationRecord { |
| 166 | + validate( name ); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private record MethodValidationRecord(int age) implements MethodValidator { |
| 171 | + private MethodValidationRecord(@Positive int age) { |
| 172 | + this.age = age; |
FE02
code> | 173 | + doSomethingSilly( age - 100 ); |
| 174 | + } |
| 175 | + |
| 176 | + public void doSomethingSilly(@Positive int arg) { |
| 177 | + validate( this, new Object() { }.getClass().getEnclosingMethod(), arg ); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private interface ConstructorValidator { |
| 182 | + default void validate(Object ... args) { |
| 183 | + Validator v = ValidatorUtil.getValidator(); |
| 184 | + Constructor c = getClass().getDeclaredConstructors()[0]; |
| 185 | + Set<ConstraintViolation<?>> violations = v.forExecutables().validateConstructorParameters( c, args ); |
| 186 | + if ( !violations.isEmpty() ) { |
| 187 | + String message = violations.stream() |
| 188 | + .map( ConstraintViolation::getMessage ) |
| 189 | + .collect( Collectors.joining( ";" ) ); |
| 190 | + throw new ConstraintViolationException( message, violations ); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private interface MethodValidator { |
| 196 | + default void validate(Object instance, Method m, Object ... args) { |
| 197 | + Validator v = ValidatorUtil.getValidator(); |
| 198 | + Set<ConstraintViolation<Object>> violations = v.forExecutables().validateParameters( instance, m, args ); |
| 199 | + if ( !violations.isEmpty() ) { |
| 200 | + String message = violations.stream() |
| 201 | + .map( ConstraintViolation::getMessage ) |
| 202 | + .collect( Collectors.joining( ";" ) ); |
| 203 | + throw new ConstraintViolationException( message, violations ); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments