29
29
import java .util .Map ;
30
30
import java .util .Optional ;
31
31
import java .util .TreeMap ;
32
+ import java .util .regex .Pattern ;
32
33
34
+ import org .bson .BsonRegularExpression ;
33
35
import org .bson .conversions .Bson ;
34
36
import org .bson .types .Code ;
35
37
import org .bson .types .ObjectId ;
36
38
import org .junit .jupiter .api .BeforeEach ;
37
39
import org .junit .jupiter .api .Test ;
40
+
38
41
import org .springframework .core .convert .converter .Converter ;
39
42
import org .springframework .data .annotation .Id ;
40
43
import org .springframework .data .annotation .Transient ;
52
55
import org .springframework .data .mongodb .core .aggregation .TypeBasedAggregationOperationContext ;
53
56
import org .springframework .data .mongodb .core .geo .GeoJsonPoint ;
54
57
import org .springframework .data .mongodb .core .geo .GeoJsonPolygon ;
55
- import org .springframework .data .mongodb .core .mapping .*;
58
+ import org .springframework .data .mongodb .core .mapping .DBRef ;
59
+ import org .springframework .data .mongodb .core .mapping .Document ;
60
+ import org .springframework .data .mongodb .core .mapping .DocumentReference ;
61
+ import org .springframework .data .mongodb .core .mapping .Field ;
56
62
import org .springframework .data .mongodb .core .mapping .FieldName .Type ;
63
+ import org .springframework .data .mongodb .core .mapping .FieldType ;
64
+ import org .springframework .data .mongodb .core .mapping .MongoId ;
65
+ import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
66
+ import org .springframework .data .mongodb .core .mapping .MongoPersistentEntity ;
67
+ import org .springframework .data .mongodb .core .mapping .TextScore ;
68
+ import org .springframework .data .mongodb .core .mapping .Unwrapped ;
57
69
import org .springframework .data .mongodb .core .query .BasicQuery ;
58
70
import org .springframework .data .mongodb .core .query .Criteria ;
59
71
import org .springframework .data .mongodb .core .query .Query ;
@@ -554,7 +566,7 @@ void queryMapperShouldNotTryToMapDBRefListPropertyIfNestedInsideDocumentWithinDo
554
566
555
567
org .bson .Document queryObject = query (
556
568
where ("referenceList" ).is (new org .bson .Document ("$nested" , new org .bson .Document ("$keys" , 0L ))))
557
- .getQueryObject ();
569
+ .getQueryObject ();
558
570
559
571
org .bson .Document mappedObject = mapper .getMappedObject (queryObject ,
560
572
context .getPersistentEntity (WithDBRefList .class ));
@@ -826,7 +838,7 @@ void mappingShouldRetainNestedNumericMapKeys() {
826
838
@ Test // GH-3688
827
839
void mappingShouldAllowSettingEntireNestedNumericKeyedMapValue () {
828
840
829
- Query query = query (where ("outerMap.1.map" ).is (null )); //newEntityWithComplexValueTypeMap()
841
+ Query query = query (where ("outerMap.1.map" ).is (null )); // newEntityWithComplexValueTypeMap()
830
842
831
843
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
832
844
context .getPersistentEntity (EntityWithIntKeyedMapOfMap .class ));
@@ -998,6 +1010,22 @@ void shouldConvertCollectionPropertyWithExplicitTargetType() {
998
1010
assertThat (document ).isEqualTo (new org .bson .Document ("scripts" , new Code (script )));
999
1011
}
1000
1012
1013
+ @ Test // GH-4649
1014
+ void shouldRetainRegexPattern () {
1015
+
1016
+ Query query = new Query (where ("text" ).regex ("foo" ));
1017
+
1018
+ org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1019
+ context .getPersistentEntity (WithExplicitTargetTypes .class ));
1020
+
1021
+ assertThat (document .get ("text" )).isInstanceOf (Pattern .class );
1022
+
1023
+ query = new Query (where ("text" ).regex (new BsonRegularExpression ("foo" )));
1024
+ document = mapper .getMappedObject (query .getQueryObject (),
1025
+ context .getPersistentEntity (WithExplicitTargetTypes .class ));
1026
+ assertThat (document .get ("text" )).isInstanceOf (BsonRegularExpression .class );
1027
+ }
1028
+
1001
1029
@ Test // DATAMONGO-2339
1002
1030
void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly () {
1003
1031
@@ -1301,7 +1329,8 @@ void resolvesFieldnameWithUnderscoresCorrectly() {
1301
1329
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1302
1330
context .getPersistentEntity (WithPropertyUsingUnderscoreInName .class ));
1303
1331
1304
- assertThat (document ).isEqualTo (new org .bson .Document ("fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1332
+ assertThat (document )
1333
+ .isEqualTo (new org .bson .Document ("fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1305
1334
}
1306
1335
1307
1336
@ Test // GH-3601
@@ -1323,7 +1352,8 @@ void resolvesSimpleNestedFieldnameWithUnderscoresCorrectly() {
1323
1352
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1324
1353
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1325
1354
1326
- assertThat (document ).isEqualTo (new org .bson .Document ("simple.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1355
+ assertThat (document )
1356
+ .isEqualTo (new org .bson .Document ("simple.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1327
1357
}
1328
1358
1329
1359
@ Test // GH-3601
@@ -1345,7 +1375,8 @@ void resolvesFieldNameWithUnderscoreOnNestedFieldnameWithUnderscoresCorrectly()
1345
1375
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1346
1376
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1347
1377
1348
- assertThat (document ).isEqualTo (new org .bson .Document ("double_underscore.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1378
+ assertThat (document ).isEqualTo (
1379
+ new org .bson .Document ("double_underscore.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1349
1380
}
1350
1381
1351
1382
@ Test // GH-3601
@@ -1356,7 +1387,8 @@ void resolvesFieldNameWithUnderscoreOnNestedMappedFieldnameWithUnderscoresCorrec
1356
1387
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1357
1388
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1358
1389
1359
- assertThat (document ).isEqualTo (new org .bson .Document ("double_underscore.renamed" , new org .bson .Document ("$exists" , true )));
1390
+ assertThat (document )
1391
+ .isEqualTo (new org .bson .Document ("double_underscore.renamed" , new org .bson .Document ("$exists" , true )));
1360
1392
}
1361
1393
1362
1394
@ Test // GH-3633
@@ -1394,7 +1426,8 @@ void allowsUsingFieldPathsForPropertiesHavingCustomConversionRegistered() {
1394
1426
1395
1427
Query query = query (where ("address.street" ).is ("1007 Mountain Drive" ));
1396
1428
1397
- MongoCustomConversions mongoCustomConversions = new MongoCustomConversions (Collections .singletonList (new MyAddressToDocumentConverter ()));
1429
+ MongoCustomConversions mongoCustomConversions = new MongoCustomConversions (
1430
+ Collections .singletonList (new MyAddressToDocumentConverter ()));
1398
1431
1399
1432
this .context = new MongoMappingContext ();
1400
1433
this .context .setSimpleTypeHolder (mongoCustomConversions .getSimpleTypeHolder ());
@@ -1406,7 +1439,8 @@ void allowsUsingFieldPathsForPropertiesHavingCustomConversionRegistered() {
1406
1439
1407
1440
this .mapper = new QueryMapper (converter );
1408
1441
1409
- assertThat (mapper .getMappedSort (query .getQueryObject (), context .getPersistentEntity (Customer .class ))).isEqualTo (new org .bson .Document ("address.street" , "1007 Mountain Drive" ));
1442
+ assertThat (mapper .getMappedSort (query .getQueryObject (), context .getPersistentEntity (Customer .class )))
1443
+ .isEqualTo (new org .bson .Document ("address.street" , "1007 Mountain Drive" ));
1410
1444
}
1411
1445
1412
1446
@ Test // GH-3790
@@ -1427,7 +1461,8 @@ void shouldAcceptExprAsCriteriaDefinition() {
1427
1461
@ Test // GH-3668
1428
1462
void mapStringIdFieldProjection () {
1429
1463
1430
- org .bson .Document mappedFields = mapper .getMappedFields (new org .bson .Document ("id" , 1 ), context .getPersistentEntity (WithStringId .class ));
1464
+ org .bson .Document mappedFields = mapper .getMappedFields (new org .bson .Document ("id" , 1 ),
1465
+ context .getPersistentEntity (WithStringId .class ));
1431
1466
assertThat (mappedFields ).containsEntry ("_id" , 1 );
1432
1467
}
1433
1468
@@ -1453,7 +1488,8 @@ void mapStringIdFieldProjection() {
1453
1488
@ Test // GH-3596
1454
1489
void considersValueConverterWhenPresent () {
1455
1490
1456
- org .bson .Document mappedObject = mapper .getMappedObject (new org .bson .Document ("text" , "value" ), context .getPersistentEntity (WithPropertyValueConverter .class ));
1491
+ org .bson .Document mappedObject = mapper .getMappedObject (new org .bson .Document ("text" , "value" ),
1492
+ context .getPersistentEntity (WithPropertyValueConverter .class ));
1457
1493
assertThat (mappedObject ).isEqualTo (new org .bson .Document ("text" , "eulav" ));
1458
1494
}
1459
1495
@@ -1514,7 +1550,8 @@ void convertsListOfValuesForPropertyThatHasValueConverterButIsNotCollectionLikeO
1514
1550
@ Test // GH-4464
1515
1551
void usesKeyNameWithDotsIfFieldNameTypeIsKey () {
1516
1552
1517
- org .bson .Document mappedObject = mapper .getMappedObject (query (where ("value" ).is ("A" )).getQueryObject (), context .getPersistentEntity (WithPropertyHavingDotsInFieldName .class ));
1553
+ org .bson .Document mappedObject = mapper .getMappedObject (query (where ("value" ).is ("A" )).getQueryObject (),
1554
+ context .getPersistentEntity (WithPropertyHavingDotsInFieldName .class ));
1518
1555
assertThat (mappedObject ).isEqualTo ("{ 'field.name.with.dots' : 'A' }" );
1519
1556
}
1520
1557
@@ -1659,23 +1696,20 @@ static class WithDocumentReference {
1659
1696
1660
1697
private String name ;
1661
1698
1662
- @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" )
1663
- private Customer customer ;
1699
+ @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" ) private Customer customer ;
1664
1700
1665
- @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" )
1666
- private List <Customer > customers ;
1701
+ @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" ) private List <Customer > customers ;
1667
1702
1668
- @ DocumentReference
1669
- private Sample sample ;
1703
+ @ DocumentReference private Sample sample ;
1670
1704
1671
- @ DocumentReference
1672
- private List <Sample > samples ;
1705
+ @ DocumentReference private List <Sample > samples ;
1673
1706
}
1674
1707
1675
1708
class WithTextScoreProperty {
1676
1709
1677
1710
@ Id String id ;
1678
- @ TextScore @ Field ("score" ) Float textScore ;
1711
+ @ TextScore
1712
+ @ Field ("score" ) Float textScore ;
1679
1713
}
1680
1714
1681
1715
static class RootForClassWithExplicitlyRenamedIdField {
@@ -1712,7 +1746,7 @@ static class EntityWithComplexValueTypeMap {
1712
1746
Map <Integer , SimpleEntityWithoutId > map ;
1713
1747
}
1714
1748
1715
- static class EntityWithIntKeyedMapOfMap {
1749
+ static class EntityWithIntKeyedMapOfMap {
1716
1750
Map <Integer , EntityWithComplexValueTypeMap > outerMap ;
1717
1751
}
1718
1752
@@ -1725,6 +1759,9 @@ static class WithExplicitTargetTypes {
1725
1759
@ Field (targetType = FieldType .SCRIPT ) //
1726
1760
String script ;
1727
1761
1762
+ @ Field (targetType = FieldType .STRING ) //
1763
+ String text ;
1764
+
1728
1765
@ Field (targetType = FieldType .SCRIPT ) //
1729
1766
List <String > scripts ;
1730
1767
}
@@ -1794,15 +1831,13 @@ static class WithPropertyUsingUnderscoreInName {
1794
1831
1795
1832
String fieldname_with_underscores ;
1796
1833
1797
- @ Field ("renamed" )
1798
- String renamed_fieldname_with_underscores ;
1834
+ @ Field ("renamed" ) String renamed_fieldname_with_underscores ;
1799
1835
}
1800
1836
1801
1837
@ Document
1802
1838
static class Customer {
1803
1839
1804
- @ Id
1805
- private ObjectId id ;
1840
+ @ Id private ObjectId id ;
1806
1841
private String name ;
1807
1842
private MyAddress address ;
1808
1843
}
@@ -1813,8 +1848,7 @@ static class MyAddress {
1813
1848
1814
1849
static class WithPropertyValueConverter {
1815
1850
1816
- @ ValueConverter (ReversingValueConverter .class )
1817
- String text ;
1851
+ @ ValueConverter (ReversingValueConverter .class ) String text ;
1818
1852
}
1819
1853
1820
1854
@ WritingConverter
@@ -1830,8 +1864,7 @@ public org.bson.Document convert(MyAddress address) {
1830
1864
1831
1865
static class WithPropertyHavingDotsInFieldName {
1832
1866
1833
- @ Field (name = "field.name.with.dots" , nameType = Type .KEY )
1834
- String value ;
1867
+ @ Field (name = "field.name.with.dots" , nameType = Type .KEY ) String value ;
1835
1868
1836
1869
}
1837
1870
}
0 commit comments