### Question:
TextSelection extends Selection { public void applyStyle(OdfStyleBase style) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; appendStyle(index, leftLength, parentElement, style); } 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 testApplyStyle() { search = null; search = new TextNavigation("delete", doc); OdfOfficeAutomaticStyles autoStyles = null; try { autoStyles = doc.getContentDom().getAutomaticStyles(); } catch (Exception e1) { Assert.fail("Failed with " + e1.getClass().getName() + ": '" + e1.getMessage() + "'"); } OdfStyleBase style = autoStyles.getStyle("T4", OdfStyleFamily.Text); Assert.assertNotNull(style); while (search.hasNext()) { TextSelection item = (TextSelection) search.getCurrentItem(); try { item.applyStyle(style); } catch (InvalidNavigationException e) { Assert.fail(e.getMessage()); } } try { doc.save(ResourceUtilities.getTestOutputFile(SAVE_FILE_STYLE)); } 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 testApplyStyle() { search = null; search = new TextNavigation("delete", doc); OdfOfficeAutomaticStyles autoStyles = null; try { autoStyles = doc.getContentDom().getAutomaticStyles(); } catch (Exception e1) { Assert.fail("Failed with " + e1.getClass().getName() + ": '" + e1.getMessage() + "'"); } OdfStyleBase style = autoStyles.getStyle("T4", OdfStyleFamily.Text); Assert.assertNotNull(style); while (search.hasNext()) { TextSelection item = (TextSelection) search.getCurrentItem(); try { item.applyStyle(style); } catch (InvalidNavigationException e) { Assert.fail(e.getMessage()); } } try { doc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_STYLE)); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }