@Test
    public void testCopyAnnotatedString() {
        StringFieldValue str = getAnnotatedString();
        StringFieldValue strCopy = str.clone();

        SpanTree balloooTree = str.getSpanTree("ballooo");
        AlternateSpanList root = (AlternateSpanList) balloooTree.getRoot();
        Span s1 = (Span) root.children(0).get(0);
        Span s2 = (Span) root.children(0).get(1);
        Span s3 = (Span) root.children(0).get(2);
        AlternateSpanList s4 = (AlternateSpanList) root.children(0).get(3);
        Span s5 = (Span) s4.children(0).get(0);
        Span s6 = (Span) s4.children(0).get(1);
        Span s7 = (Span) root.children(1).get(0);
        Span s8 = (Span) root.children(1).get(1);
        Span s9 = (Span) root.children(1).get(2);

        SpanTree balloooTreeCopy = strCopy.getSpanTree("ballooo");
        assertEquals(balloooTree, balloooTreeCopy);
        assertNotSame(balloooTree, balloooTreeCopy);
        AlternateSpanList rootCopy = (AlternateSpanList) balloooTreeCopy.getRoot();
        assertEquals(root, rootCopy);
        assertNotSame(root, rootCopy);
        Span s1Copy = (Span) rootCopy.children(0).get(0);
        assertEquals(s1, s1Copy);
        assertNotSame(s1, s1Copy);
        Span s2Copy = (Span) rootCopy.children(0).get(1);
        assertEquals(s2, s2Copy);
        assertNotSame(s2, s2Copy);
        Span s3Copy = (Span) rootCopy.children(0).get(2);
        assertEquals(s3, s3Copy);
        assertNotSame(s3, s3Copy);
        AlternateSpanList s4Copy = (AlternateSpanList) rootCopy.children(0).get(3);
        assertEquals(s4, s4Copy);
        assertNotSame(s4, s4Copy);
        Span s5Copy = (Span) s4Copy.children(0).get(0);
        assertEquals(s5, s5Copy);
        assertNotSame(s5, s5Copy);
        Span s6Copy = (Span) s4Copy.children(0).get(1);
        assertEquals(s6, s6Copy);
        assertNotSame(s6, s6Copy);
        Span s7Copy = (Span) rootCopy.children(1).get(0);
        assertEquals(s7, s7Copy);
        assertNotSame(s7, s7Copy);
        Span s8Copy = (Span) rootCopy.children(1).get(1);
        assertEquals(s8, s8Copy);
        assertNotSame(s8, s8Copy);
        Span s9Copy = (Span) rootCopy.children(1).get(2);
        assertEquals(s9, s9Copy);
        assertNotSame(s9, s9Copy);


        Iterator<Annotation> annotationsBalloooTree;
        {
            List<Annotation> allAnnotationsList = new ArrayList<>();
            for (Annotation an : balloooTree) {
                allAnnotationsList.add(an);
            }
            Collections.sort(allAnnotationsList);

            annotationsBalloooTree = allAnnotationsList.iterator();
        }

        Annotation dummyAnnForS1 = annotationsBalloooTree.next();

        Annotation dummyAnnForS2 = annotationsBalloooTree.next();

        Annotation numberAnnForS2 = annotationsBalloooTree.next();
        IntegerFieldValue integerValForS2 = (IntegerFieldValue) numberAnnForS2.getFieldValue();

        Annotation motherAnnForS2 = annotationsBalloooTree.next();
        Struct motherValForS2 = (Struct) motherAnnForS2.getFieldValue();

        Annotation dummyAnnForS3 = annotationsBalloooTree.next();

        Annotation numberAnnForS3 = annotationsBalloooTree.next();
        IntegerFieldValue integerValForS3 = (IntegerFieldValue) numberAnnForS3.getFieldValue();

        Annotation dummyAnnForS5 = annotationsBalloooTree.next();

        Annotation daughterAnnForS6 = annotationsBalloooTree.next();
        Struct daughterValForS6 = (Struct) daughterAnnForS6.getFieldValue();
        AnnotationReference refFromS6ToMotherAnn = (AnnotationReference) daughterValForS6.getFieldValue("related");


        Iterator<Annotation> annotationsBalloooTreeCopy;
        {
            List<Annotation> allAnnotationsList = new ArrayList<>();
            for (Annotation an : balloooTreeCopy) {
                allAnnotationsList.add(an);
            }
            Collections.sort(allAnnotationsList);

            annotationsBalloooTreeCopy = allAnnotationsList.iterator();
        }

        Annotation dummyAnnForS1Copy = annotationsBalloooTreeCopy.next();
        assertEquals(dummyAnnForS1, dummyAnnForS1Copy);
        assertNotSame(dummyAnnForS1, dummyAnnForS1Copy);

        Annotation dummyAnnForS2Copy = annotationsBalloooTreeCopy.next();
        assertEquals(dummyAnnForS2, dummyAnnForS2Copy);
        assertNotSame(dummyAnnForS2, dummyAnnForS2Copy);

        Annotation numberAnnForS2Copy = annotationsBalloooTreeCopy.next();
        assertEquals(numberAnnForS2, numberAnnForS2Copy);
        assertNotSame(numberAnnForS2, numberAnnForS2Copy);
        IntegerFieldValue integerValForS2Copy = (IntegerFieldValue) numberAnnForS2Copy.getFieldValue();
        assertEquals(integerValForS2, integerValForS2Copy);
        assertNotSame(integerValForS2, integerValForS2Copy);

        Annotation motherAnnForS2Copy = annotationsBalloooTreeCopy.next();
        assertEquals(motherAnnForS2, motherAnnForS2Copy);
        assertNotSame(motherAnnForS2, motherAnnForS2Copy);
        Struct motherValForS2Copy = (Struct) motherAnnForS2Copy.getFieldValue();
        assertEquals(motherValForS2, motherValForS2Copy);
        assertNotSame(motherValForS2, motherValForS2Copy);

        Annotation dummyAnnForS3Copy = annotationsBalloooTreeCopy.next();
        assertEquals(dummyAnnForS3, dummyAnnForS3Copy);
        assertNotSame(dummyAnnForS3, dummyAnnForS3Copy);

        Annotation numberAnnForS3Copy = annotationsBalloooTreeCopy.next();
        assertEquals(numberAnnForS3, numberAnnForS3Copy);
        assertNotSame(numberAnnForS3, numberAnnForS3Copy);
        IntegerFieldValue integerValForS3Copy = (IntegerFieldValue) numberAnnForS3Copy.getFieldValue();
        assertEquals(integerValForS3, integerValForS3Copy);
        assertNotSame(integerValForS3, integerValForS3Copy);

        Annotation dummyAnnForS5Copy = annotationsBalloooTreeCopy.next();
        assertEquals(dummyAnnForS5, dummyAnnForS5Copy);
        assertNotSame(dummyAnnForS5, dummyAnnForS5Copy);

        Annotation daughterAnnForS6Copy = annotationsBalloooTreeCopy.next();
        assertEquals(daughterAnnForS6, daughterAnnForS6Copy);
        assertNotSame(daughterAnnForS6, daughterAnnForS6Copy);
        Struct daughterValForS6Copy = (Struct) daughterAnnForS6Copy.getFieldValue();
        assertEquals(daughterValForS6, daughterValForS6Copy);
        assertNotSame(daughterValForS6, daughterValForS6Copy);
        AnnotationReference refFromS6ToMotherAnnCopy = (AnnotationReference) daughterValForS6Copy.getFieldValue("related");
        assertEquals(refFromS6ToMotherAnn, refFromS6ToMotherAnnCopy);
        assertNotSame(refFromS6ToMotherAnn, refFromS6ToMotherAnnCopy);

        assertEquals(str, strCopy);

    }
Tests iterating through E's annotations in a tree.
document/src/test/java/com/yahoo/document/annotation/SpanTreeTestCase.java