ScannerTest introduced, aborting of scanning any Fetcher is now possible, n/a and n/s are now shown

git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/ipscan@44 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
angryziber 2006-11-05 00:27:23 +00:00
parent 4a10d852f8
commit 98b855ed48
8 changed files with 145 additions and 23 deletions

View File

@ -126,6 +126,9 @@ fetcher.ping.ttl=TTL
fetcher.hostname=Hostname
fetcher.ports=Ports
fetcher.ports.filtered=Filtered Ports
fetcher.value.ms= ms
fetcher.value.nothing=[n/a]
fetcher.value.aborted=[n/s]
options.threads=Threads
options.threads.delay=Delay between starting threads (in ms):
options.threads.maxThreads=Maximum number of threads:

View File

@ -7,6 +7,7 @@ import net.azib.ipscan.core.Scanner;
import net.azib.ipscan.core.ScannerThreadFactory;
import net.azib.ipscan.core.ScanningResultList;
import net.azib.ipscan.fetchers.FetcherRegistry;
import net.azib.ipscan.fetchers.FetcherRegistryImpl;
import net.azib.ipscan.gui.MainMenu;
import net.azib.ipscan.gui.MainWindow;
import net.azib.ipscan.gui.ResultTable;
@ -50,7 +51,7 @@ public class GUIComponentContainer {
ComponentParameter anyComponentParameter = new ComponentParameter();
// non-GUI
container.registerComponentImplementation(FetcherRegistry.class);
container.registerComponentImplementation(FetcherRegistry.class, FetcherRegistryImpl.class);
container.registerComponentImplementation(ScanningResultList.class);
container.registerComponentImplementation(Scanner.class);
container.registerComponentImplementation(ScannerThreadFactory.class);

View File

@ -6,10 +6,9 @@ package net.azib.ipscan.core;
import java.net.InetAddress;
import java.util.Iterator;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;
import net.azib.ipscan.fetchers.PingFetcher;
/**
* Scanner functionality is encapsulated in this class.
@ -37,21 +36,18 @@ public class Scanner {
ScanningSubject scanningSubject = new ScanningSubject(address);
// populate results
boolean continueScanning = true;
int fetcherIndex = 0;
for (Iterator i = fetcherRegistry.getRegisteredFetchers().iterator(); i.hasNext();) {
Fetcher fetcher = (Fetcher) i.next();
if (continueScanning) {
if (!scanningSubject.isScanningAborted()) {
String value = fetcher.scan(scanningSubject);
// TODO: write better code
if (!Config.getGlobal().scanDeadHosts && fetcher instanceof PingFetcher) {
continueScanning = value != null;
// TODO: hardcoded [timeout]
//value = value == null ? "[timeout]" : value;
}
if (value == null)
value = Labels.getInstance().getString("fetcher.value.nothing");
result.setValue(fetcherIndex, value);
}
// TODO: display something in the else
else {
result.setValue(fetcherIndex, Labels.getInstance().getString("fetcher.value.aborted"));
}
fetcherIndex++;
}

View File

@ -30,6 +30,8 @@ public class ScanningSubject {
private Map parameters;
/** The result type constant value, can be modified by some Fetchers */
private int resultType = RESULT_TYPE_UNKNOWN;
/** Whether we need to continue scanning or it can be aborted */
private boolean isScanningAborted = false;
/**
* This constructor should only be used by the Scanner class or unit tests.
@ -80,5 +82,19 @@ public class ScanningSubject {
public void setResultType(int resultType) {
this.resultType = resultType;
}
/**
* @return true if a fetcher has instructed to abort scanning
*/
public boolean isScanningAborted() {
return isScanningAborted;
}
/**
* Can be used to inform the scanner to abort scanning
*/
public void abortScanning() {
this.isScanningAborted = true;
}
}

View File

@ -13,7 +13,7 @@ import java.util.List;
*
* @author anton
*/
public class FetcherRegistry {
public class FetcherRegistryImpl implements FetcherRegistry {
/** All available Fetcher implementations, List of Fetcher instances */
private List fetchers;
@ -21,7 +21,7 @@ public class FetcherRegistry {
/**
* Private constructor
*/
public FetcherRegistry() {
public FetcherRegistryImpl() {
fetchers = new ArrayList();
fetchers.add(new IPFetcher());
fetchers.add(new PingFetcher());
@ -32,17 +32,15 @@ public class FetcherRegistry {
fetchers = Collections.unmodifiableList(fetchers);
}
/**
* @return a List of all registered Fetchers
/* (non-Javadoc)
* @see net.azib.ipscan.fetchers.FetcherRegistry#getRegisteredFetchers()
*/
public List getRegisteredFetchers() {
return fetchers;
}
/**
* Searches for selected fetcher with the given label
* @param label
* @return the index, if found, or -1
/* (non-Javadoc)
* @see net.azib.ipscan.fetchers.FetcherRegistry#getSelectedFetcherIndex(java.lang.String)
*/
public int getSelectedFetcherIndex(String label) {
// TODO: this probably needs to be changed to reflect selected fetchers and be more effective

View File

@ -9,6 +9,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.net.Pinger;
@ -68,7 +69,13 @@ public class PingFetcher implements Fetcher {
Pinger pinger = executePing(subject);
boolean isAlive = pinger != null && !pinger.isTimeout();
subject.setResultType(isAlive ? ScanningSubject.RESULT_TYPE_ALIVE : ScanningSubject.RESULT_TYPE_DEAD);
return isAlive ? Integer.toString(pinger.getAverageTime()) + " ms" : null;
if (!isAlive && !Config.getGlobal().scanDeadHosts) {
// the host is dead, we are not going to continue...
subject.abortScanning();
}
return isAlive ? Integer.toString(pinger.getAverageTime()) + Labels.getInstance().getString("fetcher.value.ms") : null;
}
}

View File

@ -0,0 +1,101 @@
/**
*
*/
package net.azib.ipscan.core;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;
/**
* ScannerTest
*
* @author anton
*/
public class ScannerTest extends TestCase {
protected void setUp() throws Exception {
Config.initialize();
}
public void test() throws Exception {
// initialize with fake fetchers
Scanner scanner = new Scanner(new FakeFetcherRegistry());
// scan the local host
ScanningResult scanningResult = new ScanningResult(InetAddress.getLocalHost());
scanner.scan(InetAddress.getLocalHost(), scanningResult);
assertEquals(ScanningSubject.RESULT_TYPE_ALIVE, scanningResult.getType());
assertEquals(InetAddress.getLocalHost(), scanningResult.getAddress());
assertEquals(4, scanningResult.getValues().size());
assertEquals("blah", scanningResult.getValues().get(0));
assertEquals(Labels.getInstance().getString("fetcher.value.nothing"), scanningResult.getValues().get(1));
assertEquals("666 ms", scanningResult.getValues().get(2));
assertEquals(Labels.getInstance().getString("fetcher.value.aborted"), scanningResult.getValues().get(3));
}
private class FakeFetcher implements Fetcher {
public String getLabel() {
return null;
}
public String scan(ScanningSubject subject) {
try {
// check that the IP is correct
assertEquals(InetAddress.getLocalHost(), subject.getIPAddress());
// set the result type to check after scanning
subject.setResultType(ScanningSubject.RESULT_TYPE_ALIVE);
// try to set parameter here and read from another Fetcher
subject.setParameter("megaParam", new Long(211082));
}
catch (UnknownHostException e) {
fail();
}
return "blah";
}
}
private class AnotherFakeFetcher extends FakeFetcher {
public String scan(ScanningSubject subject) {
// the parameter was set by FakeFetcher
assertEquals(new Long(211082), subject.getParameter("megaParam"));
// try null as a return value
return null;
}
}
private class AbortingFetcher extends FakeFetcher {
public String scan(ScanningSubject subject) {
subject.abortScanning();
return "666 ms";
}
}
private class FailingFetcher extends FakeFetcher {
public String scan(ScanningSubject subject) {
fail("This fetcher should not be reached");
return null;
}
}
private class FakeFetcherRegistry implements FetcherRegistry {
public List getRegisteredFetchers() {
return Arrays.asList(new Fetcher[] {new FakeFetcher(), new AnotherFakeFetcher(), new AbortingFetcher(), new FailingFetcher()});
}
public int getSelectedFetcherIndex(String label) {
return 0;
}
}
}

View File

@ -5,7 +5,7 @@ package net.azib.ipscan.gui.actions;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.fetchers.FetcherRegistry;
import net.azib.ipscan.fetchers.FetcherRegistryImpl;
import net.azib.ipscan.gui.UserErrorException;
import junit.framework.TestCase;
@ -18,7 +18,7 @@ public class OpenerLauncherTest extends TestCase {
public void testReplaceValues() {
Config.initialize();
OpenerLauncher ol = new OpenerLauncher(new FetcherRegistry(), null) {
OpenerLauncher ol = new OpenerLauncher(new FetcherRegistryImpl(), null) {
String getScannedValue(int selectedItem, int fetcherIndex) {
switch (fetcherIndex) {
case 0: