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 ;
@@ -629,7 +641,7 @@ void queryMapperShouldNotTryToMapDBRefListPropertyIfNestedInsideDocumentWithinDo
629
641
630
642
org .bson .Document queryObject = query (
631
643
where ("referenceList" ).is (new org .bson .Document ("$nested" , new org .bson .Document ("$keys" , 0L ))))
632
- .getQueryObject ();
644
+ .getQueryObject ();
633
645
634
646
org .bson .Document mappedObject = mapper .getMappedObject (queryObject ,
635
647
context .getPersistentEntity (WithDBRefList .class ));
@@ -901,7 +913,7 @@ void mappingShouldRetainNestedNumericMapKeys() {
901
913
@ Test // GH-3688
902
914
void mappingShouldAllowSettingEntireNestedNumericKeyedMapValue () {
903
915
904
- Query query = query (where ("outerMap.1.map" ).is (null )); //newEntityWithComplexValueTypeMap()
916
+ Query query = query (where ("outerMap.1.map" ).is (null )); // newEntityWithComplexValueTypeMap()
905
917
906
918
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
907
919
context .getPersistentEntity (EntityWithIntKeyedMapOfMap .class ));
@@ -1073,6 +1085,22 @@ void shouldConvertCollectionPropertyWithExplicitTargetType() {
1073
1085
assertThat (document ).isEqualTo (new org .bson .Document ("scripts" , new Code (script )));
1074
1086
}
1075
1087
1088
+ @ Test // GH-4649
1089
+ void shouldRetainRegexPattern () {
1090
+
1091
+ Query query = new Query (where ("text" ).regex ("foo" ));
1092
+
1093
+ org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1094
+ context .getPersistentEntity (WithExplicitTargetTypes .class ));
1095
+
1096
+ assertThat (document .get ("text" )).isInstanceOf (Pattern .class );
1097
+
1098
+ query = new Query (where ("text" ).regex (new BsonRegularExpression ("foo" )));
1099
+ document = mapper .getMappedObject (query .getQueryObject (),
1100
+ context .getPersistentEntity (WithExplicitTargetTypes .class ));
1101
+ assertThat (document .get ("text" )).isInstanceOf (BsonRegularExpression .class );
1102
+ }
1103
+
1076
1104
@ Test // DATAMONGO-2339
1077
1105
void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly () {
1078
1106
@@ -1376,7 +1404,8 @@ void resolvesFieldnameWithUnderscoresCorrectly() {
1376
1404
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1377
1405
context .getPersistentEntity (WithPropertyUsingUnderscoreInName .class ));
1378
1406
1379
- assertThat (document ).isEqualTo (new org .bson .Document ("fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1407
+ assertThat (document )
1408
+ .isEqualTo (new org .bson .Document ("fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1380
1409
}
1381
1410
1382
1411
@ Test // GH-3601
@@ -1398,7 +1427,8 @@ void resolvesSimpleNestedFieldnameWithUnderscoresCorrectly() {
1398
1427
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1399
1428
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1400
1429
1401
- assertThat (document ).isEqualTo (new org .bson .Document ("simple.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1430
+ assertThat (document )
1431
+ .isEqualTo (new org .bson .Document ("simple.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1402
1432
}
1403
1433
1404
1434
@ Test // GH-3601
@@ -1420,7 +1450,8 @@ void resolvesFieldNameWithUnderscoreOnNestedFieldnameWithUnderscoresCorrectly()
1420
1450
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1421
1451
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1422
1452
1423
- assertThat (document ).isEqualTo (new org .bson .Document ("double_underscore.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1453
+ assertThat (document ).isEqualTo (
1454
+ new org .bson .Document ("double_underscore.fieldname_with_underscores" , new org .bson .Document ("$exists" , true )));
1424
1455
}
1425
1456
1426
1457
@ Test // GH-3601
@@ -1431,7 +1462,8 @@ void resolvesFieldNameWithUnderscoreOnNestedMappedFieldnameWithUnderscoresCorrec
1431
1462
org .bson .Document document = mapper .getMappedObject (query .getQueryObject (),
1432
1463
context .getPersistentEntity (WrapperAroundWithPropertyUsingUnderscoreInName .class ));
1433
1464
1434
- assertThat (document ).isEqualTo (new org .bson .Document ("double_underscore.renamed" , new org .bson .Document ("$exists" , true )));
1465
+ assertThat (document )
1466
+ .isEqualTo (new org .bson .Document ("double_underscore.renamed" , new org .bson .Document ("$exists" , true )));
1435
1467
}
1436
1468
1437
1469
@ Test // GH-3633
@@ -1469,7 +1501,8 @@ void allowsUsingFieldPathsForPropertiesHavingCustomConversionRegistered() {
1469
1501
1470
1502
Query query = query (where ("address.street" ).is ("1007 Mountain Drive" ));
1471
1503
1472
- MongoCustomConversions mongoCustomConversions = new MongoCustomConversions (Collections .singletonList (new MyAddressToDocumentConverter ()));
1504
+ MongoCustomConversions mongoCustomConversions = new MongoCustomConversions (
1505
+ Collections .singletonList (new MyAddressToDocumentConverter ()));
1473
1506
1474
1507
this .context = new MongoMappingContext ();
1475
1508
this .context .setSimpleTypeHolder (mongoCustomConversions .getSimpleTypeHolder ());
@@ -1481,7 +1514,8 @@ void allowsUsingFieldPathsForPropertiesHavingCustomConversionRegistered() {
1481
1514
1482
1515
this .mapper = new QueryMapper (converter );
1483
1516
1484
- assertThat (mapper .getMappedSort (query .getQueryObject (), context .getPersistentEntity (Customer .class ))).isEqualTo (new org .bson .Document ("address.street" , "1007 Mountain Drive" ));
1517
+ assertThat (mapper .getMappedSort (query .getQueryObject (), context .getPersistentEntity (Customer .class )))
1518
+ .isEqualTo (new org .bson .Document ("address.street" , "1007 Mountain Drive" ));
1485
1519
}
1486
1520
1487
1521
@ Test // GH-3790
@@ -1502,7 +1536,8 @@ void shouldAcceptExprAsCriteriaDefinition() {
1502
1536
@ Test // GH-3668
1503
1537
void mapStringIdFieldProjection () {
1504
1538
1505
- org .bson .Document mappedFields = mapper .getMappedFields (new org .bson .Document ("id" , 1 ), context .getPersistentEntity (WithStringId .class ));
1539
+ org .bson .Document mappedFields = mapper .getMappedFields (new org .bson .Document ("id" , 1 ),
1540
+ context .getPersistentEntity (WithStringId .class ));
1506
1541
assertThat (mappedFields ).containsEntry ("_id" , 1 );
1507
1542
}
1508
1543
@@ -1528,7 +1563,8 @@ void mapStringIdFieldProjection() {
1528
1563
@ Test // GH-3596
1529
1564
void considersValueConverterWhenPresent () {
1530
1565
1531
- org .bson .Document mappedObject = mapper .getMappedObject (new org .bson .Document ("text" , "value" ), context .getPersistentEntity (WithPropertyValueConverter .class ));
1566
+ org .bson .Document mappedObject = mapper .getMappedObject (new org .bson .Document ("text" , "value" ),
1567
+ context .getPersistentEntity (WithPropertyValueConverter .class ));
1532
1568
assertThat (mappedObject ).isEqualTo (new org .bson .Document ("text" , "eulav" ));
1533
1569
}
1534
1570
@@ -1589,7 +1625,8 @@ void convertsListOfValuesForPropertyThatHasValueConverterButIsNotCollectionLikeO
1589
1625
@ Test // GH-4464
1590
1626
void usesKeyNameWithDotsIfFieldNameTypeIsKey () {
1591
1627
1592
- org .bson .Document mappedObject = mapper .getMappedObject (query (where ("value" ).is ("A" )).getQueryObject (), context .getPersistentEntity (WithPropertyHavingDotsInFieldName .class ));
1628
+ org .bson .Document mappedObject = mapper .getMappedObject (query (where ("value" ).is ("A" )).getQueryObject (),
1629
+ context .getPersistentEntity (WithPropertyHavingDotsInFieldName .class ));
1593
1630
assertThat (mappedObject ).isEqualTo ("{ 'field.name.with.dots' : 'A' }" );
1594
1631
}
1595
1632
@@ -1606,7 +1643,8 @@ void mappingShouldRetainMapKeyOrder() {
1606
1643
@ Test // GH-4510
1607
1644
void convertsNestedOperatorValueForPropertyThatHasValueConverter () {
1608
1645
1609
- org .bson .Document mappedObject = mapper .getMappedObject (query (where ("text" ).gt ("spring" ).lt ( "data" )).getQueryObject (),
1646
+ org .bson .Document mappedObject = mapper .getMappedObject (
1647
+ query (where ("text" ).gt ("spring" ).lt ("data" )).getQueryObject (),
1610
1648
context .getPersistentEntity (WithPropertyValueConverter .class ));
1611
1649
1612
1650
assertThat (mappedObject ).isEqualTo ("{ 'text' : { $gt : 'gnirps', $lt : 'atad' } }" );
@@ -1615,7 +1653,8 @@ void convertsNestedOperatorValueForPropertyThatHasValueConverter() {
1615
1653
@ Test // GH-4510
1616
1654
void convertsNestedOperatorValueForPropertyContainingListThatHasValueConverter () {
1617
1655
1618
- org .bson .Document mappedObject = mapper .getMappedObject (query (where ("text" ).gt ("spring" ).in ( "data" )).getQueryObject (),
1656
+ org .bson .Document mappedObject = mapper .getMappedObject (
1657
+ query (where ("text" ).gt ("spring" ).in ("data" )).getQueryObject (),
1619
1658
context .getPersistentEntity (WithPropertyValueConverter .class ));
1620
1659
1621
1660
assertThat (mappedObject ).isEqualTo ("{ 'text' : { $gt : 'gnirps', $in : [ 'atad' ] } }" );
@@ -1752,23 +1791,20 @@ static class WithDocumentReference {
1752
1791
1753
1792
private String name ;
1754
1793
1755
- @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" )
1756
- private Customer customer ;
1794
+ @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" ) private Customer customer ;
1757
1795
1758
- @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" )
1759
- private List <Customer > customers ;
1796
+ @ DocumentReference (lookup = "{ 'name' : ?#{#target} }" ) private List <Customer > customers ;
1760
1797
1761
- @ DocumentReference
1762
- private Sample sample ;
1798
+ @ DocumentReference private Sample sample ;
1763
1799
1764
- @ DocumentReference
1765
- private List <Sample > samples ;
1800
+ @ DocumentReference private List <Sample > samples ;
1766
1801
}
1767
1802
1768
1803
class WithTextScoreProperty {
1769
1804
1770
1805
@ Id String id ;
1771
- @ TextScore @ Field ("score" ) Float textScore ;
1806
+ @ TextScore
1807
+ @ Field ("score" ) Float textScore ;
1772
1808
}
1773
1809
1774
1810
static class RootForClassWithExplicitlyRenamedIdField {
@@ -1805,7 +1841,7 @@ static class EntityWithComplexValueTypeMap {
1805
1841
Map <Integer , SimpleEntityWithoutId > map ;
1806
1842
}
1807
1843
1808
- static class EntityWithIntKeyedMapOfMap {
1844
+ static class EntityWithIntKeyedMapOfMap {
1809
1845
Map <Integer , EntityWithComplexValueTypeMap > outerMap ;
1810
1846
}
1811
1847
@@ -1818,6 +1854,9 @@ static class WithExplicitTargetTypes {
1818
1854
@ Field (targetType = FieldType .SCRIPT ) //
1819
1855
String script ;
1820
1856
1857
+ @ Field (targetType = FieldType .STRING ) //
1858
+ String text ;
1859
+
1821
1860
@ Field (targetType = FieldType .SCRIPT ) //
1822
1861
List <String > scripts ;
1823
1862
}
@@ -1887,15 +1926,13 @@ static class WithPropertyUsingUnderscoreInName {
1887
1926
1888
1927
String fieldname_with_underscores ;
1889
1928
1890
- @ Field ("renamed" )
1891
- String renamed_fieldname_with_underscores ;
1929
+ @ Field ("renamed" ) String renamed_fieldname_with_underscores ;
1892
1930
}
1893
1931
1894
1932
@ Document
1895
1933
static class Customer {
1896
1934
1897
- @ Id
1898
- private ObjectId id ;
1935
+ @ Id private ObjectId id ;
1899
1936
private String name ;
1900
1937
private MyAddress address ;
1901
1938
}
@@ -1906,8 +1943,7 @@ static class MyAddress {
1906
1943
1907
1944
static class WithPropertyValueConverter {
1908
1945
1909
- @ ValueConverter (ReversingValueConverter .class )
1910
- String text ;
1946
+ @ ValueConverter (ReversingValueConverter .class ) String text ;
1911
1947
}
1912
1948
1913
1949
@ WritingConverter
@@ -1923,8 +1959,7 @@ public org.bson.Document convert(MyAddress address) {
1923
1959
1924
1960
static class WithPropertyHavingDotsInFieldName {
1925
1961
1926
- @ Field (name = "field.name.with.dots" , nameType = Type .KEY )
1927
- String value ;
1962
+ @ Field (name = "field.name.with.dots" , nameType = Type .KEY ) String value ;
1928
1963
1929
1964
}
1930
1965
}
0 commit comments