Skip to content

Commit d4acd34

Browse files
committed
HV-1831 Add a couple of examples illustrating various cases
1 parent 0eb0e17 commit d4acd34

9 files changed

+401
-0
lines changed

engine/src/main/java/org/hibernate/validator/internal/engine/tracking/PredefinedScopeProcessedBeansTrackingStrategy.java

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class PredefinedScopeProcessedBeansTrackingStrategy implements ProcessedB
2323

2424
public PredefinedScopeProcessedBeansTrackingStrategy(PredefinedScopeBeanMetaDataManager beanMetaDataManager) {
2525
// TODO: build the maps from the information inside the beanMetaDataManager
26+
// There is a good chance we will need a structure with the whole hierarchy of constraint classes.
27+
// That's something we could add to PredefinedScopeBeanMetaDataManager, as we are already doing similar things
28+
// there (see the ClassHierarchyHelper.getHierarchy call).
29+
// In the predefined scope case, we will have the whole hierarchy of constrained classes passed to
30+
// PredefinedScopeBeanMetaDataManager.
2631

2732
this.trackingEnabledForBeans = CollectionHelper.toImmutableMap( new HashMap<>() );
2833
this.trackingEnabledForReturnValues = CollectionHelper.toImmutableMap( new HashMap<>() );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 javax.validation.Valid;
10+
import javax.validation.Validator;
11+
import javax.validation.constraints.NotNull;
12+
13+
import org.hibernate.validator.testutils.ValidatorUtil;
14+
import org.testng.annotations.Test;
15+
16+
/**
17+
* This is not a real test, just an illustration.
18+
* <p>
19+
* This is the most simple example.
20+
*
21+
* @author Guillaume Smet
22+
*/
23+
public class ProcessedBeansTrackingCycles1Test {
24+
25+
@Test
26+
public void testSerializeHibernateEmail() throws Exception {
27+
Validator validator = ValidatorUtil.getValidator();
28+
29+
validator.validate( new Parent() );
30+
}
31+
32+
private static class Parent {
33+
34+
@NotNull
35+
private String property;
36+
37+
@Valid
38+
private Child child;
39+
}
40+
41+
private static class Child {
42+
43+
@NotNull
44+
private String property;
45+
46+
@Valid
47+
private Parent parent;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.List;
10+
11+
import javax.validation.Valid;
12+
import javax.validation.Validator;
13+
import javax.validation.constraints.NotNull;
14+
15+
import org.hibernate.validator.testutils.ValidatorUtil;
16+
import org.testng.annotations.Test;
17+
18+
/**
19+
* This is not a real test, just an illustration.
20+
* <p>
21+
* Simple enough but this time the cascading annotation is in the container element.
22+
*
23+
* @author Guillaume Smet
24+
*/
25+
public class ProcessedBeansTrackingCycles2Test {
26+
27+
@Test
28+
public void testSerializeHibernateEmail() throws Exception {
29+
Validator validator = ValidatorUtil.getValidator();
30+
31+
validator.validate( new Parent() );
32+
}
33+
34+
private static class Parent {
35+
36+
@NotNull
37+
private String property;
38+
39+
private List<@Valid Child> children;
40+
}
41+
42+
private static class Child {
43+
44+
@NotNull
45+
private String property;
46+
47+
@Valid
48+
private Parent parent;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.List;
10+
import java.util.Map;
11+
12+
import javax.validation.Valid;
13+
import javax.validation.Validator;
14+
import javax.validation.constraints.NotNull;
15+
16+
import org.hibernate.validator.testutils.ValidatorUtil;
17+
import org.testng.annotations.Test;
18+
19+
/**
20+
* This is not a real test, just an illustration.
21+
* <p>
22+
* Simple enough but this time the cascading annotation is deep in a container element.
23+
*
24+
* @author Guillaume Smet
25+
*/
26+
public class ProcessedBeansTrackingCycles3Test {
27+
28+
@Test
29+
public void testSerializeHibernateEmail() throws Exception {
30+
Validator validator = ValidatorUtil.getValidator();
31+
32+
validator.validate( new Parent() );
33+
}
34+
35+
private static class Parent {
36+
37+
@NotNull
38+
private String property;
39+
40+
private Map<String, List<@Valid Child>> children;
41+
}
42+
43+
private static class Child {
44+
45+
@NotNull
46+
private String property;
47+
48+
@Valid
49+
private Parent parent;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.List;
10+
import java.util.Map;
11+
12+
import javax.validation.Valid;
13+
import javax.validation.Validator;
14+
import javax.validation.constraints.NotNull;
15+
16+
import org.hibernate.validator.testutils.ValidatorUtil;
17+
import org.testng.annotations.Test;
18+
19+
/**
20+
* This is not a real test, just an illustration.
21+
* <p>
22+
* Simple enough but this time the cascading annotation is deep in a container element with a bound.
23+
*
24+
* @author Guillaume Smet
25+
*/
26+
public class ProcessedBeansTrackingCycles4Test {
27+
28+
@Test
29+
public void testSerializeHibernateEmail() throws Exception {
30+
Validator validator = ValidatorUtil.getValidator();
31+
32+
validator.validate( new Parent() );
33+
}
34+
35+
private static class Parent {
36+
37+
@NotNull
38+
private String property;
39+
40+
private Map<String, List<@Valid ? extends Child>> children;
41+
}
42+
43+
private static class Child {
44+
45+
@NotNull
46+
private String property;
47+
48+
@Valid
49+
private Parent parent;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.List;
10+
11+
import javax.validation.Valid;
12+
import javax.validation.Validator;
13+
import javax.validation.constraints.NotNull;
14+
15+
import org.hibernate.validator.testutils.ValidatorUtil;
16+
import org.testng.annotations.Test;
17+
18+
/**
19+
* This is not a real test, just an illustration.
20+
* <p>
21+
* This one is a bit more tricky: during the validation, when cascading, we take into account the runtime type to get
22+
* the metadata, not the declared type.
23+
* <p>
24+
* So even if you couldn't have a cycle with the declared type, when trying to find the cycles, we need to take into
25+
* consideration all the subclasses too. The good news is that we are in a closed world so we have them all passed
26+
* to our PredefinedScopedValidatorFactoryImpl!
27+
*
28+
* @author Guillaume Smet
29+
*/
30+
public class ProcessedBeansTrackingCycles5Test {
31+
32+
@Test
33+
public void testSerializeHibernateEmail() throws Exception {
34+
Validator validator = ValidatorUtil.getValidator();
35+
36+
validator.validate( new Parent() );
37+
}
38+
39+
private static class Parent {
40+
41+
@NotNull
42+
private String property;
43+
44+
private List<@Valid ChildWithNoCycles> children;
45+
}
46+
47+
private static class ChildWithNoCycles {
48+
49+
@NotNull
50+
private String property;
51+
}
52+
53+
private static class Child extends ChildWithNoCycles {
54+
55+
@Valid
56+
private Parent parent;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 javax.validation.Valid;
10+
import javax.validation.Validator;
11+
import javax.validation.constraints.NotNull;
12+
13+
import org.hibernate.validator.testutils.ValidatorUtil;
14+
import org.testng.annotations.Test;
15+
16+
/**
17+
* This is not a real test, just an illustration.
18+
*
19+
* @author Guillaume Smet
20+
*/
21+
public class ProcessedBeansTrackingNoCycles1Test {
22+
23+
@Test
24+
public void testSerializeHibernateEmail() throws Exception {
25+
Validator validator = ValidatorUtil.getValidator();
26+
27+
validator.validate( new Parent() );
28+
}
29+
30+
private static class Parent {
31+
32+
@NotNull
33+
private String property;
34+
35+
@Valid
36+
private Child child;
37+
}
38+
39+
private static class Child {
40+
41+
@NotNull
42+
private String property;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.List;
10+
11+
import javax.validation.Valid;
12+
import javax.validation.Validator;
13+
import javax.validation.constraints.NotNull;
14+
15+
import org.hibernate.validator.testutils.ValidatorUtil;
16+
import org.testng.annotations.Test;
17+
18+
/**
19+
* This is not a real test, just an illustration.
20+
*
21+
* @author Guillaume Smet
22+
*/
23+
public class ProcessedBeansTrackingNoCycles2Test {
24+
25+
@Test
26+
public void testSerializeHibernateEmail() throws Exception {
27+
Validator validator = ValidatorUtil.getValidator();
28+
29+
validator.validate( new Parent() );
30+
}
31+
32+
private static class Parent {
33+
34+
@NotNull
35+
private String property;
36+
37+
private List<@Valid Child> children;
38+
}
39+
40+
private static class Child {
41+
42+
@NotNull
43+
private String property;
44+
}
45+
}

0 commit comments

Comments
 (0)