| 
4 | 4 |  */  | 
5 | 5 | package org.hibernate.orm.test.annotations.beanvalidation;  | 
6 | 6 | 
 
  | 
7 |  | -import java.lang.annotation.Annotation;  | 
8 |  | -import java.math.BigDecimal;  | 
9 | 7 | import jakarta.validation.ConstraintViolationException;  | 
10 | 8 | import jakarta.validation.constraints.NotNull;  | 
11 |  | -import jakarta.validation.groups.Default;  | 
12 |  | - | 
13 |  | -import org.junit.Test;  | 
14 |  | - | 
15 |  | -import org.hibernate.Session;  | 
16 | 9 | import org.hibernate.Transaction;  | 
17 |  | -import org.hibernate.cfg.Configuration;  | 
18 |  | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;  | 
 | 10 | +import org.hibernate.boot.beanvalidation.BeanValidationIntegrator;  | 
 | 11 | +import org.hibernate.cfg.ValidationSettings;  | 
 | 12 | +import org.hibernate.testing.orm.junit.DomainModel;  | 
 | 13 | +import org.hibernate.testing.orm.junit.ServiceRegistry;  | 
 | 14 | +import org.hibernate.testing.orm.junit.SessionFactory;  | 
 | 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope;  | 
 | 16 | +import org.hibernate.testing.orm.junit.Setting;  | 
 | 17 | +import org.junit.jupiter.api.Test;  | 
 | 18 | + | 
 | 19 | +import java.lang.annotation.Annotation;  | 
 | 20 | +import java.math.BigDecimal;  | 
19 | 21 | 
 
  | 
20 |  | -import static org.junit.Assert.assertEquals;  | 
21 |  | -import static org.junit.Assert.fail;  | 
 | 22 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 23 | +import static org.junit.jupiter.api.Assertions.fail;  | 
22 | 24 | 
 
  | 
