Skip to content

Commit

Permalink
HV-1831 Add a couple of examples illustrating various cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Feb 25, 2021
1 parent 0eb0e17 commit d4acd34
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public class PredefinedScopeProcessedBeansTrackingStrategy implements ProcessedB

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

this.trackingEnabledForBeans = CollectionHelper.toImmutableMap( new HashMap<>() );
this.trackingEnabledForReturnValues = CollectionHelper.toImmutableMap( new HashMap<>() );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.engine.tracking;

import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.testutils.ValidatorUtil;
import org.testng.annotations.Test;

/**
* This is not a real test, just an illustration.
* <p>
* This is the most simple example.
*
* @author Guillaume Smet
*/
public class ProcessedBeansTrackingCycles1Test {

@Test
public void testSerializeHibernateEmail() throws Exception {
Validator validator = ValidatorUtil.getValidator();

validator.validate( new Parent() );
}

private static class Parent {

@NotNull
private String property;

@Valid
private Child child;
}

private static class Child {

@NotNull
private String property;

@Valid
private Parent parent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.engine.tracking;

import java.util.List;

import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.testutils.ValidatorUtil;
import org.testng.annotations.Test;

/**
* This is not a real test, just an illustration.
* <p>
* Simple enough but this time the cascading annotation is in the container element.
*
* @author Guillaume Smet
*/
public class ProcessedBeansTrackingCycles2Test {

@Test
public void testSerializeHibernateEmail() throws Exception {
Validator validator = ValidatorUtil.getValidator();

validator.validate( new Parent() );
}

private static class Parent {

@NotNull
private String property;

private List<@Valid Child> children;
}

private static class Child {

@NotNull
private String property;

@Valid
private Parent parent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.engine.tracking;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.testutils.ValidatorUtil;
import org.testng.annotations.Test;

/**
* This is not a real test, just an illustration.
* <p>
* Simple enough but this time the cascading annotation is deep in a container element.
*
* @author Guillaume Smet
*/
public class ProcessedBeansTrackingCycles3Test {

@Test
public void testSerializeHibernateEmail() throws Exception {
Validator validator = ValidatorUtil.getValidator();

validator.validate( new Parent() );
}

private static class Parent {

@NotNull
private String property;

private Map<String, List<@Valid Child>> children;
}

private static class Child {

@NotNull
private String property;

@Valid
private Parent parent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.engine.tracking;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.testutils.ValidatorUtil;
import org.testng.annotations.Test;

/**
* This is not a real test, just an illustration.
* <p>
* Simple enough but this time the cascading annotation is deep in a container element with a bound.
*
* @author Guillaume Smet
*/
public class ProcessedBeansTrackingCycles4Test {

@Test
public void testSerializeHibernateEmail() throws Exception {
Validator validator = ValidatorUtil.getValidator();

validator.validate( new Parent() );
}

private static class Parent {

@NotNull
private String property;

private Map<String, List<@Valid ? extends Child>> children;
}

private static class Child {

@NotNull
private String property;

@Valid
private Parent parent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.engine.tracking;

import java.util.List;

import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.testutils.ValidatorUtil;
import org.testng.annotations.Test;

/**
* This is not a real test, just an illustration.
* <p>
* This one is a bit more tricky: during the validation, when cascading, we take into account the runtime type to get
* the metadata, not the declared type.
* <p>
* So even if you couldn't have a cycle with the declared type, when trying to find the cycles, we need to take into
* consideration all the subclasses too. The good news is that we are in a closed world so we have them all passed
* to our PredefinedScopedValidatorFactoryImpl!
*
* @author Guillaume Smet
*/
public class ProcessedBeansTrackingCycles5Test {

@Test
public void testSerializeHibernateEmail() throws Exception {
Validator validator = ValidatorUtil.getValidator();

validator.validate( new Parent() );
}

private static class Parent {

@NotNull
private String property;

private List<@Valid ChildWithNoCycles> children;
}

private static class ChildWithNoCycles {

@NotNull
private String property;
}

private static class Child extends ChildWithNoCycles {

@Valid
private Parent parent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.engine.tracking;

import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.testutils.ValidatorUtil;
import org.testng.annotations.Test;

/**
* This is not a real test, just an illustration.
*
* @author Guillaume Smet
*/
public class ProcessedBeansTrackingNoCycles1Test {

@Test
public void testSerializeHibernateEmail() throws Exception {
Validator validator = ValidatorUtil.getValidator();

validator.validate( new Parent() );
}

private static class Parent {

@NotNull
private String property;

@Valid
private Child child;
}

private static class Child {

@NotNull
private String property;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.engine.tracking;

import java.util.List;

import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.testutils.ValidatorUtil;
import org.testng.annotations.Test;

/**
* This is not a real test, just an illustration.
*
* @author Guillaume Smet
*/
public class ProcessedBeansTrackingNoCycles2Test {

@Test
public void testSerializeHibernateEmail() throws Exception {
Validator validator = ValidatorUtil.getValidator();

validator.validate( new Parent() );
}

private static class Parent {

@NotNull
private String property;

private List<@Valid Child> children;
}

private static class Child {

@NotNull
private String property;
}
}
Loading

0 comments on commit d4acd34

Please sign in to comment.