8
8
9
9
import java .math .BigDecimal ;
10
10
import java .util .Map ;
11
+ import javax .persistence .Entity ;
12
+ import javax .persistence .GeneratedValue ;
13
+ import javax .persistence .Id ;
11
14
import javax .persistence .PersistenceException ;
12
15
import javax .validation .ConstraintViolationException ;
13
16
21
24
import org .hibernate .testing .junit4 .BaseNonConfigCoreFunctionalTestCase ;
22
25
import org .junit .Test ;
23
26
27
+ import static org .hibernate .testing .transaction .TransactionUtil .doInHibernate ;
24
28
import static org .junit .Assert .assertFalse ;
25
29
import static org .junit .Assert .fail ;
26
30
29
33
* @author Hardy Ferentschik
30
34
*/
31
35
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
+
32
57
@ Test
33
58
@ RequiresDialectFeature (DialectChecks .SupportsColumnCheck .class )
34
59
public void testListeners () {
@@ -46,31 +71,27 @@ public void testMinAndMaxChecksGetApplied() {
46
71
minMax = new MinMax ( 11 );
47
72
assertDatabaseConstraintViolationThrown ( minMax );
48
73
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
+ } );
56
79
}
57
80
58
81
@ Test
59
82
@ RequiresDialectFeature (DialectChecks .SupportsColumnCheck .class )
60
83
public void testRangeChecksGetApplied () {
61
- Range range = new Range ( 1 );
84
+ RangeEntity range = new RangeEntity ( 1 );
62
85
assertDatabaseConstraintViolationThrown ( range );
63
86
64
- range = new Range ( 11 );
87
+ range = new RangeEntity ( 11 );
65
88
assertDatabaseConstraintViolationThrown ( range );
66
89
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
+ } );
74
95
}
75
96
76
97
@ Test
@@ -80,45 +101,46 @@ public void testDDLEnabled() {
80
101
assertFalse ( "DDL constraints are not applied" , countryColumn .isNullable () );
81
102
}
82
103
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
-
98
104
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" );
110
110
}
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" );
114
115
}
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
+ }
118
124
}
119
125
}
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 ;
120
144
}
121
- tx .rollback ();
122
- s .close ();
123
145
}
124
146
}
0 commit comments