DocumentNavigator extends DefaultNavigator { @Override public Object getDocumentNode(Object contextNode) { if (isDocument(contextNode)) { return contextNode; } if (null == contextNode) { throw new RuntimeException("contextNode may not be null"); } return getDocumentNode(getParentNode(contextNode)); }  @Override String getAttributeName(Object arg0); @Override String getAttributeNamespaceUri(Object arg0); @Override String getAttributeQName(Object arg0); @Override String getAttributeStringValue(Object arg0); @Override String getCommentStringValue(Object arg0); @Override String getElementName(Object node); @Override String getElementNamespaceUri(Object arg0); @Override String getElementQName(Object arg0); @Override String getElementStringValue(Object arg0); @Override String getNamespacePrefix(Object arg0); @Override String getNamespaceStringValue(Object arg0); @Override String getTextStringValue(Object arg0); @Override boolean isAttribute(Object arg0); @Override boolean isComment(Object arg0); @Override boolean isDocument(Object arg0); @Override boolean isElement(Object arg0); @Override boolean isNamespace(Object arg0); @Override boolean isProcessingInstruction(Object arg0); @Override boolean isText(Object arg0); @Override XPath parseXPath(String arg0); @Override Object getParentNode(Object arg0); @Override Iterator<Attribute> getAttributeAxisIterator(final Object arg0); @Override Iterator<Node> getChildAxisIterator(Object contextNode); @Override Iterator<Node> getParentAxisIterator(Object contextNode); @Override Iterator<Node> getFollowingSiblingAxisIterator(Object contextNode); @Override Iterator<Node> getPrecedingSiblingAxisIterator(Object contextNode); @Override Iterator<Node> getFollowingAxisIterator(Object contextNode); @Override Iterator<Node> getPrecedingAxisIterator(Object contextNode); @Override Object getDocumentNode(Object contextNode);  }
@Test public void getDocumentNode() { DocumentNavigator nav = new DocumentNavigator(); try { nav.getDocumentNode(null); fail(); } catch (RuntimeException e) { assertNotNull(e); } Node root = new DummyRootNode(1); Node n = new DummyNode(1); root.jjtAddChild(n, 0); n.jjtSetParent(root); assertSame(root, nav.getDocumentNode(n)); }