### Question:
TextDocument extends TextDocumentItem { public int offsetAt(Position position) throws BadLocationException { ILineTracker lineTracker = getLineTracker(); return lineTracker.getOffsetAt(position); } TextDocument(TextDocumentItem document);  TextDocument(String text, String uri); void setIncremental(boolean incremental); boolean isIncremental(); Position positionAt(int position); int offsetAt(Position position); String lineText(int lineNumber); String lineDelimiter(int lineNumber); Range getWordRangeAt(int textOffset, Pattern wordDefinition); void update(List<TextDocumentContentChangeEvent> changes);  }### Answer:
@Test public void testOffsetAt() throws BadLocationException { TextDocument document = new TextDocument("abcd\nefgh", ""); Position position = new Position(0, 0); int offset = document.offsetAt(position); assertEquals(0, offset); position = new Position(0, 4); offset = document.offsetAt(position); assertEquals(4, offset); position = new Position(1, 0); offset = document.offsetAt(position); assertEquals(5, offset); position = new Position(1, 4); offset = document.offsetAt(position); assertEquals(9, offset); BadLocationException ex = null; try { position = new Position(1, 5); document.offsetAt(position); } catch (BadLocationException e) { ex = e; } assertNotNull(ex); }