-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ParameterTest.java
258 lines (218 loc) · 7.35 KB
/
ParameterTest.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* 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.test.hql;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
/**
* Isolated test for various usages of parameters
*
* @author Steve Ebersole
*/
public class ParameterTest extends BaseCoreFunctionalTestCase {
@Override
protected String[] getMappings() {
return new String[] { "hql/Animal.hbm.xml" };
}
@Test
@TestForIssue( jiraKey = "HHH-9154" )
public void testClassAsParameter() {
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Human h where h.name = :class" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human where name = :class" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human h where :class = h.name" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human h where :class <> h.name" ).setParameter( "class", new Name() ).list();
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-7705")
public void testSetPropertiesMapWithNullValues() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
try {
Human human = new Human();
human.setNickName( "nick" );
s.save( human );
Map parameters = new HashMap();
parameters.put( "nickName", null );
Query q = s.createQuery(
"from Human h where h.nickName = :nickName or (h.nickName is null and :nickName is null)" );
q.setProperties( (parameters) );
assertThat( q.list().size(), is( 0 ) );
Human human1 = new Human();
human1.setNickName( null );
s.save( human1 );
parameters = new HashMap();
parameters.put( "nickName", null );
q = s.createQuery( "from Human h where h.nickName = :nickName or (h.nickName is null and :nickName is null)" );
q.setProperties( (parameters) );
assertThat( q.list().size(), is( 1 ) );
Human found = (Human) q.list().get( 0 );
assertThat( found.getId(), is( human1.getId() ) );
parameters = new HashMap();
parameters.put( "nickName", "nick" );
q = s.createQuery( "from Human h where h.nickName = :nickName or (h.nickName is null and :nickName is null)" );
q.setProperties( (parameters) );
assertThat( q.list().size(), is( 1 ) );
found = (Human) q.list().get( 0 );
assertThat( found.getId(), is( human.getId() ) );
s.delete( human );
s.delete( human1 );
t.commit();
}
catch (Exception e) {
if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE ) {
session.getTransaction().rollback();
}
throw e;
}
finally {
s.close();
}
}
@Test
@TestForIssue(jiraKey = "HHH-10796")
public void testSetPropertiesMapNotContainingAllTheParameters() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
try {
Human human = new Human();
human.setNickName( "nick" );
human.setIntValue( 1 );
s.save( human );
Map parameters = new HashMap();
parameters.put( "nickNames", "nick" );
List<Integer> intValues = new ArrayList<>();
intValues.add( 1 );
Query q = s.createQuery(
"from Human h where h.nickName in (:nickNames) and h.intValue in (:intValues)" );
q.setParameterList( "intValues" , intValues);
q.setProperties( (parameters) );
assertThat( q.list().size(), is( 1 ) );
s.delete( human );
t.commit();
}
catch (Exception e) {
if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE ) {
session.getTransaction().rollback();
}
throw e;
}
finally {
s.close();
}
}
@Test
@TestForIssue( jiraKey = "HHH-9154" )
public void testObjectAsParameter() {
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Human h where h.name = :OBJECT" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human where name = :OBJECT" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human h where :OBJECT = h.name" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human h where :OBJECT <> h.name" ).setParameter( "OBJECT", new Name() ).list();
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-13310")
public void testGetParameterListValue() {
inTransaction(
session -> {
Query query = session.createQuery( "from Animal a where a.id in :ids" );
query.setParameterList( "ids", Arrays.asList( 1L, 2L ) );
Object parameterListValue = query.getParameterValue( "ids" );
assertThat( parameterListValue, is( Arrays.asList( 1L, 2L ) ) );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-13310")
public void testGetParameterListValueAfterParameterExpansion() {
inTransaction(
session -> {
Query query = session.createQuery( "from Animal a where a.id in :ids" );
query.setParameterList( "ids", Arrays.asList( 1L, 2L ) );
query.list();
Object parameterListValue = query.getParameterValue( "ids" );
assertThat( parameterListValue, is( Arrays.asList( 1L, 2L ) ) );
}
);
}
@Test(expected = IllegalStateException.class)
@TestForIssue(jiraKey = "HHH-13310")
public void testGetNotBoundParameterListValue() {
inTransaction(
session -> {
Query query = session.createQuery( "from Animal a where a.id in :ids" );
query.getParameterValue( "ids" );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-13310")
public void testGetPositionalParameterListValue() {
inTransaction(
session -> {
Query query = session.createQuery( "from Animal a where a.id in ?1" );
query.setParameter( 1, Arrays.asList( 1L, 2L ) );
Object parameterListValue = query.getParameterValue( 1 );
assertThat( parameterListValue, is( Arrays.asList( 1L, 2L ) ) );
query.list();
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-13310")
public void testGetPositionalParameterValue() {
inTransaction(
session -> {
Query query = session.createQuery( "from Animal a where a.id = ?1" );
query.setParameter( 1, 1L );
Object parameterListValue = query.getParameterValue( 1 );
assertThat( parameterListValue, is( 1L ) );
query.list();
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-13310")
public void testGetParameterByPositionListValueAfterParameterExpansion() {
inTransaction(
session -> {
Query query = session.createQuery( "from Animal a where a.id in ?1" );
query.setParameterList( 1, Arrays.asList( 1L, 2L ) );
query.list();
Object parameterListValue = query.getParameterValue( 1 );
assertThat( parameterListValue, is( Arrays.asList( 1L, 2L ) ) );
}
);
}
@Test(expected = IllegalStateException.class)
@TestForIssue(jiraKey = "HHH-13310")
public void testGetPositionalNotBoundParameterListValue() {
inTransaction(
session -> {
Query query = session.createQuery( "from Animal a where a.id in ?1" );
query.getParameterValue( 1 );
}
);
}
}