renamed UpdateBuilder to UpdateSpec and moved to builder package
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.document.mongodb;
|
||||
package org.springframework.data.document.mongodb.builder;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -22,7 +22,7 @@ import java.util.LinkedHashMap;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
public class UpdateBuilder {
|
||||
public class UpdateSpec {
|
||||
|
||||
public enum Position {
|
||||
LAST, FIRST
|
||||
@@ -30,56 +30,56 @@ public class UpdateBuilder {
|
||||
|
||||
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
|
||||
|
||||
public UpdateBuilder set(String key, Object value) {
|
||||
public UpdateSpec set(String key, Object value) {
|
||||
criteria.put("$set", Collections.singletonMap(key, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder unset(String key) {
|
||||
public UpdateSpec unset(String key) {
|
||||
criteria.put("$unset", Collections.singletonMap(key, 1));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder inc(String key, long inc) {
|
||||
public UpdateSpec inc(String key, long inc) {
|
||||
criteria.put("$inc", Collections.singletonMap(key, inc));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder push(String key, Object value) {
|
||||
public UpdateSpec push(String key, Object value) {
|
||||
criteria.put("$push", Collections.singletonMap(key, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder pushAll(String key, Object[] values) {
|
||||
public UpdateSpec pushAll(String key, Object[] values) {
|
||||
DBObject keyValue = new BasicDBObject();
|
||||
keyValue.put(key, values);
|
||||
criteria.put("$pushAll", keyValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder addToSet(String key, Object value) {
|
||||
public UpdateSpec addToSet(String key, Object value) {
|
||||
criteria.put("$addToSet", Collections.singletonMap(key, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder pop(String key, Position pos) {
|
||||
public UpdateSpec pop(String key, Position pos) {
|
||||
criteria.put("$pop", Collections.singletonMap(key, (pos == Position.FIRST ? -1 : 1)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder pull(String key, Object value) {
|
||||
public UpdateSpec pull(String key, Object value) {
|
||||
criteria.put("$pull", Collections.singletonMap(key, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder pullAll(String key, Object[] values) {
|
||||
public UpdateSpec pullAll(String key, Object[] values) {
|
||||
DBObject keyValue = new BasicDBObject();
|
||||
keyValue.put(key, values);
|
||||
criteria.put("$pullAll", keyValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder rename(String oldName, String newName) {
|
||||
public UpdateSpec rename(String oldName, String newName) {
|
||||
criteria.put("$rename", Collections.singletonMap(oldName, newName));
|
||||
return this;
|
||||
}
|
||||
@@ -13,33 +13,34 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.document.mongodb;
|
||||
package org.springframework.data.document.mongodb.builder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.document.mongodb.builder.UpdateSpec;
|
||||
|
||||
public class UpdateBuilderTests {
|
||||
public class UpdateTests {
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.set("directory", "/Users/Test/Desktop");
|
||||
Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInc() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.inc("size", 1);
|
||||
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncAndSet() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.inc("size", 1)
|
||||
.set("directory", "/Users/Test/Desktop");
|
||||
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}",
|
||||
@@ -48,7 +49,7 @@ public class UpdateBuilderTests {
|
||||
|
||||
@Test
|
||||
public void testUnset() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.unset("directory");
|
||||
Assert.assertEquals("{ \"$unset\" : { \"directory\" : 1}}", ub.build().toString());
|
||||
}
|
||||
@@ -57,7 +58,7 @@ public class UpdateBuilderTests {
|
||||
public void testPush() {
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("name", "Sven");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.push("authors", m);
|
||||
Assert.assertEquals("{ \"$push\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString());
|
||||
}
|
||||
@@ -68,7 +69,7 @@ public class UpdateBuilderTests {
|
||||
m1.put("name", "Sven");
|
||||
Map<String, Object> m2 = new HashMap<String, Object>();
|
||||
m2.put("name", "Maria");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.pushAll("authors", new Object[] {m1, m2});
|
||||
Assert.assertEquals("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", ub.build().toString());
|
||||
}
|
||||
@@ -77,18 +78,18 @@ public class UpdateBuilderTests {
|
||||
public void testAddToSet() {
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("name", "Sven");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.addToSet("authors", m);
|
||||
Assert.assertEquals("{ \"$addToSet\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPop() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.pop("authors", UpdateBuilder.Position.FIRST);
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.pop("authors", UpdateSpec.Position.FIRST);
|
||||
Assert.assertEquals("{ \"$pop\" : { \"authors\" : -1}}", ub.build().toString());
|
||||
ub = new UpdateBuilder()
|
||||
.pop("authors", UpdateBuilder.Position.LAST);
|
||||
ub = new UpdateSpec()
|
||||
.pop("authors", UpdateSpec.Position.LAST);
|
||||
Assert.assertEquals("{ \"$pop\" : { \"authors\" : 1}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@@ -96,7 +97,7 @@ public class UpdateBuilderTests {
|
||||
public void testPull() {
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("name", "Sven");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.pull("authors", m);
|
||||
Assert.assertEquals("{ \"$pull\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString());
|
||||
}
|
||||
@@ -107,14 +108,14 @@ public class UpdateBuilderTests {
|
||||
m1.put("name", "Sven");
|
||||
Map<String, Object> m2 = new HashMap<String, Object>();
|
||||
m2.put("name", "Maria");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.pullAll("authors", new Object[] {m1, m2});
|
||||
Assert.assertEquals("{ \"$pullAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRename() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
UpdateSpec ub = new UpdateSpec()
|
||||
.rename("directory", "folder");
|
||||
Assert.assertEquals("{ \"$rename\" : { \"directory\" : \"folder\"}}", ub.build().toString());
|
||||
}
|
||||
Reference in New Issue
Block a user