package de.tudarmstadt.ukp.wikipedia.api; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; import de.tudarmstadt.ukp.wikipedia.api.WikiConstants.Language; import de.tudarmstadt.ukp.wikipedia.api.exception.WikiApiException; public class TestPageIterator { private static DatabaseConfiguration dbConfig; @BeforeClass public static void _setup() { /*dbConfig = new DatabaseConfiguration(); dbConfig.setDatabase("wikiapi_test"); dbConfig.setHost("bender.ukp.informatik.tu-darmstadt.de"); dbConfig.setUser("student"); dbConfig.setPassword("student"); dbConfig.setLanguage(Language._test);*/ dbConfig = Mockito.mock(DatabaseConfiguration.class); Mockito.when(dbConfig.getLanguage()).thenReturn(Language._test); Mockito.when(dbConfig.getHost()).thenReturn("bender.ukp.informatik.tu-darmstadt.de"); Mockito.when(dbConfig.getDatabase()).thenReturn("wikiapi_test"); Mockito.when(dbConfig.getUser()).thenReturn("student"); Mockito.when(dbConfig.getPassword()).thenReturn("student"); } @Test public void fittestHasNextTest() { Wikipedia wiki = Mockito.mock(Wikipedia.class); Page pageMock = Mockito.mock(Page.class); List pages = new ArrayList(); Mockito.when(wiki.getPages()).thenReturn(pages); Iterator pageIter = wiki.getPages().iterator(); assertFalse(pageIter.hasNext()); Set ids = new HashSet(); ids.add("1"); Set titles = new HashSet(); titles.add("page 1"); boolean exception = false; try { Mockito.when(wiki.getPage(1)).thenReturn(pageMock); } catch (WikiApiException e) { exception = true; } assertFalse(exception); PageIterator it = new PageIterator(wiki, ids, titles, 500); int counter = 0; while(it.hasNext()) { it.next(); counter++; } assertEquals(2, counter); } @Test public void fittestRemoveTest() { Wikipedia wiki = Mockito.mock(Wikipedia.class); Page pageMock = Mockito.mock(Page.class); Set ids = new HashSet(); ids.add("1"); Set titles = new HashSet(); titles.add("page 1"); boolean exception = false; try { Mockito.when(wiki.getPage(1)).thenReturn(pageMock); } catch (WikiApiException e) { exception = true; } assertFalse(exception); PageIterator it = new PageIterator(wiki, ids, titles, 500); assertTrue(it.hasNext()); exception = false; try { it.remove(); } catch(UnsupportedOperationException e) { exception = true; } assertTrue(exception); } }