11
11
12
12
import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
13
13
import org .hibernate .testing .orm .junit .Jpa ;
14
+ import org .junit .jupiter .api .Assertions ;
14
15
import org .junit .jupiter .api .Test ;
15
16
16
17
import jakarta .persistence .Entity ;
17
18
import jakarta .persistence .Id ;
18
19
import jakarta .persistence .ManyToOne ;
19
20
import jakarta .persistence .Tuple ;
21
+ import jakarta .persistence .criteria .CriteriaBuilder ;
20
22
import jakarta .persistence .criteria .CriteriaQuery ;
21
23
import jakarta .persistence .criteria .Fetch ;
22
24
import jakarta .persistence .criteria .From ;
25
27
import jakarta .persistence .criteria .Root ;
26
28
import jakarta .persistence .criteria .Selection ;
27
29
28
- import static org .junit .jupiter .api .Assertions .fail ;
29
-
30
30
@ Jpa (
31
31
annotatedClasses = {
32
32
IllegalArgumentExceptionTest .Person .class ,
@@ -41,17 +41,16 @@ public void testCriteriaTupleQuerySameAlias(EntityManagerFactoryScope scope) {
41
41
final CriteriaQuery <Tuple > query = scope .getEntityManagerFactory ().getCriteriaBuilder ().createTupleQuery ();
42
42
final Root <Person > person = query .from ( Person .class );
43
43
44
- try {
45
- List list = new ArrayList ();
46
- list .add ( person .get ( "id" ).alias ( "a" ) );
47
- list .add ( person .get ( "name" ).alias ( "a" ) );
44
+ Assertions .assertThrows (
45
+ IllegalArgumentException .class ,
46
+ () -> {
47
+ List list = new ArrayList ();
48
+ list .add ( person .get ( "id" ).alias ( "a" ) );
49
+ list .add ( person .get ( "name" ).alias ( "a" ) );
48
50
49
- query .multiselect ( list );
50
- fail ( "TCK expects an IllegalArgumentException" );
51
- }
52
- catch (IllegalArgumentException iae ) {
53
- //expected by TCK
54
- }
51
+ query .multiselect ( list );
52
+ }
53
+ );
55
54
}
56
55
57
56
@ Test
@@ -65,13 +64,11 @@ public void testCriteriaTupleQuerySameAlias1(EntityManagerFactoryScope scope) {
65
64
person .get ( "name" ).alias ( "a" )
66
65
};
67
66
68
- try {
69
- query .multiselect ( selection );
70
- fail ( "TCK expects an IllegalArgumentException" );
71
- }
72
- catch (IllegalArgumentException iae ) {
73
- //expected by TCK
74
- }
67
+ Assertions .assertThrows (
68
+ IllegalArgumentException .class ,
69
+ () ->
70
+ query .multiselect ( selection )
71
+ );
75
72
}
76
73
77
74
@ Test
@@ -80,16 +77,14 @@ public void testCriteriaTupleQueryNonExistingAttributeNames(EntityManagerFactory
80
77
final CriteriaQuery <Tuple > query = scope .getEntityManagerFactory ().getCriteriaBuilder ().createTupleQuery ();
81
78
final Root <Person > person = query .from ( Person .class );
82
79
83
- try {
84
- query .multiselect (
85
- person .get ( "not_existing_attribute_name" ).alias ( "a1" ),
86
- person .get ( "another_not_existing_attribute_name" ).alias ( "a2" )
87
- );
88
- fail ( "TCK expects an IllegalArgumentException" );
89
- }
90
- catch (IllegalArgumentException iae ) {
91
- // expected
92
- }
80
+ Assertions .assertThrows (
81
+ IllegalArgumentException .class ,
82
+ () ->
83
+ query .multiselect (
84
+ person .get ( "not_existing_attribute_name" ).alias ( "a1" ),
85
+ person .get ( "another_not_existing_attribute_name" ).alias ( "a2" )
86
+ )
87
+ );
93
88
}
94
89
95
90
@ Test
@@ -98,29 +93,27 @@ public void testCriteriaStringQuery(EntityManagerFactoryScope scope) {
98
93
final CriteriaQuery <String > query = scope .getEntityManagerFactory ()
99
94
.getCriteriaBuilder ()
100
95
.createQuery ( String .class );
101
- try {
102
- final Root <Person > person = query .from ( Person .class );
103
- person .get ( "not_existing_attribute_name" );
104
-
105
- fail ( "TCK expects an IllegalArgumentException" );
106
- }
107
- catch (IllegalArgumentException iae ) {
108
- //expected by TCK
109
- }
96
+ Assertions .assertThrows (
97
+ IllegalArgumentException .class ,
98
+ () -> {
99
+ final Root <Person > person = query .from ( Person .class );
100
+ person .get ( "not_existing_attribute_name" );
101
+ }
102
+
103
+ );
110
104
}
111
105
112
106
@ Test
113
107
public void testGetStringNonExistingAttributeName (EntityManagerFactoryScope scope ) {
114
- try {
115
- final CriteriaQuery <Person > query = scope .getEntityManagerFactory ()
116
- .getCriteriaBuilder ()
117
- .createQuery ( Person .class );
118
- query .from ( Person .class ).get ( "not_existing_attribute_name" );
119
- fail ( "TCK expects an IllegalArgumentException" );
120
- }
121
- catch (IllegalArgumentException iae ) {
122
- //expected by TCK
123
- }
108
+ Assertions .assertThrows (
109
+ IllegalArgumentException .class ,
110
+ () -> {
111
+ final CriteriaQuery <Person > query = scope .getEntityManagerFactory ()
112
+ .getCriteriaBuilder ()
113
+ .createQuery ( Person .class );
114
+ query .from ( Person .class ).get ( "not_existing_attribute_name" );
115
+ }
116
+ );
124
117
}
125
118
126
119
@ Test
@@ -129,13 +122,11 @@ public void testJoinANonExistingAttributeNameToAFrom(EntityManagerFactoryScope s
129
122
.getCriteriaBuilder ()
130
123
.createQuery ( Person .class );
131
124
final From <Person , Person > customer = query .from ( Person .class );
132
- try {
133
- customer .join ( "not_existing_attribute_name" );
134
- fail ( "TCK expects an IllegalArgumentException" );
135
- }
136
- catch (IllegalArgumentException iae ) {
137
- //expected by TCK
138
- }
125
+ Assertions .assertThrows (
126
+ IllegalArgumentException .class ,
127
+ () ->
128
+ customer .join ( "not_existing_attribute_name" )
129
+ );
139
130
}
140
131
141
132
@ Test
@@ -144,13 +135,11 @@ public void testJoinANonExistingAttributeNameToAFrom2(EntityManagerFactoryScope
144
135
.getCriteriaBuilder ()
145
136
.createQuery ( Person .class );
146
137
final From <Person , Person > customer = query .from ( Person .class );
147
- try {
148
- customer .join ( "not_existing_attribute_name" , JoinType .INNER );
149
- fail ( "TCK expects an IllegalArgumentException" );
150
- }
151
- catch (IllegalArgumentException iae ) {
152
- //expected by TCK
153
- }
138
+ Assertions .assertThrows (
139
+ IllegalArgumentException .class ,
140
+ () ->
141
+ customer .join ( "not_existing_attribute_name" , JoinType .INNER )
142
+ );
154
143
}
155
144
156
145
@ Test
@@ -161,13 +150,11 @@ public void testJoinANonExistingAttributeNameToAJoin(EntityManagerFactoryScope s
161
150
162
151
final Root <Person > customer = query .from ( Person .class );
163
152
final Join <Person , Address > address = customer .join ( "address" );
164
- try {
165
- address .join ( "not_existing_attribute_name" );
166
- fail ( "TCK expects an IllegalArgumentException" );
167
- }
168
- catch (IllegalArgumentException iae ) {
169
- //expected by TCK
170
- }
153
+ Assertions .assertThrows (
154
+ IllegalArgumentException .class ,
155
+ () ->
156
+ address .join ( "not_existing_attribute_name" )
157
+ );
171
158
}
172
159
173
160
@ Test
@@ -178,13 +165,11 @@ public void testJoinANonExistingAttributeNameToAJoin2(EntityManagerFactoryScope
178
165
179
166
final Root <Person > customer = query .from ( Person .class );
180
167
final Join <Person , Address > address = customer .join ( "address" );
181
- try {
182
- address .join ( "not_existing_attribute_name" , JoinType .INNER );
183
- fail ( "TCK expects an IllegalArgumentException" );
184
- }
185
- catch (IllegalArgumentException iae ) {
186
- //expected by TCK
187
- }
168
+ Assertions .assertThrows (
169
+ IllegalArgumentException .class ,
170
+ () ->
171
+ address .join ( "not_existing_attribute_name" , JoinType .INNER )
172
+ );
188
173
}
189
174
190
175
@ Test
@@ -196,13 +181,56 @@ public void fetchFetchStringIllegalArgumentExceptionTest(EntityManagerFactorySco
196
181
final From <Person , Person > customer = query .from ( Person .class );
197
182
final Fetch f = customer .fetch ( "address" );
198
183
199
- try {
200
- f .fetch ( "not_existing_attribute_name" );
201
- fail ( "TCK expects an IllegalArgumentException" );
202
- }
203
- catch (IllegalArgumentException iae ) {
204
- //expected by TCK
205
- }
184
+ Assertions .assertThrows (
185
+ IllegalArgumentException .class ,
186
+ () ->
187
+ f .fetch ( "not_existing_attribute_name" )
188
+ );
189
+ }
190
+
191
+ @ Test
192
+ public void testHqlQueryWithWrongSemantic (EntityManagerFactoryScope scope ) {
193
+ scope .inEntityManager (
194
+ entityManager -> {
195
+ Assertions .assertThrows (
196
+ IllegalArgumentException .class ,
197
+ () ->
198
+ entityManager .createQuery ( "Seletc p" ).getResultList ()
199
+ );
200
+ }
201
+ );
202
+
203
+ }
204
+
205
+ @ Test
206
+ public void testCriteriaNullReturnType (EntityManagerFactoryScope scope ) {
207
+ scope .inEntityManager (
208
+ entityManager -> {
209
+ Assertions .assertThrows (
210
+ IllegalArgumentException .class ,
211
+ () -> {
212
+ CriteriaBuilder criteriaBuilder = scope .getEntityManagerFactory ().getCriteriaBuilder ();
213
+ CriteriaQuery criteriaQuery = criteriaBuilder .createQuery ( null );
214
+ entityManager .createQuery ( criteriaQuery ).getResultList ();
215
+ }
216
+ );
217
+ }
218
+ );
219
+ }
220
+
221
+ @ Test
222
+ public void testQueryWrongReturnType (EntityManagerFactoryScope scope ) {
223
+ scope .inEntityManager (
224
+ entityManager -> {
225
+ Assertions .assertThrows (
226
+ IllegalArgumentException .class ,
227
+ () -> {
228
+ entityManager .createQuery ( "select p from Peron p" , Integer .class ).getResultList ();
229
+ }
230
+ );
231
+ }
232
+ );
233
+
206
234
}
207
235
208
236
@ Entity (name = "Person" )
0 commit comments