package com.tinkerpop.pipes.pgm;

import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraphFactory;
import com.tinkerpop.pipes.SingleIterator;
import junit.framework.TestCase;

import java.util.Arrays;
import java.util.NoSuchElementException;

/**
 * @author: Marko A. Rodriguez (http://markorodriguez.com)
 */
public class VertexEdgePipeTest extends TestCase {

    public void testOutGoingEdges() {
        Graph graph = TinkerGraphFactory.createTinkerGraph();
        Vertex marko = graph.getVertex("1");
        VertexEdgePipe vsf = new VertexEdgePipe(VertexEdgePipe.Step.OUT_EDGES);
        vsf.setStarts(Arrays.asList(marko).iterator());
        assertTrue(vsf.hasNext());
        int counter = 0;
        while (vsf.hasNext()) {
            Edge e = vsf.next();
            assertEquals(e.getOutVertex(), marko);
            assertTrue(e.getInVertex().getId().equals("2") || e.getInVertex().getId().equals("3") || e.getInVertex().getId().equals("4"));
            counter++;
        }
        assertEquals(counter, 3);
        try {
            vsf.next();
            assertTrue(false);
        } catch (NoSuchElementException e) {
            assertFalse(false);
        }

        Vertex josh = graph.getVertex("4");
        vsf = new VertexEdgePipe(VertexEdgePipe.Step.OUT_EDGES);
        vsf.setStarts(Arrays.asList(josh).iterator());
        assertTrue(vsf.hasNext());
        counter = 0;
        while (vsf.hasNext()) {
            Edge e = vsf.next();
            assertEquals(e.getOutVertex(), josh);
            assertTrue(e.getInVertex().getId().equals("5") || e.getInVertex().getId().equals("3"));
            counter++;
        }
        assertEquals(counter, 2);
        try {
            vsf.next();
            assertTrue(false);
        } catch (NoSuchElementException e) {
            assertFalse(false);
        }

        Vertex lop = graph.getVertex("3");
        vsf = new VertexEdgePipe(VertexEdgePipe.Step.OUT_EDGES);
        vsf.setStarts(Arrays.asList(lop).iterator());
        assertFalse(vsf.hasNext());
        counter = 0;
        while (vsf.hasNext()) {
            counter++;
        }
        assertEquals(counter, 0);
        try {
            vsf.next();
            assertTrue(false);
        } catch (NoSuchElementException e) {
            assertFalse(false);
        }
    }

    public void testInEdges() {
        Graph graph = TinkerGraphFactory.createTinkerGraph();
        Vertex josh = graph.getVertex("4");
        VertexEdgePipe pipe = new VertexEdgePipe(VertexEdgePipe.Step.IN_EDGES);
        pipe.setStarts(new SingleIterator<Vertex>(josh));
        int counter = 0;
        while (pipe.hasNext()) {
            counter++;
            Edge edge = pipe.next();
            assertEquals(edge.getId(), "8");
        }
        assertEquals(counter, 1);
    }

    public void testBothEdges() {
        Graph graph = TinkerGraphFactory.createTinkerGraph();
        Vertex josh = graph.getVertex("4");
        VertexEdgePipe pipe = new VertexEdgePipe(VertexEdgePipe.Step.BOTH_EDGES);
        pipe.setStarts(new SingleIterator<Vertex>(josh));
        int counter = 0;
        while (pipe.hasNext()) {
            counter++;
            Edge edge = pipe.next();
            assertTrue(edge.getId().equals("8") || edge.getId().equals("10") || edge.getId().equals("11"));
        }
        assertEquals(counter, 3);
    }
}

@@ -27,16 +27,74 @@
 s.pgm;%0A%0A
+import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph;%0A
 import c
@@ -279,16 +279,49 @@
 actory;%0A
+import com.tinkerpop.pipes.Pipe;%0A
 import c
@@ -3580,11 +3580,729 @@
 );%0A    %7D
+%0A%0A    public void testBigGraphWithNoEdges() %7B%0A        // This used to cause a stack overflow. Not crashing makes this a success.%0A        TinkerGraph graph = new TinkerGraph();%0A        for (int i = 0; i %3C 100000; i++) %7B%0A            graph.addVertex(null);%0A        %7D%0A        Pipe%3CGraph, Vertex%3E vertices = new GraphElementPipe%3CVertex%3E(GraphElementPipe.ElementType.VERTEX);%0A        vertices.setStarts(new SingleIterator%3CGraph%3E(graph));%0A        VertexEdgePipe outEdges = new VertexEdgePipe(VertexEdgePipe.Step.OUT_EDGES);%0A        outEdges.setStarts(vertices);%0A        int counter = 0;%0A        while (outEdges.hasNext()) %7B%0A            outEdges.next();%0A            counter++;%0A        %7D%0A        assertEquals(counter, 0);%0A    %7D
 %0A%7D%0A

Test stack overflow.