### Question:
TextSelection extends Selection { public void replaceWith(String newText) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; delete(index, leftLength, parentElement); OdfTextSpan textSpan = new OdfTextSpan((OdfFileDom) parentElement.getOwnerDocument()); textSpan.addContentWhitespace(newText); mIsInserted = false; insertSpan(textSpan, index, parentElement); optimize(parentElement); int offset = newText.length() - leftLength; SelectionManager.refresh(this.getContainerElement(), offset, index + getText().length()); mMatchedText = newText; } TextSelection(String text, OdfElement containerElement, int index); @Override OdfElement getElement(); OdfElement getContainerElement(); @Override int getIndex(); String getText(); @Override void cut(); void applyStyle(OdfStyleBase style); void replaceWith(String newText); @Override void pasteAtFrontOf(Selection positionItem); @Override void pasteAtEndOf(Selection positionItem); void addHref(URL url); @Override String toString();  }### Answer:
@Test public void testReplacewith() { search = null; search = new TextNavigation("ODFDOM", doc); TextSelection nextSelect = null; TextNavigation nextsearch = new TextNavigation("next", doc); if (nextsearch.hasNext()) { nextSelect = (TextSelection) nextsearch.getCurrentItem(); } OdfStyle style = new OdfStyle(contentDOM); style.setProperty(StyleTextPropertiesElement.FontWeight, "bold"); style.setStyleFamilyAttribute("text"); int i = 0; while (search.hasNext()) { if (i > 0) { TextSelection item = (TextSelection) search.getCurrentItem(); try { item.replaceWith("Odf Toolkit"); item.applyStyle(style); } catch (InvalidNavigationException e) { Assert.fail(e.getMessage()); } } i++; } search = new TextNavigation("Odf Toolkit", doc); int j = 0; while (search.hasNext()) { j++; } Assert.assertTrue(i == j); try { nextSelect.replaceWith("bbb"); } catch (InvalidNavigationException e1) { Assert.fail(e1.getMessage()); } try { doc.save(ResourceUtilities.getTestOutputFile(SAVE_FILE_REPLACE)); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }
@Test public void testReplacewith() { search = null; search = new TextNavigation("ODFDOM", doc); TextSelection nextSelect = null; TextNavigation nextsearch = new TextNavigation("next", doc); if (nextsearch.hasNext()) { nextSelect = (TextSelection) nextsearch.getCurrentItem(); } OdfStyle style = new OdfStyle(contentDOM); style.setProperty(StyleTextPropertiesElement.FontWeight, "bold"); style.setStyleFamilyAttribute("text"); int i = 0; while (search.hasNext()) { if (i > 0) { TextSelection item = (TextSelection) search.getCurrentItem(); try { item.replaceWith("Odf Toolkit"); item.applyStyle(style); } catch (InvalidNavigationException e) { Assert.fail(e.getMessage()); } } i++; } search = new TextNavigation("Odf Toolkit", doc); int j = 0; while (search.hasNext()) { j++; } Assert.assertTrue(i == j); try { nextSelect.replaceWith("bbb"); } catch (InvalidNavigationException e1) { Assert.fail(e1.getMessage()); } try { doc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_REPLACE)); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }