aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/StructDataTypeTestCase.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /document/src/test/java/com/yahoo/document/StructDataTypeTestCase.java
Publish
Diffstat (limited to 'document/src/test/java/com/yahoo/document/StructDataTypeTestCase.java')
-rwxr-xr-xdocument/src/test/java/com/yahoo/document/StructDataTypeTestCase.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/document/src/test/java/com/yahoo/document/StructDataTypeTestCase.java b/document/src/test/java/com/yahoo/document/StructDataTypeTestCase.java
new file mode 100755
index 00000000000..6e5bfbb56d4
--- /dev/null
+++ b/document/src/test/java/com/yahoo/document/StructDataTypeTestCase.java
@@ -0,0 +1,68 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.document;
+
+import com.yahoo.document.datatypes.Struct;
+
+/**
+ * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
+ */
+public class StructDataTypeTestCase extends junit.framework.TestCase {
+ public void testSimpleInheritance() {
+ StructDataType personType = new StructDataType("person");
+ Field firstName = new Field("firstname", DataType.STRING);
+ Field lastName = new Field("lastname", DataType.STRING);
+ personType.addField(firstName);
+ personType.addField(lastName);
+
+ StructDataType employeeType = new StructDataType("employee");
+ Field empId = new Field("employeeid", DataType.INT);
+ employeeType.addField(empId);
+
+ assertEquals(2, personType.getFieldCount());
+ assertEquals("firstname", personType.getFields().toArray(new Field[0])[0].getName());
+ assertEquals("lastname", personType.getFields().toArray(new Field[0])[1].getName());
+ assertEquals(1, employeeType.getFieldCount());
+ assertEquals("employeeid", employeeType.getFields().toArray(new Field[0])[0].getName());
+
+ employeeType.inherit(personType);
+
+ assertEquals(2, personType.getFieldCount());
+ assertEquals("firstname", personType.getFields().toArray(new Field[0])[0].getName());
+ assertEquals("lastname", personType.getFields().toArray(new Field[0])[1].getName());
+ assertEquals(3, employeeType.getFieldCount());
+ assertEquals("employeeid", employeeType.getFields().toArray(new Field[0])[0].getName());
+ assertEquals("firstname", employeeType.getFields().toArray(new Field[0])[1].getName());
+ assertEquals("lastname", employeeType.getFields().toArray(new Field[0])[2].getName());
+ }
+
+ public void testCompatibleWith() {
+ StructDataType personType = new StructDataType("person");
+ Field firstName = new Field("firstname", DataType.STRING);
+ Field lastName = new Field("lastname", DataType.STRING);
+ personType.addField(firstName);
+ personType.addField(lastName);
+
+ StructDataType employeeType = new StructDataType("employee");
+ Field empId = new Field("employeeid", DataType.INT);
+ employeeType.addField(empId);
+ employeeType.inherit(personType);
+
+ Struct person = new Struct(personType);
+ Struct employee = new Struct(employeeType);
+
+ assertTrue(personType.isValueCompatible(person));
+ assertTrue(personType.isValueCompatible(employee));
+
+ assertTrue(employeeType.isValueCompatible(employee));
+ assertFalse(employeeType.isValueCompatible(person));
+
+ StructDataType containerType = new StructDataType("containerstruct");
+ Field structPolymorphic = new Field("structpolymorphic", personType);
+ containerType.addField(structPolymorphic);
+
+ Struct container = new Struct(containerType);
+ container.setFieldValue(structPolymorphic, person);
+ container.setFieldValue(structPolymorphic, employee);
+ }
+
+}