Some refactorings of GlobalConfig.

FetcherRegistryImpl now persists selected fetchers.

git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@98 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
angryziber 2007-01-01 22:21:59 +00:00
parent e46924df60
commit 25ccf1f29f

View File

@ -6,29 +6,40 @@ package net.azib.ipscan.fetchers;
import static org.junit.Assert.*;
import java.util.Iterator;
import java.util.prefs.Preferences;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author Anton Keks
*/
public class FetcherRegistryImplTest {
private Preferences preferences;
private Fetcher ipFetcher;
private HostnameFetcher hostnameFetcher;
private CommentFetcher commentFetcher;
private FetcherRegistry fetcherRegistry;
@Before
public void setUp() {
public void setUp() throws Exception {
preferences = Preferences.userRoot().node("ipscan-test");
preferences.clear();
ipFetcher = new IPFetcher();
hostnameFetcher = new HostnameFetcher();
commentFetcher = new CommentFetcher();
fetcherRegistry = new FetcherRegistryImpl(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher});
fetcherRegistry = new FetcherRegistryImpl(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences);
}
@After
public void tearDown() throws Exception {
preferences.removeNode();
}
@Test
public void testCreate() throws Exception {
assertEquals(3, fetcherRegistry.getRegisteredFetchers().size());
@ -48,6 +59,20 @@ public class FetcherRegistryImplTest {
assertEquals(-1, fetcherRegistry.getSelectedFetcherIndex("blah-blah"));
}
@Test
public void testLoadPreferences() throws Exception {
preferences.remove(FetcherRegistryImpl.PREFERENCE_SELECTED_FETCHERS);
fetcherRegistry = new FetcherRegistryImpl(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences);
assertEquals(3, fetcherRegistry.getSelectedFetchers().size());
preferences.put(FetcherRegistryImpl.PREFERENCE_SELECTED_FETCHERS, hostnameFetcher.getLabel() + "###" + commentFetcher.getLabel());
fetcherRegistry = new FetcherRegistryImpl(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences);
assertEquals(2, fetcherRegistry.getSelectedFetchers().size());
Iterator iterator = fetcherRegistry.getSelectedFetchers().iterator();
assertSame(hostnameFetcher, iterator.next());
assertSame(commentFetcher, iterator.next());
}
@Test
public void testUpdateSelectedFetchers() throws Exception {
// retain only one selected fetcher
@ -55,6 +80,7 @@ public class FetcherRegistryImplTest {
assertEquals(1, fetcherRegistry.getSelectedFetchers().size());
Iterator iterator = fetcherRegistry.getSelectedFetchers().iterator();
assertEquals(ipFetcher.getLabel(), ((Fetcher)iterator.next()).getLabel());
assertEquals(ipFetcher.getLabel(), preferences.get(FetcherRegistryImpl.PREFERENCE_SELECTED_FETCHERS, null));
// now return a fetcher back
fetcherRegistry.updateSelectedFetchers(new String[] {commentFetcher.getLabel(), ipFetcher.getLabel()});
@ -62,6 +88,7 @@ public class FetcherRegistryImplTest {
iterator = fetcherRegistry.getSelectedFetchers().iterator();
assertEquals(commentFetcher.getLabel(), ((Fetcher)iterator.next()).getLabel());
assertEquals(ipFetcher.getLabel(), ((Fetcher)iterator.next()).getLabel());
assertEquals(commentFetcher.getLabel() + "###" + ipFetcher.getLabel(), preferences.get(FetcherRegistryImpl.PREFERENCE_SELECTED_FETCHERS, null));
}
@Test