DATAMONGO-1778 - Polishing.

Migrate UpdateTests to AssertJ and adjust constructor visibility.

Original pull request: #503.
This commit is contained in:
Christoph Strobl
2017-09-21 15:21:01 +02:00
committed by Mark Paluch
parent c05f8f056c
commit 10f13c8f37
2 changed files with 87 additions and 85 deletions

View File

@@ -656,7 +656,7 @@ public class Update {
private Object[] values;
public Each(Object... values) {
Each(Object... values) {
this.values = extractValues(values);
}
@@ -702,7 +702,7 @@ public class Update {
private final int position;
public PositionModifier(int position) {
PositionModifier(int position) {
this.position = position;
}
@@ -727,7 +727,7 @@ public class Update {
private int count;
public Slice(int count) {
Slice(int count) {
this.count = count;
}
@@ -766,7 +766,7 @@ public class Update {
*
* @param direction must not be {@literal null}.
*/
public SortModifier(Direction direction) {
SortModifier(Direction direction) {
Assert.notNull(direction, "Direction must not be null!");
this.sort = direction.isAscending() ? 1 : -1;
@@ -777,7 +777,7 @@ public class Update {
*
* @param sort must not be {@literal null}.
*/
public SortModifier(Sort sort) {
SortModifier(Sort sort) {
Assert.notNull(sort, "Sort must not be null!");

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.mongodb.core.query;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.Collections;
import java.util.Date;
@@ -44,51 +43,52 @@ public class UpdateTests {
public void testSet() {
Update u = new Update().set("directory", "/Users/Test/Desktop");
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}")));
assertThat(u.getUpdateObject())
.isEqualTo(Document.parse("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}"));
}
@Test
public void testSetSet() {
Update u = new Update().set("directory", "/Users/Test/Desktop").set("size", 0);
assertThat(u.getUpdateObject(),
is(Document.parse("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\" , \"size\" : 0}}")));
assertThat(u.getUpdateObject())
.isEqualTo((Document.parse("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\" , \"size\" : 0}}")));
}
@Test
public void testInc() {
Update u = new Update().inc("size", 1);
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$inc\" : { \"size\" : 1}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$inc\" : { \"size\" : 1}}"));
}
@Test
public void testIncInc() {
Update u = new Update().inc("size", 1).inc("count", 1);
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$inc\" : { \"size\" : 1 , \"count\" : 1}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$inc\" : { \"size\" : 1 , \"count\" : 1}}"));
}
@Test
public void testIncAndSet() {
Update u = new Update().inc("size", 1).set("directory", "/Users/Test/Desktop");
assertThat(u.getUpdateObject(),
is(Document.parse("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}")));
assertThat(u.getUpdateObject()).isEqualTo(
Document.parse("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}"));
}
@Test
public void testUnset() {
Update u = new Update().unset("directory");
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$unset\" : { \"directory\" : 1}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$unset\" : { \"directory\" : 1}}"));
}
@Test
public void testPush() {
Update u = new Update().push("authors", Collections.singletonMap("name", "Sven"));
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$push\" : { \"authors\" : { \"name\" : \"Sven\"}}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$push\" : { \"authors\" : { \"name\" : \"Sven\"}}}"));
}
@Test
@@ -98,8 +98,8 @@ public class UpdateTests {
Map<String, String> m2 = Collections.singletonMap("name", "Maria");
Update u = new Update().pushAll("authors", new Object[] { m1, m2 });
assertThat(u.getUpdateObject(),
is(Document.parse("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}")));
assertThat(u.getUpdateObject()).isEqualTo(
Document.parse("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}"));
}
@Test // DATAMONGO-354
@@ -111,32 +111,33 @@ public class UpdateTests {
Update u = new Update().pushAll("authors", new Object[] { m1, m2 });
u.pushAll("books", new Object[] { "Spring in Action" });
assertThat(u.getUpdateObject(), is(Document.parse(
"{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}] , \"books\" : [ \"Spring in Action\"]}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse(
"{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}] , \"books\" : [ \"Spring in Action\"]}}"));
}
@Test
public void testAddToSet() {
Update u = new Update().addToSet("authors", Collections.singletonMap("name", "Sven"));
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$addToSet\" : { \"authors\" : { \"name\" : \"Sven\"}}}")));
assertThat(u.getUpdateObject())
.isEqualTo(Document.parse("{ \"$addToSet\" : { \"authors\" : { \"name\" : \"Sven\"}}}"));
}
@Test
public void testPop() {
Update u = new Update().pop("authors", Update.Position.FIRST);
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$pop\" : { \"authors\" : -1}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$pop\" : { \"authors\" : -1}}"));
u = new Update().pop("authors", Update.Position.LAST);
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$pop\" : { \"authors\" : 1}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$pop\" : { \"authors\" : 1}}"));
}
@Test
public void testPull() {
Update u = new Update().pull("authors", Collections.singletonMap("name", "Sven"));
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$pull\" : { \"authors\" : { \"name\" : \"Sven\"}}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$pull\" : { \"authors\" : { \"name\" : \"Sven\"}}}"));
}
@Test
@@ -146,77 +147,77 @@ public class UpdateTests {
Map<String, String> m2 = Collections.singletonMap("name", "Maria");
Update u = new Update().pullAll("authors", new Object[] { m1, m2 });
assertThat(u.getUpdateObject(),
is(Document.parse("{ \"$pullAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}")));
assertThat(u.getUpdateObject()).isEqualTo(
Document.parse("{ \"$pullAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}"));
}
@Test
public void testRename() {
Update u = new Update().rename("directory", "folder");
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$rename\" : { \"directory\" : \"folder\"}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$rename\" : { \"directory\" : \"folder\"}}"));
}
@Test
public void testBasicUpdateInc() {
Update u = new Update().inc("size", 1);
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$inc\" : { \"size\" : 1}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$inc\" : { \"size\" : 1}}"));
}
@Test
public void testBasicUpdateIncAndSet() {
Update u = new BasicUpdate("{ \"$inc\" : { \"size\" : 1}}").set("directory", "/Users/Test/Desktop");
assertThat(u.getUpdateObject(),
is(Document.parse("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}")));
assertThat(u.getUpdateObject()).isEqualTo(
Document.parse("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}"));
}
@Test // DATAMONGO-630
public void testSetOnInsert() {
Update u = new Update().setOnInsert("size", 1);
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$setOnInsert\" : { \"size\" : 1}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$setOnInsert\" : { \"size\" : 1}}"));
}
@Test // DATAMONGO-630
public void testSetOnInsertSetOnInsert() {
Update u = new Update().setOnInsert("size", 1).setOnInsert("count", 1);
assertThat(u.getUpdateObject(), is(Document.parse("{ \"$setOnInsert\" : { \"size\" : 1 , \"count\" : 1}}")));
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$setOnInsert\" : { \"size\" : 1 , \"count\" : 1}}"));
}
@Test // DATAMONGO-852
public void testUpdateAffectsFieldShouldReturnTrueWhenMultiFieldOperationAddedForField() {
Update update = new Update().set("foo", "bar");
assertThat(update.modifies("foo"), is(true));
assertThat(update.modifies("foo")).isTrue();
}
@Test // DATAMONGO-852
public void testUpdateAffectsFieldShouldReturnFalseWhenMultiFieldOperationAddedForField() {
Update update = new Update().set("foo", "bar");
assertThat(update.modifies("oof"), is(false));
assertThat(update.modifies("oof")).isFalse();
}
@Test // DATAMONGO-852
public void testUpdateAffectsFieldShouldReturnTrueWhenSingleFieldOperationAddedForField() {
Update update = new Update().pullAll("foo", new Object[] { "bar" });
assertThat(update.modifies("foo"), is(true));
assertThat(update.modifies("foo")).isTrue();
}
@Test // DATAMONGO-852
public void testUpdateAffectsFieldShouldReturnFalseWhenSingleFieldOperationAddedForField() {
Update update = new Update().pullAll("foo", new Object[] { "bar" });
assertThat(update.modifies("oof"), is(false));
assertThat(update.modifies("oof")).isFalse();
}
@Test // DATAMONGO-852
public void testUpdateAffectsFieldShouldReturnFalseWhenCalledOnEmptyUpdate() {
assertThat(new Update().modifies("foo"), is(false));
assertThat(new Update().modifies("foo")).isFalse();
}
@Test // DATAMONGO-852
@@ -225,7 +226,7 @@ public class UpdateTests {
Update update = new Update().set("foo", "bar");
Update clone = Update.fromDocument(update.getUpdateObject());
assertThat(clone.modifies("foo"), is(true));
assertThat(clone.modifies("foo")).isTrue();
}
@Test // DATAMONGO-852
@@ -234,7 +235,7 @@ public class UpdateTests {
Update update = new Update().set("foo", "bar");
Update clone = Update.fromDocument(update.getUpdateObject());
assertThat(clone.modifies("oof"), is(false));
assertThat(clone.modifies("oof")).isFalse();
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-853
@@ -271,10 +272,10 @@ public class UpdateTests {
.pop("authors", Update.Position.FIRST) //
.set("foo", "bar");
assertThat(actualUpdate, is(equalTo(actualUpdate)));
assertThat(actualUpdate.hashCode(), is(equalTo(actualUpdate.hashCode())));
assertThat(actualUpdate, is(equalTo(expectedUpdate)));
assertThat(actualUpdate.hashCode(), is(equalTo(expectedUpdate.hashCode())));
assertThat(actualUpdate).isEqualTo(actualUpdate);
assertThat(actualUpdate.hashCode()).isEqualTo(actualUpdate.hashCode());
assertThat(actualUpdate).isEqualTo(expectedUpdate);
assertThat(actualUpdate.hashCode()).isEqualTo(expectedUpdate.hashCode());
}
@Test // DATAMONGO-953
@@ -296,58 +297,57 @@ public class UpdateTests {
.pop("authors", Update.Position.FIRST) //
.set("foo", "bar");
assertThat(actualUpdate.toString(), is(equalTo(expectedUpdate.toString())));
assertThat(actualUpdate.getUpdateObject(),
is(Document.parse("{ \"$inc\" : { \"size\" : 1} ," //
+ " \"$set\" : { \"nl\" : null , \"directory\" : \"/Users/Test/Desktop\" , \"foo\" : \"bar\"} , " //
+ "\"$push\" : { \"authors\" : { \"name\" : \"Sven\"}} " //
+ ", \"$pop\" : { \"authors\" : -1}}"))); //
assertThat(actualUpdate.toString()).isEqualTo(expectedUpdate.toString());
assertThat(actualUpdate.getUpdateObject()).isEqualTo(Document.parse("{ \"$inc\" : { \"size\" : 1} ," //
+ " \"$set\" : { \"nl\" : null , \"directory\" : \"/Users/Test/Desktop\" , \"foo\" : \"bar\"} , " //
+ "\"$push\" : { \"authors\" : { \"name\" : \"Sven\"}} " //
+ ", \"$pop\" : { \"authors\" : -1}}")); //
}
@Test // DATAMONGO-944
public void getUpdateObjectShouldReturnCurrentDateCorrectlyForSingleFieldWhenUsingDate() {
Update update = new Update().currentDate("foo");
assertThat(update.getUpdateObject(), equalTo(new Document().append("$currentDate", new Document("foo", true))));
assertThat(update.getUpdateObject()).isEqualTo(new Document().append("$currentDate", new Document("foo", true)));
}
@Test // DATAMONGO-944
public void getUpdateObjectShouldReturnCurrentDateCorrectlyForMultipleFieldsWhenUsingDate() {
Update update = new Update().currentDate("foo").currentDate("bar");
assertThat(update.getUpdateObject(),
equalTo(new Document().append("$currentDate", new Document("foo", true).append("bar", true))));
assertThat(update.getUpdateObject())
.isEqualTo(new Document().append("$currentDate", new Document("foo", true).append("bar", true)));
}
@Test // DATAMONGO-944
public void getUpdateObjectShouldReturnCurrentDateCorrectlyForSingleFieldWhenUsingTimestamp() {
Update update = new Update().currentTimestamp("foo");
assertThat(update.getUpdateObject(),
equalTo(new Document().append("$currentDate", new Document("foo", new Document("$type", "timestamp")))));
assertThat(update.getUpdateObject())
.isEqualTo(new Document().append("$currentDate", new Document("foo", new Document("$type", "timestamp"))));
}
@Test // DATAMONGO-944
public void getUpdateObjectShouldReturnCurrentDateCorrectlyForMultipleFieldsWhenUsingTimestamp() {
Update update = new Update().currentTimestamp("foo").currentTimestamp("bar");
assertThat(update.getUpdateObject(), equalTo(new Document().append("$currentDate",
new Document("foo", new Document("$type", "timestamp")).append("bar", new Document("$type", "timestamp")))));
assertThat(update.getUpdateObject()).isEqualTo(new Document().append("$currentDate",
new Document("foo", new Document("$type", "timestamp")).append("bar", new Document("$type", "timestamp"))));
}
@Test // DATAMONGO-944
public void getUpdateObjectShouldReturnCurrentDateCorrectlyWhenUsingMixedDateAndTimestamp() {
Update update = new Update().currentDate("foo").currentTimestamp("bar");
assertThat(update.getUpdateObject(), equalTo(new Document().append("$currentDate",
new Document("foo", true).append("bar", new Document("$type", "timestamp")))));
assertThat(update.getUpdateObject()).isEqualTo(new Document().append("$currentDate",
new Document("foo", true).append("bar", new Document("$type", "timestamp"))));
}
@Test // DATAMONGO-1002
public void toStringWorksForUpdateWithComplexObject() {
Update update = new Update().addToSet("key", new DateTime());
assertThat(update.toString(), is(notNullValue()));
assertThat(update.toString()).isNotNull();
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1097
@@ -360,7 +360,7 @@ public class UpdateTests {
Update update = new Update().multiply("key", 10);
assertThat(update.getUpdateObject(), equalTo(new Document().append("$mul", new Document("key", 10D))));
assertThat(update.getUpdateObject()).isEqualTo(new Document().append("$mul", new Document("key", 10D)));
}
@Test // DATAMONGO-1101
@@ -368,8 +368,8 @@ public class UpdateTests {
Update update = new Update().bitwise("key").and(10L);
assertThat(update.getUpdateObject(),
equalTo(new Document().append("$bit", new Document("key", new Document("and", 10L)))));
assertThat(update.getUpdateObject())
.isEqualTo(new Document().append("$bit", new Document("key", new Document("and", 10L))));
}
@Test // DATAMONGO-1101
@@ -377,8 +377,8 @@ public class UpdateTests {
Update update = new Update().bitwise("key").or(10L);
assertThat(update.getUpdateObject(),
equalTo(new Document().append("$bit", new Document("key", new Document("or", 10L)))));
assertThat(update.getUpdateObject())
.isEqualTo(new Document().append("$bit", new Document("key", new Document("or", 10L))));
}
@Test // DATAMONGO-1101
@@ -386,8 +386,8 @@ public class UpdateTests {
Update update = new Update().bitwise("key").xor(10L);
assertThat(update.getUpdateObject(),
equalTo(new Document().append("$bit", new Document("key", new Document("xor", 10L)))));
assertThat(update.getUpdateObject())
.isEqualTo(new Document().append("$bit", new Document("key", new Document("xor", 10L))));
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-943
@@ -406,8 +406,8 @@ public class UpdateTests {
Document pullAll = DocumentTestUtils.getAsDocument(updateObject, "$pullAll");
assertThat(pullAll.get("field1"), is(notNullValue()));
assertThat(pullAll.get("field2"), is(notNullValue()));
assertThat(pullAll.get("field1")).isNotNull();
assertThat(pullAll.get("field2")).isNotNull();
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1404
@@ -425,7 +425,7 @@ public class UpdateTests {
Update update = new Update().max("key", 10);
assertThat(update.getUpdateObject(), equalTo(new Document("$max", new Document("key", 10))));
assertThat(update.getUpdateObject()).isEqualTo(new Document("$max", new Document("key", 10)));
}
@Test // DATAMONGO-1404
@@ -433,7 +433,7 @@ public class UpdateTests {
Update update = new Update().min("key", 10);
assertThat(update.getUpdateObject(), equalTo(new Document("$min", new Document("key", 10))));
assertThat(update.getUpdateObject()).isEqualTo(new Document("$min", new Document("key", 10)));
}
@Test // DATAMONGO-1404
@@ -442,7 +442,7 @@ public class UpdateTests {
Update update = new Update().max("key", 10);
update.max("key", 99);
assertThat(update.getUpdateObject(), equalTo(new Document("$max", new Document("key", 99))));
assertThat(update.getUpdateObject()).isEqualTo(new Document("$max", new Document("key", 99)));
}
@Test // DATAMONGO-1404
@@ -451,7 +451,7 @@ public class UpdateTests {
Update update = new Update().min("key", 10);
update.min("key", 99);
assertThat(update.getUpdateObject(), equalTo(new Document("$min", new Document("key", 99))));
assertThat(update.getUpdateObject()).isEqualTo(new Document("$min", new Document("key", 99)));
}
@Test // DATAMONGO-1404
@@ -460,7 +460,7 @@ public class UpdateTests {
Date date = new Date();
Update update = new Update().max("key", date);
assertThat(update.getUpdateObject(), equalTo(new Document("$max", new Document("key", date))));
assertThat(update.getUpdateObject()).isEqualTo(new Document("$max", new Document("key", date)));
}
@Test // DATAMONGO-1404
@@ -469,21 +469,21 @@ public class UpdateTests {
Date date = new Date();
Update update = new Update().min("key", date);
assertThat(update.getUpdateObject(), equalTo(new Document("$min", new Document("key", date))));
assertThat(update.getUpdateObject()).isEqualTo(new Document("$min", new Document("key", date)));
}
@Test // DATAMONGO-1777
public void toStringShouldPrettyPrintModifiers() {
assertThat(new Update().push("key").atPosition(Position.FIRST).value("Arya").toString(), is(equalTo(
"{ \"$push\" : { \"key\" : { \"$java\" : { \"$position\" : { \"$java\" : { \"$position\" : 0} }, \"$each\" : { \"$java\" : { \"$each\" : [ \"Arya\"]} } } } } }")));
assertThat(new Update().push("key").atPosition(Position.FIRST).value("Arya").toString()).isEqualTo(
"{ \"$push\" : { \"key\" : { \"$java\" : { \"$position\" : { \"$java\" : { \"$position\" : 0} }, \"$each\" : { \"$java\" : { \"$each\" : [ \"Arya\"]} } } } } }");
}
@Test // DATAMONGO-1777
public void toStringConsidersIsolated() {
assertThat(new Update().set("key", "value").isolated().toString(),
is(equalTo("{ \"$set\" : { \"key\" : \"value\" }, \"$isolated\" : 1 }")));
assertThat(new Update().set("key", "value").isolated().toString())
.isEqualTo("{ \"$set\" : { \"key\" : \"value\" }, \"$isolated\" : 1 }");
}
@Test // DATAMONGO-1778
@@ -493,8 +493,8 @@ public class UpdateTests {
Update update2 = new Update().inc("version", 1).push("someField").slice(-10).each("test");
Update update3 = new Update().inc("version", 1).push("someField").slice(10).each("test");
assertThat(update1, is(equalTo(update2)));
assertThat(update1, is(not(equalTo(update3))));
assertThat(update1).isEqualTo(update2);
assertThat(update1).isNotEqualTo(update3);
}
@Test // DATAMONGO-1778
@@ -503,7 +503,7 @@ public class UpdateTests {
Update update1 = new Update().inc("version", 1).isolated();
Update update2 = new Update().inc("version", 1).isolated();
assertThat(update1, is(equalTo(update2)));
assertThat(update1).isEqualTo(update2);
}
@Test // DATAMONGO-1778
@@ -513,16 +513,18 @@ public class UpdateTests {
Update update2 = new Update().inc("version", 1).push("someField").slice(-10).each("test");
Update update3 = new Update().inc("version", 1).push("someField").slice(10).each("test");
assertThat(update1.hashCode(), is(equalTo(update2.hashCode())));
assertThat(update1.hashCode(), is(not(equalTo(update3.hashCode()))));
assertThat(update1.hashCode()).isEqualTo(update2.hashCode());
assertThat(update1.hashCode()).isNotEqualTo(update3.hashCode());
}
@Test // DATAMONGO-1778
public void hashCodeShouldConsiderIsolated() {
Update update1 = new Update().inc("version", 1).isolated();
Update update2 = new Update().inc("version", 1);
Update update2 = new Update().inc("version", 1).isolated();
Update update3 = new Update().inc("version", 1);
assertThat(update1.hashCode(), is(not(equalTo(update2.hashCode()))));
assertThat(update1.hashCode()).isEqualTo(update2.hashCode());
assertThat(update1.hashCode()).isNotEqualTo(update3.hashCode());
}
}