1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
// Copyright Yahoo. 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;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Einar M R Rosenvinge
*/
public class StructDataTypeTestCase {
@Test
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());
}
@Test
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);
}
}
|