|
15 | 15 | */
|
16 | 16 | package org.springframework.data.jpa.repository.query;
|
17 | 17 |
|
18 |
| -import static org.assertj.core.api.Assertions.*; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
19 | 20 | import static org.springframework.data.jpa.repository.query.QueryUtils.*;
|
20 | 21 |
|
21 | 22 | import java.util.Collections;
|
|
46 | 47 | * @author Jędrzej Biedrzycki
|
47 | 48 | * @author Darin Manica
|
48 | 49 | * @author Chris Fraser
|
| 50 | + * @author Michał Pachucki |
49 | 51 | */
|
50 | 52 | class QueryUtilsUnitTests {
|
51 | 53 |
|
@@ -184,55 +186,82 @@ void testRemoveSubqueries() throws Exception {
|
184 | 186 | @Test // GH-2581
|
185 | 187 | void testRemoveMultilineSubqueries() {
|
186 | 188 |
|
187 |
| - assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n" |
188 |
| - + " where not exists (\n" |
189 |
| - + " from User u2\n" |
190 |
| - + " )"))) |
191 |
| - .isEqualTo("select u from User u where not exists"); |
192 |
| - assertThat(normalizeWhitespace(removeSubqueries("(\n" |
193 |
| - + " select u from User u \n" |
194 |
| - + " where not exists (\n" |
195 |
| - + " from User u2\n" |
196 |
| - + " )\n" |
197 |
| - + ")"))) |
198 |
| - .isEqualTo("( select u from User u where not exists )"); |
199 |
| - assertThat(normalizeWhitespace( |
200 |
| - removeSubqueries("select u from User u \n" |
201 |
| - + " where not exists (\n" |
202 |
| - + " from User u2 \n" |
203 |
| - + " where not exists (\n" |
204 |
| - + " from User u3\n" |
205 |
| - + " )\n" |
206 |
| - + " )"))) |
207 |
| - .isEqualTo("select u from User u where not exists"); |
208 |
| - assertThat(normalizeWhitespace( |
209 |
| - removeSubqueries("select u from User u \n" |
210 |
| - + " where not exists (\n" |
211 |
| - + " (\n" |
212 |
| - + " from User u2 \n" |
213 |
| - + " where not exists (\n" |
214 |
| - + " from User u3\n" |
215 |
| - + " )\n" |
216 |
| - + " )\n" |
217 |
| - + " )"))) |
218 |
| - .isEqualTo("select u from User u where not exists ( )"); |
219 |
| - assertThat(normalizeWhitespace( |
220 |
| - removeSubqueries("(\n" |
221 |
| - + " select u from User u \n" |
222 |
| - + " where not exists (\n" |
223 |
| - + " (\n" |
224 |
| - + " from User u2 \n" |
225 |
| - + " where not exists (\n" |
226 |
| - + " from User u3\n" |
227 |
| - + " )\n" |
228 |
| - + " )\n" |
229 |
| - + " )\n" |
230 |
| - + ")"))) |
231 |
| - .isEqualTo("( select u from User u where not exists ( ) )"); |
| 189 | + assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n" // |
| 190 | + + " where not exists (\n" // |
| 191 | + + " from User u2\n" // |
| 192 | + + " )"))).isEqualTo("select u from User u where not exists"); |
| 193 | + |
| 194 | + assertThat(normalizeWhitespace(removeSubqueries("(\n" // |
| 195 | + + " select u from User u \n" // |
| 196 | + + " where not exists (\n" // |
| 197 | + + " from User u2\n" // |
| 198 | + + " )\n" // |
| 199 | + + ")"))).isEqualTo("( select u from User u where not exists )"); |
| 200 | + |
| 201 | + assertThat(normalizeWhitespace(removeSubqueries("select u from User u \n" // |
| 202 | + + " where not exists (\n" // |
| 203 | + + " from User u2 \n" // |
| 204 | + + " where not exists (\n" // |
| 205 | + + " from User u3\n" // |
| 206 | + + " )\n" // |
| 207 | + + " )"))).isEqualTo("select u from User u where not exists"); |
| 208 | + |
| 209 | + assertThat(normalizeWhitespace(removeSubqueries("select u from User u \n" // |
| 210 | + + " where not exists (\n" // |
| 211 | + + " (\n" // |
| 212 | + + " from User u2 \n" // |
| 213 | + + " where not exists (\n" // |
| 214 | + + " from User u3\n" // |
| 215 | + + " )\n" // |
| 216 | + + " )\n" // |
| 217 | + + " )"))).isEqualTo("select u from User u where not exists ( )"); |
| 218 | + |
| 219 | + assertThat(normalizeWhitespace(removeSubqueries("(\n" // |
| 220 | + + " select u from User u \n" // |
| 221 | + + " where not exists (\n" // |
| 222 | + + " (\n" // |
| 223 | + + " from User u2 \n" // |
| 224 | + + " where not exists (\n" // |
| 225 | + + " from User u3\n" // |
| 226 | + + " )\n" // |
| 227 | + + " )\n" // |
| 228 | + + " )\n" // |
| 229 | + + ")"))).isEqualTo("( select u from User u where not exists ( ) )"); |
| 230 | + } |
| 231 | + |
| 232 | + @Test // GH-2557 |
| 233 | + void applySortingAccountsForNewlinesInSubselect() { |
| 234 | + |
| 235 | + Sort sort = Sort.by(Order.desc("age")); |
| 236 | + |
| 237 | + assertThat(QueryUtils.applySorting("select u\n" + // |
| 238 | + "from user u\n" + // |
| 239 | + "where exists (select u2\n" + // |
| 240 | + "from user u2\n" + // |
| 241 | + ")\n" + // |
| 242 | + "", sort)).isEqualTo("select u\n" + // |
| 243 | + "from user u\n" + // |
| 244 | + "where exists (select u2\n" + // |
| 245 | + "from user u2\n" + // |
| 246 | + ")\n" + // |
| 247 | + " order by u.age desc"); |
| 248 | + } |
| 249 | + |
| 250 | + @Test // GH-2563 |
| 251 | + void aliasDetectionProperlyHandlesNewlinesInSubselects() { |
| 252 | + |
| 253 | + assertThat(detectAlias("SELECT o\n" + // |
| 254 | + "FROM Order o\n" + // |
| 255 | + "AND EXISTS(SELECT 1\n" + // |
| 256 | + "FROM Vehicle vehicle\n" + // |
| 257 | + "WHERE vehicle.vehicleOrderId = o.id\n" + // |
| 258 | + "AND LOWER(COALESCE(vehicle.make, '')) LIKE :query)")).isEqualTo("o"); |
232 | 259 | }
|
233 | 260 |
|
234 | 261 | private String normalizeWhitespace(String s) {
|
| 262 | + |
235 | 263 | Matcher matcher = MULTI_WHITESPACE.matcher(s);
|
| 264 | + |
236 | 265 | if (matcher.find()) {
|
237 | 266 | return matcher.replaceAll(" ").trim();
|
238 | 267 | }
|
|
0 commit comments