public Document annotate(Document document, DocumentTypeManager manager) {
        AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();

        AnnotationType company = registry.getType("company");
        AnnotationType industry = registry.getType("industry");
        AnnotationType person = registry.getType("person");
        AnnotationType location = registry.getType("location");

        Map<String, AnnotationType> m = registry.getTypes();

        SpanTree tree = new SpanTree("testannotations");
        SpanList root = (SpanList)tree.getRoot();

        SpanNode companySpan = new Span(0, 5);
        SpanNode industrySpan = new Span(5, 10);
        SpanNode personSpan = new Span(10, 15);
        SpanNode locationSpan = new Span(15, 20);

        root.add(companySpan);
        root.add(industrySpan);
        root.add(personSpan);
        root.add(locationSpan);

        Struct companyValue = (Struct)company.getDataType().createFieldValue();
        companyValue.setFieldValue("name", new StringFieldValue("Sun"));
        companyValue.setFieldValue("ceo", new StringFieldValue("Scott Mcnealy"));
        companyValue.setFieldValue("lat", new DoubleFieldValue(37.7));
        companyValue.setFieldValue("lon", new DoubleFieldValue(-122.44));
        companyValue.setFieldValue("vertical", new StringFieldValue("software"));
        Annotation compAn = new Annotation(company, companyValue);
        tree.annotate(companySpan, compAn);

        Struct personValue = new Struct(person.getDataType());
        personValue.setFieldValue("name", new StringFieldValue("Richard Bair"));
        Annotation personAn = new Annotation(person, personValue);
        tree.annotate(personSpan, personAn);

        Struct locValue = new Struct(location.getDataType());
        locValue.setFieldValue("name", new StringFieldValue("Prinsens Gate"));
        Annotation loc = new Annotation(location, locValue);
        tree.annotate(locationSpan, loc);

        Struct locValue2 = new Struct(location.getDataType());
        locValue2.setFieldValue("name", new StringFieldValue("Kongens Gate"));
        Annotation locAn = new Annotation(location, locValue2);
        tree.annotate(locationSpan, locAn);

        SpanList branch = new SpanList();

        SpanNode span1 = new Span(0, 3);
        SpanNode span2 = new Span(1, 9);
        SpanNode span3 = new Span(12, 10);

        branch.add(span1);
        branch.add(span3);
        branch.add(span2);

        Struct industryValue = new Struct(industry.getDataType());
        industryValue.setFieldValue("vertical", new StringFieldValue("Manufacturing"));
        Annotation ind = new Annotation(industry, industryValue);
        tree.annotate(span1, ind);

        Struct pValue = new Struct(person.getDataType());
        pValue.setFieldValue("name", new StringFieldValue("Praveen Mohan"));
        Annotation pAn = new Annotation(person, pValue);
        tree.annotate(span2, pAn);

        Struct lValue = new Struct(location.getDataType());
        lValue.setFieldValue("name", new StringFieldValue("Embassy Golf Links"));
        Annotation locn = new Annotation(location, lValue);
        tree.annotate(span3, locn);

        Struct cValue = (Struct)company.getDataType().createFieldValue();
        cValue.setFieldValue("name", new StringFieldValue("Yahoo"));
        cValue.setFieldValue("ceo", new StringFieldValue("Carol Bartz"));
        cValue.setFieldValue("lat", new DoubleFieldValue(127.7));
        cValue.setFieldValue("lon", new DoubleFieldValue(-42.44));
        cValue.setFieldValue("vertical", new StringFieldValue("search"));
        Annotation cAn = new Annotation(company, cValue);
        tree.annotate(branch, cAn);

        Struct pVal = new Struct(person.getDataType());
        pVal.setFieldValue("name", new StringFieldValue("Kim Omar"));
        Annotation an = new Annotation(person, pVal);
        tree.annotate(root, an);
        root.add(branch);

        StringFieldValue body = (StringFieldValue)document.getFieldValue(document.getDataType().getField("body"));

        root.remove(branch);
        tree.cleanup();

        assertEquals(5, tree.numAnnotations());
        body.setSpanTree(tree);

        document.setFieldValue(document.getField("body"), body);

        return document;
    }
Test for bug 4066566. No assertions, but works if it runs without exceptions.
document/src/test/java/com/yahoo/document/datatypes/StringTestCase.java