|
| 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.internal.engine.tracking; |
| 8 | + |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | +import jakarta.validation.ConstraintViolation; |
| 15 | +import jakarta.validation.Valid; |
| 16 | +import jakarta.validation.Validation; |
| 17 | +import jakarta.validation.Validator; |
| 18 | +import jakarta.validation.constraints.NotNull; |
| 19 | + |
| 20 | +import org.hibernate.validator.PredefinedScopeHibernateValidator; |
| 21 | +import org.hibernate.validator.PredefinedScopeHibernateValidatorFactory; |
| 22 | +import org.hibernate.validator.internal.engine.PredefinedScopeValidatorFactoryImpl; |
| 23 | +import org.hibernate.validator.internal.engine.ValidatorFactoryScopedContext; |
| 24 | +import org.hibernate.validator.internal.engine.tracking.ProcessedBeansTrackingStrategy; |
| 25 | + |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import static org.testng.Assert.assertFalse; |
| 29 | +import static org.testng.Assert.assertTrue; |
| 30 | + |
| 31 | +/** |
| 32 | + * An example of beans with cascading constraints, some cycle and others do not. |
| 33 | + * |
| 34 | + * A -> B ---> C ------> F -> G <- |
| 35 | + * | ^ | ^ ^ | |
| 36 | + * | | | | | | |
| 37 | + * | -- D <-- | | | |
| 38 | + * --------------------> E ------- |
| 39 | + * |
| 40 | + * A, B, C, D, E, F, and G are beans that get validated. |
| 41 | + * |
| 42 | + * An arrow, ->, indicates a cascading constraint.ProcessedBeansTrackingCyclesNoCyclesMapTest |
| 43 | + * |
| 44 | + * The following are the properties with cascading Map constraints: |
| 45 | + * A.bToEs |
| 46 | + * B.cToCs |
| 47 | + * C.dToFs |
| 48 | + * D.bToBs |
| 49 | + * E.fToGs |
| 50 | + * .gToGs |
| 51 | + * F.gToGs |
| 52 | + * |
| 53 | + * @author Gail Badner |
| 54 | + * |
| 55 | + */ |
| 56 | + |
| 57 | +public class ProcessedBeansTrackingCyclesNoCyclesMapTest { |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testTrackingEnabled() { |
| 61 | + |
| 62 | + final ValidatorFactoryScopedContext validatorFactoryScopedContext = getValidatorFactoryScopedContext(); |
| 63 | + final ProcessedBeansTrackingStrategy processedBeansTrackingStrategy = |
| 64 | + validatorFactoryScopedContext.getProcessedBeansTrackingStrategy(); |
| 65 | + assertTrue( processedBeansTrackingStrategy.isEnabledForBean( |
| 66 | + A.class, |
| 67 | + true |
| 68 | + ) ); |
| 69 | + assertTrue( processedBeansTrackingStrategy.isEnabledForBean( |
| 70 | + B.class, |
| 71 | + true |
| 72 | + ) ); |
| 73 | + assertTrue( processedBeansTrackingStrategy.isEnabledForBean( |
| 74 | + C.class, |
| 75 | + true |
| 76 | + ) ); |
| 77 | + assertTrue( processedBeansTrackingStrategy.isEnabledForBean( |
| 78 | + D.class, |
| 79 | + true |
| 80 | + ) ); |
| 81 | + assertFalse( processedBeansTrackingStrategy.isEnabledForBean( |
| 82 | + E.class, |
| 83 | + true |
| 84 | + ) ); |
| 85 | + assertFalse( processedBeansTrackingStrategy.isEnabledForBean( |
| 86 | + F.class, |
| 87 | + true |
| 88 | + ) ); |
| 89 | + assertFalse( processedBeansTrackingStrategy.isEnabledForBean( |
| 90 | + G.class, |
| 91 | + false |
| 92 | + ) ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testValidate() { |
| 97 | + final A a = new A(); |
| 98 | + final B b = new B(); |
| 99 | + final C c = new C(); |
| 100 | + final D d = new D(); |
| 101 | + final E e = new E(); |
| 102 | + final F f = new F(); |
| 103 | + final G g = new G(); |
| 104 | + |
| 105 | + a.bToEs.put( b, e ); |
| 106 | + b.cToCs.put( c, c ); |
| 107 | + c.dToFs.put( d, f ); |
| 108 | + d.bToBs.put( b, b ); |
| 109 | + e.fToGs.put( f, g ); |
| 110 | + e.gToGs.put( g, g ); |
| 111 | + |
| 112 | + final Validator validator = getValidator(); |
| 113 | + final Set<ConstraintViolation<A>> violationsA = validator.validate( a ); |
| 114 | + final Set<ConstraintViolation<B>> violationsB = validator.validate( b ); |
| 115 | + final Set<ConstraintViolation<C>> violationsC = validator.validate( c ); |
| 116 | + final Set<ConstraintViolation<D>> violationsD = validator.validate( d ); |
| 117 | + final Set<ConstraintViolation<E>> violationsE = validator.validate( e ); |
| 118 | + final Set<ConstraintViolation<F>> violationsF = validator.validate( f ); |
| 119 | + final Set<ConstraintViolation<G>> violationsG = validator.validate( g ); |
| 120 | + } |
| 121 | + |
| 122 | + private Validator getValidator() { |
| 123 | + return getValidatorFactory().getValidator(); |
| 124 | + } |
| 125 | + |
| 126 | + private PredefinedScopeHibernateValidatorFactory getValidatorFactory() { |
| 127 | + return Validation.byProvider( PredefinedScopeHibernateValidator.class ) |
| 128 | + .configure() |
| 129 | + .builtinConstraints( new HashSet<>( Arrays.asList( NotNull.class.getName() ) ) ) |
| 130 | + .initializeBeanMetaData( new HashSet<>( Arrays.asList( |
| 131 | + A.class, B.class, C.class, D.class, E.class, F.class, G.class |
| 132 | + ) ) ) |
| 133 | + .buildValidatorFactory().unwrap( PredefinedScopeHibernateValidatorFactory.class ); |
| 134 | + } |
| 135 | + |
| 136 | + private ValidatorFactoryScopedContext getValidatorFactoryScopedContext() { |
| 137 | + return ( (PredefinedScopeValidatorFactoryImpl) getValidatorFactory() ).getValidatorFactoryScopedContext(); |
| 138 | + } |
| 139 | + |
| 140 | + private static class A { |
| 141 | + |
| 142 | + private String description; |
| 143 | + |
| 144 | + private Map<@Valid B, @Valid E> bToEs = new HashMap<>(); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + private static class B { |
| 149 | + @Valid |
| 150 | + private String description; |
| 151 | + |
| 152 | + private Map<@Valid C, @Valid C> cToCs = new HashMap<>(); |
| 153 | + } |
| 154 | + |
| 155 | + private static class C { |
| 156 | + |
| 157 | + private String description; |
| 158 | + |
| 159 | + private Map<@Valid D, @Valid F> dToFs = new HashMap<>(); |
| 160 | + } |
| 161 | + |
| 162 | + private static class D { |
| 163 | + |
| 164 | + private String description; |
| 165 | + |
| 166 | + private Map<@Valid B, @Valid B> bToBs = new HashMap<>(); |
| 167 | + } |
| 168 | + |
| 169 | + private static class E { |
| 170 | + |
| 171 | + private String description; |
| 172 | + |
| 173 | + private Map<@Valid F, G> fToGs = new HashMap<>(); |
| 174 | + |
| 175 | + private Map<@Valid G, @Valid G> gToGs = new HashMap<>(); |
| 176 | + } |
| 177 | + |
| 178 | + private static class F { |
| 179 | + |
| 180 | + private String description; |
| 181 | + |
| 182 | + private Map<@Valid G, @Valid G> gToGs = new HashMap<>(); |
| 183 | + } |
| 184 | + |
| 185 | + private static class G { |
| 186 | + |
| 187 | + private String description; |
| 188 | + } |
| 189 | +} |
0 commit comments