Skip to content

Commit c79ce44

Browse files
committed
HHH-13163 - Fix DDLWithoutCallbackTest#testRangeChecksGetApplied which fails on MariaDB
1 parent 0f8fc67 commit c79ce44

File tree

2 files changed

+72
-87
lines changed

2 files changed

+72
-87
lines changed

hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLWithoutCallbackTest.java

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import java.math.BigDecimal;
1010
import java.util.Map;
11+
import javax.persistence.Entity;
12+
import javax.persistence.GeneratedValue;
13+
import javax.persistence.Id;
1114
import javax.persistence.PersistenceException;
1215
import javax.validation.ConstraintViolationException;
1316

@@ -21,6 +24,7 @@
2124
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
2225
import org.junit.Test;
2326

27+
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
2428
import static org.junit.Assert.assertFalse;
2529
import static org.junit.Assert.fail;
2630

@@ -29,6 +33,27 @@
2933
* @author Hardy Ferentschik
3034
*/
3135
public class DDLWithoutCallbackTest extends BaseNonConfigCoreFunctionalTestCase {
36+
37+
@Override
38+
protected void addSettings(Map settings) {
39+
settings.put( "javax.persistence.validation.mode", "ddl" );
40+
}
41+
42+
@Override
43+
protected Class<?>[] getAnnotatedClasses() {
44+
return new Class<?>[] {
45+
Address.class,
46+
CupHolder.class,
47+
MinMax.class,
48+
RangeEntity.class
49+
};
50+
}
51+
52+
@Override
53+
protected boolean isCleanupTestDataRequired() {
54+
return true;
55+
}
56+
3257
@Test
3358
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class)
3459
public void testListeners() {
@@ -46,31 +71,27 @@ public void testMinAndMaxChecksGetApplied() {
4671
minMax = new MinMax( 11 );
4772
assertDatabaseConstraintViolationThrown( minMax );
4873

49-
minMax = new MinMax( 5 );
50-
Session s = openSession();
51-
Transaction tx = s.beginTransaction();
52-
s.persist( minMax );
53-
s.flush();
54-
tx.rollback();
55-
s.close();
74+
final MinMax validMinMax = new MinMax( 5 );
75+
76+
doInHibernate( this::sessionFactory, session -> {
77+
session.persist( validMinMax );
78+
} );
5679
}
5780

5881
@Test
5982
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class)
6083
public void testRangeChecksGetApplied() {
61-
Range range = new Range( 1 );
84+
RangeEntity range = new RangeEntity( 1 );
6285
assertDatabaseConstraintViolationThrown( range );
6386

64-
range = new Range( 11 );
87+
range = new RangeEntity( 11 );
6588
assertDatabaseConstraintViolationThrown( range );
6689

67-
range = new Range( 5 );
68-
Session s = openSession();
69-
Transaction tx = s.beginTransaction();
70-
s.persist( range );
71-
s.flush();
72-
tx.rollback();
73-
s.close();
90+
RangeEntity validRange = new RangeEntity( 5 );
91+
92+
doInHibernate( this::sessionFactory, session -> {
93+
session.persist( validRange );
94+
} );
7495
}
7596

7697
@Test
@@ -80,45 +101,46 @@ public void testDDLEnabled() {
80101
assertFalse( "DDL constraints are not applied", countryColumn.isNullable() );
81102
}
82103

83-
@Override
84-
protected void addSettings(Map settings) {
85-
settings.put( "javax.persistence.validation.mode", "ddl" );
86-
}
87-
88-
@Override
89-
protected Class<?>[] getAnnotatedClasses() {
90-
return new Class<?>[] {
91-
Address.class,
92-
CupHolder.class,
93-
MinMax.class,
94-
Range.class
95-
};
96-
}
97-
98104
private void assertDatabaseConstraintViolationThrown(Object o) {
99-
Session s = openSession();
100-
Transaction tx = s.beginTransaction();
101-
try {
102-
s.persist( o );
103-
s.flush();
104-
fail( "expecting SQL constraint violation" );
105-
}
106-
catch (PersistenceException pe) {
107-
final Throwable cause = pe.getCause();
108-
if ( cause instanceof ConstraintViolationException ) {
109-
fail( "invalid object should not be validated" );
105+
doInHibernate( this::sessionFactory, session -> {
106+
try {
107+
session.persist( o );
108+
session.flush();
109+
fail( "expecting SQL constraint violation" );
110110
}
111-
else if ( cause instanceof org.hibernate.exception.ConstraintViolationException ) {
112-
if ( getDialect().supportsColumnCheck() ) {
113-
// expected
111+
catch (PersistenceException pe) {
112+
final Throwable cause = pe.getCause();
113+
if ( cause instanceof ConstraintViolationException ) {
114+
fail( "invalid object should not be validated" );
114115
}
115-
else {
116-
org.hibernate.exception.ConstraintViolationException cve = (org.hibernate.exception.ConstraintViolationException) cause;
117-
fail( "Unexpected SQL constraint violation [" + cve.getConstraintName() + "] : " + cve.getSQLException() );
116+
else if ( cause instanceof org.hibernate.exception.ConstraintViolationException ) {
117+
if ( getDialect().supportsColumnCheck() ) {
118+
// expected
119+
}
120+
else {
121+
org.hibernate.exception.ConstraintViolationException cve = (org.hibernate.exception.ConstraintViolationException) cause;
122+
fail( "Unexpected SQL constraint violation [" + cve.getConstraintName() + "] : " + cve.getSQLException() );
123+
}
118124
}
119125
}
126+
} );
127+
}
128+
129+
@Entity(name = "RangeEntity")
130+
public static class RangeEntity {
131+
132+
@Id
133+
@GeneratedValue
134+
private Long id;
135+
136+
@org.hibernate.validator.constraints.Range(min = 2, max = 10)
137+
private Integer rangeProperty;
138+
139+
private RangeEntity() {
140+
}
141+
142+
public RangeEntity(Integer value) {
143+
this.rangeProperty = value;
120144
}
121-
tx.rollback();
122-
s.close();
123145
}
124146
}

hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/Range.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)