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 ;
@@ -553,7 +556,7 @@ void queryMapperShouldNotTryToMapDBRefListPropertyIfNestedInsideDocumentWithinDo
553
556
554
557
org .bson .Document queryObject = query (
555
558
where ("referenceList" ).is (new org .bson .Document ("$nested" , new org .bson .Document ("$keys" , 0L ))))
556
- .getQueryObject ();
559
+ .getQueryObject ();
557
560
558
561
org .bson .Document mappedObject = mapper .getMappedObject (queryObject ,
559
562
context .getPersistentEntity (WithDBRefList .class ));
@@ -825,7 +828,7 @@ void mappingShouldRetainNestedNumericMapKeys() {
825
828
@ Test // GH-3688
826
829
void mappingShouldAllowSettingEntireNestedNumericKeyedMapValue () {
827
830
828
- Query query = query (where ("outerMap.1.map" ).is (null )); //newEntityWithComplexValueTypeMap()
831
+ Query query = query (where ("outerMap.1.map" ).is (null )); // newEntityWithComplexValueTypeMap()
829
832
830
833
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
831
834
context .getPersistentEntity (EntityWithIntKeyedMapOfMap .class ));
@@ -997,6 +1000,22 @@ void shouldConvertCollectionPropertyWithExplicitTargetType() {
997
1000
assertThat (document ).isEqualTo (new org .bson .Document ("scripts" , new Code (script )));
998
1001
}
999
1002
1003
+ @ Test // GH-4649
1004
+ void shouldRetainRegexPattern () {
1005
+
1006
+ Query query = new Query (where ("text" ).regex ("foo" ));
1007
+
1008
+ org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1009
+ context .getPersistentEntity (WithExplicitTargetTypes .class ));
1010
+
1011
+ assertThat (document .get ("text" )).isInstanceOf (Pattern .class );
1012
+
1013
+ query = new Query (where ("text" ).regex (new BsonRegularExpression ("foo" )));
1014
+ document = mapper .getMappedObject (query .getQueryObject (),
1015
+ context .getPersistentEntity (WithExplicitTargetTypes .class ));
1016
+ assertThat (document .get ("text" )).isInstanceOf (BsonRegularExpression .class );
1017
+ }
1018
+
1000
1019
@ Test // DATAMONGO-2339
1001
1020
void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly () {
1002
1021
@@ -1300,7 +1319,8 @@ void resolvesFieldnameWithUnderscoresCorrectly() {
1300
1319
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1301
1320
context .getPersistentEntity (WithPropertyUsingUnderscoreInName .class ));
1302
1321
1303
- assertThat (document ).isEqualTo (new org .bson .Document ("fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1322
+ assertThat (document )
1323
+ .isEqualTo (new org .bson .Document ("fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1304
1324
}
1305
1325
1306
1326
@ Test // GH-3601
@@ -1322,7 +1342,8 @@ void resolvesSimpleNestedFieldnameWithUnderscoresCorrectly() {
1322
1342
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1323
1343
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1324
1344
1325
- assertThat (document ).isEqualTo (new org .bson .Document ("simple.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1345
+ assertThat (document )
1346
+ .isEqualTo (new org .bson .Document ("simple.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1326
1347
}
1327
1348
1328
1349
@ Test // GH-3601
@@ -1344,7 +1365,8 @@ void resolvesFieldNameWithUnderscoreOnNestedFieldnameWithUnderscoresCorrectly()
1344
1365
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1345
1366
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1346
1367
1347
- assertThat (document ).isEqualTo (new org .bson .Document ("double_underscore.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1368
+ assertThat (document ).isEqualTo (
1369
+ new org .bson .Document ("double_underscore.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1348
1370
}
1349
1371
1350
1372
@ Test // GH-3601
@@ -1355,7 +1377,8 @@ void resolvesFieldNameWithUnderscoreOnNestedMappedFieldnameWithUnderscoresCorrec
1355
1377
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1356
1378
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1357
1379
1358
- assertThat (document ).isEqualTo (new org .bson .Document ("double_underscore.renamed" , new org .bson .Document ("$exists" , true )));
1380
+ assertThat (document )
1381
+ .isEqualTo (new org .bson .Document ("double_underscore.renamed" , new org .bson .Document ("$exists" , true )));
1359
1382
}
1360
1383
1361
1384
@ Test // GH-3633
@@ -1393,7 +1416,8 @@ void allowsUsingFieldPathsForPropertiesHavingCustomConversionRegistered() {
1393
1416
1394
1417
Query query = query (where ("address.street" ).is ("1007 Mountain Drive" ));
1395
1418
1396
- MongoCustomConversions mongoCustomConversions = new MongoCustomConversions (Collections .singletonList (new MyAddressToDocumentConverter ()));
1419
+ MongoCustomConversions mongoCustomConversions = new MongoCustomConversions (
1420
+ Collections .singletonList (new MyAddressToDocumentConverter ()));
1397
1421
1398
1422
this .context = new MongoMappingContext ();
1399
1423
this .context .setSimpleTypeHolder (mongoCustomConversions .getSimpleTypeHolder ());
@@ -1405,7 +1429,8 @@ void allowsUsingFieldPathsForPropertiesHavingCustomConversionRegistered() {
1405
1429
1406
1430
this .mapper = new QueryMapper (converter );
1407
1431
1408
- assertThat (mapper .getMappedSort (query .getQueryObject (), context .getPersistentEntity (Customer .class ))).isEqualTo (new org .bson .Document ("address.street" , "1007 Mountain Drive" ));
1432
+ assertThat (mapper .getMappedSort (query .getQueryObject (), context .getPersistentEntity (Customer .class )))
1433
+ .isEqualTo (new org .bson .Document ("address.street" , "1007 Mountain Drive" ));
1409
1434
}
1410
1435
1411
1436
@ Test // GH-3790
@@ -1426,7 +1451,8 @@ void shouldAcceptExprAsCriteriaDefinition() {
1426
1451
@ Test // GH-3668
1427
1452
void mapStringIdFieldProjection () {
1428
1453
1429
- org .bson .Document mappedFields = mapper .getMappedFields (new org .bson .Document ("id" , 1 ), context .getPersistentEntity (WithStringId .class ));
1454
+ org .bson .Document mappedFields = mapper .getMappedFields (new org .bson .Document ("id" , 1 ),
1455
+ context .getPersistentEntity (WithStringId .class ));
1430
1456
assertThat (mappedFields ).containsEntry ("_id" , 1 );
1431
1457
}
1432
1458
@@ -1452,7 +1478,8 @@ void mapStringIdFieldProjection() {
1452
1478
@ Test // GH-3596
1453
1479
void considersValueConverterWhenPresent () {
1454
1480
1455
- org .bson .Document mappedObject = mapper .getMappedObject (new org .bson .Document ("text" , "value" ), context .getPersistentEntity (WithPropertyValueConverter .class ));
1481
+ org .bson .Document mappedObject = mapper .getMappedObject (new org .bson .Document ("text" , "value" ),
1482
+ context .getPersistentEntity (WithPropertyValueConverter .class ));
1456
1483
assertThat (mappedObject ).isEqualTo (new org .bson .Document ("text" , "eulav" ));
1457
1484
}
1458
1485
@@ -1651,23 +1678,20 @@ static class WithDocumentReference {
1651
1678
1652
1679
private String name ;
1653
1680
1654
- @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" )
1655
- private Customer customer ;
1681
+ @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" ) private Customer customer ;
1656
1682
1657
- @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" )
1658
- private List <Customer > customers ;
1683
+ @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" ) private List <Customer > customers ;
1659
1684
1660
- @ DocumentReference
1661
- private Sample sample ;
1685
+ @ DocumentReference private Sample sample ;
1662
1686
1663
- @ DocumentReference
1664
- private List <Sample > samples ;
1687
+ @ DocumentReference private List <Sample > samples ;
1665
1688
}
1666
1689
1667
1690
class WithTextScoreProperty {
1668
1691
1669
1692
@ Id String id ;
1670
- @ TextScore @ Field ("score" ) Float textScore ;
1693
+ @ TextScore
1694
+ @ Field ("score" ) Float textScore ;
1671
1695
}
1672
1696
1673
1697
static class RootForClassWithExplicitlyRenamedIdField {
@@ -1704,7 +1728,7 @@ static class EntityWithComplexValueTypeMap {
1704
1728
Map <Integer , SimpleEntityWithoutId > map ;
1705
1729
}
1706
1730
1707
- static class EntityWithIntKeyedMapOfMap {
1731
+ static class EntityWithIntKeyedMapOfMap {
1708
1732
Map <Integer , EntityWithComplexValueTypeMap > outerMap ;
1709
1733
}
1710
1734
@@ -1717,6 +1741,9 @@ static class WithExplicitTargetTypes {
1717
1741
@ Field (targetType = FieldType .SCRIPT ) //
1718
1742
String script ;
1719
1743
1744
+ @ Field (targetType = FieldType .STRING ) //
1745
+ String text ;
1746
+
1720
1747
@ Field (targetType = FieldType .SCRIPT ) //
1721
1748
List <String > scripts ;
1722
1749
}
@@ -1786,15 +1813,13 @@ static class WithPropertyUsingUnderscoreInName {
1786
1813
1787
1814
String fieldname_with_underscores ;
1788
1815
1789
- @ Field ("renamed" )
1790
- String renamed_fieldname_with_underscores ;
1816
+ @ Field ("renamed" ) String renamed_fieldname_with_underscores ;
1791
1817
}
1792
1818
1793
1819
@ Document
1794
1820
static class Customer {
1795
1821
1796
- @ Id
1797
- private ObjectId id ;
1822
+ @ Id private ObjectId id ;
1798
1823
private String name ;
1799
1824
private MyAddress address ;
1800
1825
}
@@ -1805,8 +1830,7 @@ static class MyAddress {
1805
1830
1806
1831
static class WithPropertyValueConverter {
1807
1832
1808
- @ ValueConverter (ReversingValueConverter .class )
1809
- String text ;
1833
+ @ ValueConverter (ReversingValueConverter .class ) String text ;
1810
1834
}
1811
1835
1812
1836
@ WritingConverter
0 commit comments