From f945f55835f1cfb1038bc207e0427f1d3335bc55 Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Tue, 22 Feb 2011 23:22:37 +0100 Subject: [PATCH] fixed bug with lazy evaluation and non-returning PathMapper (i.e PathCallbackHandler) --- .../template/PathMappingIteratorTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/graph/neo4j/template/PathMappingIteratorTest.java diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/graph/neo4j/template/PathMappingIteratorTest.java b/spring-data-neo4j/src/test/java/org/springframework/data/graph/neo4j/template/PathMappingIteratorTest.java new file mode 100644 index 000000000..a918c309f --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/graph/neo4j/template/PathMappingIteratorTest.java @@ -0,0 +1,61 @@ +package org.springframework.data.graph.neo4j.template; + +import org.junit.Test; +import org.neo4j.graphdb.Path; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +/** + * @author mh + * @since 22.02.11 + */ +public class PathMappingIteratorTest { + + @Test + public void lazyIteratorShouldNotCallBackBeforeUse() { + runAndCheckMode(0, 3, IterationController.IterationMode.LAZY); + } + @Test + public void eagerIteratorShouldBeCalledImmediately() { + runAndCheckMode(3, 3, IterationController.IterationMode.EAGER); + } + @Test + public void eagerStopOnNullIteratorShouldBeCalledImmediatelyAndReturnReducedResult() { + runAndCheckMode(2, 2, IterationController.IterationMode.EAGER_STOP_ON_NULL); + } + + private void runAndCheckMode(int countAfterMap, int countAfterIteration, IterationController.IterationMode iterationMode) { + TestPathMapper lazyMapper = new TestPathMapper(iterationMode); + Path anyPath = mock(Path.class); + List paths = Arrays.asList(anyPath, anyPath, anyPath); + Iterable result = new PathMappingIterator().mapPaths(paths, lazyMapper); + assertEquals(countAfterMap, lazyMapper.counter); + for (Integer integer : result); + assertEquals(countAfterIteration, lazyMapper.counter); + } + + private static class TestPathMapper implements PathMapper, IterationController { + public int counter; + private IterationMode iterationMode; + + public TestPathMapper(IterationMode iterationMode) { + this.iterationMode = iterationMode; + } + + @Override + public Integer mapPath(Path path) { + counter++; + if (counter==2) return null; + return counter; + } + + @Override + public IterationMode getIterationMode() { + return iterationMode; + } + } +}