SingleGraphTest.java

package org.graphstream.graph.implementations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.stream.AttributeSink;
import org.graphstream.stream.ElementSink;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SingleGraphTest {

	/* Expert - Virgi, Torre, Ciani - 8/04/2014 */

	//----------------------------------------------------------------------
	
	/* We test that a graph after flush is really empty but with listeners */
	@Test
	public void clearTest() {
		/* Adding some sinks. They should be still there after clear */
		singleGraph.addElementSink(new ElementSink() {

			@Override
			public void stepBegins(String sourceId, long timeId, double step) {
				// TODO Auto-generated method stub

			}

			@Override
			public void nodeRemoved(String sourceId, long timeId, String nodeId) {
				// TODO Auto-generated method stub

			}

			@Override
			public void nodeAdded(String sourceId, long timeId, String nodeId) {
				// TODO Auto-generated method stub

			}

			@Override
			public void graphCleared(String sourceId, long timeId) {
				// TODO Auto-generated method stub

			}

			@Override
			public void edgeRemoved(String sourceId, long timeId, String edgeId) {
				// TODO Auto-generated method stub

			}

			@Override
			public void edgeAdded(String sourceId, long timeId, String edgeId,
					String fromNodeId, String toNodeId, boolean directed) {
				// TODO Auto-generated method stub

			}
		});
		singleGraph.addAttributeSink(new AttributeSink() {

			@Override
			public void nodeAttributeRemoved(String sourceId, long timeId,
					String nodeId, String attribute) {
				// TODO Auto-generated method stub

			}

			@Override
			public void nodeAttributeChanged(String sourceId, long timeId,
					String nodeId, String attribute, Object oldValue,
					Object newValue) {
				// TODO Auto-generated method stub

			}

			@Override
			public void nodeAttributeAdded(String sourceId, long timeId,
					String nodeId, String attribute, Object value) {
				// TODO Auto-generated method stub

			}

			@Override
			public void graphAttributeRemoved(String sourceId, long timeId,
					String attribute) {
				// TODO Auto-generated method stub

			}

			@Override
			public void graphAttributeChanged(String sourceId, long timeId,
					String attribute, Object oldValue, Object newValue) {
				// TODO Auto-generated method stub

			}

			@Override
			public void graphAttributeAdded(String sourceId, long timeId,
					String attribute, Object value) {
				// TODO Auto-generated method stub

			}

			@Override
			public void edgeAttributeRemoved(String sourceId, long timeId,
					String edgeId, String attribute) {
				// TODO Auto-generated method stub

			}

			@Override
			public void edgeAttributeChanged(String sourceId, long timeId,
					String edgeId, String attribute, Object oldValue,
					Object newValue) {
				// TODO Auto-generated method stub

			}

			@Override
			public void edgeAttributeAdded(String sourceId, long timeId,
					String edgeId, String attribute, Object value) {
				// TODO Auto-generated method stub

			}
		});

		singleGraph.clear();
		assertEquals(singleGraph.getEdgeCount(), 0);
		assertEquals(singleGraph.getNodeCount(), 0);

		assertTrue(singleGraph.attributeSinks().iterator().hasNext());
		assertTrue(singleGraph.elementSinks().iterator().hasNext());

	}


	/*The specification of the method getEdgeSet() claims that the set we get can only be read, not modified.*/
	@Test(expected=UnsupportedOperationException.class)
	public void getEdgeSetAddTest() {
		Collection<Edge> edges = singleGraph.getEdgeSet();
		assertEquals(2, edges.size());
		
		Node node1 = singleGraph.addNode("node1");
		Edge edge1 = singleGraph.addEdge("edge1", singleGraph.getNode("leftNode"), node1);
		edges.add(edge1);
	}
	
	@Test//(expected=UnsupportedOperationException.class)
	public void getEdgeSetRemoveTest() {
		Collection<Edge> edges = singleGraph.getEdgeSet();
		assertEquals(2, edges.size());
		
		System.out.println(edges.getClass());
		
		//edges.remove(singleGraph.getEdge("root-left"));
		
		edges.clear();
		
		assertEquals(2, singleGraph.getEdgeCount());

		assertEquals(2, edges.size());
	}
	
	/*The specification of the method getNodeSet() claims that the set we get can only be read, not modified.*/
	@Test(expected=UnsupportedOperationException.class)
	public void getNodeSetAddTest() {
		Collection<Node> nodes = singleGraph.getNodeSet();
		assertEquals(3, nodes.size());
		
		Node node1 = singleGraph.addNode("node1");
		nodes.add(node1);
	}
	
	@Test
	public void getNodeSetRemoveTest() {
		Collection<Node> nodes = singleGraph.getNodeSet();
		assertEquals(3, nodes.size());
		
		nodes.remove(singleGraph.getNode("leftNode"));
		
		assertEquals(3, singleGraph.getNodeCount());

		assertEquals(3, nodes.size());
	}
	
	/* Testing methods regarding attributes*/
	@Test
	public void addAttributeNoValueTest() {
		singleGraph.addAttribute("directed"); //The value stored according to the specification should be "true".
		
		assertEquals(true, singleGraph.getAttribute("directed"));
	}
	
	@Test
	public void addAttributeTest() {
		singleGraph.addAttribute("directed", 1);
		assertEquals(1, singleGraph.getAttribute("directed"));
	}
	
	@Test
	public void addAttributeValuesTest() {
		singleGraph.addAttribute("directed", 1, "yes", true); //an array should be stored.
		Object[] attributes = singleGraph.getAttribute("directed");
		
		assertEquals(3, attributes.length);
	}
	
	@Test
	public void getArrayManyValuesTest() {
		singleGraph.addAttribute("directed", 1, "yes", true);
		assertTrue(singleGraph.getArray("directed") instanceof Object[]);
		assertEquals(3, singleGraph.getArray("directed").length);
	}
	
	@Test
	public void getArrayOneValueTest() {
		singleGraph.addAttribute("directed", 1);
		assertFalse(singleGraph.getArray("directed") instanceof Object[]);
		assertNull(singleGraph.getArray("directed"));
	}
	
	@Test
	public void hasVectorTrueTest() {
		//singleGraph.addAttribute("directed", new Integer(1), new Integer(2), new Integer(3));
		ArrayList<Number> attributes = new ArrayList<Number>();
		attributes.add(new Integer(1));
		attributes.add(new Integer(2));
		attributes.add(new Integer(3));

		singleGraph.addAttribute("directed", attributes);
		assertTrue(singleGraph.hasVector("directed"));
	}
	
	@Test
	public void hasVectorFalseTest() {
		singleGraph.addAttribute("directed", new Integer(1), new Integer(2), new Integer(3));
		assertFalse(singleGraph.hasVector("directed"));
	}
	
	@Test
	public void hasNumberTestTrue() {
		singleGraph.addAttribute("directed", new Integer(1));
		assertTrue(singleGraph.hasNumber("directed"));
	}
	
	@Test
	public void hasNumberTestFalse() {
		singleGraph.addAttribute("directed", true);
		assertFalse(singleGraph.hasNumber("directed"));
	}
	
	@Test
	public void hasLabelTrueTest() {
		singleGraph.addAttribute("directed", "yes");
		assertTrue(singleGraph.hasLabel("directed"));
	}
	
	@Test
	public void removeAttributeTest() {
		singleGraph.addAttribute("directed", "yes");
		singleGraph.addAttribute("cycles", "yes", 2);
		
		assertEquals(2, singleGraph.getAttributeCount());
		singleGraph.removeAttribute("cycles");
		assertEquals(1, singleGraph.getAttributeCount());
		assertNull(singleGraph.getArray("cycles"));
	}
	
	@Test
	public void clearAttributesTest() {
		singleGraph.addAttribute("directed", "yes");
		singleGraph.addAttribute("cycles", "yes", 2);
		
		assertEquals(2, singleGraph.getAttributeCount());
		singleGraph.clearAttributes();
		assertEquals(0, singleGraph.getAttributeCount());
	}
	
	@Test
	public void setAttributeTest() {
		singleGraph.addAttribute("directed", "yes");
		singleGraph.addAttribute("cycles", "yes", 2);
		
		singleGraph.setAttribute("directed", "no");
		assertEquals("no", singleGraph.getAttribute("directed"));
	}
	
	@Test
	public void changeAttributesTest() {
		singleGraph.addAttribute("directed", "yes");
		singleGraph.addAttribute("cycles", "yes", 2);
		
		singleGraph.changeAttribute("directed", "no");
		assertEquals("no", singleGraph.getAttribute("directed"));
	}
	
	@Test
	public void getEdgeByIndexTest() {
		assertEquals(singleGraph.getEdge("root-left"), singleGraph.getEdge(0));
	}
	
	@Test(expected=IndexOutOfBoundsException.class)
	public void getEdgeByIndexLessThanZeroTest() {
		singleGraph.getEdge(-1);
	}
	
	@Test(expected=IndexOutOfBoundsException.class)
	public void getEdgeByIndexGreaterThanEdgeCountTest() {
		singleGraph.getEdge(singleGraph.getEdgeCount()+1);
	}
	
	@Test
	public void getNodeByIndexTest() {
		assertEquals(singleGraph.getNode("root"), singleGraph.getNode(0));
	}
	
	@Test(expected=IndexOutOfBoundsException.class)
	public void getNodeByIndexLessThanZeroTest() {
		singleGraph.getNode(-1);
	}
	
	@Test(expected=IndexOutOfBoundsException.class)
	public void getNodeByIndexGreaterThanNodeCountTest() {
		singleGraph.getNode(singleGraph.getNodeCount()+1);
	}
	
	/*Iterators test*/
	
	@Test(expected=NoSuchElementException.class)
	public void getEdgeIteratorNoSuchElementTest() {
		Iterator<Edge> iterator = singleGraph.getEdgeIterator();
		iterator.next();
		iterator.next();
		iterator.next();
		
		Assert.fail();
	}
	
	@Test(expected=IllegalStateException.class)
	public void getEdgeIteratorIllegalStateTest() {
		Iterator<Edge> iterator = singleGraph.getEdgeIterator();
		iterator.remove();
		iterator.remove();
		iterator.remove();
		
		Assert.fail();
	}
	
	@Test(expected=NoSuchElementException.class)
	public void getNodeIteratorNoSuchElementTest() {
		Iterator<Node> iterator = singleGraph.getNodeIterator();
		iterator.next();
		iterator.next();
		iterator.next();
		iterator.next();
		
		Assert.fail();
	}
	
	@Test(expected=IllegalStateException.class)
	public void getNodeIteratorIllegalStateTest() {
		Iterator<Node> iterator = singleGraph.getNodeIterator();
		iterator.remove();
		iterator.remove();
		iterator.remove();
		
		Assert.fail();
	}
	
	/* -------- */

	private Graph singleGraph;

	@Before
	public void testSetup() {
		singleGraph = new SingleGraph("single-graph");
		Node root = singleGraph.addNode("root");
		Node leftNode = singleGraph.addNode("leftNode");
		Node rightNode = singleGraph.addNode("rightNode");
		Edge RL = singleGraph.addEdge("root-left", "root", "leftNode");
		Edge RR = singleGraph.addEdge("root-right", "root", "rightNode");
	}
	
	//----------------------------------------------------------------------

	// TC-SG_C01
	@Test
	public void EdgeAditionTest() {
		Node root = singleGraph.getNode("root");
		Edge RL = singleGraph.getEdge("root-left");
		Node leftNode = singleGraph.getNode("leftNode");

		assertEquals(2, singleGraph.getEdgeCount());

		// id returns the correct node
		assertEquals(root, singleGraph.getNode("root"));

		// is undirected graph
		assertFalse(RL.isDirected());

		// left and right node of the edge RL
		assertEquals(root, RL.getNode0());
		assertEquals(leftNode, RL.getNode1());

		// source and target of the edge RL
		assertEquals(root, RL.getSourceNode());
		assertEquals(leftNode, RL.getTargetNode());
	}

	// TC-SG_C02
	@Test
	public void EdgeRemovalTest() {
		Edge RL = singleGraph.addEdge("right-left", "leftNode", "rightNode");
		assertEquals(3, singleGraph.getEdgeCount());
		singleGraph.removeEdge("right-left");

		assertEquals(2, singleGraph.getEdgeCount());
	}

	// TC-SG_C03
	@Test
	public void AddNodeTest() {

		assertEquals(singleGraph.getNodeCount(), 3);
	}

	// TC-SG_C04
	@Test
	public void RemoveNodeTest() {

		singleGraph.removeNode("root");

		assertEquals(2, singleGraph.getNodeCount());
		assertEquals(0, singleGraph.getEdgeCount());

	}

	// TC-SG_C05
	@Test
	public void SingleGraphBasicsTest() {
		assertNotNull(singleGraph);
		assertEquals("single-graph", singleGraph.getId());
	}

	// TC-SG_C06
	@Test
	public void SingleGraphNodesTest() {

		Node n1 = singleGraph.getNode("leftNode");
		Node n2 = singleGraph.getNode("rightNode");

		assertNotNull(n1);
		assertNotNull(n2);

		assertEquals("leftNode", n1.getId());
		assertEquals("rightNode", n2.getId());
	}

	// TC-SG_C07
	@Test
	public void SingleGraphEdgesBasicsTest() {
		Edge e1 = singleGraph.getEdge("root-left");

		assertNotNull(e1);
		assertEquals("root-left", e1.getId());

	}

	// TC-SG_C08
	@Test
	public void SingleGraphEdges1Test() {
		Node n1 = singleGraph.getNode("leftNode");
		Node n2 = singleGraph.getNode("root");
		Edge e1 = singleGraph.getEdge("root-left");

		assertEquals(n1.getEdgeBetween(n2), e1);
		assertEquals(n2.getEdgeBetween(n1), e1);
		assertEquals(n1.getEdgeBetween(n2), n2.getEdgeBetween(n1));
	}

	// TC-SG_C09
	@Test
	public void SingleGraphEmptyTest() {
		SingleGraph sg = new SingleGraph("empty", false, false, 0, 0);
		assertEquals(0, sg.getNodeCount());
		assertEquals(0, sg.getEdgeCount());
	}

	// TC-SG_C10
	@Test
	public void SingleGraphEdgeSourceAndTargetTest() {
		SingleGraph sg = new SingleGraph("first", false, false, 1, 0);
		sg.addNode("node_1");
		sg.addNode("node_2");
		AbstractNode node1 = sg.getNode("node_1");
		AbstractNode node2 = sg.getNode("node_2");

		AbstractEdge edge = new AbstractEdge("edge_1", node1, node2, true);

		assertEquals(node1, edge.getSourceNode());
		assertEquals(node2, edge.getTargetNode());
	}

	// TC-SG_C11
	@Test
	public void SingleGraphLoopTest() {
		AbstractNode node1 = singleGraph.getNode("leftNode");

		singleGraph.addEdge("edgeLoop", node1, node1, true);
		AbstractEdge edge = singleGraph.getEdge("edgeLoop");

		assertEquals(node1, edge.getTargetNode());
		singleGraph.removeEdge("edgeLoop"); // This line is needed in order to
											// not break the test
											// "testEdgeRemoval"
	}

	// TC-SG_C12
	@Test
	public void SingleGraphNodeDegreeTest() {
		AbstractNode root = singleGraph.getNode("root");
		assertEquals(2, root.getDegree());
	}

	// TC-SG_C13
	@Test
	public void singleGraphNotMultiTestTest() {
		SingleGraph sg = new SingleGraph("first", false, false, 3, 2);
		sg.addNode("node_1");
		sg.addNode("node_2");

		AbstractNode node1 = sg.getNode("node_1");
		AbstractNode node2 = sg.getNode("node_2");

		sg.addEdge("edge_1", node1, node2, false);
		sg.addEdge("edge_2", node2, node1, false);

		AbstractEdge edge2 = sg.getEdge("edge_2");

		assertFalse(node1.getDegree() == 2);
		assertNull(edge2);
	}
}