|
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 |
|
@@ -182,55 +184,82 @@ void testRemoveSubqueries() throws Exception { |
182 | 184 | @Test // GH-2581 |
183 | 185 | void testRemoveMultilineSubqueries() { |
184 | 186 |
|
185 | | - assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n" |
186 | | - + " where not exists (\n" |
187 | | - + " from User u2\n" |
188 | | - + " )"))) |
189 | | - .isEqualTo("select u from User u where not exists"); |
190 | | - assertThat(normalizeWhitespace(removeSubqueries("(\n" |
191 | | - + " select u from User u \n" |
192 | | - + " where not exists (\n" |
193 | | - + " from User u2\n" |
194 | | - + " )\n" |
195 | | - + ")"))) |
196 | | - .isEqualTo("( select u from User u where not exists )"); |
197 | | - assertThat(normalizeWhitespace( |
198 | | - removeSubqueries("select u from User u \n" |
199 | | - + " where not exists (\n" |
200 | | - + " from User u2 \n" |
201 | | - + " where not exists (\n" |
202 | | - + " from User u3\n" |
203 | | - + " )\n" |
204 | | - + " )"))) |
205 | | - .isEqualTo("select u from User u where not exists"); |
206 | | - assertThat(normalizeWhitespace( |
207 | | - removeSubqueries("select u from User u \n" |
208 | | - + " where not exists (\n" |
209 | | - + " (\n" |
210 | | - + " from User u2 \n" |
211 | | - + " where not exists (\n" |
212 | | - + " from User u3\n" |
213 | | - + " )\n" |
214 | | - + " )\n" |
215 | | - + " )"))) |
216 | | - .isEqualTo("select u from User u where not exists ( )"); |
217 | | - assertThat(normalizeWhitespace( |
218 | | - removeSubqueries("(\n" |
219 | | - + " select u from User u \n" |
220 | | - + " where not exists (\n" |
221 | | - + " (\n" |
222 | | - + " from User u2 \n" |
223 | | - + " where not exists (\n" |
224 | | - + " from User u3\n" |
225 | | - + " )\n" |
226 | | - + " )\n" |
227 | | - + " )\n" |
228 | | - + ")"))) |
229 | | - .isEqualTo("( select u from User u where not exists ( ) )"); |
| 187 | + assertThat(normalizeWhitespace(removeSubqueries("select u from User u\n" // |
| 188 | + + " where not exists (\n" // |
| 189 | + + " from User u2\n" // |
| 190 | + + " )"))).isEqualTo("select u from User u where not exists"); |
| 191 | + |
| 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 | + + ")"))).isEqualTo("( select u from User u where not exists )"); |
| 198 | + |
| 199 | + assertThat(normalizeWhitespace(removeSubqueries("select u from User u \n" // |
| 200 | + + " where not exists (\n" // |
| 201 | + + " from User u2 \n" // |
| 202 | + + " where not exists (\n" // |
| 203 | + + " from User u3\n" // |
| 204 | + + " )\n" // |
| 205 | + + " )"))).isEqualTo("select u from User u where not exists"); |
| 206 | + |
| 207 | + assertThat(normalizeWhitespace(removeSubqueries("select u from User u \n" // |
| 208 | + + " where not exists (\n" // |
| 209 | + + " (\n" // |
| 210 | + + " from User u2 \n" // |
| 211 | + + " where not exists (\n" // |
| 212 | + + " from User u3\n" // |
| 213 | + + " )\n" // |
| 214 | + + " )\n" // |
| 215 | + + " )"))).isEqualTo("select u from User u where not exists ( )"); |
| 216 | + |
| 217 | + assertThat(normalizeWhitespace(removeSubqueries("(\n" // |
| 218 | + + " select u from User u \n" // |
| 219 | + + " where not exists (\n" // |
| 220 | + + " (\n" // |
| 221 | + + " from User u2 \n" // |
| 222 | + + " where not exists (\n" // |
| 223 | + + " from User u3\n" // |
| 224 | + + " )\n" // |
| 225 | + + " )\n" // |
| 226 | + + " )\n" // |
| 227 | + + ")"))).isEqualTo("( select u from User u where not exists ( ) )"); |
| 228 | + } |
| 229 | + |
| 230 | + @Test // GH-2557 |
| 231 | + void applySortingAccountsForNewlinesInSubselect() { |
| 232 | + |
| 233 | + Sort sort = Sort.by(Order.desc("age")); |
| 234 | + |
| 235 | + assertThat(QueryUtils.applySorting("select u\n" + // |
| 236 | + "from user u\n" + // |
| 237 | + "where exists (select u2\n" + // |
| 238 | + "from user u2\n" + // |
| 239 | + ")\n" + // |
| 240 | + "", sort)).isEqualTo("select u\n" + // |
| 241 | + "from user u\n" + // |
| 242 | + "where exists (select u2\n" + // |
| 243 | + "from user u2\n" + // |
| 244 | + ")\n" + // |
| 245 | + " order by u.age desc"); |
| 246 | + } |
| 247 | + |
| 248 | + @Test // GH-2563 |
| 249 | + void aliasDetectionProperlyHandlesNewlinesInSubselects() { |
| 250 | + |
| 251 | + assertThat(detectAlias("SELECT o\n" + // |
| 252 | + "FROM Order o\n" + // |
| 253 | + "AND EXISTS(SELECT 1\n" + // |
| 254 | + "FROM Vehicle vehicle\n" + // |
| 255 | + "WHERE vehicle.vehicleOrderId = o.id\n" + // |
| 256 | + "AND LOWER(COALESCE(vehicle.make, '')) LIKE :query)")).isEqualTo("o"); |
230 | 257 | } |
231 | 258 |
|
232 | 259 | private String normalizeWhitespace(String s) { |
| 260 | + |
233 | 261 | Matcher matcher = MULTI_WHITESPACE.matcher(s); |
| 262 | + |
234 | 263 | if (matcher.find()) { |
235 | 264 | return matcher.replaceAll(" ").trim(); |
236 | 265 | } |
|
0 commit comments