AbstractJcrItem implements Item, Namespaced { @Override public Item getAncestor( int depth ) throws RepositoryException { checkSession(); if (depth < 0) { throw new ItemNotFoundException(JcrI18n.noNegativeDepth.text(depth)); } depth = getDepth() - depth; if (depth < 0) { throw new ItemNotFoundException(JcrI18n.tooDeep.text(depth)); } Item ancestor = this; while (--depth >= 0) { ancestor = ancestor.getParent(); } return ancestor; } protected  AbstractJcrItem( JcrSession session ); @Override Item getAncestor( int depth ); @Override int getDepth(); @Override abstract AbstractJcrNode getParent();  }
@Test public void zerothAncestorOfPropertyShouldBeRootNode() throws Exception { assertThat(newPropertyOnModifiedNode.getAncestor(0), is((Item)rootNode)); assertThat(modifiedPropertyOnModifiedNode.getAncestor(0), is((Item)rootNode)); assertThat(unmodifiedPropertyOnModifiedNode.getAncestor(0), is((Item)rootNode)); }
@Test public void firstAncestorOfPropertyShouldBeParentNode() throws Exception { assertThat(newPropertyOnModifiedNode.getAncestor(1), is((Item)nodeWithModifiedProperty)); assertThat(modifiedPropertyOnModifiedNode.getAncestor(1), is((Item)nodeWithModifiedProperty)); assertThat(unmodifiedPropertyOnModifiedNode.getAncestor(1), is((Item)nodeWithModifiedProperty)); }
@Test public void secondAncestorOfPropertyShouldBeParentOfParentNode() throws Exception { assertThat(newPropertyOnModifiedNode.getAncestor(2), is((Item)newPropertyOnModifiedNode)); assertThat(modifiedPropertyOnModifiedNode.getAncestor(2), is((Item)modifiedPropertyOnModifiedNode)); assertThat(unmodifiedPropertyOnModifiedNode.getAncestor(2), is((Item)unmodifiedPropertyOnModifiedNode)); }