/driver-core/src/test/unit/com/mongodb/internal/client/model/AbstractConstructibleBsonElementTest.java

https://github.com/jyemin/mongo-java-driver · Java · 125 lines · 97 code · 13 blank · 15 comment · 0 complexity · 180505f266ed1f9dbf160e595caac447 MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.internal.client.model;
  17. import org.bson.BsonDocument;
  18. import org.bson.BsonString;
  19. import org.bson.Document;
  20. import org.bson.conversions.Bson;
  21. import org.junit.jupiter.api.Test;
  22. import static org.junit.jupiter.api.Assertions.assertEquals;
  23. import static org.junit.jupiter.api.Assertions.assertSame;
  24. final class AbstractConstructibleBsonElementTest {
  25. @Test
  26. void of() {
  27. BsonDocument value = new BsonDocument("n", new BsonString("v"));
  28. AbstractConstructibleBsonElement<?> constructible = AbstractConstructibleBsonElement.of(
  29. new BsonDocument("name", value));
  30. assertUnmodifiable(constructible);
  31. assertEquals(new BsonDocument("name", value), constructible.toBsonDocument());
  32. }
  33. @Test
  34. void ofPreventsDoubleWrapping() {
  35. BsonDocument value = new BsonDocument("n", new BsonString("v"));
  36. AbstractConstructibleBsonElement<?> constructible = AbstractConstructibleBsonElement.of(
  37. new BsonDocument("name", value));
  38. assertUnmodifiable(constructible);
  39. AbstractConstructibleBsonElement<?> constructible2 = AbstractConstructibleBsonElement.of(constructible);
  40. assertUnmodifiable(constructible2);
  41. assertSame(constructible, constructible2);
  42. }
  43. @Test
  44. void nameConstructor() {
  45. final class Constructible extends AbstractConstructibleBsonElement<Constructible> {
  46. private Constructible(final String name) {
  47. super(name);
  48. }
  49. private Constructible(final Bson baseElement, final Bson appendedElementValue) {
  50. super(baseElement, appendedElementValue);
  51. }
  52. @Override
  53. protected Constructible newSelf(final Bson baseElement, final Bson appendedElementValue) {
  54. return new Constructible(baseElement, appendedElementValue);
  55. }
  56. }
  57. AbstractConstructibleBsonElement<?> constructible = new Constructible("name");
  58. assertUnmodifiable(constructible);
  59. assertEquals(new BsonDocument("name", new BsonDocument()), constructible.toBsonDocument());
  60. }
  61. @Test
  62. void nameValueConstructor() {
  63. final class Constructible extends AbstractConstructibleBsonElement<Constructible> {
  64. private Constructible(final String name, final Bson value) {
  65. super(name, value);
  66. }
  67. private Constructible(final Bson baseElement, final Bson appendedElementValue) {
  68. super(baseElement, appendedElementValue);
  69. }
  70. @Override
  71. protected Constructible newSelf(final Bson baseElement, final Bson appendedElementValue) {
  72. return new Constructible(baseElement, appendedElementValue);
  73. }
  74. }
  75. BsonDocument value = new BsonDocument("n", new BsonString("v"));
  76. AbstractConstructibleBsonElement<?> constructible = new Constructible("name", value);
  77. assertUnmodifiable(constructible);
  78. assertEquals(new BsonDocument("name", value), constructible.toBsonDocument());
  79. }
  80. @Test
  81. void newWithAppendedValue() {
  82. AbstractConstructibleBsonElement<?> constructible = AbstractConstructibleBsonElement.of(
  83. new BsonDocument("name", new BsonDocument("n", new BsonString("v"))));
  84. assertUnmodifiable(constructible);
  85. AbstractConstructibleBsonElement<?> appendedConstructible = constructible.newWithAppendedValue("n2", "v2");
  86. assertUnmodifiable(appendedConstructible);
  87. assertEquals(
  88. new BsonDocument("name", new BsonDocument("n", new BsonString("v")).append("n2", new BsonString("v2"))),
  89. appendedConstructible.toBsonDocument());
  90. }
  91. @Test
  92. void tostring() {
  93. assertEquals(
  94. new Document("name",
  95. new Document("double", 0.5)
  96. .append("doc", new Document("i", 42))
  97. .append("constructible", new Document("s", "")))
  98. .toString(),
  99. AbstractConstructibleBsonElement.of(new Document("name",
  100. AbstractConstructibleBson.of(new Document("double", 0.5))
  101. .newAppended("doc", new Document("i", 42))))
  102. .newWithAppendedValue("constructible", AbstractConstructibleBson.of(AbstractConstructibleBson.of(new Document("s", ""))))
  103. .toString());
  104. }
  105. private static void assertUnmodifiable(final AbstractConstructibleBsonElement<?> constructible) {
  106. String expected = constructible.toBsonDocument().toJson();
  107. constructible.newWithAppendedValue("assertUnmodifiableN", "assertUnmodifiableV");
  108. assertEquals(expected, constructible.toBsonDocument().toJson());
  109. constructible.newWithMutatedValue(doc -> doc.append("assertUnmodifiableN", "assertUnmodifiableV"));
  110. assertEquals(expected, constructible.toBsonDocument().toJson());
  111. }
  112. }