/** * {@inheritDoc} * * @see org.modeshape.graph.property.Path#getAncestor(int) */ public Path getAncestor( int degree ) { CheckArg.isNonNegative(degree, "degree"); if (degree == 0) return this; if (degree == 1) return parent; return parent.getAncestor(degree - 1); }
@Test public void shouldDelegateToParentForAncestorOfDegreeGreaterThanOne() { parent = mock(Path.class); when(parent.getAncestor(anyInt())).thenReturn(null); path = new ChildPath(parent, segment("d")); for (int i = 2; i != 10; ++i) { path.getAncestor(i); verify(parent).getAncestor(i - 1); } }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowNegativeAncestorDegree() { path.getAncestor(-1); }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowNegativeAncestorDegree() { path.getAncestor(-1); }
@Test public void shouldReturnNthDegreeAncestor() { assertThat(path.getAncestor(1), is(pathFactory.create("/mode:a/mode:b"))); assertThat(path.getAncestor(2), is(pathFactory.create("/mode:a"))); assertThat(path.getAncestor(3), is(ROOT)); }
@Test public void shouldReturnRootForAnyAncestorExactDegreeFromRoot() { assertThat(path.getAncestor(path.size()), is(ROOT)); assertThat(ROOT.getAncestor(0), is(ROOT)); }
@Test public void shouldReturnNthDegreeAncestor() { assertThat(path.getAncestor(1), is(pathFactory.create("/mode:a/mode:b"))); assertThat(path.getAncestor(2), is(pathFactory.create("/mode:a"))); assertThat(path.getAncestor(3), is(ROOT)); }
@Test( expected = InvalidPathException.class ) public void shouldNotAllowAncestorDegreeLargerThanSize() { path.getAncestor(path.size() + 1); }
@Test( expected = InvalidPathException.class ) public void shouldNotAllowAncestorDegreeLargerThanSize() { path.getAncestor(path.size() + 1); }
@Test public void shouldReturnRootForAnyAncestorExactDegreeFromRoot() { assertThat(path.getAncestor(path.size()).isRoot(), is(true)); }
@Test public void shouldReturnParentForAncestorOfDegreeOne() { assertThat(path.getAncestor(1), is(sameInstance(parent))); assertThat(path.getParent(), is(sameInstance(parent))); }
@Test public void shouldNotConsiderAncestorToBeAtOrBelowTheDecendant() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.getAncestor(i).isAtOrBelow(path1), is(false)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.getAncestor(i).isAtOrBelow(path2), is(false)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.getAncestor(i).isAtOrBelow(path3), is(false)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.getAncestor(i).isAtOrBelow(path4), is(false)); } }
@Test public void shouldConsiderDecendantToBeAtOrBelowTheAncestor() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.isAtOrBelow(path1.getAncestor(i)), is(true)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.isAtOrBelow(path2.getAncestor(i)), is(true)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.isAtOrBelow(path3.getAncestor(i)), is(true)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.isAtOrBelow(path4.getAncestor(i)), is(true)); } }
@Test public void shouldConsiderDecendantToBeAtOrBelowTheAncestor() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.isAtOrBelow(path1.getAncestor(i)), is(true)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.isAtOrBelow(path2.getAncestor(i)), is(true)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.isAtOrBelow(path3.getAncestor(i)), is(true)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.isAtOrBelow(path4.getAncestor(i)), is(true)); } }
@Test public void shouldNotConsiderAncestorToBeAtOrBelowTheDecendant() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.getAncestor(i).isAtOrBelow(path1), is(false)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.getAncestor(i).isAtOrBelow(path2), is(false)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.getAncestor(i).isAtOrBelow(path3), is(false)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.getAncestor(i).isAtOrBelow(path4), is(false)); } }
@Test public void shouldNotConsiderDecendantToBeAtOrAboveTheAncestor() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.isAtOrAbove(path1.getAncestor(i)), is(false)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.isAtOrAbove(path2.getAncestor(i)), is(false)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.isAtOrAbove(path3.getAncestor(i)), is(false)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.isAtOrAbove(path4.getAncestor(i)), is(false)); } }
@Test public void shouldNotConsiderDecendantToBeAtOrAboveTheAncestor() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.isAtOrAbove(path1.getAncestor(i)), is(false)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.isAtOrAbove(path2.getAncestor(i)), is(false)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.isAtOrAbove(path3.getAncestor(i)), is(false)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.isAtOrAbove(path4.getAncestor(i)), is(false)); } }
@Test public void shouldConsiderAncestorToBeAtOrAboveTheDecendant() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.getAncestor(i).isAtOrAbove(path1), is(true)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.getAncestor(i).isAtOrAbove(path2), is(true)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.getAncestor(i).isAtOrAbove(path3), is(true)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.getAncestor(i).isAtOrAbove(path4), is(true)); } }
@Test public void shouldConsiderAncestorToBeAtOrAboveTheDecendant() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.getAncestor(i).isAtOrAbove(path1), is(true)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.getAncestor(i).isAtOrAbove(path2), is(true)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.getAncestor(i).isAtOrAbove(path3), is(true)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.getAncestor(i).isAtOrAbove(path4), is(true)); } }
@Test public void shouldConsiderNodeToBeDecendantOfEveryAncestorNode() { Path path1 = pathFactory.create("/a/y/z"); Path path2 = pathFactory.create("/a/b/c"); Path path3 = pathFactory.create("/x/b/c"); Path path4 = pathFactory.create("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"); Path common = pathFactory.create("/a"); assertThat(path1.isDecendantOf(common), is(true)); assertThat(path2.isDecendantOf(common), is(true)); assertThat(path3.isDecendantOf(common), is(false)); assertThat(path1.getParent().isAncestorOf(path1), is(true)); for (int i = 1; i < path1.size(); ++i) { assertThat(path1.isDecendantOf(path1.getAncestor(i)), is(true)); } for (int i = 1; i < path2.size(); ++i) { assertThat(path2.isDecendantOf(path2.getAncestor(i)), is(true)); } for (int i = 1; i < path3.size(); ++i) { assertThat(path3.isDecendantOf(path3.getAncestor(i)), is(true)); } for (int i = 1; i < path4.size(); ++i) { assertThat(path4.isDecendantOf(path4.getAncestor(i)), is(true)); } }