|
1 | 1 | package org.springframework.data.jpa.repository.query;
|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.*;
|
4 |
| -import static org.springframework.data.jpa.repository.query.QueryUtils.getOuterJoinAliases; |
| 4 | +import static org.springframework.data.jpa.repository.query.QueryUtils.detectAlias; |
5 | 5 |
|
6 | 6 | import java.util.Collections;
|
7 | 7 | import java.util.Set;
|
8 | 8 |
|
| 9 | +import org.assertj.core.api.SoftAssertions; |
9 | 10 | import org.junit.jupiter.api.Test;
|
10 | 11 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
11 | 12 | import org.springframework.data.domain.Sort;
|
@@ -58,11 +59,6 @@ void detectAliasThrowsErrorOnInvalidSQL() {
|
58 | 59 | .isInstanceOf(IllegalArgumentException.class);
|
59 | 60 | }
|
60 | 61 |
|
61 |
| - @Test |
62 |
| - void allowsFullyQualifiedEntityNamesInQuery() { |
63 |
| - assertThat(JSqlParserQueryUtils.detectAlias(FQ_QUERY)).isEqualTo("u"); |
64 |
| - } |
65 |
| - |
66 | 62 | @Test // DATAJPA-798
|
67 | 63 | void detectsAliasInQueryContainingLineBreaks() {
|
68 | 64 | assertThat(JSqlParserQueryUtils.detectAlias("select \n u \n from \n User \nu")).isEqualTo("u");
|
@@ -347,6 +343,161 @@ void discoversCorrectAliasForJoinFetch() {
|
347 | 343 | assertThat(aliases).containsExactly("authority");
|
348 | 344 | }
|
349 | 345 |
|
| 346 | + @Test // DATAJPA-420 |
| 347 | + void createsCountQueryForScalarSelects() { |
| 348 | + assertCountQuery("select p.lastname,p.firstname from Person p", "select count(p) from Person p"); |
| 349 | + } |
| 350 | + |
| 351 | + @Test // DATAJPA-456 |
| 352 | + void createCountQueryFromTheGivenCountProjection() { |
| 353 | + assertThat(JSqlParserQueryUtils.createCountQueryFor("select p.lastname,p.firstname from Person p", "p.lastname")) |
| 354 | + .isEqualToIgnoringCase("select count(p.lastname) from Person p"); |
| 355 | + } |
| 356 | + |
| 357 | + @Test // DATAJPA-736 |
| 358 | + void supportsNonAsciiCharactersInEntityNames() { |
| 359 | + assertThat(JSqlParserQueryUtils.createCountQueryFor("select u from Usèr u")) |
| 360 | + .isEqualToIgnoringCase("select count(u) from Usèr u"); |
| 361 | + } |
| 362 | + |
| 363 | + @Test // DATAJPA-1500 |
| 364 | + void createCountQuerySupportsWhitespaceCharacters() { |
| 365 | + |
| 366 | + assertThat(JSqlParserQueryUtils.createCountQueryFor("select * from User user\n" + // |
| 367 | + " where user.age = 18\n" + // |
| 368 | + " order by user.name\n ")).isEqualToIgnoringCase("select count(user) from User user where user.age = 18"); |
| 369 | + } |
| 370 | + |
| 371 | + @Test |
| 372 | + void createCountQuerySupportsLineBreaksInSelectClause() { |
| 373 | + |
| 374 | + assertThat(JSqlParserQueryUtils.createCountQueryFor("select user.age,\n" + // |
| 375 | + " user.name\n" + // |
| 376 | + " from User user\n" + // |
| 377 | + " where user.age = 18\n" + // |
| 378 | + " order\nby\nuser.name\n ")).isEqualToIgnoringCase("select count(user) from User user where user.age = 18"); |
| 379 | + } |
| 380 | + |
| 381 | + @Test |
| 382 | + void createCountQuerySupportsLineBreakRightAfterDistinct() { |
| 383 | + |
| 384 | + assertThat(JSqlParserQueryUtils.createCountQueryFor("select\ndistinct\nuser.age,\n" + // |
| 385 | + "user.name\n" + // |
| 386 | + "from\nUser\nuser")).isEqualTo(JSqlParserQueryUtils.createCountQueryFor("select\ndistinct user.age,\n" + // |
| 387 | + "user.name\n" + // |
| 388 | + "from\nUser\nuser")); |
| 389 | + } |
| 390 | + |
| 391 | + @Test |
| 392 | + void createsCountQueryCorrectly() { |
| 393 | + assertCountQuery(QUERY, COUNT_QUERY); |
| 394 | + } |
| 395 | + |
| 396 | + @Test |
| 397 | + void createsCountQueriesCorrectlyForCapitalLetter() { |
| 398 | + assertCountQuery("SELECT u FROM User u where u.foo.bar = ?", "select count(u) FROM User u where u.foo.bar = ?"); |
| 399 | + } |
| 400 | + |
| 401 | + @Test |
| 402 | + void createsCountQueryForDistinctQueries() { |
| 403 | + |
| 404 | + assertCountQuery("select distinct u from User u where u.foo = ?", |
| 405 | + "select count(distinct u) from User u where u.foo = ?"); |
| 406 | + } |
| 407 | + |
| 408 | + @Test |
| 409 | + void failsOnConstructorQueries() { |
| 410 | + |
| 411 | + assertThatThrownBy( |
| 412 | + () -> JSqlParserQueryUtils.createCountQueryFor("select distinct new User(u.name) from User u where u.foo = ?")) |
| 413 | + .isInstanceOf(IllegalArgumentException.class); |
| 414 | + } |
| 415 | + |
| 416 | + @Test |
| 417 | + void createsCountQueryForJoins() { |
| 418 | + |
| 419 | + assertCountQuery("select distinct u.name from User u left outer join u.roles r WHERE r = ?", |
| 420 | + "select count(distinct u.name) from User u left outer join u.roles r WHERE r = ?"); |
| 421 | + } |
| 422 | + |
| 423 | + @Test |
| 424 | + void createsCountQueryForQueriesWithSubSelects() { |
| 425 | + |
| 426 | + assertCountQuery("select u from User u left outer join u.roles r where r in (select r from Role)", |
| 427 | + "select count(u) from User u left outer join u.roles r where r in (select r from Role)"); |
| 428 | + } |
| 429 | + |
| 430 | + @Test |
| 431 | + void createsCountQueryForAliasesCorrectly() { |
| 432 | + |
| 433 | + assertCountQuery("select u from User as u", "select count(u) from User as u"); |
| 434 | + } |
| 435 | + |
| 436 | + @Test |
| 437 | + void doesNotAllowShortJpaSyntax() { |
| 438 | + assertThatThrownBy(() -> assertCountQuery(INVALID_QUERY, COUNT_QUERY)).isInstanceOf(IllegalArgumentException.class); |
| 439 | + } |
| 440 | + |
| 441 | + @Test |
| 442 | + void allowsFullyQualifiedEntityNamesInQuery() { |
| 443 | + |
| 444 | + assertThat(detectAlias(FQ_QUERY)).isEqualTo("u"); |
| 445 | + assertCountQuery(FQ_QUERY, "select count(u) from org.acme.domain.User$Foo_Bar u"); |
| 446 | + } |
| 447 | + |
| 448 | + @Test // DATAJPA-342 |
| 449 | + void usesReturnedVariableInCOuntProjectionIfSet() { |
| 450 | + |
| 451 | + assertCountQuery("select distinct m.genre from Media m where m.user = ?1 order by m.genre asc", |
| 452 | + "select count(distinct m.genre) from Media m where m.user = ?1"); |
| 453 | + } |
| 454 | + |
| 455 | + @Test // DATAJPA-343 |
| 456 | + void projectsCOuntQueriesForQueriesWithSubselects() { |
| 457 | + |
| 458 | + assertCountQuery("select o from Foo o where cb.id in (select b from Bar b)", |
| 459 | + "select count(o) from Foo o where cb.id in (select b from Bar b)"); |
| 460 | + } |
| 461 | + |
| 462 | + @Test // DATAJPA-377 |
| 463 | + void removesOrderByInGeneratedCountQueryFromOriginalQueryIfPresent() { |
| 464 | + |
| 465 | + assertCountQuery("select distinct m.genre from Media m where m.user = ?1 OrDer By m.genre ASC", |
| 466 | + "select count(distinct m.genre) from Media m where m.user = ?1"); |
| 467 | + } |
| 468 | + |
| 469 | + @Test // DATAJPA-409 |
| 470 | + void createsCountQueryForNestedReferenceCorrectly() { |
| 471 | + assertCountQuery("select a.b from A a", "select count(a.b) from A a"); |
| 472 | + } |
| 473 | + |
| 474 | + @Test // DATAJPA-1679 |
| 475 | + void findProjectionClauseWithDistinct() { |
| 476 | + |
| 477 | + SoftAssertions.assertSoftly(sofly -> { |
| 478 | + sofly.assertThat(JSqlParserQueryUtils.getProjection("select * from x")).isEqualTo("*"); |
| 479 | + sofly.assertThat(JSqlParserQueryUtils.getProjection("select a, b, c from x")).isEqualTo("a, b, c"); |
| 480 | + sofly.assertThat(JSqlParserQueryUtils.getProjection("select distinct a, b, c from x")).isEqualTo("a, b, c"); |
| 481 | + sofly.assertThat(JSqlParserQueryUtils.getProjection("select DISTINCT a, b, c from x")).isEqualTo("a, b, c"); |
| 482 | + }); |
| 483 | + } |
| 484 | + |
| 485 | + @Test // DATAJPA-1696 |
| 486 | + void findProjectionClauseWithSubselect() { |
| 487 | + |
| 488 | + // This is not a required behavior the testcase in QueryUtilsUnitTests#findProjectionClauseWithSubselect tells why |
| 489 | + assertThat(JSqlParserQueryUtils.getProjection("select * from (select x from y)")).isEqualTo("*"); |
| 490 | + } |
| 491 | + |
| 492 | + @Test // DATAJPA-1696 |
| 493 | + void findProjectionClauseWithIncludedFrom() { |
| 494 | + assertThat(JSqlParserQueryUtils.getProjection("select x, frommage, y from t")).isEqualTo("x, frommage, y"); |
| 495 | + } |
| 496 | + |
| 497 | + private static void assertCountQuery(String originalQuery, String countQuery) { |
| 498 | + assertThat(JSqlParserQueryUtils.createCountQueryFor(originalQuery)).isEqualToIgnoringCase(countQuery); |
| 499 | + } |
| 500 | + |
350 | 501 | private static void endsIgnoringCase(String original, String endWithIgnoreCase) {
|
351 | 502 | // https://github.com/assertj/assertj-core/pull/2451
|
352 | 503 | // can be removed when upgrading to version 3.23.0 assertJ
|
|
0 commit comments