CategoryPartitionAddEdgeSingleGraph.java

package org.graphstream.graph.implementations;

import static org.junit.Assert.*;

import java.util.Random;

import org.databene.benerator.anno.Source;
import org.databene.feed4junit.Feeder;
import org.graphstream.graph.IdAlreadyInUseException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;


@RunWith(Feeder.class)
public class CategoryPartitionAddEdgeSingleGraph {

	//idExistance, idValue, graphNrNodes, graphStrictChecking, graphNrEdges,
	//graphAutoCreate, sourceExistance, sourceEqTarget, sourceDegree
	//sourceConnectedTarget, targetExistance
	@Rule
	public ExpectedException exception = ExpectedException.none();

	@Test
	@Source("csvFiles/CategoryPartitionAddEdgeSingleGraph.csv")
	public void testAddEdge(Boolean idExistance, String idValue, String graphNrNodes,
			Boolean graphStrictChecking, String graphNrEdges, Boolean graphAutoCreate,
			Boolean sourceEx, Boolean sourceEqTarget, String sourceDegree, Boolean sourceConnectedTarget,
			Boolean targetExistence) {
		// Creating existence graph ////////////////////////////////////////////////////////////////
		// creating the graph
		SingleGraph singleGraph = new SingleGraph("singleGraph", graphStrictChecking, graphAutoCreate);

		// creating n nodes
		int nrNodes = 0;
		if (graphNrNodes.equals("many"))
			nrNodes = new Random().nextInt(100)+1;
		for (int i = 0; i < nrNodes; i++) {
			singleGraph.addNode("Node_" + i);
		}

		// creaning n-1 edges
		int nrEdges = 0;
		if (graphNrEdges.equals("many"))
			nrNodes = new Random().nextInt(nrNodes) + 1;
		for (int i = 0; i < nrEdges; i++) {
			singleGraph.addEdge("Edge_"+i, "id_"+i, "id_"+(i+1));
		}

		////////////////////////////////////////////////////////////////
		/// generate id //
		String id = null;
		switch (idValue) {
		case "correct":
			id = "myEdge";
			break;
		case "malformed":
			id = "#$%$%^%&";
			break;
		case "":
			id = "";
			break;
		case "null":
			id = null;
			break;
		default:
			break;
		}

		/*
		 * create an edge with given id to control the id value existence
		 * if strict checking is true it will raise IdAlreadynUseException
		 * otherwise it will remove the existence edge and add new edge
		 * changing the target and source node
		 */
		if (graphStrictChecking)
			exception.expect(IdAlreadyInUseException.class);
		if (idExistance && nrNodes > 1) {
			singleGraph.addEdge(id, "Node_1", "Node_"+(nrNodes-1));
			singleGraph.addEdge(id, "Node_2", "Node_"+(nrNodes-2));	
			if (!graphStrictChecking) // control if source and target nodes are changed to the new one
				assertEquals("Node_2", singleGraph.getEdge(id).getSourceNode().getId());
				assertEquals("Node_"+(nrNodes-2), singleGraph.getEdge(id).getTargetNode().getId());

		}
		singleGraph.removeEdge(id); // removing the edge added to the graph
		
		/*
		 * Add an edge where the source node is already exist 
		 */
		if (sourceEx) {
			singleGraph.addEdge(id, "Node_1", "Node_"+(nrNodes-1));
		}

		
	}






}