package de.tudarmstadt.ukp.wikipedia.api; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.hibernate.Query; import org.hibernate.Session; import org.jgrapht.DirectedGraph; import org.jgrapht.graph.DefaultEdge; import org.junit.Assume; import org.junit.Test; import org.mockito.Mockito; import de.tudarmstadt.ukp.wikipedia.api.exception.WikiApiException; public class TestWikipediaInfo { @Test public void fittestGetAverageFanOutTest() { Wikipedia wiki = Mockito.mock(Wikipedia.class); Session session = Mockito.mock(Session.class); org.hibernate.Transaction transaction = Mockito.mock(org.hibernate.Transaction.class); Query query = Mockito.mock(Query.class); Object[] values = new Object[2]; values[0] = new HashSet(); values[1] = new Integer(1); java.util.List list = new ArrayList(); List pages = new ArrayList(); Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(query); Mockito.when(session.getTransaction()).thenReturn(transaction); Mockito.when(query.list()).thenReturn(list); Mockito.when(wiki.__getHibernateSession()).thenReturn(session); Mockito.when(wiki.getPages()).thenReturn(pages); boolean exception = false; try { WikipediaInfo info = new WikipediaInfo(wiki); assertEquals(0.0, info.getAverageFanOut(), 0); /*Page p = Mockito.mock(Page.class); Mockito.when(p.getPageId()).thenReturn(1); ((HashSet)values[0]).add("1"); ((HashSet)values[0]).add("2"); pages.add(p); info = new WikipediaInfo(wiki); assertEquals(2, info.getAverageFanOut(), 0);*/ } catch (WikiApiException e) { e.printStackTrace(); exception = true; } assertFalse(exception); } @Test public void fittestGetAveragePathLengthFromRootTest() { try { List pages = new ArrayList(); Wikipedia wiki = Mockito.mock(Wikipedia.class); Mockito.when(wiki.getPages()).thenReturn(pages); MetaData data = Mockito.mock(MetaData.class); Category cat = Mockito.mock(Category.class); Mockito.when(cat.getPageId()).thenReturn(1); Mockito.when(data.getMainCategory()).thenReturn(cat); Mockito.when(wiki.getMetaData()).thenReturn(data); CategoryGraph catGraph = Mockito.mock(CategoryGraph.class); Set neighbours = new HashSet(); neighbours.add(2); Mockito.when(catGraph.getNeighbors(1)).thenReturn(neighbours); Mockito.when(catGraph.getNeighbors(2)).thenReturn(new HashSet()); DirectedGraph dg = (DirectedGraph)Mockito.mock(DirectedGraph.class); Set vertexSet = new HashSet(); //add 2 to avoid division by zero exception vertexSet.add(1); vertexSet.add(2); Mockito.when(dg.vertexSet()).thenReturn(vertexSet); Mockito.when(catGraph.getGraph()).thenReturn(dg); WikipediaInfo info = new WikipediaInfo(wiki); double d = info.getAveragePathLengthFromRoot(wiki, catGraph); assertEquals(1.0, d, 0); } catch (WikiApiException e) { Assume.assumeNoException(e); } } }