diff options
author | Tor Brede Vekterli <vekterli@yahoo-inc.com> | 2017-02-06 15:10:44 +0100 |
---|---|---|
committer | Tor Brede Vekterli <vekterli@yahoo-inc.com> | 2017-02-06 15:10:44 +0100 |
commit | efd5c47293e19639efa3a3b21b8990e714c1eec3 (patch) | |
tree | 9f3b2cef86e43761c62b5f68033ad64515701a0c /document | |
parent | acde58693f8731405c551e6edfc95e546c7a14b0 (diff) |
Allow assigning ReferenceFieldValue instances
Diffstat (limited to 'document')
-rw-r--r-- | document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java | 11 | ||||
-rw-r--r-- | document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java | 32 |
2 files changed, 43 insertions, 0 deletions
diff --git a/document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java index 3adce1fd1ed..fc7981905aa 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/ReferenceFieldValue.java @@ -97,6 +97,8 @@ public class ReferenceFieldValue extends FieldValue { public void assign(Object o) { if (o == null) { clear(); + } else if (o instanceof ReferenceFieldValue) { + assignFromFieldValue((ReferenceFieldValue) o); } else if (o instanceof DocumentId) { setDocumentId((DocumentId) o); } else { @@ -106,6 +108,15 @@ public class ReferenceFieldValue extends FieldValue { } } + private void assignFromFieldValue(ReferenceFieldValue rhs) { + if (!getDataType().equals(rhs.getDataType())) { + throw new IllegalArgumentException(String.format( + "Can't assign reference of type %s to reference of type %s", + rhs.getDataType().getName(), getDataType().getName())); + } + rhs.getDocumentId().ifPresent(this::setDocumentId); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java b/document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java index 7c30ff45fa5..456ab7adc41 100644 --- a/document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java +++ b/document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java @@ -6,7 +6,9 @@ import com.yahoo.document.DocumentId; import com.yahoo.document.DocumentType; import com.yahoo.document.Field; import com.yahoo.document.ReferenceDataType; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -19,6 +21,9 @@ import static org.junit.Assert.assertTrue; */ public class ReferenceFieldValueTestCase { + @Rule + public final ExpectedException exceptionRule = ExpectedException.none(); + private static DocumentType createDocumentType(String name) { DocumentType type = new DocumentType(name); type.addField(new Field("foo", DataType.STRING)); @@ -100,6 +105,33 @@ public class ReferenceFieldValueTestCase { } @Test + public void can_assign_empty_reference_field_value_instance__to_existing_reference() { + ReferenceFieldValue existing = new ReferenceFieldValue(referenceTypeFoo()); + ReferenceFieldValue newValue = new ReferenceFieldValue(referenceTypeFoo()); + // Logically a no-op, but still worth testing. + existing.assign(newValue); + assertEquals(newValue, existing); + } + + @Test + public void can_assign_reference_field_value_instance__with_id_to_existing_reference() { + ReferenceFieldValue existing = new ReferenceFieldValue(referenceTypeFoo()); + ReferenceFieldValue newValue = new ReferenceFieldValue(referenceTypeFoo(), docId("id:ns:foo::toad")); + existing.assign(newValue); + assertEquals(newValue, existing); + } + + @Test + public void assigning_reference_field_with_different_type_to_existing_reference_throws_exception() { + ReferenceFieldValue existing = new ReferenceFieldValue(referenceTypeFoo()); + ReferenceFieldValue newValue = new ReferenceFieldValue(referenceTypeBar()); + exceptionRule.expect(IllegalArgumentException.class); + exceptionRule.expectMessage("Can't assign reference of type Reference<bar> " + + "to reference of type Reference<foo>"); + existing.assign(newValue); + } + + @Test public void reference_value_can_be_cleared() { ReferenceFieldValue value = new ReferenceFieldValue(referenceTypeFoo(), docId("id:ns:foo::yoshi-egg-feast")); value.clear(); |