@Test
    public void testCyclicReferences() {
        DocumentTypeManager docMan = new DocumentTypeManager();
        AnnotationTypeRegistry reg = docMan.getAnnotationTypeRegistry();

        StringFieldValue strfval = new StringFieldValue("hohohoho");
        SpanList root = new SpanList();
        SpanTree tree = new SpanTree("habahaba", root);
        strfval.setSpanTree(tree);

        //set up types:
        AnnotationType endTagType = new AnnotationType("endtag");
        AnnotationType beginTagType = new AnnotationType("begintag");
        AnnotationReferenceDataType refToBeginTagDataType = new AnnotationReferenceDataType(beginTagType);
        AnnotationReferenceDataType refToEndTagDataType = new AnnotationReferenceDataType(endTagType);
        endTagType.setDataType(refToBeginTagDataType);
        beginTagType.setDataType(refToEndTagDataType);

        //register types:
        reg.register(endTagType);
        reg.register(beginTagType);
        docMan.register(refToBeginTagDataType);
        docMan.register(refToEndTagDataType);

        //set up annotations and their references to each other:
        AnnotationReference refToBeginTag = new AnnotationReference(refToBeginTagDataType);
        AnnotationReference refToEndTag = new AnnotationReference(refToEndTagDataType);
        Annotation beginTag = new Annotation(beginTagType, refToEndTag);
        Annotation endTag = new Annotation(endTagType, refToBeginTag);
        refToBeginTag.setReference(beginTag);
        refToEndTag.setReference(endTag);

        //create spans:
        Span beginTagSpan = new Span(0, 1);
        Span endTagSpan = new Span(1, 1);
        root.add(beginTagSpan);
        root.add(endTagSpan);

        //annotate spans:
        tree.annotate(beginTagSpan, beginTag);
        tree.annotate(endTagSpan, endTag);


        //none of the below statements should lead to a StackOverflowError:
        assertFalse(endTag.toString().equals(beginTag.toString()));

        assertTrue(endTag.hashCode() != beginTag.hashCode());

        assertFalse(endTag.equals(beginTag));
        assertFalse(beginTag.equals(endTag));

        StringFieldValue copyString = strfval.clone();
        assertEquals(strfval, copyString);


        GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
        DocumentSerializer serializer = DocumentSerializerFactory.createHead(buffer);

        serializer.write(new Field("stringfield", DataType.STRING), strfval);
        buffer.flip();

        DocumentDeserializer deserializer = DocumentDeserializerFactory.createHead(docMan, buffer);
        StringFieldValue stringFieldValue2 = new StringFieldValue();
        deserializer.read(new Field("stringfield", DataType.STRING), stringFieldValue2);

        assertEquals(strfval, stringFieldValue2);
        assertNotSame(strfval, stringFieldValue2);
    }
Tests iterating through E's annotations in a tree.
document/src/test/java/com/yahoo/document/annotation/SpanTreeTestCase.java