5
5
*/
6
6
package org .hibernate .reactive ;
7
7
8
- import io .vertx .ext .unit .TestContext ;
8
+ import java .util .Objects ;
9
+ import javax .persistence .Entity ;
10
+ import javax .persistence .Id ;
11
+ import javax .persistence .Table ;
12
+
9
13
import org .hibernate .cfg .Configuration ;
14
+ import org .hibernate .reactive .stage .Stage ;
15
+
10
16
import org .junit .After ;
11
17
import org .junit .Before ;
12
18
import org .junit .Test ;
13
19
14
- import javax .persistence .Entity ;
15
- import javax .persistence .Id ;
16
- import javax .persistence .Table ;
17
- import java .util .Objects ;
18
-
19
- import static org .hibernate .reactive .util .impl .CompletionStages .completedFuture ;
20
+ import io .vertx .ext .unit .TestContext ;
20
21
21
22
/**
22
23
* Tests queries using named parameters like ":name",
@@ -37,195 +38,147 @@ protected Configuration constructConfiguration() {
37
38
38
39
@ Before
39
40
public void populateDb (TestContext context ) {
40
- test ( context , completedFuture ( openSession () )
41
- .thenCompose ( s -> s .persist ( spelt )
42
- .thenCompose ( v -> s .persist ( rye ) )
43
- .thenCompose ( v -> s .persist ( almond ) )
44
- .thenCompose ( v -> s .flush () ) )
45
- );
41
+ test ( context , getMutinySessionFactory ()
42
+ .withTransaction ( (session , transaction ) -> session .persistAll ( spelt , rye , almond ) ) );
46
43
}
47
44
48
45
@ After
49
46
public void cleanDb (TestContext context ) {
50
- test ( context , completedFuture ( openSession () )
51
- .thenCompose ( s -> s .createQuery ("delete Flour" ).executeUpdate () ) );
47
+ test ( context , getSessionFactory ( )
48
+ .withTransaction ( ( s , t ) -> s .createQuery ( "delete Flour" ).executeUpdate () ) );
52
49
}
53
50
54
51
@ Test
55
52
public void testAutoFlushOnSingleResult (TestContext context ) {
56
- Flour semolina = new Flour ( 678 , "Semoline" , "the coarse, purified wheat middlings of durum wheat used in making pasta." , "Wheat flour" );
57
- test (
58
- context ,
59
- completedFuture ( openSession () )
60
- .thenCompose ( s -> s .persist ( semolina )
61
- .thenCompose ( v -> s .createQuery ( "from Flour where id = :id" )
62
- .setParameter ( "id" , semolina .getId () )
63
- .getSingleResult ()
64
- )
65
- .thenAccept ( found -> context .assertEquals ( semolina , found ) )
66
- .thenCompose ( v -> s .remove ( semolina ) )
67
- .thenAccept ( v -> s .flush () )
53
+ Flour semolina = new Flour (678 , "Semoline" , "the coarse, purified wheat middlings of durum wheat used in making pasta." , "Wheat flour" );
54
+ test ( context , getSessionFactory ()
55
+ .withSession ( s -> s
56
+ .persist ( semolina )
57
+ .thenCompose ( v -> s .createQuery ( "from Flour where id = :id" )
58
+ .setParameter ( "id" , semolina .getId () )
59
+ .getSingleResult ()
68
60
)
61
+ .thenAccept ( found -> context .assertEquals ( semolina , found ) )
62
+ )
69
63
);
70
64
}
71
65
72
66
@ Test
73
67
public void testSelectScalarValues (TestContext context ) {
74
- test (
75
- context ,
76
- completedFuture ( openSession () )
77
- .thenApply ( s ->
78
- s .createQuery ( "SELECT 'Prova' FROM Flour WHERE id = :id" )
79
- .setParameter ( "id" , rye .getId () ) )
80
- .thenCompose ( qr -> {
81
- context .assertNotNull ( qr );
82
- return qr .getSingleResult ();
83
- } )
84
- .thenAccept ( found -> context .assertEquals ( "Prova" , found ) )
68
+ test ( context , getSessionFactory ().withSession ( s -> {
69
+ Stage .Query <Object > qr = s .createQuery ( "SELECT 'Prova' FROM Flour WHERE id = :id" )
70
+ .setParameter ( "id" , rye .getId () );
71
+ context .assertNotNull ( qr );
72
+ return qr .getSingleResult ();
73
+ } ).thenAccept ( found -> context .assertEquals ( "Prova" , found ) )
85
74
);
86
75
}
87
76
88
77
@ Test
89
78
public void testSelectWithMultipleScalarValues (TestContext context ) {
90
- test (
91
- context ,
92
- completedFuture ( openSession () )
93
- .thenApply ( s ->
94
- s .createQuery ( "SELECT 'Prova', f.id FROM Flour f WHERE f.id = :id" )
95
- .setParameter ("id" , rye .getId () ))
96
- .thenCompose ( qr -> {
97
- context .assertNotNull ( qr );
98
- return qr .getSingleResult ();
99
- } )
100
- .thenAccept ( found -> {
101
- context .assertTrue ( found instanceof Object [] );
102
- context .assertEquals ( "Prova" , ( (Object []) found )[0 ] );
103
- context .assertEquals ( rye .getId (), ( (Object []) found )[1 ] );
104
- } )
79
+ test ( context , getSessionFactory ().withSession ( s -> {
80
+ Stage .Query <Object > qr = s .createQuery ( "SELECT 'Prova', f.id FROM Flour f WHERE f.id = :id" )
81
+ .setParameter ( "id" , rye .getId () );
82
+ context .assertNotNull ( qr );
83
+ return qr .getSingleResult ();
84
+ } ).thenAccept ( found -> {
85
+ context .assertTrue ( found instanceof Object [] );
86
+ context .assertEquals ( "Prova" , ( (Object []) found )[0 ] );
87
+ context .assertEquals ( rye .getId (), ( (Object []) found )[1 ] );
88
+ } )
105
89
);
106
90
}
107
91
108
92
@ Test
109
93
public void testSingleResultQueryOnId (TestContext context ) {
110
- test (
111
- context ,
112
- completedFuture ( openSession () )
113
- .thenApply ( s -> s .createQuery ( "FROM Flour WHERE id = :id" ).setParameter ( "id" , 1 ) )
114
- .thenCompose ( qr -> {
115
- context .assertNotNull ( qr );
116
- return qr .getSingleResult ();
117
- } )
118
- .thenAccept ( flour -> context .assertEquals ( spelt , flour ) )
94
+ test ( context , getSessionFactory ().withSession ( s -> {
95
+ Stage .Query <Object > qr = s .createQuery ( "FROM Flour WHERE id = :id" )
96
+ .setParameter ( "id" , 1 );
97
+ context .assertNotNull ( qr );
98
+ return qr .getSingleResult ();
99
+ } ).thenAccept ( flour -> context .assertEquals ( spelt , flour ) )
119
100
);
120
101
}
121
102
122
103
@ Test
123
104
public void testSingleResultQueryOnName (TestContext context ) {
124
- test (
125
- context ,
126
- completedFuture ( openSession () )
127
- .thenApply ( s -> s .createQuery ( "FROM Flour WHERE name = :name" ).setParameter ( "name" , "Almond" ) )
128
- .thenCompose ( qr -> {
129
- context .assertNotNull ( qr );
130
- return qr .getSingleResult ();
131
- } )
132
- .thenAccept ( flour -> context .assertEquals ( almond , flour ) )
105
+ test ( context , getSessionFactory ().withSession ( s -> {
106
+ Stage .Query <Object > qr = s .createQuery ( "FROM Flour WHERE name = :name" )
107
+ .setParameter ( "name" , "Almond" );
108
+ context .assertNotNull ( qr );
109
+ return qr .getSingleResult ();
110
+ } ).thenAccept ( flour -> context .assertEquals ( almond , flour ) )
133
111
);
134
112
}
135
113
136
114
@ Test
137
115
public void testSingleResultMultipleParameters (TestContext context ) {
138
- test (
139
- context ,
140
- completedFuture ( openSession () )
141
- .thenApply ( s -> s .createQuery ( "FROM Flour WHERE name = :name and description = :desc" )
142
- .setParameter ( "name" , almond .getName () )
143
- .setParameter ( "desc" , almond .getDescription () )
144
- )
145
- .thenCompose ( qr -> {
146
- context .assertNotNull ( qr );
147
- return qr .getSingleResult ();
148
- } )
149
- .thenAccept ( flour -> context .assertEquals ( almond , flour ) )
116
+ test ( context , getSessionFactory ().withSession ( s -> {
117
+ Stage .Query <Object > qr = s .createQuery ( "FROM Flour WHERE name = :name and description = :desc" )
118
+ .setParameter ( "name" , almond .getName () )
119
+ .setParameter ( "desc" , almond .getDescription () );
120
+ context .assertNotNull ( qr );
121
+ return qr .getSingleResult ();
122
+ } ).thenAccept ( flour -> context .assertEquals ( almond , flour ) )
150
123
);
151
124
}
152
125
153
126
@ Test
154
127
public void testSingleResultMultipleParametersReversed (TestContext context ) {
155
- test (
156
- context ,
157
- completedFuture ( openSession () )
158
- .thenApply ( s -> s .createQuery ( "FROM Flour WHERE name = :name and description = :desc" )
159
- .setParameter ( "desc" , almond .getDescription () )
160
- .setParameter ( "name" , almond .getName () )
161
- )
162
- .thenCompose ( qr -> {
163
- context .assertNotNull ( qr );
164
- return qr .getSingleResult ();
165
- } )
166
- .thenAccept ( flour -> context .assertEquals ( almond , flour ) )
128
+ test ( context , getSessionFactory ().withSession ( s -> {
129
+ Stage .Query <Object > qr = s .createQuery ( "FROM Flour WHERE name = :name and description = :desc" )
130
+ .setParameter ( "desc" , almond .getDescription () )
131
+ .setParameter ( "name" , almond .getName () );
132
+ context .assertNotNull ( qr );
133
+ return qr .getSingleResult ();
134
+ } ).thenAccept ( flour -> context .assertEquals ( almond , flour ) )
167
135
);
168
136
}
169
137
170
138
@ Test
171
139
public void testSingleResultMultipleParametersReused (TestContext context ) {
172
- test (
173
- context ,
174
- completedFuture ( openSession () )
175
- .thenApply ( s -> s .createQuery ( "FROM Flour WHERE name = :name or cast(:name as string) is null" )
176
- .setParameter ( "name" , almond .getName () )
177
- )
178
- .thenCompose ( qr -> {
179
- context .assertNotNull ( qr );
180
- return qr .getSingleResult ();
181
- } )
182
- .thenAccept ( flour -> context .assertEquals ( almond , flour ) )
140
+ test ( context , getSessionFactory ().withSession ( s -> {
141
+ Stage .Query <Object > qr = s .createQuery ( "FROM Flour WHERE name = :name or cast(:name as string) is null" )
142
+ .setParameter ( "name" , almond .getName () );
143
+ context .assertNotNull ( qr );
144
+ return qr .getSingleResult ();
145
+ } ).thenAccept ( flour -> context .assertEquals ( almond , flour ) )
183
146
);
184
147
}
185
148
186
149
@ Test
187
150
public void testPlaceHolderInString (TestContext context ) {
188
- test (
189
- context ,
190
- completedFuture ( openSession () )
191
- .thenApply ( s -> s .createQuery ( "select ':', ':name', f FROM Flour f WHERE f.name = :name" )
192
- .setParameter ( "name" , almond .getName () )
193
- )
194
- .thenCompose ( qr -> {
195
- context .assertNotNull ( qr );
196
- return qr .getSingleResult ();
197
- } )
198
- .thenAccept ( result -> {
199
- context .assertEquals ( Object [].class , result .getClass () );
200
- final Object [] objects = (Object []) result ;
201
- context .assertEquals ( 3 , objects .length );
202
- context .assertEquals ( ":" , objects [0 ] );
203
- context .assertEquals ( ":name" , objects [1 ] );
204
- context .assertEquals ( almond , objects [2 ] );
205
- })
151
+ test ( context , getSessionFactory ().withSession ( s -> {
152
+ Stage .Query <Object > qr = s .createQuery ( "select ':', ':name', f FROM Flour f WHERE f.name = :name" )
153
+ .setParameter ( "name" , almond .getName () );
154
+ context .assertNotNull ( qr );
155
+ return qr .getSingleResult ();
156
+ } ).thenAccept ( result -> {
157
+ context .assertEquals ( Object [].class , result .getClass () );
158
+ final Object [] objects = (Object []) result ;
159
+ context .assertEquals ( 3 , objects .length );
160
+ context .assertEquals ( ":" , objects [0 ] );
161
+ context .assertEquals ( ":name" , objects [1 ] );
162
+ context .assertEquals ( almond , objects [2 ] );
163
+ } )
206
164
);
207
165
}
208
166
209
167
@ Test
210
168
public void testPlaceHolderAndSingleQuoteInString (TestContext context ) {
211
- test (
212
- context ,
213
- completedFuture ( openSession () )
214
- .thenApply ( s -> s .createQuery ("select ''':', ''':name''', f FROM Flour f WHERE f.name = :name" )
215
- .setParameter ( "name" , almond .getName () )
216
- )
217
- .thenCompose ( qr -> {
218
- context .assertNotNull ( qr );
219
- return qr .getSingleResult ();
220
- } )
221
- .thenAccept ( result -> {
222
- context .assertEquals ( Object [].class , result .getClass () );
223
- final Object [] objects = (Object []) result ;
224
- context .assertEquals ( 3 , objects .length );
225
- context .assertEquals ( "':" , objects [0 ] );
226
- context .assertEquals ( "':name'" , objects [1 ] );
227
- context .assertEquals ( almond , objects [2 ] );
228
- })
169
+ test ( context , getSessionFactory ().withSession ( s -> {
170
+ Stage .Query <Object > qr = s .createQuery ( "select ''':', ''':name''', f FROM Flour f WHERE f.name = :name" )
171
+ .setParameter ( "name" , almond .getName () );
172
+ context .assertNotNull ( qr );
173
+ return qr .getSingleResult ();
174
+ } ).thenAccept ( result -> {
175
+ context .assertEquals ( Object [].class , result .getClass () );
176
+ final Object [] objects = (Object []) result ;
177
+ context .assertEquals ( 3 , objects .length );
178
+ context .assertEquals ( "':" , objects [0 ] );
179
+ context .assertEquals ( "':name'" , objects [1 ] );
180
+ context .assertEquals ( almond , objects [2 ] );
181
+ } )
229
182
);
230
183
}
231
184
0 commit comments