Skip to content

Commit d61cf8f

Browse files
committed
Polishing.
Simplify assertions, reformat code. See #3921 Original pull request: #3930.
1 parent 50a1212 commit d61cf8f

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2021 the original author or authors.
2+
* Copyright 2011-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1412,11 +1412,11 @@ public KeyMapper(String key,
14121412
}
14131413

14141414
String nextToken() {
1415-
return pathParts.get(currentIndex+1);
1415+
return pathParts.get(currentIndex + 1);
14161416
}
14171417

14181418
boolean hasNexToken() {
1419-
return pathParts.size() > currentIndex+1;
1419+
return pathParts.size() > currentIndex + 1;
14201420
}
14211421

14221422
/**
@@ -1428,24 +1428,25 @@ boolean hasNexToken() {
14281428
protected String mapPropertyName(MongoPersistentProperty property) {
14291429

14301430
StringBuilder mappedName = new StringBuilder(PropertyToFieldNameConverter.INSTANCE.convert(property));
1431-
if(!hasNexToken()) {
1431+
if (!hasNexToken()) {
14321432
return mappedName.toString();
14331433
}
14341434

14351435
String nextToken = nextToken();
1436-
if(isPositionalParameter(nextToken)) {
1436+
if (isPositionalParameter(nextToken)) {
14371437

14381438
mappedName.append(".").append(nextToken);
1439-
currentIndex+=2;
1439+
currentIndex += 2;
14401440
return mappedName.toString();
14411441
}
14421442

1443-
if(property.isMap()) {
1443+
if (property.isMap()) {
14441444

14451445
mappedName.append(".").append(nextToken);
1446-
currentIndex+=2;
1446+
currentIndex += 2;
14471447
return mappedName.toString();
14481448
}
1449+
14491450
currentIndex++;
14501451
return mappedName.toString();
14511452
}

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

+21-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2021 the original author or authors.
2+
* Copyright 2013-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1208,7 +1208,7 @@ void mapNestedStringFieldCorrectly() {
12081208
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
12091209
context.getPersistentEntity(EntityWithNestedMap.class));
12101210

1211-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.a.b.d","e")));
1211+
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.a.b.d", "e")));
12121212
}
12131213

12141214
@Test // GH-3775
@@ -1218,7 +1218,7 @@ void mapNestedIntegerFieldCorrectly() {
12181218
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
12191219
context.getPersistentEntity(EntityWithNestedMap.class));
12201220

1221-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.0.1.3","4")));
1221+
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.3", "4")));
12221222
}
12231223

12241224
@Test // GH-3775
@@ -1228,7 +1228,7 @@ void mapNestedMixedStringIntegerFieldCorrectly() {
12281228
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
12291229
context.getPersistentEntity(EntityWithNestedMap.class));
12301230

1231-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.0.1.c","4")));
1231+
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.c", "4")));
12321232
}
12331233

12341234
@Test // GH-3775
@@ -1238,7 +1238,7 @@ void mapNestedMixedStringIntegerWithStartNumberFieldCorrectly() {
12381238
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
12391239
context.getPersistentEntity(EntityWithNestedMap.class));
12401240

1241-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.0a.1b.3c","4")));
1241+
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0a.1b.3c", "4")));
12421242
}
12431243

12441244
@Test // GH-3688
@@ -1262,7 +1262,7 @@ void updateWithDocuRefOnId() {
12621262
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
12631263
context.getPersistentEntity(WithDocumentReference.class));
12641264

1265-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("sample","s1")));
1265+
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("sample", "s1")));
12661266
}
12671267

12681268
@Test // GH-3853
@@ -1276,7 +1276,8 @@ void updateListWithDocuRefOnId() {
12761276
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
12771277
context.getPersistentEntity(WithDocumentReference.class));
12781278

1279-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("samples",Arrays.asList("s1"))));
1279+
assertThat(mappedUpdate)
1280+
.isEqualTo(new org.bson.Document("$set", new org.bson.Document("samples", Arrays.asList("s1"))));
12801281
}
12811282

12821283
@Test // GH-3853
@@ -1291,7 +1292,7 @@ void updateWithDocuRefOnProperty() {
12911292
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
12921293
context.getPersistentEntity(WithDocumentReference.class));
12931294

1294-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("customer","c-name")));
1295+
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("customer", "c-name")));
12951296
}
12961297

12971298
@Test // GH-3853
@@ -1306,7 +1307,8 @@ void updateListWithDocuRefOnProperty() {
13061307
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
13071308
context.getPersistentEntity(WithDocumentReference.class));
13081309

1309-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("customers", Arrays.asList("c-name"))));
1310+
assertThat(mappedUpdate)
1311+
.isEqualTo(new org.bson.Document("$set", new org.bson.Document("customers", Arrays.asList("c-name"))));
13101312
}
13111313

13121314
@Test // GH-3921
@@ -1316,7 +1318,7 @@ void mapNumericKeyInPathHavingComplexMapValyeTypes() {
13161318
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
13171319
context.getPersistentEntity(TestData.class));
13181320

1319-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("testInnerData.testMap.1.intValue","4")));
1321+
assertThat(mappedUpdate).isEqualTo("{ $set: { 'testInnerData.testMap.1.intValue': '4' }}");
13201322
}
13211323

13221324
@Test // GH-3921
@@ -1326,7 +1328,7 @@ void mapNumericKeyInPathNotMatchingExistingProperties() {
13261328
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
13271329
context.getPersistentEntity(TestData.class));
13281330

1329-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("testInnerData.imaginaryMap.1.noExistingProperty","4")));
1331+
assertThat(mappedUpdate).isEqualTo("{ $set: { 'testInnerData.imaginaryMap.1.nonExistingProperty': '4' }}");
13301332
}
13311333

13321334
@Test // GH-3921
@@ -1336,7 +1338,7 @@ void mapNumericKeyInPathPartiallyMatchingExistingProperties() {
13361338
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
13371339
context.getPersistentEntity(TestData.class));
13381340

1339-
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("testInnerData.testMap.1.nonExistingProperty.2.someValue","4")));
1341+
assertThat(mappedUpdate).isEqualTo("{ $set: { 'testInnerData.testMap.1.nonExistingProperty.2.someValue': '4' }}");
13401342
}
13411343

13421344
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
@@ -1575,7 +1577,7 @@ static class EntityWithObjectMap {
15751577
Map<Object, NestedDocument> concreteMap;
15761578
}
15771579

1578-
static class EntityWithIntKeyedMap{
1580+
static class EntityWithIntKeyedMap {
15791581
Map<Integer, EntityWithObjectMap> intKeyedMap;
15801582
}
15811583

@@ -1711,8 +1713,7 @@ static class EntityWithNestedMap {
17111713

17121714
static class Customer {
17131715

1714-
@Id
1715-
private ObjectId id;
1716+
@Id private ObjectId id;
17161717
private String name;
17171718
}
17181719

@@ -1727,23 +1728,18 @@ static class WithDocumentReference {
17271728

17281729
private String name;
17291730

1730-
@DocumentReference(lookup = "{ 'name' : ?#{#target} }")
1731-
private Customer customer;
1731+
@DocumentReference(lookup = "{ 'name' : ?#{#target} }") private Customer customer;
17321732

1733-
@DocumentReference(lookup = "{ 'name' : ?#{#target} }")
1734-
private List<Customer> customers;
1733+
@DocumentReference(lookup = "{ 'name' : ?#{#target} }") private List<Customer> customers;
17351734

1736-
@DocumentReference
1737-
private Sample sample;
1735+
@DocumentReference private Sample sample;
17381736

1739-
@DocumentReference
1740-
private List<Sample> samples;
1737+
@DocumentReference private List<Sample> samples;
17411738
}
17421739

17431740
@Data
17441741
private static class TestData {
1745-
@Id
1746-
private String id;
1742+
@Id private String id;
17471743
private TestInnerData testInnerData;
17481744
}
17491745

0 commit comments

Comments
 (0)