PathTest.java
package org.graphstream.graph.implementations;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.List;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Node;
import org.graphstream.graph.Path;
import org.junit.Before;
import org.junit.Test;
public class PathTest {
/* Expert - Virgi, Torre, Ciani - 8/04/2014 */
private SingleGraph singleGraph;
@Before
public void testSetup() {
singleGraph = new SingleGraph("single-graph");
Node root = singleGraph.addNode("root");
Node node1 = singleGraph.addNode("node1");
Node node2 = singleGraph.addNode("node2");
Node node3 = singleGraph.addNode("node3");
Edge edge1 = singleGraph.addEdge("edge1", "root", "node1");
Edge edge2 = singleGraph.addEdge("edge2", "root", "node2");
Edge edge3 = singleGraph.addEdge("edge3", "node1", "node3");
}
@Test
public void getPathWeightTest(){
SingleGraph sg = new SingleGraph("single");
sg.addNode("node1");
sg.addNode("node2");
sg.addNode("node3");
sg.addEdge("edge1", sg.getNode("node1"), sg.getNode("node2"),true);
sg.addEdge("edge2", sg.getNode("node2"), sg.getNode("node3"),true);
AbstractNode node1 = sg.getNode("node1");
AbstractNode node2 = sg.getNode("node2");
AbstractNode node3 = sg.getNode("node3");
AbstractEdge edge1 = new AbstractEdge("edge_1", node1, node2, true);
edge1.setAttribute("weight", 1.0);
AbstractEdge edge2 = new AbstractEdge("edge_2", node2, node3, true);
edge2.setAttribute("weight", 8.0);
Path p = new Path();
p.setRoot(node1);
p.add(node1, edge1);
p.add(node2, edge2);
assertEquals(p.getPathWeight("weight").intValue(),9);
}
@Test
public void addEdgeTest() {
Path path = new Path();
AbstractNode node1 = singleGraph.getNode("node1");
AbstractNode node2 = singleGraph.getNode("node2");
AbstractNode node3 = singleGraph.getNode("node3");
//assertEquals(0, path.size());
AbstractEdge edge1 = singleGraph.getEdge("edge1");
path.setRoot(singleGraph.getNode("root"));
path.add(edge1);
assertEquals(2, path.size());
assertEquals(path.getRoot(), edge1.getSourceNode());
}
@Test
public void clearEmptyPathTest() {
Path path = new Path();
path.clear();
assertEquals(0, path.size());
//path.setRoot(singleGraph.getNode("root"));
}
@Test
public void clearPathTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(singleGraph.getEdge("edge1"));
path.clear();
assertEquals(0, path.size());
}
@Test
public void getACopyTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(singleGraph.getEdge("edge1"));
path.add(singleGraph.getEdge("edge3"));
Path newPath = path.getACopy();
assertEquals(newPath.size(), path.size());
assertEquals(newPath.getNodePath(), path.getNodePath());
assertEquals(newPath.getEdgePath(), path.getEdgePath());
assertEquals(newPath.getRoot(), path.getRoot());
}
@Test
public void equalsPathEqualsTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(singleGraph.getEdge("edge1"));
path.add(singleGraph.getEdge("edge3"));
Path newPath = path.getACopy();
assertTrue(path.equals(newPath));
}
@Test
public void equalsPathNotEqualsDiffrentSizeTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(singleGraph.getEdge("edge1"));
path.add(singleGraph.getEdge("edge3"));
Path newPath = new Path();
assertFalse(path.equals(newPath));
}
@Test
public void equalsPathNotEqualsTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(singleGraph.getEdge("edge1"));
Path newPath = new Path();
newPath.setRoot(singleGraph.getNode("node1"));
newPath.add(newPath.getRoot(), singleGraph.getEdge("edge3"));
assertFalse(path.equals(newPath));
}
@Test
public void setRootNullTest() {
Path path = new Path();
path.setRoot(null);
assertNull(path.getRoot());
}
/* ------ */
//TC-P_C01
@Test
public void emptyPathTest() {
SingleGraph sg = new SingleGraph("single");
Path path = new Path();
boolean isEmpty = path.empty();
assertTrue(isEmpty);
}
//TC-P_C02
@Test
public void rootPathSetRootMethodTest() {
SingleGraph sg = new SingleGraph("single");
Path path = new Path();
sg.addNode("node1");
AbstractNode node1 = sg.getNode("node1");
path.setRoot(node1);
assertEquals(node1, path.getRoot());
}
//TC-P_C03
@Test
public void rootPathAddMethodTest() {
SingleGraph sg = new SingleGraph("single");
Path path = new Path();
sg.addNode("node1");
sg.addNode("node2");
AbstractNode node1 = sg.getNode("node1");
AbstractNode node2 = sg.getNode("node2");
AbstractEdge edge1 = new AbstractEdge("edge_1", node1, node2, true);
path.add(node1, edge1);
assertEquals(node1, path.getRoot());
}
//TC-P_C04
@Test
public void circularPathTest() {
SingleGraph sg = new SingleGraph("single");
Path path = new Path();
sg.addNode("node1");
sg.addNode("node2");
sg.addNode("node3");
AbstractNode node1 = sg.getNode("node1");
AbstractNode node2 = sg.getNode("node2");
AbstractNode node3 = sg.getNode("node3");
AbstractEdge edge1 = new AbstractEdge("edge_1", node1, node2, true);
AbstractEdge edge2 = new AbstractEdge("edge_2", node2, node3, true);
AbstractEdge edge3 = new AbstractEdge("edge_3", node3, node1, true);
path.setRoot(node1);
path.add(node1, edge1);
path.add(node2, edge2);
path.add(node3, edge3);
List<Node> nodes = path.getNodePath();
int size = path.size();
Node lastNode = nodes.get(size-1);
assertEquals(path.getRoot(), lastNode);
}
//TC-P_C05
@Test
public void removeLoopsTest() {
SingleGraph sg = new SingleGraph("single");
Path path = new Path();
sg.addNode("node1");
sg.addNode("node2");
sg.addNode("node3");
AbstractNode node1 = sg.getNode("node1");
AbstractNode node2 = sg.getNode("node2");
AbstractNode node3 = sg.getNode("node3");
AbstractEdge edge1 = new AbstractEdge("edge_1", node1, node2, true);
AbstractEdge edge2 = new AbstractEdge("edge_2", node2, node3, true);
AbstractEdge edge3 = new AbstractEdge("edge_3", node3, node3, true);
path.setRoot(node1);
path.add(node1, edge1);
path.add(node2, edge2);
path.add(node3, edge3);
path.removeLoops();
assertFalse(path.contains(edge3));
}
@Test
public void setRootNotNullTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.setRoot(singleGraph.getNode("node1"));
assertEquals(path.getRoot(), singleGraph.getNode("root"));
}
@Test
public void containsTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
assertTrue(path.contains(singleGraph.getNode("root")));
}
//Test add method
// @Test
// public void addWithRootNull() {
// Path path = new Path();
// //path.setRoot(singleGraph.getNode("root"));
// path.add(null, singleGraph.getEdge("edge1"));
// //assertTrue(path.empty());
// Assert.fail();
// }
@Test
public void addNullWithRootNotNullTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(null, singleGraph.getEdge("edge1"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(2, nodes.size());
assertEquals(1, edges.size());
}
@Test
public void addNodePathSizeGreaterThanOneTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("node3"));
path.add(path.getRoot(), singleGraph.getEdge("edge3"));
assertEquals(2, path.getNodePath().size()); //nodePath greater than one.
//path.add(path.peekNode(), path.peekEdge());
path.add(path.peekNode(), singleGraph.getEdge("edge1"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(3, nodes.size());
assertEquals(2, edges.size());
}
@Test
public void addNodePathEqualToFromTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(path.getRoot(), singleGraph.getEdge("edge1"));
assertEquals(2, path.getNodePath().size()); //nodePath greater than one.
//path.add(path.peekNode(), path.peekEdge());
path.add(path.getRoot(), singleGraph.getEdge("edge2"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(2, nodes.size());
assertEquals(1, edges.size());
}
@Test
public void addReverseTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("node3"));
path.add(path.getRoot(), singleGraph.getEdge("edge3"));
assertEquals(2, path.getNodePath().size()); //nodePath greater than one.
//path.add(path.peekNode(), path.peekEdge());
path.add(path.getRoot(), singleGraph.getEdge("edge1"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(2, nodes.size());
assertEquals(1, edges.size());
}
@Test
public void addFromAsTargetTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(path.getRoot(), singleGraph.getEdge("edge1"));
assertEquals(2, path.getNodePath().size()); //nodePath greater than one.
singleGraph.addEdge("edge4", singleGraph.getNode("node1"), singleGraph.getNode("node1"));
path.add(path.peekNode(), singleGraph.getEdge("edge4"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(3, nodes.size());
assertEquals(2, edges.size());
}
// @Test
// public void addEdgeWithPathEmptyTest() {
// Path path = new Path();
// path.add(singleGraph.getEdge("edge1"));
// Collection<Node> nodes = path.getNodeSet();
// Collection<Edge> edges = path.getEdgeSet();
// assertEquals(2, nodes.size());
// assertEquals(1, edges.size());
// }
//Push methods test
@Test
public void pushEdge() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.push(singleGraph.getEdge("edge1"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(2, nodes.size());
assertEquals(1, edges.size());
}
@Test
public void pushEdgeAndNode() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.push(singleGraph.getNode("root"), singleGraph.getEdge("edge1"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(2, nodes.size());
assertEquals(1, edges.size());
}
//Pop methods test
@Test
public void popEdge() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(path.getRoot(), singleGraph.getEdge("edge1"));
path.add(singleGraph.getEdge("edge3"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(3, nodes.size());
assertEquals(2, edges.size());
path.popEdge();
assertEquals(2, nodes.size());
assertEquals(1, edges.size());
}
@Test
public void popNode() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(path.getRoot(), singleGraph.getEdge("edge1"));
path.add(singleGraph.getEdge("edge3"));
Collection<Node> nodes = path.getNodeSet();
Collection<Edge> edges = path.getEdgeSet();
assertEquals(3, nodes.size());
assertEquals(2, edges.size());
path.popNode();
assertEquals(2, nodes.size());
assertEquals(1, edges.size());
}
//Count test methods
@Test
public void getNodeCountTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(path.getRoot(), singleGraph.getEdge("edge1"));
path.add(singleGraph.getEdge("edge3"));
assertEquals(3, path.getNodeCount());
}
@Test
public void getNodeCountEmptyTest() {
Path path = new Path();
assertEquals(0, path.getNodeCount());
}
@Test
public void getEdgeCountTest() {
Path path = new Path();
path.setRoot(singleGraph.getNode("root"));
path.add(path.getRoot(), singleGraph.getEdge("edge1"));
path.add(singleGraph.getEdge("edge3"));
assertEquals(2, path.getEdgeCount());
}
@Test
public void getEdgeCountEmptyTest() {
Path path = new Path();
assertEquals(0, path.getEdgeCount());
}
}