22
22
23
23
import java .lang .reflect .Method ;
24
24
import java .util .Arrays ;
25
+ import java .util .List ;
25
26
import java .util .regex .Pattern ;
26
27
27
28
import javax .lang .model .element .Modifier ;
37
38
import org .springframework .data .geo .Point ;
38
39
import org .springframework .data .geo .Polygon ;
39
40
import org .springframework .data .mongodb .core .MongoOperations ;
41
+ import org .springframework .data .mongodb .core .annotation .Collation ;
40
42
import org .springframework .data .mongodb .core .geo .GeoJsonPolygon ;
41
43
import org .springframework .data .mongodb .core .geo .Sphere ;
44
+ import org .springframework .data .mongodb .repository .Hint ;
42
45
import org .springframework .data .mongodb .repository .ReadPreference ;
43
46
import org .springframework .data .repository .Repository ;
44
47
import org .springframework .data .repository .aot .generate .AotQueryMethodGenerationContext ;
@@ -211,8 +214,36 @@ void rendersRegexCriteria() throws NoSuchMethodException {
211
214
MethodSpec methodSpec = codeOf (UserRepository .class , "findByFirstnameRegex" , Pattern .class );
212
215
213
216
assertThat (methodSpec .toString ()) //
214
- .contains ("createQuery(\" {'firstname':{'$regex':?0}}\" " ) //
215
- .contains ("Object[]{ pattern }" );
217
+ .contains ("createQuery(\" {'firstname':{'$regex':?0}}\" " ) //
218
+ .contains ("Object[]{ pattern }" );
219
+ }
220
+
221
+ @ Test // GH-4939
222
+ void rendersHint () throws NoSuchMethodException {
223
+
224
+ MethodSpec methodSpec = codeOf (UserRepoWithMeta .class , "findByFirstname" , String .class );
225
+
226
+ assertThat (methodSpec .toString ()) //
227
+ .contains (".withHint(\" fn-idx\" )" );
228
+ }
229
+
230
+ @ Test // GH-4939
231
+ void rendersCollation () throws NoSuchMethodException {
232
+
233
+ MethodSpec methodSpec = codeOf (UserRepoWithMeta .class , "findByFirstname" , String .class );
234
+
235
+ assertThat (methodSpec .toString ()) //
236
+ .containsPattern (".*\\ .collation\\ (.*Collation\\ .parse\\ (\" en_US\" \\ )\\ )" );
237
+ }
238
+
239
+ @ Test // GH-4939
240
+ void rendersCollationFromExpression () throws NoSuchMethodException {
241
+
242
+ MethodSpec methodSpec = codeOf (UserRepoWithMeta .class , "findWithCollationByFirstname" , String .class , String .class );
243
+
244
+ assertThat (methodSpec .toString ()) //
245
+ .containsIgnoringWhitespaces (
246
+ "collationOf(evaluate(\" ?#{[1]}\" , java.util.Map.of(\" firstname\" , firstname, \" locale\" , locale)))" );
216
247
}
217
248
218
249
private static MethodSpec codeOf (Class <?> repository , String methodName , Class <?>... args )
@@ -228,7 +259,7 @@ private static MethodSpec codeOf(Class<?> repository, String methodName, Class<?
228
259
Assertions .fail ("No contribution for method %s.%s(%s)" .formatted (repository .getSimpleName (), methodName ,
229
260
Arrays .stream (args ).map (Class ::getSimpleName ).toList ()));
230
261
}
231
- AotRepositoryFragmentMetadata metadata = new AotRepositoryFragmentMetadata (ClassName .get (UserRepository . class ));
262
+ AotRepositoryFragmentMetadata metadata = new AotRepositoryFragmentMetadata (ClassName .get (repository ));
232
263
metadata .addField (
233
264
FieldSpec .builder (MongoOperations .class , "mongoOperations" , Modifier .PRIVATE , Modifier .FINAL ).build ());
234
265
@@ -247,6 +278,13 @@ protected TestQueryMethodGenerationContext(RepositoryInformation repositoryInfor
247
278
248
279
interface UserRepoWithMeta extends Repository <User , String > {
249
280
281
+ @ Hint ("fn-idx" )
282
+ @ Collation ("en_US" )
283
+ List <User > findByFirstname (String firstname );
284
+
285
+ @ Collation ("?#{[1]}" )
286
+ List <User > findWithCollationByFirstname (String firstname , String locale );
287
+
250
288
@ ReadPreference ("NEAREST" )
251
289
GeoResults <User > findByLocationCoordinatesNear (Point point , Distance maxDistance );
252
290
}
0 commit comments