package de.tudarmstadt.ukp.wikipedia.api; import static org.junit.Assert.*; import java.util.HashSet; import java.util.Set; import org.jgrapht.DirectedGraph; import org.jgrapht.graph.DefaultEdge; import org.junit.Assume; import org.junit.Test; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import de.tudarmstadt.ukp.wikipedia.api.exception.WikiApiException; import de.tudarmstadt.ukp.wikipedia.api.exception.WikiTitleParsingException; public class TestCycleHandler { @Test public void containsCycleTest() { Wikipedia wiki = Mockito.mock(Wikipedia.class); CategoryGraph categoryGraph = Mockito.mock(CategoryGraph.class); DirectedGraph dgraph = (DirectedGraph)Mockito.mock(DirectedGraph.class); Set emptyVertexSet = new HashSet(); Mockito.when(categoryGraph.getGraph()).thenReturn(dgraph); Mockito.when(dgraph.vertexSet()).thenReturn(emptyVertexSet); CycleHandler chandler = new CycleHandler(wiki, categoryGraph); try { assertFalse(chandler.containsCycle()); } catch(WikiApiException e) { Assume.assumeNoException(e); } Set vertexSet = new HashSet(); vertexSet.add(1); vertexSet.add(2); vertexSet.add(3); Mockito.when(dgraph.vertexSet()).thenReturn(vertexSet); Set defaultEdgesNode1 = new HashSet(); DefaultEdge defaultEdge1 = Mockito.mock(DefaultEdge.class); defaultEdgesNode1.add(defaultEdge1); Set defaultEdgesNode2 = new HashSet(); DefaultEdge defaultEdge2 = Mockito.mock(DefaultEdge.class); defaultEdgesNode1.add(defaultEdge2); Set defaultEdgesNode3 = new HashSet(); DefaultEdge defaultEdge3 = Mockito.mock(DefaultEdge.class); defaultEdgesNode1.add(defaultEdge3); Mockito.when(dgraph.outgoingEdgesOf(1)).thenReturn(defaultEdgesNode1); Mockito.when(dgraph.outgoingEdgesOf(2)).thenReturn(defaultEdgesNode2); Mockito.when(dgraph.outgoingEdgesOf(3)).thenReturn(defaultEdgesNode3); Mockito.when(dgraph.getEdgeTarget(defaultEdge1)).thenReturn(2); Mockito.when(dgraph.getEdgeTarget(defaultEdge2)).thenReturn(3); Mockito.when(dgraph.getEdgeTarget(defaultEdge3)).thenReturn(1); /// Category dummyCat = Mockito.mock(Category.class); Title dummyTitle = Mockito.mock(Title.class); Mockito.when(dummyTitle.toString()).thenReturn("title"); try { Mockito.when(dummyCat.getTitle()).thenReturn(dummyTitle); } catch (WikiTitleParsingException e1) { Assume.assumeNoException(e1); } Mockito.when(dgraph.getEdgeSource(Mockito.any(DefaultEdge.class))).thenReturn(1); //Mockito.when(dgraph.getEdgeTarget(Mockito.any(DefaultEdge.class))).thenReturn(null); Mockito.when(wiki.getCategory(Mockito.anyInt())).thenReturn(dummyCat); try { assertTrue(chandler.containsCycle()); } catch(WikiApiException e) { Assume.assumeNoException(e); } } @Test public void removeCyclesTest() { Wikipedia wiki = Mockito.mock(Wikipedia.class); CategoryGraph categoryGraph = Mockito.mock(CategoryGraph.class); final DirectedGraph dgraph = (DirectedGraph)Mockito.mock(DirectedGraph.class); Set emptyVertexSet = new HashSet(); Mockito.when(categoryGraph.getGraph()).thenReturn(dgraph); CycleHandler chandler = new CycleHandler(wiki, categoryGraph); final Set vertexSet = new HashSet(); vertexSet.add(1); vertexSet.add(2); vertexSet.add(3); Mockito.when(dgraph.vertexSet()).thenReturn(vertexSet); final Set defaultEdgesNode1 = new HashSet(); final DefaultEdge defaultEdge1 = Mockito.mock(DefaultEdge.class); defaultEdgesNode1.add(defaultEdge1); final Set defaultEdgesNode2 = new HashSet(); DefaultEdge defaultEdge2 = Mockito.mock(DefaultEdge.class); defaultEdgesNode1.add(defaultEdge2); final Set defaultEdgesNode3 = new HashSet(); DefaultEdge defaultEdge3 = Mockito.mock(DefaultEdge.class); defaultEdgesNode1.add(defaultEdge3); Mockito.when(dgraph.outgoingEdgesOf(1)).thenReturn(defaultEdgesNode1); Mockito.when(dgraph.outgoingEdgesOf(2)).thenReturn(defaultEdgesNode2); Mockito.when(dgraph.outgoingEdgesOf(3)).thenReturn(defaultEdgesNode3); Mockito.when(dgraph.getEdgeTarget(defaultEdge1)).thenReturn(2); Mockito.when(dgraph.getEdgeTarget(defaultEdge2)).thenReturn(3); Mockito.when(dgraph.getEdgeTarget(defaultEdge3)).thenReturn(1); /// Category dummyCat = Mockito.mock(Category.class); Title dummyTitle = Mockito.mock(Title.class); Mockito.when(dummyTitle.toString()).thenReturn("title"); try { Mockito.when(dummyCat.getTitle()).thenReturn(dummyTitle); } catch (WikiTitleParsingException e1) { Assume.assumeNoException(e1); } Mockito.when(dgraph.getEdgeSource(Mockito.any(DefaultEdge.class))).thenReturn(1); //Mockito.when(dgraph.getEdgeTarget(Mockito.any(DefaultEdge.class))).thenReturn(null); Mockito.when(wiki.getCategory(Mockito.anyInt())).thenReturn(dummyCat); Mockito.stub(dgraph.removeEdge(Mockito.any(DefaultEdge.class))).toAnswer(new Answer(){ @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { vertexSet.clear(); defaultEdgesNode3.clear(); defaultEdgesNode2.clear(); defaultEdgesNode1.clear(); return null; } }); try { chandler.removeCycles(); } catch(WikiApiException e) { Assume.assumeNoException(e); } } }