From 98b855ed482bfd064248ac2f2af035393596a518 Mon Sep 17 00:00:00 2001 From: angryziber Date: Sun, 5 Nov 2006 00:27:23 +0000 Subject: [PATCH] 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 --- resources/Labels.txt | 3 + .../ipscan/config/GUIComponentContainer.java | 3 +- src/net/azib/ipscan/core/Scanner.java | 18 ++-- src/net/azib/ipscan/core/ScanningSubject.java | 16 +++ ...Registry.java => FetcherRegistryImpl.java} | 14 ++- src/net/azib/ipscan/fetchers/PingFetcher.java | 9 +- test/net/azib/ipscan/core/ScannerTest.java | 101 ++++++++++++++++++ .../gui/actions/OpenerLauncherTest.java | 4 +- 8 files changed, 145 insertions(+), 23 deletions(-) rename src/net/azib/ipscan/fetchers/{FetcherRegistry.java => FetcherRegistryImpl.java} (78%) create mode 100755 test/net/azib/ipscan/core/ScannerTest.java diff --git a/resources/Labels.txt b/resources/Labels.txt index 5400b252..592d2cee 100755 --- a/resources/Labels.txt +++ b/resources/Labels.txt @@ -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: diff --git a/src/net/azib/ipscan/config/GUIComponentContainer.java b/src/net/azib/ipscan/config/GUIComponentContainer.java index dd6c3f1e..4e6f9452 100755 --- a/src/net/azib/ipscan/config/GUIComponentContainer.java +++ b/src/net/azib/ipscan/config/GUIComponentContainer.java @@ -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); diff --git a/src/net/azib/ipscan/core/Scanner.java b/src/net/azib/ipscan/core/Scanner.java index 9955f85f..94900bbb 100755 --- a/src/net/azib/ipscan/core/Scanner.java +++ b/src/net/azib/ipscan/core/Scanner.java @@ -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++; } diff --git a/src/net/azib/ipscan/core/ScanningSubject.java b/src/net/azib/ipscan/core/ScanningSubject.java index 5ff78103..a0a76807 100755 --- a/src/net/azib/ipscan/core/ScanningSubject.java +++ b/src/net/azib/ipscan/core/ScanningSubject.java @@ -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; + } + } diff --git a/src/net/azib/ipscan/fetchers/FetcherRegistry.java b/src/net/azib/ipscan/fetchers/FetcherRegistryImpl.java similarity index 78% rename from src/net/azib/ipscan/fetchers/FetcherRegistry.java rename to src/net/azib/ipscan/fetchers/FetcherRegistryImpl.java index 76bd2fdb..42d5b23e 100755 --- a/src/net/azib/ipscan/fetchers/FetcherRegistry.java +++ b/src/net/azib/ipscan/fetchers/FetcherRegistryImpl.java @@ -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 diff --git a/src/net/azib/ipscan/fetchers/PingFetcher.java b/src/net/azib/ipscan/fetchers/PingFetcher.java index 244b846a..a5e9aec9 100755 --- a/src/net/azib/ipscan/fetchers/PingFetcher.java +++ b/src/net/azib/ipscan/fetchers/PingFetcher.java @@ -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; } } diff --git a/test/net/azib/ipscan/core/ScannerTest.java b/test/net/azib/ipscan/core/ScannerTest.java new file mode 100755 index 00000000..3882ea9b --- /dev/null +++ b/test/net/azib/ipscan/core/ScannerTest.java @@ -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; + } + } + +} diff --git a/test/net/azib/ipscan/gui/actions/OpenerLauncherTest.java b/test/net/azib/ipscan/gui/actions/OpenerLauncherTest.java index 2d6d15fb..80a89ef6 100755 --- a/test/net/azib/ipscan/gui/actions/OpenerLauncherTest.java +++ b/test/net/azib/ipscan/gui/actions/OpenerLauncherTest.java @@ -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: