-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
DDLWithoutCallbackTest.java
150 lines (125 loc) · 4.26 KB
/
DDLWithoutCallbackTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.annotations.beanvalidation;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PersistenceException;
import javax.validation.ConstraintViolationException;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.dialect.Dialect;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
/**
* @author Vladimir Klyushnikov
* @author Hardy Ferentschik
*/
public class DDLWithoutCallbackTest extends SessionFactoryBasedFunctionalTest {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
Address.class,
CupHolder.class,
MinMax.class,
RangeEntity.class
};
}
@Override
protected void applySettings(StandardServiceRegistryBuilder builder) {
builder.applySetting( "javax.persistence.validation.mode", "ddl" );
}
protected boolean exportSchema() {
return true;
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Test
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class)
public void testListeners() {
CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "12" ) );
assertDatabaseConstraintViolationThrown( ch );
}
@Test
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class)
public void testMinAndMaxChecksGetApplied() {
MinMax minMax = new MinMax( 1 );
assertDatabaseConstraintViolationThrown( minMax );
minMax = new MinMax( 11 );
assertDatabaseConstraintViolationThrown( minMax );
final MinMax validMinMax = new MinMax( 5 );
doInHibernate( this::sessionFactory, session -> {
session.persist( validMinMax );
} );
}
@Test
@RequiresDialectFeature(DialectChecks.SupportsColumnCheck.class)
public void testRangeChecksGetApplied() {
RangeEntity range = new RangeEntity( 1 );
assertDatabaseConstraintViolationThrown( range );
range = new RangeEntity( 11 );
assertDatabaseConstraintViolationThrown( range );
RangeEntity validRange = new RangeEntity( 5 );
doInHibernate( this::sessionFactory, session -> {
session.persist( validRange );
} );
}
@Test
public void testDDLEnabled() {
PersistentClass classMapping = getMetadata().getEntityBinding( Address.class.getName() );
Column countryColumn = (Column) classMapping.getProperty( "country" ).getMappedColumns().get( 0 );
assertFalse( "DDL constraints are not applied", countryColumn.isNullable() );
}
private void assertDatabaseConstraintViolationThrown(Object o) {
doInHibernate( this::sessionFactory, session -> {
try {
session.persist( o );
session.flush();
fail( "expecting SQL constraint violation" );
}
catch (PersistenceException pe) {
final Throwable cause = pe.getCause();
if ( cause instanceof ConstraintViolationException ) {
fail( "invalid object should not be validated" );
}
else if ( cause instanceof org.hibernate.exception.ConstraintViolationException ) {
if ( Dialect.getDialect().supportsColumnCheck() ) {
// expected
}
else {
org.hibernate.exception.ConstraintViolationException cve = (org.hibernate.exception.ConstraintViolationException) cause;
fail( "Unexpected SQL constraint violation [" + cve.getConstraintName() + "] : " + cve.getSQLException() );
}
}
}
} );
}
@Entity(name = "RangeEntity")
public static class RangeEntity {
@Id
@GeneratedValue
private Long id;
@org.hibernate.validator.constraints.Range(min = 2, max = 10)
private Integer rangeProperty;
private RangeEntity() {
}
public RangeEntity(Integer value) {
this.rangeProperty = value;
}
}
}