23 | 25 | /**  | 
24 | 26 |  * @author Emmanuel Bernard  | 
25 | 27 |  */  | 
26 |  | -public class BeanValidationGroupsTest extends BaseCoreFunctionalTestCase {  | 
27 |  | -	@Test  | 
28 |  | -	public void testListeners() {  | 
29 |  | -		CupHolder ch = new CupHolder();  | 
30 |  | -		ch.setRadius( new BigDecimal( "12" ) );  | 
31 |  | -		Session s = openSession();  | 
32 |  | -		Transaction tx = s.beginTransaction();  | 
33 |  | -		try {  | 
34 |  | -			s.persist( ch );  | 
35 |  | -			s.flush();  | 
36 |  | -		}  | 
37 |  | -		catch ( ConstraintViolationException e ) {  | 
38 |  | -			fail( "invalid object should not be validated" );  | 
39 |  | -		}  | 
40 |  | -		try {  | 
41 |  | -			ch.setRadius( null );  | 
42 |  | -			s.flush();  | 
43 |  | -		}  | 
44 |  | -		catch ( ConstraintViolationException e ) {  | 
45 |  | -			fail( "invalid object should not be validated" );  | 
46 |  | -		}  | 
47 |  | -		try {  | 
48 |  | -			s.remove( ch );  | 
49 |  | -			s.flush();  | 
50 |  | -			fail( "invalid object should not be persisted" );  | 
51 |  | -		}  | 
52 |  | -		catch ( ConstraintViolationException e ) {  | 
53 |  | -			assertEquals( 1, e.getConstraintViolations().size() );  | 
54 |  | -			// TODO - seems this explicit case is necessary with JDK 5 (at least on Mac). With Java 6 there is no problem  | 
55 |  | -			Annotation annotation = e.getConstraintViolations()  | 
56 |  | -					.iterator()  | 
57 |  | -					.next()  | 
58 |  | -					.getConstraintDescriptor()  | 
59 |  | -					.getAnnotation();  | 
60 |  | -			assertEquals(  | 
61 |  | -					NotNull.class,  | 
62 |  | -					annotation.annotationType()  | 
63 |  | -			);  | 
64 |  | -		}  | 
65 |  | -		tx.rollback();  | 
66 |  | -		s.close();  | 
67 |  | -	}  | 
 | 28 | +@ServiceRegistry(settings = {  | 
 | 29 | +		@Setting(name = ValidationSettings.JAKARTA_PERSIST_VALIDATION_GROUP, value = ""),  | 
 | 30 | +		@Setting(name = ValidationSettings.JAKARTA_UPDATE_VALIDATION_GROUP, value = ""),  | 
 | 31 | +		@Setting(name = ValidationSettings.JAKARTA_REMOVE_VALIDATION_GROUP,  | 
 | 32 | +				value = "jakarta.validation.groups.Default, org.hibernate.orm.test.annotations.beanvalidation.Strict"),  | 
 | 33 | +		@Setting(name = BeanValidationIntegrator.APPLY_CONSTRAINTS, value = "false"),  | 
 | 34 | +		@Setting(name = ValidationSettings.JAKARTA_VALIDATION_MODE, value = "auto"),  | 
 | 35 | +})  | 
 | 36 | +@DomainModel(annotatedClasses = {  | 
 | 37 | +		CupHolder.class  | 
 | 38 | +})  | 
 | 39 | +@SessionFactory  | 
 | 40 | +class BeanValidationGroupsTest {  | 
68 | 41 | 
 
  | 
69 |  | -	@Override  | 
70 |  | -	protected void configure(Configuration cfg) {  | 
71 |  | -		super.configure( cfg );  | 
72 |  | -		cfg.setProperty(  | 
73 |  | -				"javax.persistence.validation.group.pre-persist",  | 
74 |  | -				""  | 
75 |  | -		);  | 
76 |  | -		cfg.setProperty(  | 
77 |  | -				"javax.persistence.validation.group.pre-update",  | 
78 |  | -				""  | 
79 |  | -		);  | 
80 |  | -		cfg.setProperty(  | 
81 |  | -				"javax.persistence.validation.group.pre-remove",  | 
82 |  | -				Default.class.getName() + ", " + Strict.class.getName()  | 
83 |  | -		);  | 
84 |  | -		cfg.setProperty( "hibernate.validator.apply_to_ddl", "false" );  | 
85 |  | -		cfg.setProperty( "jakarta.persistence.validation.mode", "auto" );  | 
86 |  | -	}  | 
87 |  | - | 
88 |  | -	@Override  | 
89 |  | -	protected Class<?>[] getAnnotatedClasses() {  | 
90 |  | -		return new Class<?>[] {  | 
91 |  | -				CupHolder.class  | 
92 |  | -		};  | 
 | 42 | +	@Test  | 
 | 43 | +	void testListeners(SessionFactoryScope scope) {  | 
 | 44 | +		scope.inSession( s -> {  | 
 | 45 | +			CupHolder ch = new CupHolder();  | 
 | 46 | +			ch.setRadius( new BigDecimal( "12" ) );  | 
 | 47 | +			Transaction tx = s.beginTransaction();  | 
 | 48 | +			try {  | 
 | 49 | +				s.persist( ch );  | 
 | 50 | +				s.flush();  | 
 | 51 | +			}  | 
 | 52 | +			catch (ConstraintViolationException e) {  | 
 | 53 | +				fail( "invalid object should not be validated" );  | 
 | 54 | +			}  | 
 | 55 | +			try {  | 
 | 56 | +				ch.setRadius( null );  | 
 | 57 | +				s.flush();  | 
 | 58 | +			}  | 
 | 59 | +			catch (ConstraintViolationException e) {  | 
 | 60 | +				fail( "invalid object should not be validated" );  | 
 | 61 | +			}  | 
 | 62 | +			try {  | 
 | 63 | +				s.remove( ch );  | 
 | 64 | +				s.flush();  | 
 | 65 | +				fail( "invalid object should not be persisted" );  | 
 | 66 | +			}  | 
 | 67 | +			catch (ConstraintViolationException e) {  | 
 | 68 | +				assertThat( e.getConstraintViolations() ).hasSize( 1 );  | 
 | 69 | +				// TODO - seems this explicit case is necessary with JDK 5 (at least on Mac). With Java 6 there is no problem  | 
 | 70 | +				Annotation annotation = e.getConstraintViolations()  | 
 | 71 | +						.iterator()  | 
 | 72 | +						.next()  | 
 | 73 | +						.getConstraintDescriptor()  | 
 | 74 | +						.getAnnotation();  | 
 | 75 | +				assertThat( annotation.annotationType() ).isEqualTo( NotNull.class );  | 
 | 76 | +			}  | 
 | 77 | +			tx.rollback();  | 
 | 78 | +		} );  | 
93 | 79 | 	}  | 
94 | 80 | }  | 
0 commit comments