@Override protected void setUp() throws Exception { super.setUp(); doc = new DefaultStyledDocument(); root = doc.getDefaultRootElement(); buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc, root); doc.buffer = buf; paragraph = root.getElement(0); content = doc.getContent(); content.insertString(0, "plainbolditalic\ntext"); // Create the structure equivalent to this sequence: // doc.insertString(doc.getLength(), "plain", null); // 5 chars // doc.insertString(doc.getLength(), "bold", bold); // 4 chars // doc.insertString(doc.getLength(), "italic", italic); // 6 chars // doc.insertString(doc.getLength(), "\ntext", null); // 5 chars doc.writeLock(); // Write lock needed to modify document structure Element[] leaves = new Element[4]; leaves[0] = doc.createLeafElement(paragraph, null, 0, 5); leaves[1] = doc.createLeafElement(paragraph, bold, 5, 9); leaves[2] = doc.createLeafElement(paragraph, italic, 9, 15); leaves[3] = doc.createLeafElement(paragraph, null, 15, 16); ((BranchElement) paragraph).replace(0, 1, leaves); BranchElement branch = (BranchElement) doc.createBranchElement(root, null); leaves = new Element[1]; leaves[0] = doc.createLeafElement(branch, null, 16, 21); branch.replace(0, 0, leaves); // Add this branch to the root ((BranchElement) root).replace(1, 0, new Element[] {branch}); insertOffset = 5 + 2; }