aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java
blob: 3e5c2633ae723367ff3a2a64aefa1479090f8a2d (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;

import com.yahoo.document.annotation.*;
import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.FloatFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.StructuredFieldValue;
import org.junit.Test;

import java.util.HashSet;
import java.util.Iterator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

/**
 * @author Thomas Gundersen
 */
public class DocumentTypeManagerTestCase {

    // Verify that we can register and retrieve fields.
    @Test
    public void testRegisterAndGet() {
        DocumentTypeManager manager = new DocumentTypeManager();

        DocumentType newDocType = new DocumentType("testdoc");
        newDocType.addField("Fjomp", DataType.INT);
        newDocType.addField("Fjols", DataType.STRING);

        manager.registerDocumentType(newDocType);

        DocumentType fetchedDocType = manager.getDocumentType(new DataTypeName("testdoc"));

        Field fetched4 = fetchedDocType.getField("Fjomp");

        assertEquals("Fjomp", fetched4.getName());
        assertEquals(fetched4.getDataType(), DataType.INT);
    }

    @Test
    public void testBasicTypes() {
        DocumentTypeManager dtm = new DocumentTypeManager();
        DataType intType = dtm.getDataTypeInternal("int");
        assertSame(DataType.INT, intType);

        DataType stringType = dtm.getDataTypeInternal("string");
        assertSame(DataType.STRING, stringType);

        DataType longType = dtm.getDataTypeInternal("long");
        assertSame(DataType.LONG, longType);

        DataType doubleType = dtm.getDataTypeInternal("double");
        assertSame(DataType.DOUBLE, doubleType);
    }

    @Test
    public void testRecursiveRegister() {
        StructDataType struct = new StructDataType("mystruct");
        DataType wset1 = DataType.getWeightedSet(DataType.getArray(DataType.INT));
        DataType wset2 = DataType.getWeightedSet(DataType.getArray(DataType.TAG));
        struct.addField(new Field("foo", wset1));
        struct.addField(new Field("bar", wset2));
        DataType array = DataType.getArray(struct);
        DocumentType docType = new DocumentType("mydoc");
        docType.addField("hmm", array);
        DocumentType docType2 = new DocumentType("myotherdoc");
        docType2.addField("myint", DataType.INT);
        docType2.inherit(docType);

        DocumentTypeManager manager = new DocumentTypeManager();
        manager.register(docType2);

        assertEquals(struct, manager.getDataTypeByCode(struct.getId()));
        assertEquals(struct, manager.getDataTypeInternal("mystruct"));
        assertEquals(wset1, manager.getDataTypeByCode(wset1.getId()));
        assertEquals(wset2, manager.getDataTypeByCode(wset2.getId()));
        assertEquals(array, manager.getDataTypeByCode(array.getId()));
        assertEquals(docType, manager.getDataTypeInternal("mydoc"));
        assertEquals(docType2, manager.getDataTypeInternal("myotherdoc"));
        assertEquals(docType, manager.getDocumentType(new DataTypeName("mydoc")));
        assertEquals(docType2, manager.getDocumentType(new DataTypeName("myotherdoc")));
    }

    @Test
    public void testMultipleDocuments() {
        DocumentType docType1 = new DocumentType("foo0");
        docType1.addField("bar", DataType.INT);

        DocumentType docType2 = new DocumentType("foo1");
        docType2.addField("bar", DataType.STRING);

        DocumentType docType3 = new DocumentType("foo2");
        docType3.addField("bar", DataType.FLOAT);

        DocumentType docType4 = new DocumentType("foo3");
        docType4.addField("bar", DataType.RAW);

        DocumentTypeManager manager = new DocumentTypeManager();
        manager.internalClear();
        manager.register(docType1);
        manager.register(docType2);
        manager.register(docType3);
        manager.register(docType4);

        assertSame(docType1, manager.getDocumentType(new DataTypeName("foo0")));
        assertSame(docType2, manager.getDocumentType(new DataTypeName("foo1")));
        assertSame(docType3, manager.getDocumentType(new DataTypeName("foo2")));
        assertSame(docType4, manager.getDocumentType(new DataTypeName("foo3")));

        assertEquals(manager.getDocumentTypes().size(), 5);
        assertNotNull(manager.getDocumentTypes().get(new DataTypeName("document")));
        assertEquals(manager.getDocumentTypes().get(new DataTypeName("foo0")), docType1);
        assertEquals(manager.getDocumentTypes().get(new DataTypeName("foo0")), new DocumentType("foo0"));
        assertEquals(manager.getDocumentTypes().get(new DataTypeName("foo1")), docType2);
    }

    @Test
    public void testReverseMapOrder() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.map.cfg");

        assertNotNull(manager.getDataTypeByCode(1000));
        assertNotNull(manager.getDataTypeByCode(1001));
    }

    @Test
    public void testConfigure() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.cfg");

        Iterator typeIt = manager.documentTypeIterator();
        DocumentType type = null;

        while (typeIt.hasNext()) {
            type = (DocumentType) typeIt.next();
            if (type.getName().equals("foobar")) {
                break;
            }
        }
        assertNotNull(type);

        assertTrue(type.hasField("foobarfield0"));
        assertTrue(type.hasField("foobarfield1"));

        Field foobarfield0 = type.getField("foobarfield0");
        assertTrue(foobarfield0.getDataType().getCode() == 2);

        Field foobarfield1 = type.getField("foobarfield1");
        assertTrue(foobarfield1.getDataType().getCode() == 4);


        typeIt = manager.documentTypeIterator();
        while (typeIt.hasNext()) {
            type = (DocumentType) typeIt.next();
            if (type.getName().equals("banana")) {
                break;
            }
        }

        assertTrue(type.hasField("bananafield0"));

        Field bananafield0 = type.getField("bananafield0");
        assertTrue(bananafield0.getDataType().getCode() == 16);

        //inheritance:
        Iterator inhIt = type.getInheritedTypes().iterator();

        DocumentType inhType = (DocumentType) inhIt.next();
        assertTrue(inhType.getName().equals("foobar"));

        assertFalse(inhIt.hasNext());

        typeIt = manager.documentTypeIterator();
        while (typeIt.hasNext()) {
            type = (DocumentType) typeIt.next();
            if (type.getName().equals("customtypes")) {
                break;
            }
        }

        assertTrue(type.hasField("arrayfloat"));
        assertTrue(type.hasField("arrayarrayfloat"));

        Field arrayfloat = type.getField("arrayfloat");
        ArrayDataType dataType = (ArrayDataType) arrayfloat.getDataType();
        // assertTrue(dataType.getCode() == 99);
        assertTrue(dataType.getValueClass().equals(Array.class));
        assertTrue(dataType.getNestedType().getCode() == 1);
        assertTrue(dataType.getNestedType().getValueClass().equals(FloatFieldValue.class));


        Field arrayarrayfloat = type.getField("arrayarrayfloat");
        ArrayDataType subType = (ArrayDataType) arrayarrayfloat.getDataType();
        // assertTrue(subType.getCode() == 4003);
        assertTrue(subType.getValueClass().equals(Array.class));
        // assertTrue(subType.getNestedType().getCode() == 99);
        assertTrue(subType.getNestedType().getValueClass().equals(Array.class));
        ArrayDataType subSubType = (ArrayDataType) subType.getNestedType();
        assertTrue(subSubType.getNestedType().getCode() == 1);
        assertTrue(subSubType.getNestedType().getValueClass().equals(FloatFieldValue.class));
    }

    @Test
    public void testConfigureUpdate() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.cfg");

        DocumentType banana = manager.getDocumentType(new DataTypeName("banana"));
        DocumentType customtypes = manager.getDocumentType(new DataTypeName("customtypes"));

        assertNull(banana.getField("newfield"));
        assertEquals(new Field("arrayfloat", 9489, new ArrayDataType(DataType.FLOAT)), customtypes.getField("arrayfloat"));

        var sub = DocumentTypeManagerConfigurer.configure(manager, "file:src/test/document/documentmanager.updated.cfg");
        sub.close();
        banana = manager.getDocumentType(new DataTypeName("banana"));
        customtypes = manager.getDocumentType(new DataTypeName("customtypes"));

        assertEquals(new Field("newfield", 12345, DataType.STRING), banana.getField("newfield"));
        assertNull(customtypes.getField("arrayfloat"));
    }

    @Test
    public void testConfigureWithAnnotations() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.annotationtypes1.cfg");

        /*
  annotation banana {
    field brand type string { }
  }

  annotation food {
    field what type annotationreference<banana> { }
  }

  annotation cyclic {
    field blah type annotationreference<cyclic> { }
  }

  annotation a {
    field foo type annotationreference<b> { }
  }

  annotation b {
  }
         */

        AnnotationTypeRegistry atr = manager.getAnnotationTypeRegistry();
        assertEquals(AnnotationTypes.ALL_TYPES.size() + 5, atr.getTypes().size());

        AnnotationType banana = atr.getType("banana");
        assertTrue(banana.getDataType() instanceof StructDataType);
        assertEquals("annotation.banana", banana.getDataType().getName());
        assertEquals(1, ((StructDataType) banana.getDataType()).getFields().size());
        assertEquals(DataType.STRING, ((StructDataType) banana.getDataType()).getField("brand").getDataType());

        AnnotationType food = atr.getType("food");
        assertTrue(food.getDataType() instanceof StructDataType);
        assertEquals("annotation.food", food.getDataType().getName());
        assertEquals(1, ((StructDataType) food.getDataType()).getFields().size());
        AnnotationReferenceDataType what = (AnnotationReferenceDataType) ((StructDataType) food.getDataType()).getField("what").getDataType();
        assertSame(banana, what.getAnnotationType());

        AnnotationType cyclic = atr.getType("cyclic");
        assertTrue(cyclic.getDataType() instanceof StructDataType);
        assertEquals("annotation.cyclic", cyclic.getDataType().getName());
        assertEquals(1, ((StructDataType) cyclic.getDataType()).getFields().size());
        AnnotationReferenceDataType blah = (AnnotationReferenceDataType) ((StructDataType) cyclic.getDataType()).getField("blah").getDataType();
        assertSame(cyclic, blah.getAnnotationType());

        AnnotationType b = atr.getType("b");
        assertNull(b.getDataType());

        AnnotationType a = atr.getType("a");
        assertTrue(a.getDataType() instanceof StructDataType);
        assertEquals("annotation.a", a.getDataType().getName());
        assertEquals(1, ((StructDataType) a.getDataType()).getFields().size());
        AnnotationReferenceDataType foo = (AnnotationReferenceDataType) ((StructDataType) a.getDataType()).getField("foo").getDataType();
        assertSame(b, foo.getAnnotationType());

    }

    @Test
    public void testConfigureWithAnnotationsWithInheritance() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.annotationtypes2.cfg");

        /*
  annotation fruit {
  }

  annotation banana inherits fruit {
    field brand type string { }
  }

  annotation vehicle {
    field numwheels type int { }
  }

  annotation car inherits vehicle {
    field color type string { }
  }

  annotation intern inherits employee {
    field enddate type long { }
  }

  annotation employee inherits worker {
    field employeeid type int { }
  }

  annotation worker inherits person {
  }

  annotation person {
    field name type string { }
  }
         */

        AnnotationTypeRegistry atr = manager.getAnnotationTypeRegistry();
        assertEquals(AnnotationTypes.ALL_TYPES.size() + 8, atr.getTypes().size());

        AnnotationType fruit = atr.getType("fruit");
        assertNull(fruit.getDataType());

        AnnotationType banana = atr.getType("banana");
        assertTrue(banana.getDataType() instanceof StructDataType);
        assertEquals("annotation.banana", banana.getDataType().getName());
        assertEquals(1, ((StructDataType) banana.getDataType()).getFields().size());
        assertEquals(DataType.STRING, ((StructDataType) banana.getDataType()).getField("brand").getDataType());
        assertEquals(0, ((StructDataType) banana.getDataType()).getInheritedTypes().size());

        AnnotationType vehicle = atr.getType("vehicle");
        assertTrue(vehicle.getDataType() instanceof StructDataType);
        assertEquals("annotation.vehicle", vehicle.getDataType().getName());
        assertEquals(1, ((StructDataType) vehicle.getDataType()).getFields().size());
        assertEquals(DataType.INT, ((StructDataType) vehicle.getDataType()).getField("numwheels").getDataType());
        assertEquals(0, ((StructDataType) vehicle.getDataType()).getInheritedTypes().size());

        AnnotationType car = atr.getType("car");
        assertTrue(car.getDataType() instanceof StructDataType);
        assertEquals("annotation.car", car.getDataType().getName());
        assertEquals(2, ((StructDataType) car.getDataType()).getFields().size());
        assertEquals(DataType.INT, ((StructDataType) car.getDataType()).getField("numwheels").getDataType());
        assertEquals(DataType.STRING, ((StructDataType) car.getDataType()).getField("color").getDataType());
        assertEquals(1, ((StructDataType) car.getDataType()).getInheritedTypes().size());
        assertSame(vehicle.getDataType(), ((StructDataType) car.getDataType()).getInheritedTypes().toArray()[0]);

        AnnotationType person = atr.getType("person");
        assertTrue(person.getDataType() instanceof StructDataType);
        assertEquals("annotation.person", person.getDataType().getName());
        assertEquals(1, ((StructDataType) person.getDataType()).getFields().size());
        assertEquals(DataType.STRING, ((StructDataType) person.getDataType()).getField("name").getDataType());
        assertEquals(0, ((StructDataType) person.getDataType()).getInheritedTypes().size());

        AnnotationType worker = atr.getType("worker");
        assertTrue(worker.getDataType() instanceof StructDataType);
        assertEquals("annotation.worker", worker.getDataType().getName());
        assertEquals(1, ((StructDataType) worker.getDataType()).getFields().size());
        assertEquals(DataType.STRING, ((StructDataType) worker.getDataType()).getField("name").getDataType());
        assertEquals(1, ((StructDataType) worker.getDataType()).getInheritedTypes().size());
        assertSame(person.getDataType(), ((StructDataType) worker.getDataType()).getInheritedTypes().toArray()[0]);

        AnnotationType employee = atr.getType("employee");
        assertTrue(employee.getDataType() instanceof StructDataType);
        assertEquals("annotation.employee", employee.getDataType().getName());
        assertEquals(2, ((StructDataType) employee.getDataType()).getFields().size());
        assertEquals(DataType.STRING, ((StructDataType) employee.getDataType()).getField("name").getDataType());
        assertEquals(DataType.INT, ((StructDataType) employee.getDataType()).getField("employeeid").getDataType());
        assertEquals(1, ((StructDataType) employee.getDataType()).getInheritedTypes().size());
        assertSame(worker.getDataType(), ((StructDataType) employee.getDataType()).getInheritedTypes().toArray()[0]);

        AnnotationType intern = atr.getType("intern");
        assertTrue(intern.getDataType() instanceof StructDataType);
        assertEquals("annotation.intern", intern.getDataType().getName());
        assertEquals(3, ((StructDataType) intern.getDataType()).getFields().size());
        assertEquals(DataType.STRING, ((StructDataType) intern.getDataType()).getField("name").getDataType());
        assertEquals(DataType.INT, ((StructDataType) intern.getDataType()).getField("employeeid").getDataType());
        assertEquals(DataType.LONG, ((StructDataType) intern.getDataType()).getField("enddate").getDataType());
        assertEquals(1, ((StructDataType) intern.getDataType()).getInheritedTypes().size());
        assertSame(employee.getDataType(), ((StructDataType) intern.getDataType()).getInheritedTypes().toArray()[0]);

    }

    @Test
    public void testStructsAnyOrder() {
        /*
search annotationsimplicitstruct {

  document annotationsimplicitstruct {
      field structfield type sct {
      }

      field structarrayfield type array<sct> {
      }
  }

  struct sct {
    field s1 type string {}
    field s2 type string {}
    field s3 type sct {}
    field s4 type foo {}
  }

  struct foo {
    field s1 type int {}
  }
}
         */

        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.structsanyorder.cfg");

        StructDataType foo = (StructDataType) manager.getDataTypeInternal("foo");
        assertNotNull(foo);
        assertEquals(1, foo.getFields().size());
        Field foos1 = foo.getField("s1");
        assertSame(DataType.INT, foos1.getDataType());

        StructDataType sct = (StructDataType) manager.getDataTypeInternal("sct");
        assertNotNull(sct);
        assertEquals(4, sct.getFields().size());
        Field s1 = sct.getField("s1");
        assertSame(DataType.STRING, s1.getDataType());
        Field s2 = sct.getField("s2");
        assertSame(DataType.STRING, s2.getDataType());
        Field s3 = sct.getField("s3");
        assertSame(sct, s3.getDataType());
        Field s4 = sct.getField("s4");
        assertSame(foo, s4.getDataType());
    }

    @Test
    public void testSombrero1() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.sombrero1.cfg");

        {
            StringFieldValue sfv = new StringFieldValue("ballooooo");
            String text = sfv.getString();
            AnnotationType type = manager.getAnnotationTypeRegistry().getType("base");
            StructuredFieldValue value = (StructuredFieldValue) type.getDataType().createFieldValue();
            value.setFieldValue("x", 10);
            SpanNode span = new Span(0, text.length());
            SpanTree tree = new SpanTree("span", span);
            tree.annotate(span, new Annotation(type, value));
            sfv.setSpanTree(tree);
        }

        {
            StringFieldValue sfv = new StringFieldValue("ballooooo");
            String text = sfv.getString();
            AnnotationType type = manager.getAnnotationTypeRegistry().getType("derived");
            StructuredFieldValue value = (StructuredFieldValue) type.getDataType().createFieldValue();
            value.setFieldValue("x", 10);
            SpanNode span = new Span(0, text.length());
            SpanTree tree = new SpanTree("span", span);
            tree.annotate(span, new Annotation(type, value));
            sfv.setSpanTree(tree);
        }
    }

    @Test
    public void testPolymorphy() {
        /*
  annotation super {
  }
  annotation sub inherits super {
  }
  annotation blah {
    field a type annotationreference<super> {}
  }
         */


        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.annotationspolymorphy.cfg");

        AnnotationType suuper = manager.getAnnotationTypeRegistry().getType("super");
        AnnotationType sub = manager.getAnnotationTypeRegistry().getType("sub");


        //reference type for super annotation type
        AnnotationReferenceDataType refType = (AnnotationReferenceDataType) ((StructDataType) manager.getAnnotationTypeRegistry().getType("blah").getDataType()).getField("a").getDataType();

        Annotation superAnnotation = new Annotation(suuper);
        Annotation subAnnotation = new Annotation(sub);

        new AnnotationReference(refType, superAnnotation);
        //this would fail without polymorphy support:
        new AnnotationReference(refType, subAnnotation);

    }

    @Test
    public void single_reference_type_is_mapped_to_correct_document_target_type() {
        final DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.singlereference.cfg");

        assertReferenceTypePresentInManager(manager, 12345678, "referenced_type");
    }

    private static void assertReferenceTypePresentInManager(DocumentTypeManager manager, int refTypeId,
                                                            String refTargetTypeName) {
        DataType type = manager.getDataTypeByCode(refTypeId);
        assertTrue(type instanceof ReferenceDataType);
        ReferenceDataType refType = (ReferenceDataType) type;

        DocumentType targetDocType = manager.getDocumentType(refTargetTypeName);
        assertTrue(refType.getTargetType() == targetDocType);
    }

    private static DocumentTypeManager createConfiguredManager(String configFilePath) {
        DocumentTypeManager manager = new DocumentTypeManager();
        var sub = DocumentTypeManagerConfigurer.configure(manager, configFilePath);
        sub.close();
        return manager;
    }

    @Test
    public void multiple_reference_types_are_mapped_to_correct_document_target_types() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.multiplereferences.cfg");

        assertReferenceTypePresentInManager(manager, 12345678, "referenced_type");
        assertReferenceTypePresentInManager(manager, 87654321, "referenced_type2");
    }

    @Test
    public void no_temporary_targets_in_references_or_names() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.replaced_temporary.cfg");
        DocumentType docType = manager.getDocumentType("ad");
        Field f = docType.getField("campaign_ref");
        assertTrue(f.getDataType() instanceof ReferenceDataType);
        assertEquals("Reference<mystiqueCampaign>", f.getDataType().getName());
    }

    @Test
    public void can_have_reference_type_pointing_to_own_document_type() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.selfreference.cfg");

        assertReferenceTypePresentInManager(manager, 12345678, "type_with_ref");
    }

    @Test
    public void reference_field_has_correct_reference_type() {
        DocumentTypeManager manager = createConfiguredManager("file:src/test/document/documentmanager.singlereference.cfg");

        DocumentType docType = manager.getDocumentType("type_with_ref");
        Field field = docType.getField("my_ref_field");

        assertTrue(field.getDataType() instanceof ReferenceDataType);
        ReferenceDataType fieldRefType = (ReferenceDataType) field.getDataType();

        DocumentType targetDocType = manager.getDocumentType("referenced_type");
        assertTrue(fieldRefType.getTargetType() == targetDocType);
    }

    @Test
    public void imported_fields_are_empty_if_no_fields_provided_in_config() {
        var manager = createConfiguredManager("file:src/test/document/documentmanager.singlereference.cfg");
        var docType = manager.getDocumentType("type_with_ref");

        assertNotNull(docType.getImportedFieldNames());
        assertEquals(docType.getImportedFieldNames().size(), 0);
        assertFalse(docType.hasImportedField("foo"));
    }

    @Test
    public void imported_fields_are_populated_from_config() {
        var manager = createConfiguredManager("file:src/test/document/documentmanager.importedfields.cfg");
        var docType = manager.getDocumentType("type_with_ref");

        var expectedFields = new HashSet<String>();
        expectedFields.add("my_cool_imported_field");
        expectedFields.add("my_awesome_imported_field");
        assertEquals(docType.getImportedFieldNames(), expectedFields);

        assertTrue(docType.hasImportedField("my_cool_imported_field"));
        assertTrue(docType.hasImportedField("my_awesome_imported_field"));
        assertFalse(docType.hasImportedField("a_missing_imported_field"));
    }

    @Test
    public void position_type_is_recognized_as_v8() {
        var manager = DocumentTypeManager.fromFile("src/test/document/documentmanager.testv8pos.cfg");
        var docType = manager.getDocumentType("foobar");
        var simplepos = docType.getField("simplepos").getDataType();
        assertTrue(simplepos instanceof StructDataType);
        var arraypos = docType.getField("arraypos").getDataType();
        assertTrue(arraypos instanceof ArrayDataType);
        var array = (ArrayDataType) arraypos;
        assertTrue(array.getNestedType() instanceof StructDataType);
    }

    @Test
    public void declared_struct_types_available() {
        var manager = DocumentTypeManager.fromFile("src/test/document/documentmanager.declstruct.cfg");
        var docType = manager.getDocumentType("foo");
        var struct = docType.getDeclaredStructType("mystructinfoo");
        assertNotNull(struct);
        struct = docType.getStructType("mystructinfoo");
        assertNotNull(struct.getField("f1"));
        struct = docType.getDeclaredStructType("mystructinbar");
        assertNull(struct);
        struct = docType.getStructType("mystructinbar");
        assertNull(struct);
        struct = docType.getDeclaredStructType("mystructinfoobar");
        assertNull(struct);
        struct = docType.getStructType("mystructinfoobar");
        assertNull(struct);
        struct = docType.getDeclaredStructType("mystruct");
        assertNull(struct);
        struct = docType.getStructType("mystruct");
        assertNotNull(struct);
        assertNotNull(struct.getField("f0"));

        docType = manager.getDocumentType("bar");
        struct = docType.getDeclaredStructType("mystructinfoo");
        assertNull(struct);
        struct = docType.getStructType("mystructinfoo");
        assertNull(struct);
        struct = docType.getDeclaredStructType("mystructinbar");
        assertNotNull(struct);
        assertNotNull(struct.getField("f2"));
        struct = docType.getStructType("mystructinbar");
        assertNotNull(struct);
        assertNotNull(struct.getField("f2"));
        struct = docType.getDeclaredStructType("mystructinfoobar");
        assertNull(struct);
        struct = docType.getStructType("mystructinfoobar");
        assertNull(struct);
        struct = docType.getDeclaredStructType("mystruct");
        assertNull(struct);
        struct = docType.getStructType("mystruct");
        assertNotNull(struct);
        assertNotNull(struct.getField("f0"));

        docType = manager.getDocumentType("foobar");
        struct = docType.getDeclaredStructType("mystructinfoo");
        assertNull(struct);
        struct = docType.getStructType("mystructinfoo");
        assertNotNull(struct);
        assertNotNull(struct.getField("f1"));
        struct = docType.getDeclaredStructType("mystructinbar");
        assertNull(struct);
        struct = docType.getStructType("mystructinbar");
        assertNotNull(struct);
        assertNotNull(struct.getField("f2"));
        struct = docType.getDeclaredStructType("mystructinfoobar");
        assertNotNull(struct);
        assertNotNull(struct.getField("f3"));
        struct = docType.getStructType("mystructinfoobar");
        assertNotNull(struct);
        assertNotNull(struct.getField("f3"));
        struct = docType.getDeclaredStructType("mystruct");
        assertNull(struct);
        struct = docType.getStructType("mystruct");
        assertNotNull(struct);
        assertNotNull(struct.getField("f0"));

        assertNull(manager.getDataTypeInternal("mystruct@common"));
        assertNull(manager.getDataTypeInternal("mystructinfoo@foo"));
        assertNull(manager.getDataTypeInternal("mystructinbar@bar"));
        assertNull(manager.getDataTypeInternal("mystructinfoobar@foobar"));
        assertNull(manager.getDataTypeInternal("mystruct"));
        assertNull(manager.getDataTypeInternal("mystructinfoo"));
        assertNull(manager.getDataTypeInternal("mystructinbar"));
        assertNull(manager.getDataTypeInternal("mystructinfoobar"));
        assertNull(manager.getDataTypeInternal("foo.header"));
        assertNull(manager.getDataTypeInternal("position"));
    }

    // TODO test clone(). Also fieldSets not part of clone()..!

    // TODO add imported field to equals()/hashCode() for DocumentType? fieldSets not part of this...

}